表 1 在 R 包中没有给出正确的 p 值

问题描述 投票:0回答:1

通过以下 R 帮助文档给出的 p 值函数没有给我正确的 p 值

https://cran.r-project.org/web/packages/table1/vignettes/table1-examples.html

library(table1)

pvalue <- function(x, ...) {
  # Construct vectors of data y, and groups (strata) g
  y <- unlist(x)
  g <- factor(rep(1:length(x), times=sapply(x, length)))
  if (is.numeric(y)) {
    # For numeric variables, perform a standard 2-sample t-test
    p <- kruskal.test(y ~ g)$p.value
  } else {
    # For categorical variables, perform a chi-squared test of independence
    p <- chisq.test(table(y, g))$p.value
  }
  # Format the p-value, using an HTML entity for the less-than sign.
  # The initial empty string places the output on the line below the variable label.
  c("", sub("<", "&lt;", format.pval(p, digits=3, eps=0.001)))
}


Table1 <- table1(~ mpg+disp+hp+drat+wt+qsec+vs+am+gear+carb | as.factor(cyl)
             , data=mtcars, extra.col=list(`P-value`=pvalue), render.missing=NULL, render.categorical="FREQ (PCTnoNA%)",
             topclass="Rtable1-zebra")
4(N=11) 6(N=7) 8(N=14) 总体(N=32) P 值
该死
平均值(SD) 4.07 (0.365) 3.59 (0.476) 3.23 (0.372) 3.60 (0.535) 0.00216
齿轮
平均值(SD) 4.09 (0.539) 3.86 (0.690) 3.29 (0.726) 3.69 (0.738) 0.016
kruskal.test(mtcars$drat, mtcars$cyl)

Kruskal-Wallis rank sum test

data:  mtcars$drat and mtcars$cyl
Kruskal-Wallis chi-squared = 14.395, df = 2, p-value = **0.0007486**


chisq.test(mtcars$gear,mtcars$cyl)

Pearson's Chi-squared test

data:  mtcars$gear and mtcars$cyl
X-squared = 18.036, df = 4, p-value = **0.001214**
函数给出的

0.00216和测试给出的0.0007486应该是相同的数字。

0.0160.001214 也应该是相同的数字。

有谁知道为什么库(表1)给出的p值和R文档中的“pvalue”函数不起作用?以及如何解决它?非常感谢您的帮助。

r p-value
1个回答
0
投票

R 中的 p 值函数考虑了“总体”列。必须在没有总体的情况下运行表 1,以获得适当的 p 值,然后编辑 HTML 代码,以便将它们包含在具有总体列的表 1 中。

© www.soinside.com 2019 - 2024. All rights reserved.