简体   繁体   English

在表 1 package 中创建一列 p 值

[英]Creating a column of p values in the table1 package

I'm using the example given here to try to generate a column with p-values, however, my code didn't work... and the original example from the package manual doesn't seem to generate a third column either.我正在使用此处给出的示例来尝试生成具有 p 值的列,但是,我的代码不起作用...... package 手册中的原始示例似乎也没有生成第三列。 I've been trying to figure out the issue and I'm at a loss.我一直在试图找出问题所在,但我很茫然。 Anyone have any ideas?有人有想法么?

Source: https://cran.r-project.org/web/packages/table1/vignettes/table1-examples.html资料来源: https://cran.r-project.org/web/packages/table1/vignettes/table1-examples.html

library(table1)
library(MatchIt) 
data(lalonde)

lalonde$treat    <- factor(lalonde$treat, levels=c(0, 1), labels=c("Control", "Treatment"))
lalonde$married  <- as.logical(lalonde$married == 1)
lalonde$nodegree <- as.logical(lalonde$nodegree == 1)
lalonde$race     <- factor(lalonde$race, levels=c("white", "black", "hispan"),
                           labels=c("White", "Black", "Hispanic"))

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 <- t.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(~ age + race + married + nodegree + re74 + re75 + re78 | treat,
       data=lalonde, overall=F, extra.col=list(`P-value`=pvalue))

Solved problem:解决的问题:

(1) Uninstall Table1 downloaded from CRAN (1) 卸载从CRAN下载的Table1

(1.1) Make sure RTools is downloaded and installed https://cran.r-project.org/bin/windows/Rtools/ at first my PC was not recognizing the program, but after several tries I was able to execute it (1.1) 确保 RTools 已下载并安装https://cran.r-project.org/bin/windows/Rtools/起初我的电脑无法识别该程序,但经过几次尝试我能够执行它

(2) Reinstall version from github remotes::install_github("benjaminrich/table1") (2) 从 github remotes::install_github("benjaminrich/table1") 重新安装版本

(2.1) Make sure other packages such as xfun are also updated to the latest version (2.1) 确保 xfun 等其他软件包也更新到最新版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM