Search code examples
rrglplot3d

How do I plot3d a locfit?


I have variables x,y,z which form a point cloud when plotted by:

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

I did a locfit on them

myfit = locfit(y~lp(x,z),maxk=200)

I understand that this will give me a curve that goes through the most dense region of space.

How do I plot this curve in plot3d / RGL?


Solution

  • Use surface3d. x and y are vectors for the margins and z is a matrix:

    require(locfit)
    fit <- locfit(NOx~lp(E,C,nn=0.5,scale=0), data=ethanol)
    plot(locfit)  # there is an ordinary contour plot method for locfit objects.
    
    require(rgl)
    open3d()
    surface3d( x=seq(0.5, 1.3, by=0.1), y=seq(7.5,18,by=.5) ,
               z= matrix(  predict(fit, newdata= 
                               expand.grid(E=seq(0.5, 1.3, by=0.1), 
                                           C=seq(7.5,18,by=.5) ) ) ),
                          ,nrow= length(seq(0.5, 1.3, by=0.1)) ,
                           ncol= length(seq(7.5,18,by=.5) ) ,
               xlim=c(.5, 1.3) )
     # grab and spin
    

    I actually find the contour plots more informative, but the 3d plots can be useful, too.