# Re: [R] Plotting Prediction Surface with persp()

From: <rory.winston_at_gmail.com>
Date: Thu, 03 Jul 2008 07:34:03 +0000

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

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 03 Jul 2008 - 07:39:15 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 03 Jul 2008 - 08:31:01 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.