[R] new to repeated measures anova in R
peter dalgaard
pdalgd at gmail.com
Mon Mar 5 11:54:20 CET 2012
[Apologies for duplicate. Forgot to Cc. the list 1st time around.]
On Mar 5, 2012, at 03:15 , Tamre Cardoso wrote:
> Data set up as one observation/subject looks like (with a total of 10 subjects)
> Two treatments: shoe type with 3 categories and region with 8 categories ==> 24 "treatment" columns
>
> Subject PHallux PMidToes PLatToe PMTH1 PMidMTH PLatMTH PMidfoot PRearfoot LHallux LMidToes LLatToe LMTH1 LMidMTH LLatMTH LMidfoot LRearfoot DHallux DMidToes DLatToe DMTH1 DMidMTH DLatMTH DMidfoot DRearfoot
> 1 203.230 169.970 75.090 208.420 168.860 129.150 104.840 209.960 200.005 88.880 30.820 315.535 105.445 72.265 88.195 211.280 198.970 113.525 65.640 237.175 148.790 86.105 69.830 222.230
>
> R Code:
>
> library(car)
> pressure=read.csv("Shoe_data.csv",header=TRUE,sep=",")
> datin.model=cbind(pressure[,2],pressure[,3],pressure[,4],pressure[,5],pressure[,6],pressure[,7],pressure[,8],pressure[,9],pressure[,10],pressure[,11],pressure[,12],pressure[,13],
> pressure[,14],pressure[,15],pressure[,16],pressure[,17],pressure[,18],pressure[,19],pressure[,20],pressure[,21],pressure[,22],pressure[,23],pressure[,24],pressure[,25])
> multmodel=lm(datin.model ~ 1)
> Shoe <- factor(c(rep("P",8),rep("L",8),rep("D",8)))
> Region <-factor(rep(c("Hallux","MidToes","LatToe","MTH1","MidMTH","LatMTH","MidFoot","Rearfoot"),3))
> fact.idata <- data.frame(Shoe,Region)
> pressure.aov = Anova(multmodel, idata=fact.idata, idesign = ~Shoe + Region, type="III")
>
>> summary(pressure.aov,multivariate=F)
> Univariate Type III Repeated-Measures ANOVA Assuming Sphericity
>
> SS num Df Error SS den Df F Pr(>F)
> (Intercept) 6275173 1 192361 9 293.5961 3.535e-08 ***
> Shoe 2340 2 11839 18 1.7786 0.1973
> Region 748644 7 299408 63 22.5037 6.181e-15 ***
> ---
> Signif. codes: 0 Œ***‚ 0.001 Œ**‚ 0.01 Œ*‚ 0.05 Œ.‚ 0.1 Œ ‚ 1
>
>
> Mauchly Tests for Sphericity
>
> Test statistic p-value
> Shoe 0.72437 0.275329
> Region 0.00032 0.006714
>
>
> Greenhouse-Geisser and Huynh-Feldt Corrections
> for Departure from Sphericity
>
> GG eps Pr(>F[GG])
> Shoe 0.78393 0.2065
> Region 0.37482 8.391e-07 ***
> ---
> Signif. codes: 0 Œ***‚ 0.001 Œ**‚ 0.01 Œ*‚ 0.05 Œ.‚ 0.1 Œ ‚ 1
>
> HF eps Pr(>F[HF])
> Shoe 0.92023 0.2008
> Region 0.54302 5.227e-09 ***
> ---
> Signif. codes: 0 Œ***‚ 0.001 Œ**‚ 0.01 Œ*‚ 0.05 Œ.‚ 0.1 Œ ‚ 1
>
> Everything runs fine, except that Anova will not allow idesign = ~Shoe*Region
>
> So to look for interaction I set up a long format data set with columns Subject, Pressure, Shoe, Region--df now has 240 rows
>
> Then I ran:
>
> pressure.alt.df=read.csv("ShoeDataAltFormat.csv",header=TRUE,sep=",")
> pressure.aovalt=aov(Pressure~(Shoe*Region)+Error(Subject/(Shoe*Region)),data=pressure.alt.df)
>
>> summary(pressure.aovalt)
>
> Error: Subject
> Df Sum Sq Mean Sq F value Pr(>F)
> Residuals 1 2346.6 2346.6
>
> Error: Subject:Shoe
> Df Sum Sq Mean Sq
> Shoe 2 3248 1624.0
>
> Error: Subject:Region
> Df Sum Sq Mean Sq
> Region 7 606772 86682
>
> Error: Subject:Shoe:Region
> Df Sum Sq Mean Sq
> Shoe:Region 14 34345 2453.2
>
> Error: Within
> Df Sum Sq Mean Sq F value Pr(>F)
> Shoe 2 35 17.5 0.0063 0.9937
> Region 7 152734 21819.2 7.8272 2.469e-08 ***
> Shoe:Region 14 15479 1105.6 0.3966 0.9747
> Residuals 192 535219 2787.6
> ---
> Signif. codes: 0 Œ***‚ 0.001 Œ**‚ 0.01 Œ*‚ 0.05 Œ.‚ 0.1 Œ ‚ 1
>
> QUESTIONS:
>
> 1) Why does the Anova function not allow Shoe*Region?
> 2) Does the use of aov provide a correct test for Shoe:Region Interaction?
> 3) The main effect for Shoe from Anova has a denominator df=18; shouldn't that correspond to one of the error terms from aov?
> 4) Is the Anova p-value of 0.1973 for the main effect of Shoe the correct test
>
Notice that much of this comes from the "car" package, which has a maintainer, who is the real expert on what is going on. Anyways
1) You're not saying what goes wrong, nor giving complete instructions for reproduction, but I would kind of suspect that something is unhappy with having a saturated design (0 df for within variation).
2) I think it would, sort of, if specified correctly. It's not giving you the GG and HF corrections though.
3) It should, and aov() is not giving you any residuals except for "Error: Within". Did you forget to make Subject a 10-level factor?
4) Assuming sphericity, yes, I think so; it compares the Shoe main effect to the Shoe:Subject interaction. However, as you have the HF corrected value of 0.2008, why not use it? For Region, that does seem to be of some importance since it fails the sphericity test.
Notice that most of what Anova does can also be done with plain anova(), with a little more legwork. Check ?anova.mlm for a very similar example to yours.
> Any help trying to understand exactly what is happening in Anova versus aov is greatly appreciated. Looking at interaction plots, there does not appear to be a lot going on except for two regions with relatively (compared to other regions) different means for at least one Shoe type within the Region.
>
> Thank you,
> Tamre
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
--
Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
More information about the R-help
mailing list