Skip to content
Prev 334024 / 398528 Next

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

On 04/12/2013 11:36 AM, Jun Shen wrote:
There are many ways to do that.  You need to fit a model that predicts z 
from (x, y), and then plot the predictions from that model.
An example below follows yours.
Don't use rgl.points, use points3d() or plot3d().  Here's the full script:


x<-runif(20)
y<-runif(20)
z<-runif(20)

library(rgl)
plot3d(x,y,z)

fit <- lm(z ~ x + y + x*y + x^2 + y^2)

xnew <- seq(min(x), max(x), len=20)
ynew <- seq(min(y), max(y), len=20)
df <- expand.grid(x = xnew,
                   y = ynew)

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

surface3d(xnew, ynew, df$z, col="red")


Duncan Murdoch