[R] lme: how to nest a random factor in a fixed factor?
Dear all, I have an experimental setup in which a random variable is nested within a fixed variable; however I have troubles specifying the correct LMM with lme. I have searched the lists but haven't been able to find an example like my setup, which I unfortunately need to get this stuff right. Pinheiro Bates is great but I still can't figure out how to do it. My experimental setup was as follows: 100 measurements per treatment plot 2 treatment plots per site 4 sites: 2 in one area and 2 in another area Both treatment and area are fixed factors,while site is random. I am interested in the significance of the fixed effects,less in the magnitude of the random effect. I have tried: mod1 - lme(response ~ area*treatment, data=data,random= ~1|site) but now site is not nested in area… mod2 - lme(response ~ area*treatment, data=data,random= ~1|area/site) but now area is both a fixed and a random variable, which doesn't seem to make sense, plus I run out of df for treatment mod3 - lme(response ~ area*treatment, data=data,random= ~1| plot) but here plots are not grouped according to site I hope someone would be willing to help me, thank you in advance! Robert Buitenwerf Ecologist South African Environmental Observation Network _ [[elided Hotmail spam]] __ 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.
Re: [R] Lattice: groups and no groups with panel.superpose
Thanks Gabor and Baptiste, Both your suggestions give me the results I was looking for! Robert CC: r-help@r-project.org From: [EMAIL PROTECTED] To: [EMAIL PROTECTED] Subject: Re: [R] Lattice: groups and no groups with panel.superpose Date: Sat, 15 Nov 2008 16:59:31 + Hi, I think the following code should do what you want, xyplot(yvar~year|week,data=df,layout = c(4, 5), type='p', groups = temp , panel = function(x, y, ...) { panel.superpose(x, y, ...) panel.abline(lm(y~x)) }, panel.groups = function(x, y, ..., lty) { panel.xyplot(x, y, ..., lty = lty) panel.abline(lm(y~x), lty=3, ...) }) Hope this helps, baptiste On 15 Nov 2008, at 14:25, Robert Buitenwerf wrote: Dear R listers, I am trying to create a lattice plot with independent regression lines per group for each panel, plus one overall regression line per panel. I succeed in having the separate regression lines for each group per panel, but I can't figure out how to add the overall regression line over all groups. I have learnt a lot from the list, but I can't find an example for what I want to do... with the following example data (from another post): years-2000:2006 weeks-1:20 yr-rep(years,rep(length(weeks)*6,length(years))) wk-rep(weeks,rep(6,length(weeks))) temp-rep(4:9,length(years)*length(weeks)) yvar-round(rnorm(length(years)*length(weeks)*6,mean=30,sd=4),0) xvar-(rnorm(length(years)*length(weeks)*6)+5)/10 df-data.frame(year=yr,week=wk,temp=temp, yvar=yvar, xvar=xvar) I get the regression lines per group for each panel using: xyplot(yvar~year|week,data=df,layout = c(4, 5), type='p', groups = temp , panel = panel.superpose, panel.groups = function(x, y, ..., lty) { panel.xyplot(x, y, ..., lty = lty) panel.abline(lm(y~x), lty=3, ...) }) And obviously the overall regression lines with: xyplot(yvar~year|week,data=df,layout = c(4, 5), type='p', panel = function(x, y, ...) { panel.xyplot(x, y, ...) panel.abline(lm(y~x)) }) Any advice on how to merge the output from the two above graphs would be greatly appreciated! Kind regards, Robert Buitenwerf Ecologist SAEON South Africa _ [[elided Hotmail spam]] __ 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. _ Baptiste Auguié School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag __ _ [[elided Hotmail spam]] __ 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.
[R] Lattice: groups and no groups with panel.superpose
Dear R listers, I am trying to create a lattice plot with independent regression lines per group for each panel, plus one overall regression line per panel. I succeed in having the separate regression lines for each group per panel, but I can't figure out how to add the overall regression line over all groups. I have learnt a lot from the list, but I can't find an example for what I want to do... with the following example data (from another post): years-2000:2006 weeks-1:20 yr-rep(years,rep(length(weeks)*6,length(years))) wk-rep(weeks,rep(6,length(weeks))) temp-rep(4:9,length(years)*length(weeks)) yvar-round(rnorm(length(years)*length(weeks)*6,mean=30,sd=4),0) xvar-(rnorm(length(years)*length(weeks)*6)+5)/10 df-data.frame(year=yr,week=wk,temp=temp, yvar=yvar, xvar=xvar) I get the regression lines per group for each panel using: xyplot(yvar~year|week,data=df,layout = c(4, 5), type='p', groups = temp , panel = panel.superpose, panel.groups = function(x, y, ..., lty) { panel.xyplot(x, y, ..., lty = lty) panel.abline(lm(y~x), lty=3, ...) }) And obviously the overall regression lines with: xyplot(yvar~year|week,data=df,layout = c(4, 5), type='p', panel = function(x, y, ...) { panel.xyplot(x, y, ...) panel.abline(lm(y~x)) }) Any advice on how to merge the output from the two above graphs would be greatly appreciated! Kind regards, Robert Buitenwerf Ecologist SAEON South Africa _ [[elided Hotmail spam]] __ 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.
[R] (nlme) Repeated measures with continuous covariate in lme
Dear readers, I have a basic question about how to use lme for my design. I haven't been able to find an example in r-help that made it clear to me how to tackle this problem and unfortunately I also cannot get hold of Pinheiro Bates 2000. I hope someone can help. Data for the response variable foan were collected in: · 60 plots · plots were re-sampled yearly for 15 years (certain years are missing for certain plots) · plots are divided over 4 reserves (but not equally, so the design is unbalanced) · rain is a continuous covariate, which has the same value for all plots within one year. Since plots are random within reserves, but reserves can be seen as fixed factors, it seems I should use a mixed-model and so I have come up with the following models: model.1 - lme(foan ~ year * reserve, data=data, random= ~1 | plot) model.2 - lme(foan ~ year * reserve, data=data, random= ~year | plot) Model.2 does not provide a better fit than model.1, so apparently the slope of the relationship between foan and year does not vary for plots. I want to know if (and how) the response variable changes over the years, while correcting for variance explained by rain. My question is how to incorporate rain in this model. Can I simply use: model.3 - lme(foan ~ year * reserve * rain, data=data, random= ~ 1 | plot) This somehow seems wrong (forgive my ignorance) since rain has the same value within each year. On the other hand it has different values within each plot. I hope someone can point me in the right direction. Thanks in advance, Robert Buitenwerf South African Environmental Observation Network Phalaborwa South Africa _ [[elided Hotmail spam]] [[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.