# KeepNotes blog

Stay hungry, Stay Foolish.

0%

``````library(MatchIt)
library(table1)
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"))

label(lalonde\$race)     <- "Race"
label(lalonde\$married)  <- "Married"
label(lalonde\$nodegree) <- "No high school diploma"
label(lalonde\$age)      <- "Age"
label(lalonde\$re74)     <- "1974 Income"
label(lalonde\$re75)     <- "1975 Income"
label(lalonde\$re78)     <- "1978 Income"
units(lalonde\$age)      <- "years"

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)))
}

stats <- function(x, ...) {
y <- unlist(x)
g <- factor(rep(1:length(x), times=sapply(x, length)))
if (is.numeric(y)) {
s <- t.test(y ~ g)\$statistic
} else {
s <- chisq.test(table(y, g))\$statistic
}
c("", sprintf("%.4f", s))
}

table1(~ age + race + married + nodegree + re74 + re75 + re78 | treat,
data=lalonde, overall=F, extra.col=list(`P-value`=pvalue, `statistic`=stats))``````

arsenal包也支持在原有的统计列基础上新增个性化需求的统计列，但是其可能只能增加一列（可能也是我没调试出来），但一个折中的办法是：将多列合并在一列中；因此还是以lalonde数据集为例，代码如下：

``````library(arsenal)
library(MatchIt)
data(lalonde)

lalonde\$black    <- factor(lalonde\$black, levels = c(0,1), labels = c("0","1"))
lalonde\$hispan   <- factor(lalonde\$hispan, levels = c(0,1), labels = c("0","1"))
lalonde\$married  <- factor(lalonde\$married, levels = c(0,1), labels = c("0","1"))
lalonde\$nodegree <- factor(lalonde\$nodegree, levels = c(0,1), labels = c("0","1"))

table_one <- tableby(treat ~ age + black + hispan + married + nodegree, data = lalonde)

mypval <- data.frame(
byvar = "treat",
variable = c('age','black','hispan','married','nodegree'),
adj.pvalue = c("X = 1, P = 0.01", "X = 2, P = 0.02", "X = 3, P = 0.03", "X = 4, P = 0.04", "X = 5, P = 0.05"),
method = c('Age/Sex adjusted model results')
)
tab_two <- modpval.tableby(table_one, mypval, use.pname=TRUE)
summary(tab_two)
write2html(tab_two, "../Desktop/222.html")``````