Michael Rennie
2004-Apr-01 16:14 UTC
[R] modelling nested random effects with interactions in R
Hi there Please excuse this elementary question, but I have been fumbling with this for hours and can't seem to get it right. I have a nested anova, with random factor "lakefac" nested within factor "fishfac" (fixed), with an additional fixed factor "Habfac". If I consider everything as fixed effects, it's addmittedly not the correct model, but I can at least get this to work:> nested <- anova(lm(ltotinv ~ habfac + fishfac/lakefac + habfac:fishfac +habfac:(lakefac %in% fishfac)))> nestedAnalysis of Variance Table Response: ltotinv Df Sum Sq Mean Sq F value Pr(>F) habfac 2 17.3140 8.6570 25.0568 3.534e-08 *** fishfac 1 0.9131 0.9131 2.6428 0.11057 fishfac:lakefac 2 2.3802 1.1901 3.4447 0.04000 * habfac:fishfac 2 13.0101 6.5050 18.8281 9.196e-07 *** habfac:fishfac:lakefac 4 3.0103 0.7526 2.1783 0.08557 . Residuals 48 16.5838 0.3455 --- So now I try to run it using the lme4 package, treating lakefac as random;> > lakernd <- lme(ltotinv ~ habfac + fishfac/lakefac + habfac:fishfac + habfac:(lakefac %in% fishfac), random = ~ lakefac) Error in .class1(object) : Argument "data" is missing, with no default> lakerndError: Object "lakernd" not found The lme help file suggests that if "data" is not specified, that it defaults to whatever object is currently in use in the environment (as was the case in the fixed effects model- I am using a matrix called "use" in this example). If I (naively) simply try to add data= use to the above formula, this happens:> lakernd <- lme(ltotinv ~ habfac + fishfac/lakefac + habfac:fishfac + habfac:(lakefac %in% fishfac), data = use, random = ~ lakefac) Error in as(data, "data.frame") : No method or default for coercing "matrix" to "data.frame" So how do I get lme to use my data matrix "use" for the model? My guess is that my syntax is off, but does anyone have any suggestions on how to fix it? Unfortunately, the only resources I have available to me at the moment are an "S-plus v. 4. guide to statistics", the archived help files on the R website, an the examples and help files in R. Your help with this problem is greatly appreciated- I do hope to hear from someone as to how I might get this to work. Sincerely, -- Michael Rennie Ph.D. Candidate University of Toronto at Mississauga 3359 Mississauga Rd. N. Mississauga ON L5L 1C6 Ph: 905-828-5452 Fax: 905-828-3792
Lorenz.Gygax@fat.admin.ch
2004-Apr-02 05:40 UTC
[R] modelling nested random effects with interactions in R
Hi Michael,> I have a nested anova, with random factor "lakefac" nested within > factor "fishfac" (fixed), with an additional fixed factor > "Habfac". If I consider everything as fixed effects, it's addmittedly > not the correct model, but I can at least get this to work: > ... > So now I try to run it using the lme4 package, treating > lakefac as random;I am not quite sure about lme4 but in limrary ('nlme') you would need to do something like:> lakernd <- lme(ltotinv ~ habfac * fishfac,random = ~ 1 | fishfac/lakefac, data= use) with this model you have the fixed effect of habfac and fishfac and their interaction, lme should get the df's of the model ok if you specify it that way. In this way, the random term is only for the intercept (which to my understanding is the same as saying there is no interaction between random and fixed effects). If you want to include the interactions you need to do something like this:> lakernd.int <- lme(ltotinv ~ habfac * fishfac,random = ~ habfac * fishfac | fishfac/lakefac, data= use) Thus, you specify random effects for the habfac, fishfac and their interactions, which is the same as saying that there are interactions between random and fixed effects. Possibly you might need to do something like the following (because fishfac is in the fixed and the random term, thus you specify interactions seperately for both levels:> lakernd.int <- lme(ltotinv ~ habfac * fishfac,random = list (~ 1 | fishfac, ~ habfac * fishfac | lakefac) data= use) To test whether interactions are significant you need to compare models. Thus the following would test whether all these included interactions togethter lead to a statistically better model: anova (lakernd, lakernd.int) I hope this helps to get you on the right track. Regards, Lorenz - Lorenz Gygax Tel: +41 (0)52 368 33 84 / lorenz.gygax at fat.admin.ch Center for proper housing of ruminants and pigs Swiss Veterinary Office agroscope FAT T??nikon, CH-8356 Ettenhausen / Switzerland Fax : +41 (0)52 365 11 90 / Tel: +41 (0)52 368 31 31
Where Can I Have More information about Formulae in R (Nesti8ng, Crossing Factors, Etc). I??ve tried to obtain this information in R Help and that source were a kind of inconclusive for my Doubts. []s Leonard Assis Estat??stico CONFE 7439