[R-sig-eco] Linear Model of Data in Matrix Form
Jane Shevtsov
jane.eco at gmail.com
Mon Apr 4 05:05:20 CEST 2011
Thanks, but that doesn't quite work. Using your toy example gives an
error about variable lengths.
> z <- matrix(1:9,nrow=3,dimnames=list(1:3,4:6))
> z.melt <- melt(z)
> z.melt
X1 X2 value
1 1 4 1
2 2 4 2
3 3 4 3
4 1 5 4
5 2 5 5
6 3 5 6
7 1 6 7
8 2 6 8
9 3 6 9
> lm(z ~ z.melt$X1 + z.melt$X2)
Error in model.frame.default(formula = z ~ z.melt$X1 + z.melt$X2,
drop.unused.levels = TRUE) :
variable lengths differ (found for 'z.melt$X1')
I'm using R 2.12.0 on Windows and just installed the reshape package.
Jane
On Sun, Apr 3, 2011 at 3:40 PM, Ben Bolker <bbolker at gmail.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 11-04-02 10:39 PM, Jane Shevtsov wrote:
>> I have data in matrix form from a parameter scan of a food web
>> simulation. (The matrix entry M[i,j] gives the variable of interest
>> for a web size i and connectance j.) How would I find an equation
>> expressing the variable of interest to web size and connectance? Can
>> lm() do it?
>>
>> Thanks,
>> Jane Shevtsov
>>
>
> suppose your size and connectance vectors were sizevec and connvec.
> Then something like
>
> dimnames(M) <- list(sizevec,connvec)
> library(reshape)
> X <- melt(M)
> lm(value~X1+X2)
>
> will do it.
>
> Mini example:
>
>> z <- matrix(1:9,nrow=3,dimnames=list(1:3,4:6))
>> library(reshape)
> Loading required package: plyr
>
> Attaching package: 'reshape'
>
> The following object(s) are masked from 'package:plyr':
>
> round_any
>
>> str(melt(z))
> 'data.frame': 9 obs. of 3 variables:
> $ X1 : int 1 2 3 1 2 3 1 2 3
> $ X2 : int 4 4 4 5 5 5 6 6 6
> $ value: int 1 2 3 4 5 6 7 8 9
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAk2Y90MACgkQc5UpGjwzenOmPQCdFAOGcxnzADXZNv9ikgDzJcPQ
> 3/IAn1ayn1HVOQOdlqFDPoq+Et6rR5nt
> =cfIQ
> -----END PGP SIGNATURE-----
>
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>
--
-------------
Jane Shevtsov
Ecology Ph.D. candidate, University of Georgia
co-founder, <www.worldbeyondborders.org>
Check out my blog, <http://perceivingwholes.blogspot.com>Perceiving Wholes
"In the long run, education intended to produce a molecular
geneticist, a systems ecologist, or an immunologist is inferior, both
for the individual and for society, than that intended to produce a
broadly educated person who has also written a dissertation." --John
Janovy, Jr., "On Becoming a Biologist"
More information about the R-sig-ecology
mailing list