Skip to content
Prev 334057 / 398513 Next

How to generate a smoothed surface for a three dimensional dataset?

On 05/12/2013 10:33 AM, Jun Shen wrote:
After the error, use traceback() to find which function called 
`$<-.data.frame`.  It shows that it was your final assignment

df$z<-predict(fit.loess,newdata=df)


which causes the error, because the predict function returns a matrix.  
So you can get the plot using

surface3d(xnew, ynew, predict(fit.loess,newdata=df), col="gray")

You may want

aspect3d(1,1,1)

afterwards; loess isn't so good at extrapolation.  Or you may want to 
set predictions to NA outside the convex hull of your data.  (I'm not 
sure which function is easiest to calculate that, but here's one way:

hullx <- x[chull(x,y)]
hully <- y[chull(x,y)]
keep <- sp::point.in.polygon(df$x, df$y, hullx, hully)
znew <- predict(fit.loess,newdata=df)
znew[!keep] <- NA
plot3d(x,y,z)
surface3d(xnew, ynew, znew, col="gray")
aspect3d(1,1,1)