afex icon indicating copy to clipboard operation
afex copied to clipboard

afex_plot missmatch when DV is transformed

Open mattansb opened this issue 5 years ago • 2 comments

When data is transformed, afex_plot shows data on transformed scale, but emmeans on response scale:

library(afex)
#> Loading required package: lme4
#> Loading required package: Matrix
#> Registered S3 methods overwritten by 'car':
#>   method                          from
#>   influence.merMod                lme4
#>   cooks.distance.influence.merMod lme4
#>   dfbeta.influence.merMod         lme4
#>   dfbetas.influence.merMod        lme4
#> ************
#> Welcome to afex. For support visit: http://afex.singmann.science/
#> - Functions for ANOVAs: aov_car(), aov_ez(), and aov_4()
#> - Methods for calculating p-values with mixed(): 'KR', 'S', 'LRT', and 'PB'
#> - 'afex_aov' and 'mixed' objects can be passed to emmeans() for follow-up tests
#> - NEWS: library('emmeans') now needs to be called explicitly!
#> - Get and set global package options with: afex_options()
#> - Set orthogonal sum-to-zero contrasts globally: set_sum_contrasts()
#> - For example analyses see: browseVignettes("afex")
#> ************
#> 
#> Attaching package: 'afex'
#> The following object is masked from 'package:lme4':
#> 
#>     lmer

data(stroop, package = "afex")

stroop <- na.omit(stroop)
stroop <- subset(stroop, study == "1")

fit <- aov_ez("pno", "rt", stroop,
              within = c("congruency", "condition"),
              include_aov = FALSE)
#> Warning: More than one observation per cell, aggregating the data using mean
#> (i.e, fun_aggregate = mean)!

fit2 <- aov_ez("pno", "rt", stroop,
               transformation = "log",
              within = c("congruency", "condition"),
              include_aov = FALSE)
#> Warning: More than one observation per cell, aggregating the data using mean
#> (i.e, fun_aggregate = mean)!

afex_plot(fit, ~ congruency)
#> Substituting multivariate/lm model, as aov object missing.
#> Warning: Panel(s) show within-subjects factors, but not within-subjects error bars.
#> For within-subjects error bars use: error = "within"

afex_plot(fit2, ~ congruency)
#> Substituting multivariate/lm model, as aov object missing.
#> Warning: Panel(s) show within-subjects factors, but not within-subjects error bars.
#> For within-subjects error bars use: error = "within"

Created on 2020-06-10 by the reprex package (v0.3.0)

mattansb avatar Jun 10 '20 18:06 mattansb

Here is a work-around for this case:

library(afex)
#> Loading required package: lme4
#> Warning: package 'lme4' was built under R version 4.3.3
#> Loading required package: Matrix
#> ************
#> Welcome to afex. For support visit: http://afex.singmann.science/
#> - Functions for ANOVAs: aov_car(), aov_ez(), and aov_4()
#> - Methods for calculating p-values with mixed(): 'S', 'KR', 'LRT', and 'PB'
#> - 'afex_aov' and 'mixed' objects can be passed to emmeans() for follow-up tests
#> - Get and set global package options with: afex_options()
#> - Set sum-to-zero contrasts globally: set_sum_contrasts()
#> - For example analyses see: browseVignettes("afex")
#> ************
#> 
#> Attaching package: 'afex'
#> The following object is masked from 'package:lme4':
#> 
#>     lmer


data(stroop, package = "afex")

stroop <- na.omit(stroop)
stroop <- subset(stroop, study == "1")

fit2 <- aov_ez("pno", "rt", stroop,
               transformation = "log",
               within = c("congruency", "condition"),
               include_aov = FALSE)
#> Warning: More than one observation per design cell, aggregating data using `fun_aggregate = mean`.
#> To turn off this warning, pass `fun_aggregate = mean` explicitly.

# 1) save ggplot object
p2 <- afex_plot(fit2, ~ congruency)
#> Warning: Panel(s) show within-subjects factors, but not within-subjects error bars.
#> For within-subjects error bars use: error = "within"

# 2) back transform the y column in the first layer (raw data)
p2$layers[[1]]$data$y <- exp(p2$layers[[1]]$data$y)

p2 + ggplot2::labs(y = "rt")

Created on 2024-06-04 with reprex v2.1.0

mattansb avatar Jun 04 '24 06:06 mattansb