MSS MSS - 2 months ago 7
R Question

Difference between 3D plots using fitted and predicted values

I have two 3D plots, one was made using fitted values using library

(left side). In the second one I used
and then
command from package
and plotted using
(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:

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

Second graph code:

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]])
zfacet <- z[-1, -1] + z[-1, -ncz] + z[-nrz, -1] + z[-nrz, -ncz]
facetcol <- cut(zfacet, nbcol)
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:

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 wich 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?



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


y2temp <- ytemp^2

You will get a similar curve.

Using simulated data:

enter image description here