Jun Shen
2013-Dec-04 16:36 UTC
[R] How to generate a smoothed surface for a three dimensional dataset?
Hi, I have a dataset with two independent variables (x, y) and a response variable (z). I was hoping to generate a response surface by plotting x, y, z on a three dimensional plot. I can plot the data with rgl.points(x, y, z). I understand I may not have enough data to generate a surface. Is there a way to smooth out the data points to generate a surface? Thanks a lot. Jun ========================== An example: x<-runif(20) y<-runif(20) z<-runif(20) library(rgl) rgl.points(x,y,z) [[alternative HTML version deleted]]
Federico Andreis
2013-Dec-04 16:46 UTC
[R] How to generate a smoothed surface for a three dimensional dataset?
Hi Jun, I'd for a local fitting by means of the loess() function. Assuming z is the third axis: x<-runif(25) y<-runif(25) z<-runif(25) xyz.fit <- loess(z~x+y,control=loess.control(surface='direct'),span=.5,degree=1) #tune parameters as you like z.predict <-matrix(predict(xyz.fit,cbind(x,y)),5,5) persp(z=z.predict,phi=30,theta=45,shade=.5) #tune parameters as you like Hope this helps, /f On Wed, Dec 4, 2013 at 5:36 PM, Jun Shen <jun.shen.ut@gmail.com> wrote:> Hi, > > I have a dataset with two independent variables (x, y) and a response > variable (z). I was hoping to generate a response surface by plotting x, y, > z on a three dimensional plot. I can plot the data with rgl.points(x, y, > z). I understand I may not have enough data to generate a surface. Is there > a way to smooth out the data points to generate a surface? Thanks a lot. > > Jun > > ==========================> > An example: > > x<-runif(20) > y<-runif(20) > z<-runif(20) > > library(rgl) > rgl.points(x,y,z) > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Duncan Murdoch
2013-Dec-04 16:56 UTC
[R] How to generate a smoothed surface for a three dimensional dataset?
On 04/12/2013 11:36 AM, Jun Shen wrote:> Hi, > > I have a dataset with two independent variables (x, y) and a response > variable (z). I was hoping to generate a response surface by plotting x, y, > z on a three dimensional plot. I can plot the data with rgl.points(x, y, > z). I understand I may not have enough data to generate a surface. Is there > a way to smooth out the data points to generate a surface? Thanks a lot.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.> > Jun > > ==========================> > An example: > > x<-runif(20) > y<-runif(20) > z<-runif(20) > > library(rgl) > rgl.points(x,y,z)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> > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.