I have a question which I can't find an answer online. When we ask a software (SAS, R,...) to calculate the LSMeans from a linear model, like regression, what is the procedure? Does the model find predicted values and simply calculates the mean, or is it slightly different?
1 Answers
I haven't heard the term LSmeans before, but from looking at the documentation of the lsmeans package, it looks like it simply computes predicted values from a regression.
Suppose you estimate the linear model $Y = \beta_1 X_1 + \beta_2 X_2 + \epsilon$ using OLS, to produce coefficient estimates $\hat{\beta}_1$ and $\hat{\beta}_2$. The LSmeans procedure simply computes the fitted value at some pre-specified values $\tilde{X}_1$ and $\tilde{X}_2$, as follows: $\tilde{Y} = \hat{\beta}_1 \tilde{X}_1 + \hat{\beta}_2 \tilde{X}_2$.
Using R's mtcars dataset, we can regress MPG on weight and an indicator for American-made. Then, we'll use the lsmeans package to get predicted values for both levels of American, holding weight at its mean.
> library(lsmeans)
> data(mtcars)
> mod = lm(mpg ~ wt + factor(am), mtcars)
> lsmeans.result = ref.grid(mod)
> summary(lsmeans.result)
wt am prediction SE df
3.21725 0 20.10022 0.8331837 29
3.21725 1 20.07660 1.0687077 29
We can reproduce the same prediction manually using predict:
> newdat = data.frame(wt = mean(mtcars$wt),
am = c(0, 1))
> manual.result = predict(mod, newdata = newdat)
> manual.result
1 2
20.10022 20.07660
Note that this matches the "prediction" column from the LSmeans result exactly.
- 380
-
They are computed by obtaining predictions on the grid of all factor combinations, then averaging them together if you want marginal results. – Russ Lenth Jun 08 '17 at 21:42