Hello all, Thank you for the previous assistance I received from this listserve! My current question is: How can I create an appropriate matrix of values from a GAM (actually a GAMM) to make a 3-D plot? This model is fit as a tensor product spline of two predictors and I have used it to make specific predictions by calling: YoyRasPred6<-predict(YoyRas.Distribution.T.L.DT.gamm$gam,newdata=YoyRasSubset6,se.fit=T) However, this type of command, I believe, produces only a vector of values. I have plotted these values and know how to do so in 2-D. However, I would like to create a 3-D plot, which I believe will require an entire matrix of predictions (z values) based on all possible inputs (x and y values). What is an efficient way to do this? It seems there must be a better way than simply predicting vector after vector ... as I am now, then piecing these together. Thank you very much for any answers or assistance in general! Best wishes. Sincerely, Paul Simonin
3-D Plotting of predictions from GAM/GAMM object
2 messages · Paul Simonin, Duncan Murdoch
On 21/11/2009 3:11 PM, Paul Simonin wrote:
Hello all, Thank you for the previous assistance I received from this listserve! My current question is: How can I create an appropriate matrix of values from a GAM (actually a GAMM) to make a 3-D plot? This model is fit as a tensor product spline of two predictors and I have used it to make specific predictions by calling: YoyRasPred6<-predict(YoyRas.Distribution.T.L.DT.gamm$gam,newdata=YoyRasSubset6,se.fit=T) However, this type of command, I believe, produces only a vector of values. I have plotted these values and know how to do so in 2-D. However, I would like to create a 3-D plot, which I believe will require an entire matrix of predictions (z values) based on all possible inputs (x and y values). What is an efficient way to do this? It seems there must be a better way than simply predicting vector after vector ... as I am now, then piecing these together.
You can use expand.grid() to generate a dataframe corresponding to the grid of values, then reshape the results into a matrix. I don't have your data to show a fit, but this shows how the graphing is done. > x <- 1:10 > y <- 1:12 > grid <- expand.grid(x=x, y=y) # give names to get named columns > z <- matrix(apply(grid, 1, prod), nrow=10) # use predict instead here > library(rgl) > persp3d(x,y,z, col="red") Duncan Murdoch