WinVector / sigr

Concise formatting of significances in R (GPL3 license).
https://winvector.github.io/sigr/
Other
27 stars 2 forks source link

Significance summary report from ezANOVA #1

Closed ajmoralesa closed 7 years ago

ajmoralesa commented 7 years ago

Hello,

I frequently use ezANOVA and I was wondering if sigr would also be suitable to format its output.

I get the following error when testing in in a simple ezANOVAlist object output:

require("ez")
data(ANT)

#Run an ANOVA on the mean correct RT data.
rt_anova = ezANOVA(
  data = ANT[ANT$error==0,]
  , dv = rt
  , wid = subnum
  , within = .(cue,flank)
  , between = group
)

#Show the ANOVA and assumption tests.
print(rt_anova)

cat(render(wrapFTest(rt_anova),
           pLargeCutoff=1))

> cat(render(wrapFTest(rt_anova),
+            pLargeCutoff=1))
Error in UseMethod("wrapFTest") : 
  no applicable method for 'wrapFTest' applied to an object of class "list"

Thank you very much Antonio

JohnMount commented 7 years ago

Antonio,

Currently sigr only accepts lm/glm models, so it won't be able to format the ANOVA results. That being said, you are correct: the ANOVA has the fields needed to populated a sigr result and is an important case. I'll try and take a look at this (probably about 2 weeks) and see if this is an easy extension.

Thanks for filing the issue.

MZLABS commented 7 years ago

(John here again).

It looks like ez includes a very complete ANOVA table. Below is how you format it with the current version of sigr. The difference are in the rounding of the F-statistics (whichsigr did not have a control for, but I am now adding as a new statDigits argument to sigr::render()).

I'll probably eventually add some sort of adapter to make the above a bit easier at some point. But for now please try the following.


library("sigr")
require("ez")
#> Loading required package: ez
data(ANT)

#Run an ANOVA on the mean correct RT data.
rt_anova = ezANOVA(
  data = ANT[ANT$error==0,]
  , dv = rt
  , wid = subnum
  , within = .(cue,flank)
  , between = group
)
#> Warning: Collapsing data to cell means. *IF* the requested effects are a
#> subset of the full design, you must use the "within_full" argument, else
#> results may be inaccurate.

#Show the ANOVA and assumption tests.
print(rt_anova)
#> $ANOVA
#>            Effect DFn DFd           F            p p<.05        ges
#> 2           group   1  18   18.430592 4.377562e-04     * 0.07633358
#> 3             cue   3  54  516.605213 1.005518e-39     * 0.89662286
#> 5           flank   2  36 1350.598810 1.386546e-34     * 0.92710583
#> 4       group:cue   3  54    2.553236 6.497492e-02       0.04110445
#> 6     group:flank   2  36    8.768499 7.900829e-04     * 0.07627434
#> 7       cue:flank   6 108    5.193357 9.938494e-05     * 0.11436699
#> 8 group:cue:flank   6 108    6.377225 9.012515e-06     * 0.13686958
#> 
#> $`Mauchly's Test for Sphericity`
#>            Effect         W         p p<.05
#> 3             cue 0.7828347 0.5366835      
#> 4       group:cue 0.7828347 0.5366835      
#> 5           flank 0.8812738 0.3415406      
#> 6     group:flank 0.8812738 0.3415406      
#> 7       cue:flank 0.1737053 0.1254796      
#> 8 group:cue:flank 0.1737053 0.1254796      
#> 
#> $`Sphericity Corrections`
#>            Effect       GGe        p[GG] p[GG]<.05       HFe        p[HF]
#> 3             cue 0.8652559 1.115029e-34         * 1.0239520 1.005518e-39
#> 4       group:cue 0.8652559 7.472046e-02           1.0239520 6.497492e-02
#> 5           flank 0.8938738 3.763312e-31         * 0.9858964 3.964046e-34
#> 6     group:flank 0.8938738 1.297752e-03         * 0.9858964 8.438369e-04
#> 7       cue:flank 0.6022111 1.546166e-03         * 0.7721473 4.745714e-04
#> 8 group:cue:flank 0.6022111 3.424499e-04         * 0.7721473 7.170939e-05
#>   p[HF]<.05
#> 3         *
#> 4          
#> 5         *
#> 6         *
#> 7         *
#> 8         *

fs <- lapply(seq_len(nrow(rt_anova$ANOVA)),
                  function(rowIndex) {
                    row <- rt_anova$ANOVA[rowIndex, , drop=FALSE]
                    sigr::wrapFTestImpl(row$DFn, row$DFd, row$F)
                  })
names(fs) <- rt_anova$ANOVA$Effect
rt_anova$ANOVA$str <- vapply(fs,
                             function(fi) { 
                               render(fi, 
                                      format= 'ascii',
                                      pLargeCutoff=1, 
                                      sigDigits=4, 
                                      pSmallCutoff=1.0e-6)
                             },
                             character(1))

