Learn R Programming

doBy (version 4.4.0)

popMeans: Calculate population means (LSMEANS in SAS jargon)

Description

Calculate population means (LSMEANS in SAS jargon).

Usage

popMeans(object, effect, at = NULL, only.at=TRUE, engine=esticon,...)
## S3 method for class 'lm':
popMeans(object, effect, at = NULL, only.at=TRUE, engine=esticon,...)

Arguments

object
A model object; currently only available for 'lm' objects
effect
A vector of variables for which the population means should be calculated.
at
A list of covariate values
only.at
If FALSE and at is not NULL then the contrast matrix is formed as if at was not specified. Then the values of at are set afterwards such that the contrast matrix will contain several identical rows.
engine
The function for actually calculating the linear combination of the parameters. See 'details'.
...
Additional arguments; currently not used.

Value

  • A dataframe with a few additional attributes.

Warning

This function is a recent addition to the 'doBy' package and it is not thoroughly tested. Use with caution!

Details

The function is lsMeans() is just an alias for popMeans(). The popMeans() function generates the relevant contrast matrix and then calls the engine on that matrix. Another possible engine is the glht function from the multcomp package

See Also

esticon glht

Examples

Run this code
dat <- expand.grid(list(AA=factor(1:2), BB=factor(1:3), CC=factor(1:3)))
dat$y <- rnorm(nrow(dat))
dat$x <- rnorm(nrow(dat))
dat$x2 <- dat$x^2

## Examples

## 1) LSMEANS with factors only.
mod1 <- lm(y ~ AA + BB*CC + x, data=dat)
## Average over AA for each combination of (BB,CC); evaluate at x=mean(x)
popMeans(mod1, c("BB","CC"))
## Average over (AA,BB) for each value of CC; evaluate at x=mean(x)
popMeans(mod1, c("CC"))

## 2) The call to popMeans() below is equivalent to the following SAS code
## proc glm data=dat;
##	class AA BB CC;
##	model y = AA BB|CC x x*x;
##  lsmeans CC BB*CC / stderr;
## run;
##
mod2 <- lm(y ~ AA + BB*CC + x + x2, data=dat)
popMeans(mod2, "CC")

## Notice the difference to:
mod3 <- lm(y ~ AA + BB*CC + x + I(x^2), data=dat)
popMeans(mod3, "CC")

## The difference arises because in the former case, x2 is evaluated at mean(x2) whereas
## in the latter case x is evaluated at mean(x)^2

## 3) Plug in particular values of covariates
## The call to popMeans() below is equivalent to the following SAS code
## proc glm data=dat;
##	class AA BB CC;
##	model y = AA BB|CC x x2;
##  lsmeans CC BB*CC / at x=2 stderr;
## run;
popMeans(mod2, c("CC"), at=list(x=2))
## Above, x=2 is used while x2 is set to mean(x2)

## The call to popMeans() below is equivalent to the following SAS code
## proc glm data=dat;
##	class AA BB CC;
##	model y = AA BB|CC x x*x;
##    lsmeans CC BB*CC / at x=2 stderr;
## run;
##
popMeans(mod3, c("CC"), at=list(x=2))
## Above, x=2 is used while I(x^2) is set to mean(x^2)=4. Notice that setting
## popMeans(mod3, c("CC"), at=list(x=2,"I(x^2)"=123))
## has no effect: I(x^2) is still 4 because x=2. Hence the following two results
## are identical

popMeans(mod2, c("CC"), at=list(x=2, x2=4))
popMeans(mod3, c("CC"), at=list(x=2))

## END

Run the code above in your browser using DataLab