Hi:

On Fri, Jul 16, 2010 at 3:44 PM, skan <juanp...@gmail.com> wrote:

>
> Hello
>
> I have a table of this kind:
>
> function        x1      x2      x3
> 2.232           1       1       1.00
> 2.242           1       1       1.01
> 2.732           1       1       1.02
> 2.770           1       2       1.00
> 1.932           1       2       1.01
> 2.132           1       2       1.02
> 3.222           1.2     1       1
> .....           ...     ..      ..
>
>
> The table represents the values of a function(x1, x2, x3) for each
> combination x1, x2, x3.
>
> I'd like to generate a plot where each point has the coordinates x=x1,
> y=x2,
> z=(mean of function(x1, x2) for all different x3).  How can I do it?
>

Here are three ways to get the function means over the x1 * x2 combinations
-
there are others, too. I changed the variable function to y since function
is a basic object in R and therefore not a good choice of variable name.

library(doBy)
library(plyr)

# (1) aggregate
(s1 <- aggregate(y ~ x1 + x2, data = d, FUN = mean))

# (2) function summaryBy() in package doBy:
(s2 <- summaryBy(y ~ x1 + x2, data = d, FUN = mean))

# (3)  function ddply() in package plyr:
(s3 <- ddply(d, .(x1, x2), summarise, y = mean(y)))

All three give the same answer (apart from the name of the y mean):

   x1 x2     y
1 1.0  1 2.402
2 1.0  2 2.278
3 1.2  1 3.222

fox example, with the data from above the first point would be:
> x=x1=1, y=x2=1, z=(2.232+2.242+2.732)/3
>
>
> In truth, my table has many columns and I want to take the mean over all
> the
> variables except the ones I represent at the axes, for example represent
> function(x1, x2) taking the mean over x3, x4, x5.
>

Same as above. The response is averaged over all x1 * x2 combinations.

>
> Or using the maximum value of function(x1, x2) over all x1, x2, x3
>

Substitute FUN = max for FUN = mean in aggregate() and summaryBy(),
and use y = max(y) in place of y = mean(y) in the ddply() call. I presume
you meant max f(x1, x2) over all x3 with the same (x1, x2) combination.


> How can I do it?
>
>
For the 3D plot, one choice is persp() in base graphics; another is
cloud() in the lattice package. persp() requires setting up a rectangular
grid of (x1, x2) pairs, with y = f(x1, x2) supplied as a n1 x n2 matrix.
cloud() in lattice only requires a formula of the form y ~ x1 + x2. See
their respective help pages for more details. The scatterplot3d package
could also be used in this context.


> Another question.
> How can I plot function(x1, x2, x3) with x=x1, y=x2, z=x3, different
> colours=function
>

One can create a vector of colors, but the question is too vaguely worded
to give any specific advice.

HTH,
Dennis

>
>
>
> thanks
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/multivariate-graphs-averaging-on-some-vars-tp2292039p2292039.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to