【问题标题】:Plot a thin plate spline using scatterplot3d使用 scatterplot3d 绘制薄板样条曲线
【发布时间】:2016-02-19 02:14:28
【问题描述】:

样条线对我来说还是相当新的。

我正在尝试弄清楚如何创建薄板样条曲线的三维图,类似于出现在《统计学习简介》(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 进行其他可视化,所以我喜欢这个包作为预先存在的团队选择的结果。任何帮助将不胜感激。

【问题讨论】:

  • 一般你应该避免attach()。它出现在许多较旧的教程中,但除了简单的示例之外,它经常会导致问题并使事情难以跟踪。
  • 这是一个简单的例子,所以 attach() 不会引起任何问题,实际上使代码更容易阅读。
  • 是的 - 在一个简单的例子中没有问题。此外,data(trees) 是不必要的行。几年来,延迟加载数据已成为常态,因此几乎总是不需要调用data()

标签: r plot 3d spline tps


【解决方案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")

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

【讨论】:

  • 这很有帮助,但我仍然感到困惑。我预计脊椎会有点颠簸,而不是平坦的平面。我可以得到几乎相同的结果: pln
  • 不是平的。它有一个轻微的曲线。(注意左右两侧“线”的斜率不同。)你想要不那么平坦吗?然后调整 Tps 拟合的参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-29
  • 1970-01-01
  • 1970-01-01
  • 2019-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多