Hi all I have a question about correct usage of persp(). I have a simple neural net-based XOR example, as follows: library(nnet) xor.data <- data.frame(cbind(expand.grid(c(0,1),c(0,1)), c(0,1,1,0))) names(xor.data) <- c("x","y","o") xor.nn <- nnet(o ~ x + y, data=xor.data, linout=FALSE, size=1) # Create an (x.y) surface and predict over all points d <- data.frame(expand.grid(seq(0,1,.1), seq(0,1,.1))) names(d) <- c("x","y") p <- predict(xor.nn, d) zmat <- as.matrix(cbind(d,p)) Now my z matrix consists of x and y points, and the corresponding prediction value for each (x,y) tuple. What would be the best way to plot these? I tried persp(), but it didnt like the z matrix. Is there an alternative plot function that I could use (I am presuming I need one of the 3d variants)? Thanks Rory [[alternative HTML version deleted]]
On 02/07/2008 8:47 PM, Rory Winston wrote:> Hi all > > I have a question about correct usage of persp(). I have a simple neural > net-based XOR example, as follows: > > library(nnet) > xor.data <- data.frame(cbind(expand.grid(c(0,1),c(0,1)), c(0,1,1,0))) > names(xor.data) <- c("x","y","o") > xor.nn <- nnet(o ~ x + y, data=xor.data, linout=FALSE, size=1) > > # Create an (x.y) surface and predict over all points > d <- data.frame(expand.grid(seq(0,1,.1), seq(0,1,.1))) > names(d) <- c("x","y") > p <- predict(xor.nn, d) > zmat <- as.matrix(cbind(d,p)) > > Now my z matrix consists of x and y points, and the corresponding prediction > value for each (x,y) tuple. What would be the best way to plot these? I > tried persp(), but it didnt like the z matrix. Is there an alternative plot > function that I could use (I am presuming I need one of the 3d variants)?You were close, but your zmat was constructed incorrectly. persp() wants a vector of values corresponding to its rows (e.g. x <- seq(0,1,.1)) and a vector of values corresponding to its columns (e.g. y <- seq(0,1,.1)), and it wants the z values in a matrix matching those. So you need the lines I give above, then dim(p) <- c(length(x),length(y)) persp(x,y,p) You could also use persp3d() somewhat interchangeably (but it handles colour specs differently). Duncan Murdoch
rory.winston at gmail.com
2008-Jul-03 07:34 UTC
[R] Plotting Prediction Surface with persp()
Great! Thanks for the advice. Cheers Rory ------Original Message------ From: Duncan Murdoch To: Rory Winston Cc: r-help at r-project.org Sent: 3 Jul 2008 05:08 Subject: Re: [R] Plotting Prediction Surface with persp() On 02/07/2008 8:47 PM, Rory Winston wrote:> Hi all > > I have a question about correct usage of persp(). I have a simple neural > net-based XOR example, as follows: > > library(nnet) > xor.data <- data.frame(cbind(expand.grid(c(0,1),c(0,1)), c(0,1,1,0))) > names(xor.data) <- c("x","y","o") > xor.nn <- nnet(o ~ x + y, data=xor.data, linout=FALSE, size=1) > > # Create an (x.y) surface and predict over all points > d <- data.frame(expand.grid(seq(0,1,.1), seq(0,1,.1))) > names(d) <- c("x","y") > p <- predict(xor.nn, d) > zmat <- as.matrix(cbind(d,p)) > > Now my z matrix consists of x and y points, and the corresponding prediction > value for each (x,y) tuple. What would be the best way to plot these? I > tried persp(), but it didnt like the z matrix. Is there an alternative plot > function that I could use (I am presuming I need one of the 3d variants)?You were close, but your zmat was constructed incorrectly. persp() wants a vector of values corresponding to its rows (e.g. x <- seq(0,1,.1)) and a vector of values corresponding to its columns (e.g. y <- seq(0,1,.1)), and it wants the z values in a matrix matching those. So you need the lines I give above, then dim(p) <- c(length(x),length(y)) persp(x,y,p) You could also use persp3d() somewhat interchangeably (but it handles colour specs differently). Duncan Murdoch Sent using BlackBerry? from Orange