使用scatterplot3d绘制薄板样条

时间:2015-11-17 16:01:03

标签: r plot 3d spline tps

Splines对我来说还是一个新手。

我试图弄清楚如何创建薄板样条的三维图,类似于统计学习简介(http://www-bcf.usc.edu/~gareth/ISL/ISLR%20Sixth%20Printing.pdf)第24-25页上出现的可视化。我在scatterplot3d工作,为了便于重现数据,我们可以使用'树'数据集代替我的实际数据。

设置初始图是微不足道的:

data(trees)
attach(trees)
s3d <- scatterplot3d(Girth, Height, Volume,
                 type = "n", grid = FALSE, angle = 70,
                 zlab = 'volume',
                 xlab = 'girth', 
                 ylab = 'height',
                 main = "TREES") # blank 3d plot

我使用字段库中的Tps函数来创建样条线:

my.spline <- Tps(cbind(Girth, Height), Volume)

我可以开始直观地表示样条:

for(i in nrow(my.spline$x):1) # for every girth . . .
s3d$points3d(my.spline$x[,1], rep(my.spline$x[i,2], times=nrow(my.spline$x)), # repeat every height . . . 
              my.spline$y, type='l') # and match these values to a predicted volume

但是当我尝试通过高度访问的交叉阴影线完成样条曲线时,结果会出现问题:

for(i in nrow(my.spline$x):1) # for every height . . .
s3d$points3d(rep(my.spline$x[i,1], times=nrow(my.spline$x)), my.spline$x[,2],  # repeat every girth . . . 
           my.spline$y, type='l') # and match these values to a predicted volume 

我看到得到的情节越多,我就越不确定我甚至使用my.spline中的正确数据。

请注意,此项目使用scatterplot3d进行其他可视化,因此我会根据预先存在的团队选择结合此包。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我认为你没有得到预测的Tps。这需要使用predict.Tps

require(fields)
require(scatterplot3d)
data(trees)
attach(trees)   # this worries me. I generally use data in dataframe form.
s3d <- scatterplot3d(Girth, Height, Volume,
                 type = "n", grid = FALSE, angle = 70,
                 zlab = 'volume',
                 xlab = 'girth', 
                 ylab = 'height',
                 main = "TREES") # blank 3d plot
grid<- make.surface.grid( list( girth=seq( 8,22), height= seq( 60,90) ))
surf <- predict(my.spline, grid)
 str(surf)
# num [1:465, 1] 5.07 8.67 12.16 15.6 19.1 ...
str(grid)
#------------
 int [1:465, 1:2] 8 9 10 11 12 13 14 15 16 17 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:2] "girth" "height"
 - attr(*, "grid.list")=List of 2
  ..$ girth : int [1:15] 8 9 10 11 12 13 14 15 16 17 ...
  ..$ height: int [1:31] 60 61 62 63 64 65 66 67 68 69 ...
#-------------
s3d$points3d(grid[,1],grid[,2],surf, cex=.2, col="blue")

您可以添加预测点数。这样可以更好地了解x-y区域,其中对估计的表面有“支持”:

s3d$points3d(my.spline$x[,1], my.spline$x[,2],  
           predict(my.spline) ,col="red")

enter image description here

scatterplot3d包中没有surface3d函数。 (我只是搜索了Rhelp档案,看看我是否遗漏了一些东西,但图形专家总是说你需要使用lattice::wireframegraphics::persp或'rgl'包函数。你已经对scatterplot3d做出了承诺,我认为最简单的转换不会是对那些名为plot3d的功能更强大的基础图形包。它能够有很多变化而且makes quite beautiful surfaces with its surf3D function: