[英]R: How to “extract” the p-values of a Dunnetts-test (Post-Hoc after ANOVA)
我是一名生物化学家,与 R 作为非专业人士一起工作,现在遇到了问题。 我有一个 dataframe,我想比较我的不同治疗组和阳性对照与中等对照。 我想使用的统计检验是方差分析,然后是 Dunnetts 检验。 我为此使用了multcomp
- 和DescTools
- 包,然后我用这段代码到达了那里
Particle <- factor(c("Medium", "PosCon", "Trt1", "Trt2", "Trt3", "Medium", "PosCon", "Trt1", "Trt2", "Trt3", "Medium", "PosCon", "Trt1", "Trt2", "Trt3"))
Values <- c(1.0, 263.0, 3.1, 1.2, 0.9, 1.0, 244.0, 2.4, 1.6, 1.1, 1.0, 255.0, 3.8, 2.0, 0.8)
myDataframe <- data.frame(Particle, Values)
str(myDataframe)
a1 <- aov(Values ~ Particle, data= myDataframe)
summary(a1)
#Output
# Df Sum Sq Mean Sq F value Pr(>F)
#Particle 4 152832 38208 2084 1.48e-14 ***
#Residuals 10 183 18
#---
#Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
myDataframe.dunnett <- glht(a1, linfct = mcp(Particle= "Dunnett"))
myDataframe.dunnett
summary(myDataframe.dunnett)
# Output:
# Simultaneous Tests for General Linear Hypotheses
#
#Multiple Comparisons of Means: Dunnett Contrasts
#
#
#Fit: aov(formula = Values ~ Particle, data = myDataframe)
#
#Linear Hypotheses:
# Estimate Std. Error t value Pr(>|t|)
#PosCon - Medium == 0 253.00000 3.49616 72.365 <0.001 ***
#Trt1 - Medium == 0 2.10000 3.49616 0.601 0.930
#Trt2 - Medium == 0 0.60000 3.49616 0.172 0.999
#Trt3 - Medium == 0 -0.06667 3.49616 -0.019 1.000
#---
#Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#(Adjusted p values reported -- single-step method)
现在我想获取提取的 p 值(或 Pr(>|t|)),并且我想将它们作为四位数字(三个也可以)。 我使用str(summary(myDataframe.dunnett))
和names(summary(myDataframe.dunnett))
来了解要提取什么,但是当我提取它时,它是一个没有数字的数字,因为:
str(summary(myDataframe.dunnett))
names(summary(myDataframe.dunnett)) #just to get to know the names
x <- summary(myDataframe.dunnett)$test$pvalues
x
[1] 0 1 1 1
attr(,"error")
[1] 0.0001612462
有谁知道那是什么,或者知道在 Anova 和 Dunnetts 测试到向量后“提取”显着性水平的更好方法? 我需要那些将它们转换为 plot 上方的重要星。
我觉得这可能会有所帮助,但我不知道如何为我的数据修改它:
谢谢你的帮助!
如果您查看摘要 object 的结构,您可以看到您希望提取的值位于名为test
的列表元素中
str( summary(myDataframe.dunnett) )
List of 10
$ model :List of 13
..$ coefficients : Named num [1:5] 1 253 2.1 0.6 -0.0667
.. ..- attr(*, "names")= chr [1:5] "(Intercept)" "ParticlePosCon" "ParticleTrt1" "ParticleTrt2" ...
..$ residuals : Named num [1:15] -1.48e-15 9.00 1.92e-15 -4.00e-01 -3.33e-02 ...
.. ..- attr(*, "names")= chr [1:15] "1" "2" "3" "4" ...
..$ effects : Named num [1:15] -201.8857 -390.926 -2.8833 -0.8957 0.0816 ...
.. ..- attr(*, "names")= chr [1:15] "(Intercept)" "ParticlePosCon" "ParticleTrt1" "ParticleTrt2" ...
..$ rank : int 5
..$ fitted.values: Named num [1:15] 1 254 3.1 1.6 0.933 ...
.. ..- attr(*, "names")= chr [1:15] "1" "2" "3" "4" ...
..$ assign : int [1:5] 0 1 1 1 1
..$ qr :List of 5
.. ..$ qr : num [1:15, 1:5] -3.873 0.258 0.258 0.258 0.258 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:15] "1" "2" "3" "4" ...
.. .. .. ..$ : chr [1:5] "(Intercept)" "ParticlePosCon" "ParticleTrt1" "ParticleTrt2" ...
.. .. ..- attr(*, "assign")= int [1:5] 0 1 1 1 1
.. .. ..- attr(*, "contrasts")=List of 1
.. .. .. ..$ Particle: chr "contr.treatment"
.. ..$ qraux: num [1:5] 1.26 1.54 1.54 1.53 1.52
.. ..$ pivot: int [1:5] 1 2 3 4 5
.. ..$ tol : num 1e-07
.. ..$ rank : int 5
.. ..- attr(*, "class")= chr "qr"
..$ df.residual : int 10
..$ contrasts :List of 1
.. ..$ Particle: chr "contr.treatment"
..$ xlevels :List of 1
.. ..$ Particle: chr [1:5] "Medium" "PosCon" "Trt1" "Trt2" ...
..$ call : language aov(formula = Values ~ Particle, data = myDataframe)
..$ terms :Classes 'terms', 'formula' language Values ~ Particle
.. .. ..- attr(*, "variables")= language list(Values, Particle)
.. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : chr [1:2] "Values" "Particle"
.. .. .. .. ..$ : chr "Particle"
.. .. ..- attr(*, "term.labels")= chr "Particle"
.. .. ..- attr(*, "order")= int 1
.. .. ..- attr(*, "intercept")= int 1
.. .. ..- attr(*, "response")= int 1
.. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. .. ..- attr(*, "predvars")= language list(Values, Particle)
.. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "factor"
.. .. .. ..- attr(*, "names")= chr [1:2] "Values" "Particle"
..$ model :'data.frame': 15 obs. of 2 variables:
.. ..$ Values : num [1:15] 1 263 3.1 1.2 0.9 1 244 2.4 1.6 1.1 ...
.. ..$ Particle: Factor w/ 5 levels "Medium","PosCon",..: 1 2 3 4 5 1 2 3 4 5 ...
.. ..- attr(*, "terms")=Classes 'terms', 'formula' language Values ~ Particle
.. .. .. ..- attr(*, "variables")= language list(Values, Particle)
.. .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. .. ..$ : chr [1:2] "Values" "Particle"
.. .. .. .. .. ..$ : chr "Particle"
.. .. .. ..- attr(*, "term.labels")= chr "Particle"
.. .. .. ..- attr(*, "order")= int 1
.. .. .. ..- attr(*, "intercept")= int 1
.. .. .. ..- attr(*, "response")= int 1
.. .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. .. .. ..- attr(*, "predvars")= language list(Values, Particle)
.. .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "factor"
.. .. .. .. ..- attr(*, "names")= chr [1:2] "Values" "Particle"
..- attr(*, "class")= chr [1:2] "aov" "lm"
$ linfct : num [1:4, 1:5] 0 0 0 0 1 0 0 0 0 1 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : Named chr [1:4] "PosCon - Medium" "Trt1 - Medium" "Trt2 - Medium" "Trt3 - Medium"
.. .. ..- attr(*, "names")= chr [1:4] "Particle1" "Particle2" "Particle3" "Particle4"
.. ..$ : chr [1:5] "(Intercept)" "ParticlePosCon" "ParticleTrt1" "ParticleTrt2" ...
..- attr(*, "type")= chr "Dunnett"
$ rhs : num [1:4] 0 0 0 0
$ coef : Named num [1:5] 1 253 2.1 0.6 -0.0667
..- attr(*, "names")= chr [1:5] "(Intercept)" "ParticlePosCon" "ParticleTrt1" "ParticleTrt2" ...
$ vcov : num [1:5, 1:5] 6.11 -6.11 -6.11 -6.11 -6.11 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:5] "(Intercept)" "ParticlePosCon" "ParticleTrt1" "ParticleTrt2" ...
.. ..$ : chr [1:5] "(Intercept)" "ParticlePosCon" "ParticleTrt1" "ParticleTrt2" ...
$ df : int 10
$ alternative: chr "two.sided"
$ type : chr "Dunnett"
$ focus : chr "Particle"
$ test :List of 7
..$ pfunction :function (type = c("univariate", "adjusted", p.adjust.methods), ...)
..$ qfunction :function (conf.level, adjusted = TRUE, ...)
..$ coefficients: Named num [1:4] 253 2.1 0.6 -0.0667
.. ..- attr(*, "names")= chr [1:4] "PosCon - Medium" "Trt1 - Medium" "Trt2 - Medium" "Trt3 - Medium"
..$ sigma : Named num [1:4] 3.5 3.5 3.5 3.5
.. ..- attr(*, "names")= chr [1:4] "PosCon - Medium" "Trt1 - Medium" "Trt2 - Medium" "Trt3 - Medium"
..$ tstat : Named num [1:4] 72.3652 0.6007 0.1716 -0.0191
.. ..- attr(*, "names")= chr [1:4] "PosCon - Medium" "Trt1 - Medium" "Trt2 - Medium" "Trt3 - Medium"
..$ pvalues : num [1:4] 0 0.93 0.999 1
.. ..- attr(*, "error")= num 0.000305
..$ type : chr "single-step"
..- attr(*, "class")= chr "mtest"
- attr(*, "class")= chr [1:2] "summary.glht" "glht"
...并且test
列表本身就是一个相当复杂的列表,...
> ( summary(myDataframe.dunnett)$test )
$pfunction
function (type = c("univariate", "adjusted", p.adjust.methods),
...)
{
type <- match.arg(type)
pfct <- function(q) {
switch(object$alternative, two.sided = {
low <- rep(-abs(q), dim)
upp <- rep(abs(q), dim)
}, less = {
low <- rep(q, dim)
upp <- rep(Inf, dim)
}, greater = {
low <- rep(-Inf, dim)
upp <- rep(q, dim)
})
pmvt(lower = low, upper = upp, df = df, corr = cr, ...)
}
switch(object$alternative, two.sided = {
if (df > 0) pvals <- 2 * (1 - pt(abs(tstat), df)) else pvals <- 2 *
(1 - pnorm(abs(tstat)))
}, less = {
if (df > 0) pvals <- pt(tstat, df) else pvals <- pnorm(tstat)
}, greater = {
if (df > 0) pvals <- 1 - pt(tstat, df) else pvals <- 1 -
pnorm(tstat)
})
if (type == "univariate")
return(pvals)
if (type == "adjusted") {
ret <- numeric(length(tstat))
error <- 0
for (i in 1:length(tstat)) {
tmp <- pfct(tstat[i])
if (attr(tmp, "msg") != "Normal Completion" && length(grep("^univariate",
attr(tmp, "msg"))) == 0)
warning(attr(tmp, "msg"))
if (error < attr(tmp, "error"))
error <- attr(tmp, "error")
ret[i] <- tmp
}
ret <- 1 - ret
attr(ret, "error") <- error
return(ret)
}
return(p.adjust(pvals, method = type))
}
<bytecode: 0x55c5419b3f18>
<environment: 0x55c540a7c100>
$qfunction
function (conf.level, adjusted = TRUE, ...)
{
tail <- switch(object$alternative, two.sided = "both.tails",
less = "lower.tail", greater = "upper.tail")
if (adjusted) {
calpha <- qmvt(conf.level, df = df, corr = cr, tail = tail,
...)
}
else {
calpha <- qmvt(conf.level, df = df, corr = matrix(1),
tail = tail, ...)
}
ret <- calpha$quantile
attr(ret, "error") <- calpha$estim.prec
return(ret)
}
<bytecode: 0x55c5419b9d20>
<environment: 0x55c540a7c100>
$coefficients
PosCon - Medium Trt1 - Medium Trt2 - Medium Trt3 - Medium
253.00000000 2.10000000 0.60000000 -0.06666667
$sigma
PosCon - Medium Trt1 - Medium Trt2 - Medium Trt3 - Medium
3.496157 3.496157 3.496157 3.496157
$tstat
PosCon - Medium Trt1 - Medium Trt2 - Medium Trt3 - Medium
72.36517911 0.60065959 0.17161703 -0.01906856
$pvalues
[1] 0.0000000 0.9298994 0.9992779 0.9999999
attr(,"error")
[1] 0.0001864546
$type
[1] "single-step"
attr(,"class")
[1] "mtest"
...并且 p 值位于名为pvalues
的元素中该列表的子列表中:
> ( summary(myDataframe.dunnett)$test$pvalues )
[1] 0.0000000 0.9299389 0.9992772 0.9999999
attr(,"error")
[1] 0.0001414288
... 或使用 DescTools function DunnetTest
(z <- DunnettTest(formula(a1$call)))
## Dunnett's test for comparing several treatments with a control :
## 95% family-wise confidence level
##
## $Medium
## diff lwr.ci upr.ci pval
## PosCon-Medium 253.00000000 242.883587 263.11641 <2e-16 ***
## Trt1-Medium 2.10000000 -8.016413 12.21641 0.9299
## Trt2-Medium 0.60000000 -9.516413 10.71641 0.9993
## Trt3-Medium -0.06666667 -10.183079 10.04975 1.0000
##
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
z$Medium[,"pval"]
## PosCon-Medium Trt1-Medium Trt2-Medium Trt3-Medium
## 0.0000000 0.9298875 0.9992763 0.9999999
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.