Skip to contents

Native estimated marginal means (least-squares means) for lm and glm fits, following the conventions of emmeans::emmeans() (Lenth): a reference grid crossing every level of each categorical predictor with each numeric covariate held at its mean (or at the values in at); the marginal mean for a cell of specs is the equal-weight average of the grid predictions over the predictors not named in specs, a linear combination \(L b\) with standard error \(\sqrt{L V L'}\), \(V = \) vcov(model). Degrees of freedom are the residual df for lm and gaussian / Gamma glm fits and Inf (asymptotic z) otherwise. Results are on the link scale unless type = "response", which back-transforms the estimate and the interval and scales the SE by the inverse-link derivative (delta method), naming the estimate prob (binomial), rate (poisson) or response. Cross-validated against emmeans in the tests; emmeans is not needed at run time.

Usage

morie_effects_emmeans(model, specs, ...)

Arguments

model

A fitted lm or glm object.

specs

Variables whose marginal means are wanted: a one-sided formula (~ g, ~ g * h, ~ g | h) or a character vector of predictor names.

...

Optional native settings: type ("link", default, or "response"), level (confidence level, default 0.95), at (named list of covariate values for the grid) and by (conditioning variables). Any other argument is an error.

Value

A data frame shaped like summary(emmeans::emmeans(...)): the spec (and by) variables, the estimate (emmean / prob / rate / response), SE, df and lower.CL/upper.CL (asymp.LCL/ asymp.UCL when df = Inf). Attributes carry the estimate name, scale and the linear-function matrix ("linfct"), so a contrast matrix C gives contrasts as the matrix product of C and attr(x, "linfct") times the coefficients. The emmGrid S4 object is no longer returned.

Examples

set.seed(1)
df <- data.frame(y = rnorm(60), x = rnorm(60), g = factor(rep(c("a", "b"), 30)))
fit <- stats::lm(y ~ x + g, data = df)
morie_effects_emmeans(fit, specs = "g")
#>   g     emmean        SE df    lower.CL  upper.CL
#> 1 a  0.2411587 0.1567827 57 -0.07279328 0.5551108
#> 2 b -0.0259260 0.1567827 57 -0.33987803 0.2880260