[R] Plotting Prediction Surface with persp()
Duncan Murdoch
murdoch at stats.uwo.ca
Thu Jul 3 06:08:20 CEST 2008
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
More information about the R-help
mailing list