样条线对我来说仍然是一个相当新的东西。
我正在试图弄清楚如何创建薄板样条的三维图形,类似于统计学习简介(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我使用field库中的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进行其他可视化,因此我坚持使用此包作为预先存在的团队选择的结果。任何帮助都将不胜感激。
发布于 2015-11-18 00:54:41
我不认为你得到了预测的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")

scatterplot3d包中没有surface3d函数。(我刚刚搜索了Rhelp归档文件,看看是否遗漏了什么,但图形专家总是说你需要使用lattice::wireframe、graphics::persp或‘rgl’包函数。既然你已经对scatterplot3d做出了承诺,我认为最简单的转换不会是那些,而是功能更强大的基础图形包plot3d。它可以支持许多变体和makes quite beautiful surfaces with its surf3D function:
https://stackoverflow.com/questions/33761502
复制相似问题