【发布时间】: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("<", "<", 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