1 year ago
#252551
Matthew Byrne
R: P value correcting for multiple testing in a function - table1
I am doing descriptive statistics using table1 with an added p value column, using the data and methods described here: https://cran.r-project.org/web/packages/table1/vignettes/table1-examples.html#example-a-column-of-p-values
I am using the function they describe to automatically calculate and add the p values tp the table for categorical (chi-squared test) and continuous (t-test)
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)))
However, I would like to correct for multiple testing such as by using the 'bonferroni' method described here: https://stat.ethz.ch/R-manual/R-devel/library/stats/html/p.adjust.html
But I cannot figure out how I can add that into the function so that it does it automatically as well.
I would be very grateful for any help.
r
function
statistics
bonferroni
0 Answers
Your Answer