[R-sig-eco] Linear Model of Data in Matrix Form

Gavin Simpson gavin.simpson at ucl.ac.uk
Mon Apr 4 13:01:21 CEST 2011


On Sun, 2011-04-03 at 23:22 -0400, Christian Roy wrote:
> On Sun, Apr 3, 2011 at 11:05 PM, Jane Shevtsov <jane.eco at gmail.com> wrote:
> 
> > 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)
> >
> 
> In this example the syntax for your model is:
> > lm(z.melt$value ~ z.melt$X1 + z.melt$X2)

It most certainly shouldn't be!

lm(value ~ X1 + X2, data = z.melt)

Why do people insist on abusing model formulas so? ;-) They obviously
haven't had to do any predictions vis predict() lately...

G

> Call:
> lm(formula = z.melt$value ~ z.melt$X1 + z.melt$X2)
> 
> Coefficients:
> (Intercept)    z.melt$X1    z.melt$X2
>         -12            1            3
> 
> Best,
> 
> Chris
> -------
> 
> > 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"
> >
> > _______________________________________________
> > R-sig-ecology mailing list
> > R-sig-ecology at r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
> >
> 
> 
> 
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-sig-ecology mailing list