easystats / modelbased

:chart_with_upwards_trend: Estimate effects, contrasts and means based on statistical models
https://easystats.github.io/modelbased/
GNU General Public License v3.0
234 stars 19 forks source link

Feature Request: Contrasting Simple Slopes #238

Open jmgirard opened 11 months ago

jmgirard commented 11 months ago

Let's say I fit a linear model with a continuous-by-categorical interaction.

fit <- lm(
  formula = Sepal.Width ~ Petal.Length * Species,
  data = iris
)

I can use estimate_slopes() to see what the slope of the continuous variable is for each level of the categorical variable.

modelbased::estimate_slopes(fit, trend = "Petal.Length", at = "Species")
#> Estimated Marginal Effects
#> 
#> Species    | Coefficient |   SE |        95% CI | t(144) |      p
#> -----------------------------------------------------------------
#> setosa     |        0.39 | 0.26 | [-0.13, 0.90] |   1.49 | 0.138 
#> versicolor |        0.37 | 0.10 | [ 0.18, 0.56] |   3.89 | < .001
#> virginica  |        0.23 | 0.08 | [ 0.07, 0.40] |   2.86 | 0.005 
#> Marginal effects estimated for Petal.Length

But I don't think there is currently a way in {modelbased} to contrast those slopes. However, you can do so with emtrends().

emmeans::emtrends(fit, specs = pairwise ~ Species, var = "Petal.Length")
#> $emtrends
#>  Species    Petal.Length.trend     SE  df lower.CL upper.CL
#>  setosa                  0.388 0.2602 144  -0.1264    0.902
#>  versicolor              0.374 0.0961 144   0.1843    0.564
#>  virginica               0.234 0.0819 144   0.0725    0.396
#> 
#> Confidence level used: 0.95 
#> 
#> $contrasts
#>  contrast               estimate    SE  df t.ratio p.value
#>  setosa - versicolor      0.0136 0.277 144   0.049  0.9987
#>  setosa - virginica       0.1535 0.273 144   0.563  0.8400
#>  versicolor - virginica   0.1400 0.126 144   1.108  0.5105
#> 
#> P value adjustment: tukey method for comparing a family of 3 estimates

A {modelbased} wrapper for this functionality would be nice, especially if its arguments were more intuitive than emtrend's.

strengejacke commented 11 months ago

In the long run, the goal is to use marginaleffects as backend (see #214), which will probably go along with adding more features.

For now, and if you're familiar with the syntax of ggeffects, you could also use the hypothesis_test() function. See a series of vignettes and examples starting here: https://strengejacke.github.io/ggeffects/articles/introduction_comparisons_1.html