从输出方差分析(car包)中提取P值列
我正在使用“car”包函数Anova进行一些统计测试 它给出以下输出:从输出方差分析(car包)中提取P值列,r,anova,R,Anova,我正在使用“car”包函数Anova进行一些统计测试 它给出以下输出: Y = cbind(curdata$V1, curdata$V2, curdata$V3) mymdl = lm(Y ~ curdata$V4 + curdata$V5) myanova = Anova(mymdl) Type II MANOVA Tests: Pillai test statistic Df test stat approx F num Df den
Y = cbind(curdata$V1, curdata$V2, curdata$V3)
mymdl = lm(Y ~ curdata$V4 + curdata$V5)
myanova = Anova(mymdl)
Type II MANOVA Tests: Pillai test statistic
Df test stat approx F num Df den Df Pr(>F)
curdata$V4 1 0.27941 2.9728 3 23 0.05280 .
curdata$V5 1 0.33570 3.8743 3 23 0.02228 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
我想提取‘Pr(>F)’列中的值,这样我就可以将这些p值放在另一个矩阵中,以便稍后更正多重比较
我尝试过使用unlist,但它仍然没有提供在列中找到的p值
如果我们有多个响应变量,这是一个
Manova
。我们可以捕获输出并使用正则表达式
as.numeric(sub(".*\\s*(\\d+\\.[0-9e-]+)\\s*[*.]*", "\\1", capture.output(out)[4:5]))
#[1] 8.836e-06 2.200e-16
数据
mymdl可能不是最实用的方法,但是您可以使用separate()
从tidyr
中选择列:
library(car)
library(dplyr)
library(tidyr)
#Code
v1 <- data.frame(capture.output(myanova))
v1 <- v1[3:5,,drop=F]
names(v1)<-'v1'
v2 <- separate(v1,v1,c(paste0('v',1:21)),sep = '\\s')
v2 <- v2[-1,]
警告:如果捕获操作中存在更多列,则需要在必要时更改1:21
。TLDR:
# define helper:
get_summary_for_print <- car:::print.Anova.mlm
body(get_summary_for_print) <- local({tmp <- body(get_summary_for_print);tmp[-(length(tmp)-(0:1))]})
#use it:
get_summary_for_print(Anova(mymdl))$`Pr(>F)`
在本例中,有相当多的代码行用于计算p值。但是,我们可以轻松创建一个修改版的print
函数来返回表(tests
),而不是只打印它(print(tests)
)并返回原始对象(不可见(x)
):
get\u summary\u用于打印测向测试统计数据约F num Df den Df Pr(>F)
#>物种2 0.70215 26.149 6 290<2.2e-16***
#>花瓣长度10.63487 83.461 3 144<2.2e-16***
#> ---
#>签名。代码:0'***'0.001'***'0.01'*'0.05'.'0.1''1
str(获取摘要以便打印(res))
#>类“anova”和“data.frame”:2个obs。共有6个变量:
#>$Df:num 2 1
#>$teststat:num 0.702 0.635
#>约$F:num 26.1 83.5
#>$num Df:num 6 3
#>$den Df:num 290 144
#>$Pr(>F):数字7.96e-25 2.41e-31
#>-属性(*,“标题”)=chr“\n类型II MANOVA测试:Pillai测试统计”
亲爱的Akrun,我已经尝试过了,但它会产生空输出。你知道我做错了什么吗?@pdhami对不起,你能试试正则表达式解决方案吗谢谢,正则表达式解决方案有效@pdhami您可以将代码修改为as.numeric(sub(“.*\\s*”(\\d+\\.[0-9e-]+)\\s*[*.]*”,“\\1”,capture.output(out)[4:5])
works!衷心感谢!非常感谢。此解决方案还提供了所需的输出。最好在原始包中拆分car:::print.Anova.mlm
,但托管在rforgecar
上会使贡献变得困难。
as.numeric(v2$v21)
[1] 8.836e-06 2.200e-16
# define helper:
get_summary_for_print <- car:::print.Anova.mlm
body(get_summary_for_print) <- local({tmp <- body(get_summary_for_print);tmp[-(length(tmp)-(0:1))]})
#use it:
get_summary_for_print(Anova(mymdl))$`Pr(>F)`
function (x, ...)
{
if ((!is.null(x$singular)) && x$singular)
stop("singular error SSP matrix; multivariate tests unavailable\ntry summary(object, multivariate=FALSE)")
test <- x$test
repeated <- x$repeated
ntests <- length(x$terms)
tests <- matrix(NA, ntests, 4)
if (!repeated)
SSPE.qr <- qr(x$SSPE)
for (term in 1:ntests) {
eigs <- Re(eigen(qr.coef(if (repeated) qr(x$SSPE[[term]]) else SSPE.qr,
x$SSP[[term]]), symmetric = FALSE)$values)
tests[term, 1:4] <- switch(test, Pillai = Pillai(eigs,
x$df[term], x$error.df), Wilks = Wilks(eigs, x$df[term],
x$error.df), `Hotelling-Lawley` = HL(eigs, x$df[term],
x$error.df), Roy = Roy(eigs, x$df[term], x$error.df))
}
ok <- tests[, 2] >= 0 & tests[, 3] > 0 & tests[, 4] > 0
ok <- !is.na(ok) & ok
tests <- cbind(x$df, tests, pf(tests[ok, 2], tests[ok, 3],
tests[ok, 4], lower.tail = FALSE))
rownames(tests) <- x$terms
colnames(tests) <- c("Df", "test stat", "approx F", "num Df",
"den Df", "Pr(>F)")
tests <- structure(as.data.frame(tests), heading = paste("\nType ",
x$type, if (repeated)
" Repeated Measures", " MANOVA Tests: ", test, " test statistic",
sep = ""), class = c("anova", "data.frame"))
print(tests, ...)
invisible(x)
}
<bytecode: 0x56032ea80990>
<environment: namespace:car>
get_summary_for_print <- car:::print.Anova.mlm # copy the original print function (inclusive environment)
body(get_summary_for_print) <- # replace the code of our copy
local({ # to avoid pollution of environment by tmp
tmp <- body(get_summary_for_print) # to avoid code duplication
tmp[-(length(tmp)-(0:1))] # remove the last two code lines of the function
})
library(car)
#> Loading required package: carData
res <- Anova(lm(cbind(Sepal.Width, Sepal.Length, Petal.Width) ~ Species + Petal.Length, iris))
res
#>
#> Type II MANOVA Tests: Pillai test statistic
#> Df test stat approx F num Df den Df Pr(>F)
#> Species 2 0.70215 26.149 6 290 < 2.2e-16 ***
#> Petal.Length 1 0.63487 83.461 3 144 < 2.2e-16 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
str(get_summary_for_print(res))
#> Classes 'anova' and 'data.frame': 2 obs. of 6 variables:
#> $ Df : num 2 1
#> $ test stat: num 0.702 0.635
#> $ approx F : num 26.1 83.5
#> $ num Df : num 6 3
#> $ den Df : num 290 144
#> $ Pr(>F) : num 7.96e-25 2.41e-31
#> - attr(*, "heading")= chr "\nType II MANOVA Tests: Pillai test statistic"