简体   繁体   中英

Plot a thin plate spline using scatterplot3d

Splines are still fairly new to me.

I am trying to figure out how to create a three dimensional plot of a thin plate spline, similar to the visualizations which appear on pages 24-25 of Introduction to Statistical Learning ( http://www-bcf.usc.edu/~gareth/ISL/ISLR%20Sixth%20Printing.pdf ). I'm working in scatterplot3d, and for the sake of easily reproducible data, lets use the 'trees' dataset in lieu of my actual data.

Setting the initial plot is trivial:

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

I use the Tps function from the fields library to create the spline:

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

And I can begin to represent the spline visually:

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

But when I try to complete the spline by cross hatching lines along the height access, the results become problematic:

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 

And the more that I look at the resulting plot, the less certain I am that I'm even using the right data from my.spline.

Please note that this project uses scatterplot3d for other visualizations, so I am wedded to this package as the result of preexisting team choices. Any help will be greatly appreciated.

I don't think you are getting the predicted Tps. That requires using 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")

You can add back the predicted points. This gives a better idea of xy regions where there is "support" for the estimated surface:

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

在此处输入图片说明

There is no surface3d function in scatterplot3d package. (And I just searched the Rhelp archives to see if I were missing something but the graphics experts have always said that you would need to use lattice::wireframe , the graphics::persp or the 'rgl'-package functions. Since you have made a commitment to scatterplot3d, I think the easiest transtion would not be to those but to the much more capable base-graphics package named plot3d. It is capable of many variations and makes quite beautiful surfaces with its surf3D function:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM