Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save strengejacke/862f9293b708baf6e06242e868ac3182 to your computer and use it in GitHub Desktop.
Save strengejacke/862f9293b708baf6e06242e868ac3182 to your computer and use it in GitHub Desktop.
comparisons vs contrasts
library(easystats)
#> # Attaching packages: easystats 0.4.3
#> ✔ insight 0.17.0.6 ✔ datawizard 0.4.0.14
#> ✔ bayestestR 0.12.1 ✔ performance 0.9.0.3
#> ✔ parameters 0.17.0.9 ✔ effectsize 0.6.0.7
#> ✔ modelbased 0.8.0 ✔ correlation 0.8.0.1
#> ✔ see 0.7.0.2 ✔ report 0.5.1.1
library(emmeans)
library(marginaleffects)
data(efc, package = "datawizard")
efc <- data_to_factor(efc, select = c("c172code", "e16sex"), append = TRUE)
efc$c172code_f <- data_recode(efc$c172code_f, list(`low` = 1:2, `high` = 3))
m <- lm(neg_c_7 ~ c172code_f * e16sex_f, data = efc)
emmeans(m, c("c172code_f", "e16sex_f")) |>
contrast(method = "pairwise") |>
model_parameters()
#> contrast | Coefficient | SE | 95% CI | t(83) | p
#> ---------------------------------------------------------------------
#> high 1 - low 1 | 0.53 | 2.27 | [-3.99, 5.04] | 0.23 | > .999
#> high 1 - high 2 | 0.85 | 2.55 | [-4.22, 5.92] | 0.33 | > .999
#> high 1 - low 2 | 2.83 | 2.27 | [-1.68, 7.34] | 1.25 | 0.811
#> low 1 - high 2 | 0.32 | 1.54 | [-2.74, 3.38] | 0.21 | > .999
#> low 1 - low 2 | 2.30 | 1.01 | [ 0.30, 4.31] | 2.29 | 0.212
#> high 2 - low 2 | 1.98 | 1.53 | [-1.07, 5.03] | 1.29 | 0.789
#>
#> p-value adjustment method: Tukey
comparisons(
m,
c("c172code_f", "e16sex_f"),
newdata = get_datagrid(m, at = c("c172code_f", "e16sex_f")),
# same as: datagrid(model = m, c172code_f = c("low", "high"), e16sex_f = c(1, 2)),
contrast_factor = "pairwise"
)
#> rowid term contrast comparison std.error c172code_f e16sex_f
#> 1: 1 c172code_f low - high -0.5277778 2.269220 high 1
#> 2: 2 c172code_f low - high -0.5277778 2.269220 low 1
#> 3: 3 c172code_f low - high -1.9810811 1.534532 high 2
#> 4: 4 c172code_f low - high -1.9810811 1.534532 low 2
#> 5: 1 e16sex_f 2 - 1 -0.8500000 2.547193 high 1
#> 6: 2 e16sex_f 2 - 1 -2.3033033 1.007946 low 1
#> 7: 3 e16sex_f 2 - 1 -0.8500000 2.547193 high 2
#> 8: 4 e16sex_f 2 - 1 -2.3033033 1.007946 low 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment