knitr::opts_knit$set( stop_on_error = 2L )
library(smargins)
Given the following model:
m <- lm(mpg ~ hp + wt + gear, data = mtcars)
suppose that we wish to examine the effect of hp
by calculating expected values at several levels of hp
while averaging over the observed values of wt
and gear
.
Which do you like better?
smargins_v1(m, hp = quantile(hp))
or
smargins_v2(m, "hp")
?
Now suppose that we want to choose the values of hp ourselves:
smargins_v1(m, hp = seq(50, 350, by = 50))
or
smargins_v2(m, "hp", xvalues = list(hp = seq(50, 350, by = 50)))
Now which do you prefer?
smargins_v1
is more verbose for the simple case, but less so when the user does wish to specify values. Which one should we pick? Or is there some combination or alternative that would be even better?
Currently there is a scompare
function that calculates pairwise differences for each combination of values on a particular predictor variable. For example, we can compare the differences between expected values for each level of gear
as follows:
summary(scompare(smargins(m, gear = sort(unique(gear))), var="gear"))
It would be nice to offer some flexibility here so that comparisons other than pairwise can be made. There are a few different interfaces we might choose from.
Some options include:
car::lht
, and https://github.com/goshevs/effcompcar::lht
emmeans::contrast
A few examples follow below.
summary(scompare_v1(smargins(m, gear = sort(unique(gear))), var = "gear", contrast = c(-1, 0, 1)))
summary(scompare_v1(smargins(m, gear = sort(unique(gear))), var = "gear", contrast = c(-0.5, -0.5, 1.0)))
summary(scompare_v2(smargins(m, gear = sort(unique(gear))), var = "gear", contrast.fun = function(a, b, c) a - b + 0*c))
Naming is a problem...
summary(scompare_v2b(smargins(m, gear = sort(unique(gear))), var = "gear", contrast.funs = list("3 vs 4" = function(a, b, c) a - b + 0*c)))
summary(scompare_v2b(smargins(m, gear = sort(unique(gear))), var = "gear", contrast.funs = list("3 and 4 vs 5" = function(a, b, c) .5*a + .5*b + - c)))
After looking at these examples I'm not sure we have a clear winner yet. Perhaps there is another way that I haven't thought of yet? If you think of one please let me know by opening an issue at https://github.com/izahn/smargins/issues
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.