lsmeans: Least Square Means

View source: R/lsmeans.R

lsmeansR Documentation

Least Square Means

Description

Estimates the least square means from a linear model. This is done by generating a prediction from the model using an hypothetical observation that is constructed by averaging the data. See details for more information.

Usage

lsmeans(model, ..., .weights = c("proportional", "equal"))

Arguments

model

A model created by lm.

...

Fixes specific variables to specific values i.e. trt = 1 or age = 50. The name of the argument must be the name of the variable within the dataset.

.weights

Character, specifies whether to use "proportional" or "equal" weighting for each categorical covariate combination when calculating the lsmeans.

Details

The lsmeans are obtained by calculating hypothetical patients and predicting their expected values. These hypothetical patients are constructed by expanding out all possible combinations of each categorical covariate and by setting any numerical covariates equal to the mean.

A final lsmean value is calculated by averaging these hypothetical patients. If .weights equals "proportional" then the values are weighted by the frequency in which they occur in the full dataset. If .weights equals "equal" then each hypothetical patient is given an equal weight regardless of what actually occurs in the dataset.

Use the ... argument to fix specific variables to specific values.

See the references for identical implementations as done in SAS and in R via the emmeans package. This function attempts to re-implement the emmeans derivation for standard linear models but without having to include all of it's dependencies.

References

https://CRAN.R-project.org/package=emmeans

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.3/statug/statug_glm_details41.htm

Examples

## Not run: 
mod <- lm(Sepal.Length ~ Species + Petal.Length, data = iris)
lsmeans(mod)
lsmeans(mod, Species = "virginica")
lsmeans(mod, Species = "versicolor")
lsmeans(mod, Species = "versicolor", Petal.Length = 1)

## End(Not run)

rbmi documentation built on Nov. 24, 2023, 5:11 p.m.