How to set confidence intervals in "emmeans" - r

I am have been working with the emmeans package to create an estimated marginal means for my data at .95% confidence level. Although I cannot seem to change it to .99% confidence level. Any help would be much appreciated. Usually I would use the "levels=" function but it does not seem to exist for emmeans.
library(emmeans)
emmeans(AcidLevels1, specs=~MAScore,)
Best,
-Nathan

emmeans provides method confint.emmGrid to recalculate confidence intervals, and (probably more importantly) also adjust for multiple hypothesis testing.
As you don't provide sample data, here is an example using the warpbreaks data.
library(emmeans)
lm <- lm(breaks ~ wool * tension, data = warpbreaks)
emm <- emmeans(lm, ~ wool | tension);
To recalculate confidence intervals at the 99% level (without correcting for multiple testing) do
confint(emm, adjust = "none", level = 0.99)
#tension = L:
# wool emmean SE df lower.CL upper.CL
# A 44.55556 3.646761 48 34.77420 54.33691
# B 28.22222 3.646761 48 18.44086 38.00358
#
#tension = M:
# wool emmean SE df lower.CL upper.CL
# A 24.00000 3.646761 48 14.21864 33.78136
# B 28.77778 3.646761 48 18.99642 38.55914
#
#tension = H:
# wool emmean SE df lower.CL upper.CL
# A 24.55556 3.646761 48 14.77420 34.33691
# B 18.77778 3.646761 48 8.99642 28.55914
#
#Confidence level used: 0.99
To recalculate CIs at the 99% level and correct for multiple hypothesis testing using the Bonferroni correction you can do
confint(emm, adjust = "bonferroni", level = 0.99)
#tension = L:
# wool emmean SE df lower.CL upper.CL
# A 44.55556 3.646761 48 33.82454 55.28657
# B 28.22222 3.646761 48 17.49120 38.95324
#
#tension = M:
# wool emmean SE df lower.CL upper.CL
# A 24.00000 3.646761 48 13.26898 34.73102
# B 28.77778 3.646761 48 18.04676 39.50880
#
#tension = H:
# wool emmean SE df lower.CL upper.CL
# A 24.55556 3.646761 48 13.82454 35.28657
# B 18.77778 3.646761 48 8.04676 29.50880
#
#Confidence level used: 0.99
#Conf-level adjustment: bonferroni method for 2 estimates

Related

What is the difference between adjusting pvalues in a "pairs" function and extracting pvalues to a vector and then adjust, different outcomes

