表1未给出R Package 中的正确p值

ljo96ir5  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(104)

通过以下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")

| | 6例(N=7)|8例(N=14)|总体(N=32)|p值| P-value |
| --|--|--|--|--|--|
|讨厌||||||
| 平均值(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)和R文档中的“pvalue”函数给出的p值不起作用?如何修复谢谢你的帮助

m0rkklqb

m0rkklqb1#

R中的p值函数考虑“总体”列。不得不运行表1而不运行Overall,以获得适当的p值,然后编辑HTML代码,以便将它们包含在表1中的Overall列中。

相关问题