程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了为什么转换 emmeans 与 data.frame 的对比没有报告正确的 p 值? 可重现的示例:大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决为什么转换 emmeans 与 data.frame 的对比没有报告正确的 p 值? 可重现的示例:

开发过程中遇到为什么转换 emmeans 与 data.frame 的对比没有报告正确的 p 值? 可重现的示例:的问题如何解决?下面主要结合日常开发的经验,给出你关于为什么转换 emmeans 与 data.frame 的对比没有报告正确的 p 值? 可重现的示例:的解决方法建议,希望对你解决为什么转换 emmeans 与 data.frame 的对比没有报告正确的 p 值? 可重现的示例:有所启发或帮助;

我正在运行的对比的 p 值没有正确转换为 data.frame。这是为什么,我该如何解决?

emmeans 的控制台输出:

> pairs(emmeans(lmer.mod,~ Status*Stim*Treatment),simple = "each")
$`simple contrasts for Status`
Stim = 1,Treatment = None:
 contrast               estimate     SE   df t.ratio p.value
 Control - Subclinical  -0.24213 0.0571 57.5 -4.241  0.0002 
 Control - Clinical     -0.16275 0.0571 57.5 -2.851  0.0164 
 Subclinical - Clinical  0.07938 0.0571 57.5  1.390  0.3526 

emmeans 的 data.frame 的控制台输出:

