tmalves
tmalves

Reputation: 75

How to extract the confidence limits of LSMEANS?

I am using the oranges data provided with lsmeans.

library(lsmeans)
oranges.rg1<-lm(sales1 ~ price1 + price2 + day + store, data = oranges)
days.lsm <- lsmeans(oranges.rg1, "day")
days_contr.lsm <- contrast(days.lsm, "trt.vs.ctrl", ref = c(5,6))   

The confidence intervals can be visualized by ploting plot(contrast(days.lsm, "trt.vs.ctrl", ref = c(5,6))), but they are not showed at days_contr.lsm

> days_contr.lsm
 contrast       estimate       SE df t.ratio p.value
 1 - avg(5,6) -7.8538769 2.194243 23  -3.579  0.0058
 2 - avg(5,6) -6.9234858 2.127341 23  -3.255  0.0125
 3 - avg(5,6)  0.2462789 2.155529 23   0.114  0.9979
 4 - avg(5,6) -4.6760034 2.110761 23  -2.215  0.1184

How can I extract the confidence intervals to a data.frame?

> days_contr.lsm
 contrast       estimate       SE df t.ratio p.value lower.CL upper.CL
 1 - avg(5,6) -7.8538769 2.194243 23  -3.579  0.0058  ?       ?
 2 - avg(5,6) -6.9234858 2.127341 23  -3.255  0.0125  ?       ?
 3 - avg(5,6)  0.2462789 2.155529 23   0.114  0.9979  ?       ?
 4 - avg(5,6) -4.6760034 2.110761 23  -2.215  0.1184  ?       ?

Upvotes: 2

Views: 1202

Answers (2)

Russ Lenth
Russ Lenth

Reputation: 6760

At risk of beating a dead horse, I feel that the main point of the question is getting the confidence intervals, given that what is seen in days_contr.lsm is only the t ratios and P values.

This happened because the default method for summarizing contrast() results is to show tests and not CIs, whereas the default method for summarizing emmeans() results is to show CIs and not tests. The infer argument of summary.emmGrid() controls what you see. Thus, you can get both CIs and tests using

summary(days_contr.lsm, infer = c(TRUE, TRUE))

and this would fill-in the question marks in the OP. The summary() result, by the way, is of class c("summary_emm", "data.frame"); it is a data.frame with a special print method that often shows some additional annotations.

There are additional emmGrid methods confint() and test() that run summary() with infer = c(TRUE, FALSE) and infer = c(FALSE, TRUE) respectively (though both have additional capabilities). The as.data.frame() method is just as.data.frame(summary(...)). For details, see tge help page for emmeans::summary.emmGrid.

Upvotes: 1

tmalves
tmalves

Reputation: 75

confint(contrast(days.lsm, "trt.vs.ctrl", ref = c(5,6))) worked fine

Upvotes: 2

Related Questions