Search code examples
r3dpredictdata-fitting

Difference between 3D plots using fitted and predicted values


I have two 3D plots, one was made using fitted values using library p3d (left side). In the second one I used predict and then interp command from package akima and plotted using persp (right side). The images are not showing the same angle, but it is the best way I found to show that one is flat and the other one has a curve.

enter image description here

I would like to know why the graph showing fitted values has a curve and the other one with predicted values doesn't have it.

First graph code:

library(p3d)
Init3d(family="serif", cex = 1)
Plot3d( TCL ~ reLDM+yr, nest6)
Axes3d()
fit = lm( TCL ~ reLDM+yr+I(yr^2)+I(reLDM*yr)+I(reLDM*yr^2), nest6)
Fit3d( fit )

Second graph code:

library(akima)    
x <- nest6$reLDM
y <- nest6$yr
y2 <- y^2
z <- nest6$TCL
m <- lm(z ~ x*y+y2+x:y2)

i <- 25 
xtemp <- seq(min(x),max(x),length.out=i)
xrange <- rep(xtemp,times=i) 
ytemp <- seq(min(y),max(y),length.out=i)
yrange <- rep(ytemp,each=i) 
y2temp <- seq(min(y2),max(y2),length.out=i)
y2range <- rep(y2temp,each=i) 
newdata <- data.frame(x=xrange,y=yrange,y2=y2range)
zhat <- predict(m,newdata=newdata) 
xyz <- interp(xrange,yrange,zhat)
jet.colors <- colorRampPalette( c("yellow", "red", "blue") )
nbcol <- 500
color <- jet.colors(nbcol)
nrz <- length(xyz[[1]])
ncz <- length(xyz[[2]])
z<-xyz[[3]]
zfacet <- z[-1, -1] + z[-1, -ncz] + z[-nrz, -1] + z[-nrz, -ncz]
facetcol <- cut(zfacet, nbcol)
quartz()
persp(xyz,xlab="x",ylab="y",zlab="z", cex.lab = 1,cex.axis = 1, 
         theta = 35, phi = 50,col=color[facetcol], border="grey40", ticktype = "detailed", zlim=c(1,7))

You will find the dataset in this link, it big so it was not possible to post it here: https://www.dropbox.com/s/czdascoq02alm46/TCL16_26.csv?dl=0

My model was done using lm(), I read in a post that there is no difference between fitted and predict functions in a simple linear regression model. However, in akima I am using the interp command which I understand it estimates values between two known data points (basically fills the missing data gaps).

Another difference I found is that predicted values plot uses new data from the maximum and minimum range of values in the original data set. For fitted values there is one value per observation.

I have problems explaining this to my supervisor, he thinks it is not enough reason. What would be a better explanation why is the curve missing in the second graph?


Solution

  • Replace

    y2temp <- seq(min(y2),max(y2),length.out=i)
    

    with

    y2temp <- ytemp^2
    

    You will get a similar curve.

    Using simulated data:

    enter image description here