【问题标题】:Creating a column of p values in the table1 package在 table1 包中创建一列 p 值
【发布时间】:2021-05-16 02:13:21
【问题描述】:

我正在使用此处给出的示例来尝试生成具有 p 值的列,但是,我的代码不起作用...并且 包手册中的原始示例没有' t 似乎也生成了第三列。我一直在试图找出问题所在,但我很茫然。有人有什么想法吗?

来源: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))

【问题讨论】:

    标签: r


    【解决方案1】:

    已解决的问题:

    (1) 卸载从 CRAN 下载的 Table1

    (1.1) 确保 RTools 已下载并安装 https://cran.r-project.org/bin/windows/Rtools/ 起初我的电脑无法识别该程序,但经过几次尝试后我能够执行它

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

    (2.1) 确保 xfun 等其他软件包也更新到最新版本

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-04
      • 2023-01-28
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      • 1970-01-01
      相关资源
      最近更新 更多