rt_anova$ANOVA[, c('Effect', 'DFn', 'DFd', 'F', 'p', 'str')]
#>            Effect DFn DFd           F            p
#> 2           group   1  18   18.430592 4.377562e-04
#> 3             cue   3  54  516.605213 1.005518e-39
#> 5           flank   2  36 1350.598810 1.386546e-34
#> 4       group:cue   3  54    2.553236 6.497492e-02
#> 6     group:flank   2  36    8.768499 7.900829e-04
#> 7       cue:flank   6 108    5.193357 9.938494e-05
#> 8 group:cue:flank   6 108    6.377225 9.012515e-06
#>                                                     str
#> 2   F Test summary: (R2=0.51, F(1,18)=18, p=0.0004378).
#> 3  F Test summary: (R2=0.97, F(3,54)=5.2e+02, p<1e-06).
#> 5  F Test summary: (R2=0.99, F(2,36)=1.4e+03, p<1e-06).
#> 4    F Test summary: (R2=0.12, F(3,54)=2.6, p=0.06497).
#> 6  F Test summary: (R2=0.33, F(2,36)=8.8, p=0.0007901).
#> 7 F Test summary: (R2=0.22, F(6,108)=5.2, p=9.938e-05).
#> 8 F Test summary: (R2=0.26, F(6,108)=6.4, p=9.013e-06).
ajmoralesa commented 7 years ago

Thank you very much John. I'll keep myself updated on future versions of sigr.

Antonio

JohnMount commented 7 years ago

I've made a wrapper for ezANOVA: https://winvector.github.io/sigr/reference/wrapFTestezANOVA.html . It is basically a bit of the above work-around moved into the wrapr package. I can't really integrate tighter without introducing dependencies, and ezANOVA doesn't return a class-marked structure (so I can't add fancy S3 inheritance).

library("sigr")
require("ez")
#> Loading required package: ez
data(ANT)

#Run an ANOVA on the mean correct RT data.
rt_anova = ezANOVA(
  data = ANT[ANT$error==0,]
  , dv = rt
  , wid = subnum
  , within = .(cue,flank)
  , between = group
)
#> Warning: Collapsing data to cell means. *IF* the requested effects are a
#> subset of the full design, you must use the "within_full" argument, else
#> results may be inaccurate.

#Show the ANOVA and assumption tests.
print(rt_anova)
#> $ANOVA
#>            Effect DFn DFd           F            p p<.05        ges
#> 2           group   1  18   18.430592 4.377562e-04     * 0.07633358
#> 3             cue   3  54  516.605213 1.005518e-39     * 0.89662286
#> 5           flank   2  36 1350.598810 1.386546e-34     * 0.92710583
#> 4       group:cue   3  54    2.553236 6.497492e-02       0.04110445
#> 6     group:flank   2  36    8.768499 7.900829e-04     * 0.07627434
#> 7       cue:flank   6 108    5.193357 9.938494e-05     * 0.11436699
#> 8 group:cue:flank   6 108    6.377225 9.012515e-06     * 0.13686958
#> 
#> $`Mauchly's Test for Sphericity`
#>            Effect         W         p p<.05
#> 3             cue 0.7828347 0.5366835      
#> 4       group:cue 0.7828347 0.5366835      
#> 5           flank 0.8812738 0.3415406      
#> 6     group:flank 0.8812738 0.3415406      
#> 7       cue:flank 0.1737053 0.1254796      
#> 8 group:cue:flank 0.1737053 0.1254796      
#> 
#> $`Sphericity Corrections`
#>            Effect       GGe        p[GG] p[GG]<.05       HFe        p[HF]
#> 3             cue 0.8652559 1.115029e-34         * 1.0239520 1.005518e-39
#> 4       group:cue 0.8652559 7.472046e-02           1.0239520 6.497492e-02
#> 5           flank 0.8938738 3.763312e-31         * 0.9858964 3.964046e-34
#> 6     group:flank 0.8938738 1.297752e-03         * 0.9858964 8.438369e-04
#> 7       cue:flank 0.6022111 1.546166e-03         * 0.7721473 4.745714e-04
#> 8 group:cue:flank 0.6022111 3.424499e-04         * 0.7721473 7.170939e-05
#>   p[HF]<.05
#> 3         *
#> 4          
#> 5         *
#> 6         *
#> 7         *
#> 8         *

# Show sigr wrap of results
fs <- sigr::wrapFTestezANOVA(rt_anova)
rt_anova$ANOVA$str <- vapply(fs,
                             function(fi) { 
                               render(fi, 
                                      format= 'ascii',
                                      pLargeCutoff=1, 
                                      sigDigits=4, 
                                      pSmallCutoff=1.0e-6)
                             },
                             character(1))

rt_anova$ANOVA[, c('Effect', 'DFn', 'DFd', 'F', 'p', 'str')]
#>            Effect DFn DFd           F            p
#> 2           group   1  18   18.430592 4.377562e-04
#> 3             cue   3  54  516.605213 1.005518e-39
#> 5           flank   2  36 1350.598810 1.386546e-34
#> 4       group:cue   3  54    2.553236 6.497492e-02
#> 6     group:flank   2  36    8.768499 7.900829e-04
#> 7       cue:flank   6 108    5.193357 9.938494e-05
#> 8 group:cue:flank   6 108    6.377225 9.012515e-06
#>                                                       str
#> 2  F Test summary: (R2=0.506, F(1,18)=18.4, p=0.0004378).
#> 3       F Test summary: (R2=0.966, F(3,54)=517, p<1e-06).
#> 5  F Test summary: (R2=0.987, F(2,36)=1.35e+03, p<1e-06).
#> 4    F Test summary: (R2=0.124, F(3,54)=2.55, p=0.06497).
#> 6  F Test summary: (R2=0.328, F(2,36)=8.77, p=0.0007901).
#> 7 F Test summary: (R2=0.224, F(6,108)=5.19, p=9.938e-05).
#> 8 F Test summary: (R2=0.262, F(6,108)=6.38, p=9.013e-06).