transforming data frame for use with persp

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

transforming data frame for use with persp

Denis Chabot
Hi,

This is probably documented, but I cannot find the right words or  
expression for a search. My attempts failed.

I have a data frame of 3 vectors (x, y and z) and would like to  
transform this so that I could use persp. Presently I have y-level  
copies of each x level, and a z value for each x-y pair. I need 2  
columns giving the possible levels of x and y, and then a  
transformation of z from a long vector into a matrix of x-level rows  
and y-level columns. How do I accomplish this?

In this example, I made a set of x and y values to get predictions  
from a GAM, then combined them with the predictions into a data  
frame. This is the one I'd like to transform as described above:

My.data <- expand.grid(Depth=seq(40,220, 20), Temp=seq(-1, 6, 0.5))
predgam <- predict.gam(dxt.gam, My.data, type="response")
pred.data <- data.frame(My.data, predgam)

pred.data has 150 lines and 3 columns.

Thanks for your help,

Denis Chabot

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: transforming data frame for use with persp

RKoenker

a strategy for this that I  use is just

        persp(interp(x,y,z))

where interp is from the Akima package, and x,y,z are all
of the same length.


url:    www.econ.uiuc.edu/~roger            Roger Koenker
email    [hidden email]            Department of Economics
vox:     217-333-4558                University of Illinois
fax:       217-244-6678                Champaign, IL 61820


On Feb 13, 2006, at 3:07 PM, Denis Chabot wrote:

> Hi,
>
> This is probably documented, but I cannot find the right words or
> expression for a search. My attempts failed.
>
> I have a data frame of 3 vectors (x, y and z) and would like to
> transform this so that I could use persp. Presently I have y-level
> copies of each x level, and a z value for each x-y pair. I need 2
> columns giving the possible levels of x and y, and then a
> transformation of z from a long vector into a matrix of x-level rows
> and y-level columns. How do I accomplish this?
>
> In this example, I made a set of x and y values to get predictions
> from a GAM, then combined them with the predictions into a data
> frame. This is the one I'd like to transform as described above:
>
> My.data <- expand.grid(Depth=seq(40,220, 20), Temp=seq(-1, 6, 0.5))
> predgam <- predict.gam(dxt.gam, My.data, type="response")
> pred.data <- data.frame(My.data, predgam)
>
> pred.data has 150 lines and 3 columns.
>
> Thanks for your help,
>
> Denis Chabot
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting- 
> guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: transforming data frame for use with persp

Roger Bivand
In reply to this post by Denis Chabot
On Mon, 13 Feb 2006, Denis Chabot wrote:

> Hi,
>
> This is probably documented, but I cannot find the right words or  
> expression for a search. My attempts failed.
>
> I have a data frame of 3 vectors (x, y and z) and would like to  
> transform this so that I could use persp. Presently I have y-level  
> copies of each x level, and a z value for each x-y pair. I need 2  
> columns giving the possible levels of x and y, and then a  
> transformation of z from a long vector into a matrix of x-level rows  
> and y-level columns. How do I accomplish this?

Well, image() and friends have a rather strange representation, so it
isn't obvious (see the help page:

     "Notice that 'image' interprets the 'z' matrix as a table of
     'f(x[i], y[j])' values, so that the x axis corresponds to row
     number and the y axis to column number, with column 1 at the
     bottom, i.e. a 90 degree counter-clockwise rotation of the
     conventional printed layout of a matrix.").

So:

Depth <- seq(40,220, 20)
Temp <- seq(-1, 6, 0.5)
My.data <- expand.grid(Depth=Depth, Temp=Temp)
predgam <- -0.5*My.data$Depth + 12.5*My.data$Temp + 8*rnorm(nrow(My.data))
pred.data <- data.frame(My.data, predgam)
library(lattice)
levelplot(predgam ~ Depth + Temp, pred.data) # for sanity check
z <- t(matrix(predgam, nrow=length(Temp), byrow=TRUE)) # see "Notice"
image(Depth, Temp, z)
persp(Depth, Temp, z)

should do it, since the data are already in a regular grid.

>
> In this example, I made a set of x and y values to get predictions  
> from a GAM, then combined them with the predictions into a data  
> frame. This is the one I'd like to transform as described above:
>
> My.data <- expand.grid(Depth=seq(40,220, 20), Temp=seq(-1, 6, 0.5))
> predgam <- predict.gam(dxt.gam, My.data, type="response")
> pred.data <- data.frame(My.data, predgam)
>
> pred.data has 150 lines and 3 columns.
>
> Thanks for your help,
>
> Denis Chabot
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Roger Bivand
Department of Economics
NHH Norwegian School of Economics
Helleveien 30
N-5045 Bergen, Norway