[R-sig-ME] Make a 'between-and-within-factors' ANOVA with lmer function

i white i.m.s.white at ed.ac.uk
Wed Mar 27 12:16:14 CET 2013


Vanni, Ben,

You get identical results with aov and lme:

 > foo <- read.table("aov.dat", header = T)
 > aovfit <- aov(Recall ~ Gender*Dosage*Task*Valence + Error(Subject),
+ data = foo)
 > summary(aovfit)

Error: Subject
               Df Sum Sq Mean Sq F value Pr(>F)
Gender         1  542.3   542.3   5.685 0.0345 *
Dosage         2  694.9   347.5   3.643 0.0580 .
Gender:Dosage  2   70.8    35.4   0.371 0.6976
Residuals     12 1144.6    95.4
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Error: Within
                            Df Sum Sq Mean Sq F value   Pr(>F)
Task                        1  96.33   96.33  42.258 1.77e-08 ***
Valence                     2  14.69    7.34   3.221   0.0469 *
Gender:Task                 1   1.33    1.33   0.585   0.4474
Dosage:Task                 2   8.17    4.08   1.791   0.1756
Gender:Valence              2   3.91    1.95   0.857   0.4296
Dosage:Valence              4  20.26    5.06   2.222   0.0773 .
Task:Valence                2   5.39    2.69   1.182   0.3137
Gender:Dosage:Task          2   3.17    1.58   0.695   0.5033
Gender:Dosage:Valence       4   1.04    0.26   0.114   0.9772
Gender:Task:Valence         2   2.17    1.08   0.475   0.6241
Dosage:Task:Valence         4   2.78    0.69   0.305   0.8738
Gender:Dosage:Task:Valence  4   2.67    0.67   0.292   0.8818
Residuals                  60 136.78    2.28
---

 > library(nlme)
 > lmefit <- lme(Recall ~ Gender*Dosage*Task*Valence, random = 
~1|Subject, data = foo)
 > anova(lmefit)
                            numDF denDF   F-value p-value
(Intercept)                    1    60 276.60849  <.0001
Gender                         1    12   5.68527  0.0345
Dosage                         2    12   3.64285  0.0580
Task                           1    60  42.25833  <.0001
Valence                        2    60   3.22096  0.0469
Gender:Dosage                  2    12   0.37113  0.6976
Gender:Task                    1    60   0.58489  0.4474
Dosage:Task                    2    60   1.79123  0.1756
Gender:Valence                 2    60   0.85703  0.4296
Dosage:Valence                 4    60   2.22177  0.0773
Task:Valence                   2    60   1.18197  0.3137
Gender:Dosage:Task             2    60   0.69456  0.5033
Gender:Dosage:Valence          4    60   0.11373  0.9772
Gender:Task:Valence            2    60   0.47522  0.6241
Dosage:Task:Valence            4    60   0.30463  0.8738
Gender:Dosage:Task:Valence     4    60   0.29245  0.8818