> mod.emm <- pairs(emmeans(lmer.mod,simple = "each")
> as.data.frame(mod.emm)
   Stim Treatment      Status               contrast     estimate         SE       df      t.ratio      p.value
1    1      None           .  Control - Subclinical -0.242125000 0.05709000 57.46544  -4.24111052 3.680551e-03
2    1      None           .     Control - Clinical -0.162750000 0.05709000 57.46544  -2.85076195 2.721389e-01
3    1      None           . Subclinical - Clinical  0.079375000 0.05709000 57.46544   1.39034857 1.000000e+00

可重现的示例:

@H_386_7@model1 <- lm(uptake ~ Type + Treatment + conc + Type*Treatment,data=CO2)

library(emmeans)
pairs(emmeans(model1,~ Type*Treatment),simple="each")
# $`simple contrasts for Type`
# Treatment = nonchilled:
#   contrast             estimate   SE df t.ratio p.value
# Quebec - Mississippi     9.38 1.85 79 5.068   <.0001 
# 
# Treatment = chilled:
#   contrast             estimate   SE df t.ratio p.value
# Quebec - Mississippi    15.94 1.85 79 8.610   <.0001 
# 
# 
# $`simple contrasts for Treatment`
# Type = Quebec:
#   contrast             estimate   SE df t.ratio p.value
# nonchilled - chilled     3.58 1.85 79 1.934   0.0566 
# 
# Type = Mississippi:
#   contrast             estimate   SE df t.ratio p.value
# nonchilled - chilled    10.14 1.85 79 5.477   <.0001 

as.data.frame(pairs(emmeans(model1,simple="each"))
#    Treatment        Type             contrast  estimate       SE df  t.ratio      p.value
# 1 nonchilled           . Quebec - Mississippi  9.380952 1.851185 79 5.067538 1.036140e-05
# 2    chilled           . Quebec - Mississippi 15.938095 1.851185 79 8.609670 2.252161e-12
# 3          .      Quebec nonchilled - chilled  3.580952 1.851185 79 1.934410 2.265719e-01
# 4          . Mississippi nonchilled - chilled 10.138095 1.851185 79 5.476542 1.995066e-06

@H_386_7@model1 <- lm(uptake ~ Type + Treatment + conc + Type*Treatment,data=CO2)
pairs(emmeans(model1,simple="each"))
#    Treatment        Type             contrast  estimate       SE df  t.ratio      p.value
# 1 nonchilled           . Quebec - Mississippi  9.380952 1.851185 79 5.067538 1.036140e-05
# 2    chilled           . Quebec - Mississippi 15.938095 1.851185 79 8.609670 2.252161e-12
# 3          .      Quebec nonchilled - chilled  3.580952 1.851185 79 1.934410 2.265719e-01
# 4          . Mississippi nonchilled - chilled 10.138095 1.851185 79 5.476542 1.995066e-06

来自外部帮助的更新:

“似乎pairs() 的结果本身并不是一个可以转换为数据框的emmGrID 对象,而是一个包含两个emmGrID 对象的列表。如果您从列表中按位置提取这些对象中的任何一个,请使用[[]],像这样,

pairs(emmeans(model1,simple = "each")[[2]]

然后你可以 data.frame() 每个结果,它会是正确的。您最终会得到两个不同的数据帧来保存涉及两个不同变量的对比,但这些数据帧中的每一个都包含正确的 p 值。”

我希望有人对这个问题有更好的解决方法,这样我就可以将所有对比组合到一个 data.frame 中。

解决方法

这可以很容易地完成,但您必须做的是获得基本输出,然后插入正确的 P 值。为了说明这一点,我将展示一个不同的例子,其中一个因素有两个以上的水平。

require(emmeans)
#> Loading required package: emmeans

warp.lm = lm(breaks ~ wool * tension,data = warpbreaks)
(cons = pairs(emmeans(warp.lm,~ wool * tension),simple = "each"))
#> $`simple contrasts for wool`
#> tension = L:
#>  contrast estimate   SE df t.ratio p.value
#>  A - B       16.33 5.16 48  3.167  0.0027 
#> 
#> tension = M:
#>  contrast estimate   SE df t.ratio p.value
#>  A - B       -4.78 5.16 48 -0.926  0.3589 
#> 
#> tension = H:
#>  contrast estimate   SE df t.ratio p.value
#>  A - B        5.78 5.16 48  1.120  0.2682 
#> 
#> 
#> $`simple contrasts for tension`
#> wool = A:
#>  contrast estimate   SE df t.ratio p.value
#>  L - M      20.556 5.16 48  3.986  0.0007 
#>  L - H      20.000 5.16 48  3.878  0.0009 
#>  M - H      -0.556 5.16 48 -0.108  0.9936 
#> 
#> wool = B:
#>  contrast estimate   SE df t.ratio p.value
#>  L - M      -0.556 5.16 48 -0.108  0.9936 
#>  L - H       9.444 5.16 48  1.831  0.1704 
#>  M - H      10.000 5.16 48  1.939  0.1389 
#> 
#> P value adjustment: tukey method for comparing a family of 3 estimates

# get the estimates,etc. into a data frame:
df = as.data.frame(cons)

# get the Tukey-adjusted P values:
pv = unlist(lapply(unlist(cons),function(X) as.data.frame(X)$p.value))

# replace the p values and display
df$p.value = pv
df
#>   tension wool contrast   estimate       SE df    t.ratio      p.value
#> 1       L    .    A - b 16.3333333 5.157299 48  3.1670322 0.0026768025
#> 2       M    .    A - B -4.7777778 5.157299 48 -0.9264108 0.3588672592
#> 3       H    .    A - B  5.7777778 5.157299 48  1.1203107 0.2681556374
#> 4       .    A    L - M 20.5555556 5.157299 48  3.9857208 0.0006572745
#> 5       .    A    L - H 20.0000000 5.157299 48  3.8779987 0.0009185485
#> 6       .    A    M - H -0.5555556 5.157299 48 -0.1077222 0.9936237722
#> 7       .    B    L - M -0.5555556 5.157299 48 -0.1077222 0.9936237722
#> 8       .    B    L - H  9.4444444 5.157299 48  1.8312771 0.1703517915
#> 9       .    B    M - H 10.0000000 5.157299 48  1.9389993 0.1388570254

由 reprex package (v1.0.0) 于 2021 年 3 月 15 日创建

带有 combine = TRUE 的方法不适用于除 adjust = "none" 之外的任何东西,因为家庭规模是所有对比组合的规模。此外,Tukey 方法只能应用于单组成对比较。两组或更多组配对比较组合不构成一组配对比较,因此无法使用 Tukey 方法进行调整。

如果目标是向其他人展示结果,我仍然不建议这样做;因为查看这个数据框会让人非常不清楚 P 值调整是如何进行的,以及对哪些家庭进行了调整。在这个例子中,我们有六个比较系列; cons 的原始注释显示清楚地说明了这一点,而 df 的列表没有。

,

您看到的不同 p 值反映了未调整的 p 值与针对多重比较进行调整的 p 值。

?emmeans::pairs 文档告诉我们:

通常,当 simple 是一个列表或“每个”时,返回值是一个 emm_list 对象,每个条目对应于 简单的。但是,在 combine = TRUE 的情况下,所有元素都被合并 使用单个 emmGrid 对象中的一组对比 rbind.emmGrid.. 在这种情况下,adjust 参数设置调整 组合对比集的方法。

因此,通过可重现的示例,您可以将所有简单的主效应合并到一个数据框中,并将 combine 参数设置为 TRUE。您可以通过设置 adjust 参数在未调整和调整后的 p 值之间进行选择。

@H_386_7@model1 <- lm(uptake ~ Type + Treatment + conc + Type*Treatment,data=CO2)

> pairs(emmeans(model1,~ Type*Treatment),simple = "each",combine = TRUE,+               adjust = "none")
 Treatment  Type        contrast             estimate   SE df t.ratio p.value
 nonchilled .           Quebec - Mississippi     9.38 1.85 79 5.068   <.0001 
 chilled    .           Quebec - Mississippi    15.94 1.85 79 8.610   <.0001 
 .          Quebec      nonchilled - chilled     3.58 1.85 79 1.934   0.0566 
 .          Mississippi nonchilled - chilled    10.14 1.85 79 5.477   <.0001 

这是经过 Bonferroni 调整的一个:

> pairs(emmeans(model1,+               adjust = "bonferroni")
 Treatment  Type        contrast             estimate   SE df t.ratio p.value
 nonchilled .           Quebec - Mississippi     9.38 1.85 79 5.068   <.0001 
 chilled    .           Quebec - Mississippi    15.94 1.85 79 8.610   <.0001 
 .          Quebec      nonchilled - chilled     3.58 1.85 79 1.934   0.2266 
 .          Mississippi nonchilled - chilled    10.14 1.85 79 5.477   <.0001 

P value adjustment: bonferroni method for 4 tests 

大佬总结

以上是大佬教程为你收集整理的为什么转换 emmeans 与 data.frame 的对比没有报告正确的 p 值? 可重现的示例:全部内容,希望文章能够帮你解决为什么转换 emmeans 与 data.frame 的对比没有报告正确的 p 值? 可重现的示例:所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。