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

Ben Bolker bbolker at gmail.com
Mon Apr 4 05:09:27 CEST 2011


On 11-04-03 11:05 PM, Jane Shevtsov 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)
> 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.
> 

  How about

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

 ?

 The response variable is not z, it's z.melt$value ...

  (this post brought to you courtesy of a delayed flight)



More information about the R-sig-ecology mailing list