On 03/26/2013 10:06 PM, Vanni Rovera wrote:
> Hi Ian, hi Ben,
>
>
> first of all many thanks for your answers. I tested your proposals and I
> will report you the results in a moment. But first let me say that I
> used the dataset linked above, in which we have:
>
>
> DV = Recall
>
> IV1 = Gender (between-factor)
>
> IV2 = Dosage (between-factor)
>
> IV3 = Valence (within-factor)
>
> IV4 = Task (within-factor)
>
>
> The ANOVA fitted with the aov function is the following:
>
>
>> aovRecall<-aov(Recall~(Gender*Dosage*Valence*Task)+Error(Subject/(Valence*Task)))
>
>  > summary(aovRecall)
>
>
> Error: Subject
>
>                Df Sum Sq Mean Sq F value Pr(>F)
>
> Gender         1  542.3   542.3   5.685 0.0345 *
>
> Dosage         2  694.9   347.5   3.643 0.0580 .
>
> Gender:Dosage  2   70.8    35.4   0.371 0.6976
>
> Residuals     12 1144.6    95.4
>
> ---
>
> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
>
> Error: Subject:Valence
>
>                        Df Sum Sq Mean Sq F value Pr(>F)
>
> Valence                2  14.69   7.343   2.998 0.0688 .
>
> Gender:Valence         2   3.91   1.954   0.798 0.4619
>
> Dosage:Valence         4  20.26   5.065   2.068 0.1166
>
> Gender:Dosage:Valence  4   1.04   0.259   0.106 0.9793
>
> Residuals             24  58.78   2.449
>
> ---
>
> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
>
> Error: Subject:Task
>
>                     Df Sum Sq Mean Sq F value   Pr(>F)
>
> Task                1  96.33   96.33  39.862 3.87e-05 ***
>
> Gender:Task         1   1.33    1.33   0.552    0.472
>
> Dosage:Task         2   8.17    4.08   1.690    0.226
>
> Gender:Dosage:Task  2   3.17    1.58   0.655    0.537
>
> Residuals          12  29.00    2.42
>
> ---
>
> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
>
> Error: Subject:Valence:Task
>
>                             Df Sum Sq Mean Sq F value Pr(>F)
>
> Valence:Task                2   5.39  2.6944   1.320  0.286
>
> Gender:Valence:Task         2   2.17  1.0833   0.531  0.595
>
> Dosage:Valence:Task         4   2.78  0.6944   0.340  0.848
>
> Gender:Dosage:Valence:Task  4   2.67  0.6667   0.327  0.857
>
> Residuals                  24  49.00  2.0417
>
>
> Then I tried with your proposals:
>
>
>  > testIan
>
>
> Linear mixed model fit by REML
>
> Formula: Recall ~ (Gender * Dosage * Valence * Task) + (1 | Subject)
>
>   AIC   BIC logLik deviance REMLdev
>
>   424 525.9   -174    418.9     348
>
> Random effects:
>
>   Groups   Name        Variance Std.Dev.
>
>   Subject  (Intercept) 15.5167  3.9391
>
>   Residual              2.2796  1.5098
>
> Number of obs: 108, groups: Subject, 18
>
>
> Fixed effects:
>
>                                     Estimate Std. Error t value
>
> (Intercept)                       1.733e+01  2.435e+00   7.117
>
> GenderM                          -4.667e+00  3.444e+00  -1.355
>
> DosageB                           1.324e-12  3.444e+00   0.000
>
> DosageC                           3.333e+00  3.444e+00   0.968
>
> ValenceNeu                        2.330e-13  1.233e+00   0.000
>
> ValencePos                       -3.333e-01  1.233e+00  -0.270
>
> TaskF                            -1.333e+00  1.233e+00  -1.082
>
> GenderM:DosageB                  -2.667e+00  4.871e+00  -0.547
>
> GenderM:DosageC                   2.000e+00  4.871e+00   0.411
>
> GenderM:ValenceNeu               -2.225e-13  1.743e+00   0.000
>
> GenderM:ValencePos                3.333e-01  1.743e+00   0.191
>
> DosageB:ValenceNeu                6.667e-01  1.743e+00   0.382
>
> DosageC:ValenceNeu                1.000e+00  1.743e+00   0.574
>
> DosageB:ValencePos                2.667e+00  1.743e+00   1.530
>
> DosageC:ValencePos                1.000e+00  1.743e+00   0.574
>
> GenderM:TaskF                    -3.333e-01  1.743e+00  -0.191
>
> DosageB:TaskF                    -1.000e+00  1.743e+00  -0.574
>
> DosageC:TaskF                     3.333e-01  1.743e+00   0.191
>
> ValenceNeu:TaskF                 -6.667e-01  1.743e+00  -0.382
>
> ValencePos:TaskF                  6.667e-01  1.743e+00   0.382
>
> GenderM:DosageB:ValenceNeu        1.000e+00  2.466e+00   0.406
>
> GenderM:DosageC:ValenceNeu        2.372e-13  2.466e+00   0.000
>
> GenderM:DosageB:ValencePos       -3.333e-01  2.466e+00  -0.135
>
> GenderM:DosageC:ValencePos        2.340e-13  2.466e+00   0.000
>
> GenderM:DosageB:TaskF             1.000e+00  2.466e+00   0.406
>
> GenderM:DosageC:TaskF             6.667e-01  2.466e+00   0.270
>
> GenderM:ValenceNeu:TaskF          1.333e+00  2.466e+00   0.541
>
> GenderM:ValencePos:TaskF         -1.333e+00  2.466e+00  -0.541
>
> DosageB:ValenceNeu:TaskF         -1.333e+00  2.466e+00  -0.541
>
> DosageC:ValenceNeu:TaskF         -1.333e+00  2.466e+00  -0.541
>
> DosageB:ValencePos:TaskF         -1.667e+00  2.466e+00  -0.676
>
> DosageC:ValencePos:TaskF         -6.667e-01  2.466e+00  -0.270
>
> GenderM:DosageB:ValenceNeu:TaskF -6.667e-01  3.487e+00  -0.191
>
> GenderM:DosageC:ValenceNeu:TaskF -3.333e-01  3.487e+00  -0.096
>
> GenderM:DosageB:ValencePos:TaskF  2.667e+00  3.487e+00   0.765
>
> GenderM:DosageC:ValencePos:TaskF  3.333e-01  3.487e+00   0.096
>
>
>  > testBen
>
>
> Linear mixed model fit by REML
>
> Formula: Recall ~ (Gender * Dosage * Valence * Task) + (Valence * Task
> |      Subject)
>
>     AIC   BIC logLik deviance REMLdev
>
>   413.5 569.1 -148.8    343.2   297.5
>
> Random effects:
>
>   Groups   Name             Variance Std.Dev. Corr
>
>   Subject  (Intercept)      29.38096 5.4204
>
>            ValenceNeu        6.92965 2.6324   -0.710
>
>            ValencePos       10.70611 3.2720   -0.729  0.915
>
>            TaskF             5.92752 2.4347   -0.820  0.857  0.759
>
>            ValenceNeu:TaskF  3.63960 1.9078    0.832 -0.932 -0.778 -0.930
>
>            ValencePos:TaskF 13.78699 3.7131    0.785 -0.951 -0.950 -0.809
>
>   Residual                   0.17464 0.4179
>
>    0.879
>
> Number of obs: 108, groups: Subject, 18
>
>
> Fixed effects:
>
>                                     Estimate Std. Error t value
>
> (Intercept)                       1.733e+01  3.139e+00   5.522
>
> GenderM                          -4.667e+00  4.439e+00  -1.051
>
> DosageB                           1.562e-11  4.439e+00   0.000
>
> DosageC                           3.333e+00  4.439e+00   0.751
>
> ValenceNeu                        5.690e-12  1.558e+00   0.000
>
> ValencePos                       -3.333e-01  1.920e+00  -0.174
>
> TaskF                            -1.333e+00  1.446e+00  -0.922
>
> GenderM:DosageB                  -2.667e+00  6.278e+00  -0.425
>
> GenderM:DosageC                   2.000e+00  6.278e+00   0.319
>
> GenderM:ValenceNeu               -5.003e-12  2.203e+00   0.000
>
> GenderM:ValencePos                3.333e-01  2.715e+00   0.123
>
> DosageB:ValenceNeu                6.667e-01  2.203e+00   0.303
>
> DosageC:ValenceNeu                1.000e+00  2.203e+00   0.454
>
> DosageB:ValencePos                2.667e+00  2.715e+00   0.982
>
> DosageC:ValencePos                1.000e+00  2.715e+00   0.368
>
> GenderM:TaskF                    -3.333e-01  2.046e+00  -0.163
>
> DosageB:TaskF                    -1.000e+00  2.046e+00  -0.489
>
> DosageC:TaskF                     3.333e-01  2.046e+00   0.163
>
> ValenceNeu:TaskF                 -6.667e-01  1.203e+00  -0.554
>
> ValencePos:TaskF                  6.667e-01  2.197e+00   0.303
>
> GenderM:DosageB:ValenceNeu        1.000e+00  3.115e+00   0.321
>
> GenderM:DosageC:ValenceNeu        5.803e-12  3.115e+00   0.000
>
> GenderM:DosageB:ValencePos       -3.333e-01  3.839e+00  -0.087
>
> GenderM:DosageC:ValencePos        6.274e-12  3.839e+00   0.000
>
> GenderM:DosageB:TaskF             1.000e+00  2.893e+00   0.346
>
> GenderM:DosageC:TaskF             6.667e-01  2.893e+00   0.230
>
> GenderM:ValenceNeu:TaskF          1.333e+00  1.701e+00   0.784
>
> GenderM:ValencePos:TaskF         -1.333e+00  3.108e+00  -0.429
>
> DosageB:ValenceNeu:TaskF         -1.333e+00  1.701e+00  -0.784
>
> DosageC:ValenceNeu:TaskF         -1.333e+00  1.701e+00  -0.784
>
> DosageB:ValencePos:TaskF         -1.667e+00  3.108e+00  -0.536
>
> DosageC:ValencePos:TaskF         -6.667e-01  3.108e+00  -0.215
>
> GenderM:DosageB:ValenceNeu:TaskF -6.667e-01  2.405e+00  -0.277
>
> GenderM:DosageC:ValenceNeu:TaskF -3.333e-01  2.405e+00  -0.139
>
> GenderM:DosageB:ValencePos:TaskF  2.667e+00  4.395e+00   0.607
>
> GenderM:DosageC:ValencePos:TaskF  3.333e-01  4.395e+00   0.076
>
>
> First of all, I have some difficulties in matching these two models with
> the aov one, since these two obtained with lmer function display the
> estimates for the levels of each factor, whereas the aov one displays
> the estimates for the factors themselves.
>
>
> Anyway, the main problem is that none of the two lmer models seems to
> distinguish from between and within factors. The solutions you suggested
> me seems to deal only with the concepts of fixed and random effects.
> Moreover the aov function doesn't make this distinction. I'm beginning
> to think that lmer can't perform an ANOVA.
>
>
> I need to think more about this.
>
>
>
>
> On Sun, Mar 24, 2013 at 6:12 PM, ian m s white <i.m.s.white at ed.ac.uk
> <mailto:i.m.s.white at ed.ac.uk>> wrote:
>
>     I reckon lmer can figure out for itself what is between and what is
>     within subjects, so
>
>     lmer(DV ~ IV1*IV2*IV3*IV4 + (1|Subject))
>
>     should fit the same model as your ANOVA.
>
>
>     On 24 Mar 2013, at 08:56, Vanni Rovera <vanni.rovera at gmail.com
>     <mailto:vanni.rovera at gmail.com>> wrote:
>
>      > Hi there,
>      >
>      > I'm trying to understand how to use the function lmer in order to
>     do a
>      > 'between-and-within-factors' ANOVA, but without any success. I
>     know about
>      > the usage of the function aov, but this holds only for balanced
>     designs;
>      > its documentation say to use lme function (package nlme) for
>     unbalanced
>      > designs. Furthermore I found the lmer function (package lme4) is an
>      > evolution of lme, so I wish to use this last function in order to
>     perform
>      > my ANOVA. But I'm not able to understand how to do this.
>      >
>      > More precisely, imagine you have a dependent variable DV and four
>      > independent variables IV1, IV2, IV3, IV4, where IV1, IV2 are
>      > between-factors and IV3, IV4 are within-factors. Moreover you have a
>      > variable called Subject in order to identify the subject on which
>      > measurements are done (like for example this dataset:
>      > http://personality-project.org/r/datasets/R.appendix5.data). If I
>     use the
>      > aov function, my 'between-and-within-factors' ANOVA would stand
>     as follows:
>      >
>      > aov(DV~(IV1*IV2*IV3*IV4)+Error(Subject/(IV3*IV4))).
>      >
>      > Now can you write me the precise syntax in order to obtain the
>     same result
>      > with the lmer function?
>      >
>      > Thanks a lot in advance!
>      > Vanni Rovera
>      >
>      >
>      >
>      > *Additional details:* The problem is that no one seems to be
>     interested in
>      > explain the relations of 'within-factor' and 'between-factor'
>     concepts with
>      > those of 'fixed-effect' and 'random-effect'. Textbooks and papers
>     about
>      > ANOVA talk about between and within factors, while documentations and
>      > papers about lmer function talk about mixed-effects models, i.e.
>     they talk
>      > about fixed and random effects, without mentioning between and within
>      > factors. *Thus I am not able to understand the relations between
>     the two,
>      > since I think they are completely uncorrelated each others, and
>     hence I am
>      > not able to use the syntax in lmer in order to distinguish
>     between factors
>      > from within factors.*
>      >
>      >       [[alternative HTML version deleted]]
>      >
>      > _______________________________________________
>      > R-sig-mixed-models at r-project.org
>     <mailto:R-sig-mixed-models at r-project.org> mailing list
>      > https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
>      >
>
>
>     --
>     The University of Edinburgh is a charitable body, registered in
>     Scotland, with registration number SC005336.
>
>

-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.



More information about the R-sig-mixed-models mailing list