R: Tidy least-squares means objects from the lsmeans packages

lsmeans_tidiers {broom} | R Documentation

## Tidy least-squares means objects from the lsmeans packages

### Description

Tidiers for least-squares means objects, which report the predicted means for factors or factor combinations in a linear model. This covers two classes: `lsmobj` and `ref.grid`. (The two objects have slightly different purposes within the package but have similar output).

### Usage

    ## S3 method for class 'lsmobj'
    tidy(x, conf.level = 0.95, ...)

    ## S3 method for class 'ref.grid'
    tidy(x, ...)

### Arguments

`x` | "lsmobj" or "ref.grid" object
`conf.level` | Level of confidence interval, used only for `lsmobj` objects
`...` | Extra arguments, passed on to summary.ref.grid

### Details

There are a large number of arguments that can be passed on to summary.ref.grid. By broom convention, we use `conf.level` to pass the `level` argument.

### Value

A data frame with one observation for each estimated mean, and one column for each combination of factors, along with the following variables:

`estimate` | Estimated least-squares mean
`std.error` | Standard error of estimate
`df` | Degrees of freedom
`conf.low` | Lower bound of confidence interval
`conf.high` | Upper bound of confidence interval

When the input is a contrast, each row will contain one estimated contrast, along with some of the following columns:

`level1` | One level of the factor being contrasted
`level2` | Second level
`contrast` | In cases where the contrast is not made up of two levels, describes each
`statistic` | T-ratio statistic
`p.value` | P-value

### Examples

    if (require("lsmeans", quietly = TRUE)) {
      # linear model for sales of oranges per day
      oranges_lm1 <- lm(sales1 ~ price1 + price2 + day + store, data = oranges)

      # reference grid (see lsmeans vignette)
      oranges_rg1 <- ref.grid(oranges_lm1)
      td <- tidy(oranges_rg1)
      head(td)

      # marginal averages
      marginal <- lsmeans(oranges_rg1, "day")
      tidy(marginal)

      # contrasts
      tidy(contrast(marginal))
      tidy(contrast(marginal, method = "pairwise"))

      # plot confidence intervals
      library(ggplot2)
      ggplot(tidy(marginal), aes(day, estimate)) +
        geom_point() +
        geom_errorbar(aes(ymin = conf.low, ymax = conf.high))

      # by multiple prices
      by_price <- lsmeans(oranges_lm1, "day", by = "price2",
                          at = list(price1 = 50, price2 = c(40, 60, 80),
                          day = c("2", "3", "4")) )
      by_price
      tidy(by_price)

      ggplot(tidy(by_price), aes(price2, estimate, color = day)) +
        geom_line() +
        geom_errorbar(aes(ymin = conf.low, ymax = conf.high))
    }

[Package broom version 0.4.2 Index]