When I adjust values within a "pairs" function and when I extract pvalues and adjust I get different outcome. What is actually happening in the pairs function? Is there a right way to perform it?
rm(list=ls())
id <- rep(1:5, each=3)
trt <- rep(LETTERS[1:3],5)
set.seed(1)
q1 <- runif(15)
set.seed(2)
q2 <- runif(15)
set.seed(3)
q3 <- runif(15)
df <- data.frame(id,trt,q1,q2,q3)
library(lme4)
lm <- lmer(formula= df[,3] ~trt+ (1|id), data=df)
Anova(lm)
emm <- emmeans(lm,"trt")
a <- pairs(emm) # no adjustment
a
contrast estimate SE df t.ratio p.value
A - B 0.2085 0.134 8 1.560 0.3159
A - C -0.0359 0.134 8 -0.269 0.9612
B - C -0.2444 0.134 8 -1.829 0.2211
Note: contrasts are still on the [ scale
Degrees-of-freedom method: kenward-roger
P value adjustment: tukey method for comparing a family of 3 estimates
b <- pairs(emm, adjust="bonferroni") # adjust with bonferroni
b
contrast estimate SE df t.ratio p.value
A - B 0.2085 0.134 8 1.560 0.4721
A - C -0.0359 0.134 8 -0.269 1.0000
B - C -0.2444 0.134 8 -1.829 0.3145
Note: contrasts are still on the [ scale
Degrees-of-freedom method: kenward-roger
P value adjustment: bonferroni method for 3 tests
e <- data.frame(a)
e <- e$p.value
e <- p.adjust(e, method="bonferroni") # extract p-values and adjust
e
[1] 0.9475749 1.0000000 0.6633197

Is the emmeans (R) intercept-only function broken?

I've noticed that emmeans (in R) isn't working for an intercept-only estimate after the latest update.
Reproducible example:
test=lm(mpg~1,mtcars)
library(emmeans)
emmeans::emmeans(test,~1)
The output on 2 of my machines (windows and Linux) is:
> emmeans::emmeans(test,~1)
Error in `[[<-.data.frame`(`*tmp*`, ".wgt.", value = 2) :
replacement has 1 row, data has 0
Is this a known issue, or have I messed up my system somehow?
This used to work I believe.
It does work if you include a variable:
test2=lm(mpg~as.factor(cyl),mtcars)
emmeans(test2,~cyl)
Thanks very much for the help in advance.
It turns out that the fix for issue #197 -- and incorporated in CRAN version 1.47 -- created the issue (#206) that we see here. I think I have them both fixed now:
require(emmeans)
## Loading required package: emmeans
#206...
warp.lm <- lm(breaks ~ wool * tension, data = warpbreaks)
emmeans(warp.lm, "1")
## 1 emmean SE df lower.CL upper.CL
## overall 28.1 1.49 48 25.2 31.1
##
## Results are averaged over the levels of: wool, tension
## Confidence level used: 0.95
emmeans(warp.lm, "1", by = "wool")
## wool = A:
## 1 emmean SE df lower.CL upper.CL
## overall 31.0 2.11 48 26.8 35.3
##
## wool = B:
## 1 emmean SE df lower.CL upper.CL
## overall 25.3 2.11 48 21.0 29.5
##
## Results are averaged over the levels of: tension
## Confidence level used: 0.95
#197...
model <- lm(Sepal.Length ~ poly(Petal.Length,2), data = iris)
emtrends(model, ~ 1, "Petal.Length", max.degree = 2)
## degree = linear:
## 1 Petal.Length.trend SE df lower.CL upper.CL
## overall 0.4474 0.0180 147 0.4119 0.483
##
## degree = quadratic:
## 1 Petal.Length.trend SE df lower.CL upper.CL
## overall 0.0815 0.0132 147 0.0554 0.108
##
## Confidence level used: 0.95
Created on 2020-06-01 by the reprex package (v0.3.0)
Users who need this now can install from github via
remotes::install_github("rvlenth/emmeans")
It is working fine with emmeans - 1.4.6 on macOS Catalina 10.15.4 and R 4.0
emmeans::emmeans(test,~1)
# 1 emmean SE df lower.CL upper.CL
# overall 20.1 1.07 31 17.9 22.3
#Confidence level used: 0.95

Estimating effect size with emmenas for post hoc

Is there a way to have effect size (such as Cohen's d or the most appropriate) directly using emmeans()?
I cannot find anything for obtaining effect size by using emmeans()
post <- emmeans(fit, pairwise~ favorite.pirate | sex)
emmip(fit, ~ favorite.pirate | sex)
There is not a built-in provision for effect-size calculations, but you can cobble one together by defining a custom contrast function that divides each pairwise comparison by a value of sigma:
mypw.emmc = function(..., sigma = 1) {
result = emmeans:::pairwise.emmc (...)
for (i in seq_along(result[1, ]))
result[[i]] = result[[i]] / sigma
result
}
Here's a test run:
> mypw.emmc(1:3, sigma = 4)
1 - 2 1 - 3 2 - 3
1 0.25 0.25 0.00
2 -0.25 0.00 0.25
3 0.00 -0.25 -0.25
With your model, the error SD is 9.246 (look at summary(fit); so, ...
> emmeans(fit, mypw ~ sex, sigma = 9.246, name = "effect.size")
NOTE: Results may be misleading due to involvement in interactions
$emmeans
sex emmean SE df lower.CL upper.CL
female 63.8 0.434 3.03 62.4 65.2
male 74.5 0.809 15.82 72.8 76.2
other 68.8 1.439 187.08 65.9 71.6
Results are averaged over the levels of: favorite.pirate
Degrees-of-freedom method: kenward-roger
Confidence level used: 0.95
$contrasts
effect.size estimate SE df t.ratio p.value
female - male -1.158 0.0996 399 -11.624 <.0001
female - other -0.537 0.1627 888 -3.299 0.0029
male - other 0.621 0.1717 981 3.617 0.0009
Results are averaged over the levels of: favorite.pirate
Degrees-of-freedom method: kenward-roger
P value adjustment: tukey method for comparing a family of 3 estimates
Some words of caution though:
The SEs of the effect sizes are misleading because they don't account for the variation in sigma.
This is not a very good example because
a. The factors interact (Edward Low is different in his profile).
Also, see the warning message.
b. The model is singular (as warned when the model was fitted), yielding an estimated variance of zero for college)
library(yarrr)
View(pirates)
library(lme4)
library(lmerTest)
fit <- lmer(weight~ favorite.pirate * sex +(1|college), data = pirates)
anova(fit, ddf = "Kenward-Roger")
post <- emmeans(fit, pairwise~ sex)
post

Can I use emmeans with LME model?

I am using LME model defined like:
mod4.lme <- lme(pRNFL ~ Init.Age + Status + I(Time^2), random= ~1|Patient/EyeID,data = long1, na.action = na.omit)
The output is:
> summary(mod4.lme)
Linear mixed-effects model fit by REML
Data: long1
AIC BIC logLik
2055.295 2089.432 -1018.647
Random effects:
Formula: ~1 | Patient
(Intercept)
StdDev: 7.949465
Formula: ~1 | EyeID %in% Patient
(Intercept) Residual
StdDev: 12.10405 2.279917
Fixed effects: pRNFL ~ Init.Age + Status + I(Time^2)
Value Std.Error DF t-value p-value
(Intercept) 97.27827 6.156093 212 15.801950 0.0000
Init.Age 0.02114 0.131122 57 0.161261 0.8725
StatusA -27.32643 3.762155 212 -7.263504 0.0000
StatusF -23.31652 3.984353 212 -5.852023 0.0000
StatusN -0.28814 3.744980 57 -0.076940 0.9389
I(Time^2) -0.06498 0.030223 212 -2.149921 0.0327
Correlation:
(Intr) Int.Ag StatsA StatsF StatsN
Init.Age -0.921
StatusA -0.317 0.076
StatusF -0.314 0.088 0.834
StatusN -0.049 -0.216 0.390 0.365
I(Time^2) -0.006 -0.004 0.001 -0.038 -0.007
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-2.3565641 -0.4765840 0.0100608 0.4670792 2.7775392
Number of Observations: 334
Number of Groups:
Patient EyeID %in% Patient
60 119
I wanted to get comparisons between my 'Status' factors (named A, N, F and H). So I did a emmeans model using this code:
emmeans(mod4.lme, pairwise ~ Status, adjust="bonferroni")
The output for this, is:
> emmeans(mod4.lme, pairwise ~ Status, adjust="bonferroni")
$emmeans
Status emmean SE df lower.CL upper.CL
H 98.13515 2.402248 57 93.32473 102.94557
A 70.80872 2.930072 57 64.94135 76.67609
F 74.81863 3.215350 57 68.38000 81.25726
N 97.84701 2.829706 57 92.18062 103.51340
Degrees-of-freedom method: containment
Confidence level used: 0.95
$contrasts
contrast estimate SE df t.ratio p.value
H - A 27.3264289 3.762155 212 7.264 <.0001
H - F 23.3165220 3.984353 212 5.852 <.0001
H - N 0.2881375 3.744980 57 0.077 1.0000
A - F -4.0099069 2.242793 212 -1.788 0.4513
A - N -27.0382913 4.145370 57 -6.523 <.0001
F - N -23.0283844 4.359019 57 -5.283 <.0001
The answer is yes, emmeans does the calculation based on the model

Emmeans in LME and adjusting for "time since" [duplicate]

I am using LME model defined like:
mod4.lme <- lme(pRNFL ~ Init.Age + Status + I(Time^2), random= ~1|Patient/EyeID,data = long1, na.action = na.omit)
The output is:
> summary(mod4.lme)
Linear mixed-effects model fit by REML
Data: long1
AIC BIC logLik
2055.295 2089.432 -1018.647
Random effects:
Formula: ~1 | Patient
(Intercept)
StdDev: 7.949465
Formula: ~1 | EyeID %in% Patient
(Intercept) Residual
StdDev: 12.10405 2.279917
Fixed effects: pRNFL ~ Init.Age + Status + I(Time^2)
Value Std.Error DF t-value p-value
(Intercept) 97.27827 6.156093 212 15.801950 0.0000
Init.Age 0.02114 0.131122 57 0.161261 0.8725
StatusA -27.32643 3.762155 212 -7.263504 0.0000
StatusF -23.31652 3.984353 212 -5.852023 0.0000
StatusN -0.28814 3.744980 57 -0.076940 0.9389
I(Time^2) -0.06498 0.030223 212 -2.149921 0.0327
Correlation:
(Intr) Int.Ag StatsA StatsF StatsN
Init.Age -0.921
StatusA -0.317 0.076
StatusF -0.314 0.088 0.834
StatusN -0.049 -0.216 0.390 0.365
I(Time^2) -0.006 -0.004 0.001 -0.038 -0.007
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-2.3565641 -0.4765840 0.0100608 0.4670792 2.7775392
Number of Observations: 334
Number of Groups:
Patient EyeID %in% Patient
60 119
I wanted to get comparisons between my 'Status' factors (named A, N, F and H). So I did a emmeans model using this code:
emmeans(mod4.lme, pairwise ~ Status, adjust="bonferroni")
The output for this, is:
> emmeans(mod4.lme, pairwise ~ Status, adjust="bonferroni")
$emmeans
Status emmean SE df lower.CL upper.CL
H 98.13515 2.402248 57 93.32473 102.94557
A 70.80872 2.930072 57 64.94135 76.67609
F 74.81863 3.215350 57 68.38000 81.25726
N 97.84701 2.829706 57 92.18062 103.51340
Degrees-of-freedom method: containment
Confidence level used: 0.95
$contrasts
contrast estimate SE df t.ratio p.value
H - A 27.3264289 3.762155 212 7.264 <.0001
H - F 23.3165220 3.984353 212 5.852 <.0001
H - N 0.2881375 3.744980 57 0.077 1.0000
A - F -4.0099069 2.242793 212 -1.788 0.4513
A - N -27.0382913 4.145370 57 -6.523 <.0001
F - N -23.0283844 4.359019 57 -5.283 <.0001
The answer is yes, emmeans does the calculation based on the model

Resources