-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
[paper] writing #332
base: main
Are you sure you want to change the base?
[paper] writing #332
Conversation
@strengejacke I think I'll need your help for the marginal section, the different types of marginalization etc. it's quite blurry in my head to be honest ^^ |
@easystats/core-team remaining bits:
|
@DominiqueMakowski where do you want my input, and what should it include? I must admit I have 0 familiarity with |
Your torment over you can make the switch now 😛 Mostly if you could take a look at the technical details section that talks about the backends |
modelbased currently covers classical |
paper/paper.md
Outdated
|
||
The modelbased package simplifies the extraction of these effects, providing a clear interface to understand how different predictors interact with outcomes in various scenarios. | ||
|
||
[details about types of marginalization] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@strengejacke I think here we could mention our 2 main types of marginalization (essentially copypasta our nice docs on that)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can start working/helping on this paper by mid of February, or earlier if it's just minor stuff.
paper/paper.md
Outdated
The term "Least-Squares Means" was somewhat misleading as it suggested a method specific to least-squares estimation, hence its renaming to `emmeans` in 2016 to clarify its scope for a wider range of models including generalized linear models, mixed models, and Bayesian models. | ||
- `marginaleffects` (REF) was more recently introduced and also employs the delta method to approximate variance estimation. It is compatible with a wider range of models and allows for more flexibility in the specification of the marginal effects to be estimated. | ||
|
||
[What's the difference / benefits / drawbacks of using one or ther other?] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mattansb any other interesting facts to mention here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reference for marginaleffects would be https://www.jstatsoft.org/article/view/v111i09
Arel-Bundock, V., Greifer, N., & Heiss, A. (2024). How to Interpret Statistical Models Using marginaleffects for R and Python. Journal of Statistical Software, 111(9), 1–32. https://doi.org/10.18637/jss.v111.i09
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh there's a lot that I can add.
I think the biggest difference would be:
- emmeans used a reference grid (that has by default a "complete" combination of all predictors) to generate expected values, by default conditioning on the mean of numeric values and marginalizing over categorical/binary variables, using a linear function of the model's coefficients (and v-cov matrix to get SEs) to give "predictions at the mean" (predictions for an average observation).
- marginaleffects uses unit level predictions to generate two counterfactual values - the difference of which is then taken (with SEs computed using the delta method), and averaged across all units. By default, the original model frame is used.
Of course, emmeans can also the delta method and can build complex reference grids (that aren't actually "grid" like), and marginaleffects can also generate linear perditions at the mean.
Using the delta method is more computationally costly than using a linear combination (though marginaleffects is very efficient). Using linear combinations with orthogonal "grids" also often means that results from emmeans directly correspond to a models coefficients (which is a benefit for those who are used to looking at regressions tables to understand their models - this can be shown with an example).
...
Would you like me to add all of this in? Just some of this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you like me to add all of this in? Just some of this?
Anything you think is relevant, but I think it'll be good to be quite thorough and detailed here as the inner workings of marginal stuff are not often clearly explained so having details is good!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright - I'll give it some time tomorrow.
and some edits
I've added the expanded "technical details" section. Some general comments: Statement of need I think the "statement of need" needs some restructuring, because the actual need it addresses is that emmeans and marginaleffects are sometimes hard to use - "the Predictions "For generalized linear models (GLMs), while the distinction between prediction and confidence intervals do not apply" - why not? I teach prediction intervals for non-gaussian models. Marginal effects The different "marginals" need to be stated better here - the "marginal" in "marginal means" is not the same "marginal" that is in "marginal effects". |
Since I updated my mixed models slides for teaching after working on modelbased, maybe we can use some of the "definitions" I use on my slides (hopefully, these are accurate): After fitting a model, it is useful generate model-based estimates. Usually, there are three quantities of interest:
The key difference:
Briefly:
|
What would be the difference between these differences? |
library(marginaleffects)
mtcars$vs <- factor(mtcars$vs)
mtcars$gear <- factor(mtcars$gear)
mod <- glm(am ~ vs * hp * gear,
family = binomial(),
data = mtcars)
# unit level differences
comparisons(mod, variables = "vs",
type = "response")
# average differences (mean unit level differences)
avg_comparisons(mod, variables = "vs",
type = "response")
#>
#> Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
#> -0.166 0.195 -0.851 0.395 1.3 -0.547 0.216
#>
#> Term: vs
#> Type: response
#> Comparison: 1 - 0
#>
# marginal means over a regular grid
avg_predictions(mod, variables = "vs",
# a regular grid
newdata = datagrid(hp = mean, gear = levels),
type = "response")
# marginal differences (differences between marginal means)
avg_predictions(mod, variables = "vs",
# a regular grid
newdata = datagrid(hp = mean, gear = levels),
type = "response",
hypothesis = ~ reference)
#>
#> Hypothesis Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
#> 1 - 0 -0.302 0.239 -1.26 0.207 2.3 -0.772 0.167
#>
#> Type: response
#> |
I think we should avoid using the ambiguous term "marginal" as much as possible with regard to effects/slopes/differences (probably still okay with regard to "marginal means" since that can only be the "average" kind, not the "conditional" kind). |
Unless you deal with mixed models, where you can have "marginal" and "conditional" effects/predictions/means (or whatever you'd like to call them). |
Ok, I see, I think your distinction refers to the |
Here are the modelbased equivalents: library(modelbased)
library(marginaleffects)
mtcars$vs <- factor(mtcars$vs)
mtcars$gear <- factor(mtcars$gear)
mod <- glm(am ~ vs * hp * gear, family = binomial(), data = mtcars)
# average differences (mean unit level differences)
avg_comparisons(mod, variables = "vs")
estimate_contrasts(mod, "vs", marginalize = "population")
# marginal means over a regular grid
avg_predictions(mod, variables = "vs", newdata = datagrid(vs = levels, hp = mean, gear = levels))
estimate_means(mod, "vs", predict = "response")
# note the corrected prediction type
avg_predictions(mod, variables = "vs",
newdata = datagrid(vs = levels, hp = mean, gear = levels),
type = "invlink(link)")
estimate_means(mod, "vs")
# marginal differences (differences between marginal means)
avg_predictions(mod, variables = "vs",
newdata = datagrid(hp = mean, gear = levels),
type = "response",
hypothesis = ~ reference)
estimate_contrasts(mod, "vs", comparison = ~ reference) |
We should also probably add in the paper & in the docstrings some examples about when people might want to use the non-default option for |
We may add a vignette, I'd say, which is immediately online and independent from the package version on CRAN? |
Yes, though that is probably independent from throwing a few words in about potential usecases. Did you encounter scenarios where you preferred to use |
It's definitely needed for G-computation (ATE/ATT/ATU - average treatment effects, for the treated and untreated), because you provide a subset of the data with only a specific level of your focal term, and then contrasts usually fail (you only have 1 level - nothing to compare). Then you need library(modelbased)
dat <- marginaleffects::get_dataset("lottery")
dat <- subset(dat, win_big == 1 | win == 0)
dat$win_big <- as.factor(dat$win_big)
mod <- lm(
earnings_post_avg ~ win_big * (
tickets + man + work + age + education + college + year +
earnings_pre_1 + earnings_pre_2 + earnings_pre_3),
data = dat
)
# ATE
estimate_contrasts(mod, "win_big")
#> Marginal Contrasts Analysis
#>
#> Level1 | Level2 | Difference | SE | 95% CI | t(280) | p
#> ----------------------------------------------------------------------
#> 1 | 0 | -5.56 | 2.35 | [-10.19, -0.94] | -2.37 | 0.019
#>
#> Variable predicted: earnings_post_avg
#> Predictors contrasted: win_big
#> Predictors averaged: tickets (2.6), man (0.69), work (0.78), age (53), education (14), college (0.47), year (2e+03), earnings_pre_1 (15), earnings_pre_2 (16), earnings_pre_3 (16)
#> p-values are uncorrected.
# ATT
estimate_contrasts(mod, "win_big", newdata = subset(dat, win_big == 1), estimate = "population")
#> Marginal Contrasts Analysis
#>
#> Level1 | Level2 | Difference | SE | 95% CI | t(280) | p
#> -----------------------------------------------------------------------
#> 1 | 0 | -9.49 | 1.82 | [-13.08, -5.90] | -5.20 | < .001
#>
#> Variable predicted: earnings_post_avg
#> Predictors contrasted: win_big
#> Predictors averaged: tickets (2.6), man (0.69), work (0.78), age (53), education (14), college (0.47), year (2e+03), earnings_pre_1 (15), earnings_pre_2 (16), earnings_pre_3 (16)
#> p-values are uncorrected.
# ATU
estimate_contrasts(mod, "win_big", newdata = subset(dat, win_big == 0), estimate = "population")
#> Marginal Contrasts Analysis
#>
#> Level1 | Level2 | Difference | SE | 95% CI | t(280) | p
#> ---------------------------------------------------------------------
#> 1 | 0 | -4.91 | 2.59 | [-10.01, 0.19] | -1.90 | 0.059
#>
#> Variable predicted: earnings_post_avg
#> Predictors contrasted: win_big
#> Predictors averaged: tickets (2.6), man (0.69), work (0.78), age (53), education (14), college (0.47), year (2e+03), earnings_pre_1 (15), earnings_pre_2 (16), earnings_pre_3 (16)
#> p-values are uncorrected.
# error when estimate not "population"
estimate_contrasts(mod, "win_big", newdata = subset(dat, win_big == 0))
#> Error: Sorry, calculating marginal contrasts failed with following error:
#> Items
#> of 'old' not found in column names: [estimate]. Consider
#> skip_absent=TRUE.
#>
#> It seems that not all required levels of the focal
#> terms are available in the provided data. If you want predictions
#> extrapolated to a hypothetical target population, try setting
#> `estimate="population". (see also https://marginaleffects.com/chapters/gcomputation.html) |
interesting |
This comment was marked as outdated.
This comment was marked as outdated.
It's useful for casual inference and (randomized) trials. Imagine you have 2 or more time points, and some drop outs over time. You could impute missing data, but that ideally requires MCAR. Often, drop out is rather systematic, like lower educated or less healthy persons drop out. Then you can compensate for this drop out either by calculating inverse probability weights or use g-computation to calculate accurate ATE/ATT/ATU. |
Please edit the |
@strengejacke feel free to put yourself wherever though
This comment was marked as resolved.
This comment was marked as resolved.
|
||
## Marginal means and effects | ||
|
||
The concept of "marginal" in statistical modeling typically refers to the analysis of the effect of one or more "focus" variables while all other variables are held constant at specific values |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not quite happy with this first paragraph, because it focuses on "effects" (i.e. slopes), while we want to talk about relationship, comparisons/contrasts and effects/slopes. We should maybe focus on the "averaging" aspect (and maybe even move "contrasts" into an own section? - but that's not very necessary)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol you know best here
**[TODO: details about types of marginalization]** | ||
|
||
|
||
## Technical details |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can shorten this section a bit? The paper becomes quite long, and I'm not sure if the technical details of the backend packages are helpful to understand what modelbased is and how it works, and what's the idea behind our package?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes we can streamline a bit, although in general I like the idea of having slightly more substance in that paper compared to some of our other ones (that whole domain deserves it and it will be useful as a reference to readers)
paper/paper.Rmd
Outdated
|
||
Of the two, `emmeans` [@russell2024emmeans] is the more senior package and was originally known as `lsmeans` (for "Least-Squares Means"). | ||
This term has been historically used to describe what are now more commonly referred to as "Estimated Marginal Means" or EMMs: | ||
predictions made over a regular grid—a counter-factual dataset containing all combinations of the categorical predictors in the model and typically the mean of numerical predictors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced it's always a "counterfactual" data set ;-)
# Examples | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
examples look good to me!
Let's get this bad boi out.
@mattansb @bwiernik what would you write about the two backends, emmeans and marginaleffects, their main differences etc.
Do they both rely on the delta method?