Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for overall column when adding p-value column #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions vignettes/table1-examples.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -447,12 +447,16 @@ categorical variables.

```{r}
pvalue <- function(x, ...) {
# Uncomment for the column overall/total:
#x$overall <- NULL
# 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
# Uncomment for Wilcoxon test :
#p <- wilcox.test(y ~ g)$p.value
} else {
# For categorical variables, perform a chi-squared test of independence
p <- chisq.test(table(y, g))$p.value
Expand All @@ -467,10 +471,10 @@ pvalue <- function(x, ...) {
Note that this function expects a specific input, namely a list with 2
components corresponding to the 2 strata "Treatment" and "Control" in the
`lalonde` data. These are the only 2 elements the list will have, because we
will use `overall=F` in `table1` (otherwise, there would be a third element
in the list corresponding to the overall column). Thus, this function is in some
sense specifically tailored to this examples, and would need to be adapted to
other situations (such as more than 2 strata, where a t-test would not work).
will use `overall=F` in `table1`. If you wan to include the column overall,
you have to include`x$overall <- NULL` in the `pvalue` function. If you want to
use the non-parametric Wilcoxon test instead of the t-test, include
`p <- wilcox.test(y ~ g)$p.value`.

Now, we supply our function in the `extra.col` list argument to `table1` with
the name `P-value`, which will appear as the column label (heading).
Expand Down