操纵R中输出的p值的值

时间:2017-04-28 23:57:41

标签: r p-value

假设

data11 <- c(0.5388417, 0.7263466, 0.3612457, 0.2495263, 0.1780413)
data22 <- c(0.674262245, 0.659560230, 0.001548212, 0.501228052, 0.802885484)
d=as.data.frame(cbind(dat=c(data11,data22),level=c(rep(1,length(data11)),rep(2,length(data22)))))

然后,我使用函数oneway_test来获得p-p值

library(coin)
oneway_test(d$dat~as.factor(d$level))

我明白了:

    Asymptotic Two-Sample Fisher-Pitman Permutation Test

data:  d$dat by as.factor(d$level) (1, 2)
Z = -0.70257, p-value = 0.4823
alternative hypothesis: true mu is not equal to 0

但是,如何保存P值的值? 。尝试这样做:d$p.value。我不明白

由于

2 个答案:

答案 0 :(得分:0)

我想这不是直截了当的,因为打印结果(p值)不包含在函数oneway_test的返回对象中,而是作为副作用打印。您可以使用一些正则表达式作为解决方法:

output <- oneway_test(d$dat~as.factor(d$level))

FindPValue = function(output){
  temp <- capture.output(output)
  return(as.numeric(gsub(".*p-value = ", "", temp[5])))
}

FindPValue(output)
#### [1] 0.4823

感谢Adam Quek对capture.output

的建议

答案 1 :(得分:0)

检查硬币包的文档。您可以使用pvalue函数返回pvalue:

results <- oneway_test(d$dat~as.factor(d$level))
pval <- pvalue(results)