-
-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add estimate_contrasts
method to report()
#372
Conversation
Getting same error as in #368 :
|
very cool!!! Yes that would make sense for effectsizes to be added directly in modelbased I think |
Just now did I realized that you had started working on this in https://github.com/easystats/report/blob/main/old/report.modelbased.R However, OK, so I'll transfer the effect size code to |
Codecov Report
@@ Coverage Diff @@
## main #372 +/- ##
==========================================
- Coverage 71.90% 71.62% -0.29%
==========================================
Files 46 47 +1
Lines 3300 3313 +13
==========================================
Hits 2373 2373
- Misses 927 940 +13
|
@DominiqueMakowski I have removed Cohen's d calculation and integration from |
Updated reprex. @DominiqueMakowski can I merge this? If so kindly approve the PR :P library(modelbased)
library(report)
model <- lm(Sepal.Width ~ Species, data = iris)
contr <- estimate_contrasts(model)
#> No variable was specified for contrast estimation. Selecting `contrast = "Species"`.
report(contr)
#> The marginal contrasts analysis suggests the following. The difference between
#> setosa and versicolor is positive and statistically significant (difference =
#> 0.66, 95% CI [ 0.49, 0.82], t(147) = 9.69, p < .001). The difference between
#> setosa and virginica is positive and statistically significant (difference =
#> 0.45, 95% CI [ 0.29, 0.62], t(147) = 6.68, p < .001). The difference between
#> versicolor and virginica is negative and statistically significant (difference
#> = -0.20, 95% CI [-0.37, -0.04], t(147) = -3.00, p = 0.003)
report_table(contr)
#> Marginal Contrasts Analysis
#>
#> Level1 | Level2 | Difference | 95% CI | SE | t(147) | p
#> ------------------------------------------------------------------------------
#> setosa | versicolor | 0.66 | [ 0.49, 0.82] | 0.07 | 9.69 | < .001
#> setosa | virginica | 0.45 | [ 0.29, 0.62] | 0.07 | 6.68 | < .001
#> versicolor | virginica | -0.20 | [-0.37, -0.04] | 0.07 | -3.00 | 0.003
#>
#> Marginal contrasts estimated at Species
#> p-value adjustment method: Holm (1979) Created on 2023-05-20 with reprex v2.0.2 |
This LGTM. We can add Cohen's once it's in modelbased. With respect to adjustment, if there is just one categorical predictor (with however many levels), like in the example, then dividing the contrasts by sigma is fine. If there are multiple predictors or any covariates, it's important to re-compute sigma adding back in the response variance associated with the variables that aren't part of the contrast (or else the Cohen's d scale doesn't really make sense) |
Doesn't it? That would be a conditional Cohen's d, no? (Also, tell that to Rouder et al, who define their default priors in |
Oops I missed this. All good!! Amazing thanks a lot |
@bwiernik, my savior 😻, I had to ask 6 reviewers and wait more than a month to get this simple PR approved 🤣🤣 I'll wait for @strengejacke to fix checks on main and then will squash and merge. Will also update (in a new PR) when But am I understanding correctly here that I could open a new PR for |
That should work for a simple Cohen's d implementation. For a more complete solution, you could refit the model with just the variables used in the contrasts and get sigma from that. |
#375 still has issues with snapshot tests. Tests via CI expect "2.93", but locally, it's "2.94", thus there's a mismatch between local and GitHub snapshots |
@rempsyc I don't mind them one at a time, as long as the default is none 🙃 |
@bwiernik for that first scenario, it seems like this is what library(modelbased)
model <- lm(Sepal.Width ~ Species, data = iris)
x <- estimate_contrasts(model)
x$Difference
#> [1] 0.658 0.454 -0.204
new <- x$Difference / stats::sigma(model)
new
#> [1] 1.9370732 1.3365216 -0.6005516
y <- estimate_contrasts(model, effectsize = "emmeans")
old <- y$effect_size
old
#> [1] 1.9370732 1.3365216 -0.6005516 Created on 2023-07-01 with reprex v2.0.2
I'll work on that scenario next. |
So, if I understand correctly, @bwiernik, you mean to extract the sigma used in library(modelbased)
mtcars2 <- mtcars
mtcars2$cyl <- as.factor(mtcars2$cyl)
model <- lm(mpg ~ cyl + wt * hp, mtcars2)
# For a more complete solution, you could refit the model with just the
# variables used in the contrasts and get sigma from that.
# INTERPRETATION: WE REMOVE CYL FROM MODEL AND GET SIGMA FROM THAT!
refit_model <- lm(mpg ~ wt * hp, mtcars2)
refit_sigma <- sigma(refit_model)
estimated <- get_emcontrasts(model, contrast = "cyl")
eff <- emmeans::eff_size(
estimated, sigma = refit_sigma,
edf = stats::df.residual(model), method = "identity")
eff
#> contrast effect.size SE df lower.CL upper.CL
#> (cyl4 - cyl6) 0.5849 0.697 26 -0.847 2.02
#> (cyl4 - cyl8) 0.6756 0.963 26 -1.304 2.66
#> (cyl6 - cyl8) 0.0907 0.697 26 -1.341 1.52
#>
#> sigma used for effect sizes: 2.153
#> Confidence level used: 0.95 Created on 2023-07-02 with reprex v2.0.2 Or did you mean to remove everything from the model EXCEPT the contrast variable? library(modelbased)
mtcars2 <- mtcars
mtcars2$cyl <- as.factor(mtcars2$cyl)
model <- lm(mpg ~ cyl + wt * hp, mtcars2)
# For a more complete solution, you could refit the model with just the
# variables used in the contrasts and get sigma from that.
# INTERPRETATION: WE REMOVE EVERYTHING EXCEPT CYL FROM MODEL AND GET SIGMA FROM THAT!
refit_model <- lm(mpg ~ cyl, mtcars2)
refit_sigma <- sigma(refit_model)
estimated <- get_emcontrasts(model, contrast = "cyl")
eff <- emmeans::eff_size(
estimated, sigma = refit_sigma,
edf = stats::df.residual(model), method = "identity")
eff
#> contrast effect.size SE df lower.CL upper.CL
#> (cyl4 - cyl6) 0.3906 0.465 26 -0.566 1.35
#> (cyl4 - cyl8) 0.4512 0.643 26 -0.871 1.77
#> (cyl6 - cyl8) 0.0606 0.465 26 -0.896 1.02
#>
#> sigma used for effect sizes: 3.223
#> Confidence level used: 0.95 Created on 2023-07-02 with reprex v2.0.2 |
For now, I have added the
estimate_contrasts
effect size (Cohen's d, but does it need to be adjusted for contrasts?) inreport
, to follow theh.test
model, but I wonder whether it should not be done inmodelbased
directly?Reprex so far:
Created on 2023-05-06 with reprex v2.0.2
Fixes #371