Diff for "FAQ/subsrandom" - CBU statistics Wiki
location: Diff for "FAQ/subsrandom"
Differences between revisions 2 and 3
Revision 2 as of 2010-10-20 14:36:31
Size: 1789
Editor: PeterWatson
Comment:
Revision 3 as of 2010-10-20 14:39:21
Size: 1897
Editor: PeterWatson
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
Subjects and any other factors in an ANOVA can be either fixed or random. When all the factors are fixed we can use an analysis of variance and we are only interested in the groups in the study. When one or more factors are random we use a random effects or multilevel model and wish to generalize from the groups comprising the random factor in the study. Subjects and any other factors in an ANOVA can be either fixed or random. When all the factors are fixed we can use a fixed effects (the usual) analysis of variance and we are only interested in the groups in the study. When one or more factors are random we use an analysis of variance with random effects (also called a multilevel model) and wish to generalize from the groups comprising the random factor in the study.
Line 9: Line 9:
ulibrary(nlme) library(nlme)
Line 20: Line 20:
We can alternatively assume subjects are a random sample having random intercepts indicating a random set of abilities on a particular test. We can use lme() in R to fit this model using the same data as above. We can alternatively assume subjects are a random sample having random intercepts indicating a random set of abilities on a particular test. We can use lme() in R to fit this model using the same data as above and again treating the time factor as fixed.

Using subjects as a random or fixed factor in an ANOVA

Subjects and any other factors in an ANOVA can be either fixed or random. When all the factors are fixed we can use a fixed effects (the usual) analysis of variance and we are only interested in the groups in the study. When one or more factors are random we use an analysis of variance with random effects (also called a multilevel model) and wish to generalize from the groups comprising the random factor in the study.

We can fit both sets of models to fit random or fixed effects models. For example we may wish to compare performance across 4 time points completed by each subject (in the data set [attachment:subseg.sav here.]) To fit a one-way repeated measures anova with one fixed factor, time and treating subjects as a fixed factor we can use the aov() procedure as below assuming the data is in a folder called data on the hard drive.

library(nlme)
library(lme4)
library(foreign)
dva <- read.spss("c:\\data\\DUMMY DATA 2.sav")
attach(dva)
t1 <- as.factor(time)
id1 <- as.factor(id)
aov.mod <- aov(y ~ t1 + Error(id1/t1), data=dva)
summary(aov.mod)

We can alternatively assume subjects are a random sample having random intercepts indicating a random set of abilities on a particular test. We can use lme() in R to fit this model using the same data as above and again treating the time factor as fixed.

library(nlme)
library(lme4)
library(foreign)
dva2 <- lme(y ~ t1, data=dva, random = ~ 1|id) 
summary(dva2)
anova(dva2)

In fact in this example treating subjects as fixed or random gives the same result for the time variable.

Reference

Pinheiro, J.C., and Bates, D.M. (2000) "Mixed-Effects Models in S and S-PLUS", Springer. S-PLUS is the forerunner of R and has a very similar syntax. Most of the analysis of variance procedures are common to both packages.

None: FAQ/subsrandom (last edited 2013-03-08 10:17:30 by localhost)