diff --git a/.Rbuildignore b/.Rbuildignore index bd7579b..c071ea6 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -4,9 +4,20 @@ ## Avoid git(hub) related files \.git/* README.md +cran-comment.md ## Avoid Makefile, testing-related files, and archives Makefile +revdep_check.txt tableone.Rcheck .*.tar.gz .*.gif + +## unit testing +test-all.txt +tests/testthat/ref-* + +## CRAN compatibility check +cran-check.txt +## Travis-CI +^\.travis\.yml$ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b5f15b2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*~ +*.DS_Store +*.zip +*.pdf +*.Rhistory +*.tar.gz +*.Rcheck diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8275323 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,14 @@ +# Sample .travis.yml for R projects + +language: r +warnings_are_errors: true +sudo: required + +env: + global: + - CRAN: http://cran.rstudio.com + +notifications: + email: + on_success: change + on_failure: change diff --git a/DESCRIPTION b/DESCRIPTION index bfd763f..333376d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,24 +1,33 @@ Package: tableone Type: Package -Title: Create "Table 1" to describe baseline characteristics -Version: 0.6.3 -Date: 2014-12-28 +Title: Create "Table 1" to Describe Baseline Characteristics +Version: 0.7.0 +Date: 2015-08-10 Author: Kazuki Yoshida, Justin Bohn. Maintainer: Kazuki Yoshida -Description: This package creates "Table 1", i.e., description of baseline - patient characteristics, which is essential in every medical research. This - package provides functions to create such summaries for continuous and - categorical variables, optionally with subgroup comparisons. The package - was inspired by and based on descriptive statistics functions in Deducer, - a Java-based GUI package by Ian Fellows. This package does not require GUI - or Java, and intended for command-line users. +Description: Creates "Table 1", i.e., description of baseline patient + characteristics, which is essential in every medical research. + Supports both continuous and categorical variables, as well as + p-values and standardized mean differences. Weighted data are + supported via the survey package. See github for a screencast. + tableone was inspired by descriptive statistics functions in + Deducer , a Java-based GUI package by Ian Fellows. This package + does not require GUI or Java, and intended for command-line users. License: GPL-2 Imports: + survey, + MASS, e1071, + zoo, gmodels Suggests: survival, testthat, + Matrix, + dummies, + Matching, + reshape2, + ggplot2, knitr URL: https://github.com/kaz-yos/tableone VignetteBuilder: knitr diff --git a/Makefile b/Makefile index 4030734..d3b5dd7 100644 --- a/Makefile +++ b/Makefile @@ -15,18 +15,29 @@ PKG_VERSION=$(shell grep -i ^version: DESCRIPTION | cut -d : -d \ -f 2) ## Define files to check for updates R_FILES := $(wildcard R/*.R) +TST_FILES := $(wildcard tests/testthat/*.R) SRC_FILES := $(wildcard src/*) $(addprefix src/, $(COPY_SRC)) VIG_FILES := $(wildcard vignettes/*) -PKG_FILES := DESCRIPTION NAMESPACE NEWS $(R_FILES) $(SRC_FILES) $(VIG_FILES) +PKG_FILES := DESCRIPTION NAMESPACE NEWS $(R_FILES) $(TST_FILES) $(SRC_FILES) $(VIG_FILES) ## .PHONY to allow non-file targets (file targets should not be here) ## https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html -.PHONY: build check install clean +.PHONY: test build_win build check revdep install clean ### Define targets +## test just runs testthat scripts. No dependencies. +test: + Rscript -e "devtools::test()" | tee test-all.txt + +## build_win always build regardless of file update status +## Links to results e-mailed (no useful output locally) +build_win: + Rscript -e "devtools::build_win(version = 'R-devel')" + Rscript -e "devtools::build_win(version = 'R-release')" + ## build depends on the *.tar.gz file, i.e., its own product. ## *.tar.gz file is defined seprately to prevent build execution on every invocation. build: $(PKG_NAME)_$(PKG_VERSION).tar.gz @@ -43,7 +54,11 @@ NAMESPACE: $(R_FILES) ## check requires the *.tar.gz file, and execute strict tests on it. check: $(PKG_NAME)_$(PKG_VERSION).tar.gz - R CMD check --as-cran ./$(PKG_NAME)_$(PKG_VERSION).tar.gz + R CMD check --as-cran ./$(PKG_NAME)_$(PKG_VERSION).tar.gz | tee cran-check.txt + +## revdep requires the *.tar.gz file, and execute strict tests on it. +revdep: $(PKG_NAME)_$(PKG_VERSION).tar.gz + Rscript -e "devtools::revdep_check()" | tee revdep_check.txt ## install requires the *.tar.gz file, and execute installation using it. install: $(PKG_NAME)_$(PKG_VERSION).tar.gz @@ -64,6 +79,9 @@ list: @echo "R files:" @echo $(R_FILES) @echo + @echo "Test files:" + @echo $(TST_FILES) + @echo @echo "Source files:" @echo $(SRC_FILES) @echo diff --git a/NAMESPACE b/NAMESPACE index 025f031..26ecc89 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,14 +1,34 @@ -# Generated by roxygen2 (4.1.0): do not edit by hand +# Generated by roxygen2 (4.1.1): do not edit by hand S3method(print,CatTable) S3method(print,ContTable) S3method(print,TableOne) +S3method(print,svyCatTable) +S3method(print,svyContTable) S3method(summary,CatTable) S3method(summary,ContTable) S3method(summary,TableOne) +S3method(summary,svyCatTable) +S3method(summary,svyContTable) export(CreateCatTable) export(CreateContTable) export(CreateTableOne) +export(ExtractSmd) export(ShowRegTable) -import(e1071) -import(gmodels) +export(svyCreateCatTable) +export(svyCreateContTable) +export(svyCreateTableOne) +import(survey) +importFrom(stats,as.formula) +importFrom(stats,chisq.test) +importFrom(stats,coef) +importFrom(stats,confint) +importFrom(stats,fisher.test) +importFrom(stats,kruskal.test) +importFrom(stats,median) +importFrom(stats,oneway.test) +importFrom(stats,quantile) +importFrom(stats,sd) +importFrom(stats,var) +importFrom(stats,xtabs) +importFrom(utils,combn) diff --git a/NEWS b/NEWS index 111e30d..aca2909 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,60 @@ +tableone 0.7.0 (2015-08-10) +---------------------------------------------------------------- + +NEW FEATURES + +* Weighted data are now supported via the survey package. + svydesign() should be used to create a survey design object, + and this object should be used for svyCreateTableOne() instead + of a data frame. Other options are essentially the same. All + results will be weighted results. + +* Standardized mean differences are calculated. print() methods + include smd option, which defaults to FALSE for backward + compatibility. If TRUE, SMD is shown on the right side of the + table. To suppress p-values, use test = FALSE option. + summary() methods also print SMDs. If there are more than two + strata, all possible pairwise contrasts are shown along with the + average SMD across all contrats. In this case, print methods only + prints the average SMD for brevity. For categorical variables + Yang and Dalton's suggested method (A unified approach to + measuring the effect size between two groups using SAS. + SAS Paper 335-2012) is used. SMDs for weighted data are + experimental. + +* ExtractSmd() function can be used to extract SMD values as a + numeric matrix, which then can be used for plotting, etc. + +* An new RMarkdown vignette explains the use of standardized + mean differences. + +* The includeNA option for CreateTableOne() and svyCreateTableOne() + make NA's in factors treated as a regular level. + +* ShowRegTable() now has ciFun option, which allows use of + the confint.default function if calculation is too slow for + glm results. + +* print.TableOne() aligns the sample size with other summaries. + + +OTHER CHANGES + +* ShowRegTable() uses coef to refer to coefficients. + +* Unit tests were extended to cover more functions. + +* CreateTableOne() was refactored to avoid redundant calculation. + +* New dependencies: survey, MASS, and zoo + + +BUG FIXES + +* Fixed pDigits option in print.ContTable(), which was not correctly + functional as advertised. + + tableone 0.6.3 (2014-12-28) ---------------------------------------------------------------- diff --git a/R/CreateCatTable.R b/R/CreateCatTable.R index fa8b516..a1dc05a 100644 --- a/R/CreateCatTable.R +++ b/R/CreateCatTable.R @@ -1,24 +1,21 @@ ##' Create an object summarizing categorical variables ##' -##' Create an object summarizing categorical variables optionally stratifying -##' by one or more startifying variables and performing statistical tests. The -##' object gives a table that is easy to use in medical research papers. See -##' also \code{\link{print.CatTable}} and \code{\link{summary.CatTable}}. +##' Create an object summarizing categorical variables optionally stratifying by one or more startifying variables and performing statistical tests. Usually, \code{\link{CreateTableOne}} should be used as the universal frontend for both continuous and categorical data. ##' ##' @param vars Variable(s) to be summarized given as a character vector. ##' @param strata Stratifying (grouping) variable name(s) given as a character vector. If omitted, the overall results are returned. ##' @param data A data frame in which these variables exist. All variables (both vars and strata) must be in this data frame. +##' @param includeNA If TRUE, NA is handled as a regular factor level rather than missing. NA is shown as the last factor level in the table. Only effective for categorical variables. ##' @param test If TRUE, as in the default and there are more than two groups, groupwise comparisons are performed. Both tests that require the large sample approximation and exact tests are performed. Either one of the result can be obtained from the print method. -##' @param testApprox A function used to perform the large sample approximation based tests. The default is \code{\link{chisq.test}}. This is not recommended when some of the cell have small counts like fewer than 5. -##' @param argsApprox A named list of arguments passed to the function specified in testApprox. The default is \code{list(correct = TRUE)}, which turns on the continuity correction for \code{\link{chisq.test}}. -##' @param testExact A function used to perform the exact tests. The default is fisher.test. If the cells have large numbers, it will fail because of memory limitation. In this situation, the large sample approximation based should suffice. -##' @param argsExact A named list of arguments passed to the function specified in testExact. The default is \code{list(workspace = 2*10^5)}, which specifies the memory space allocated for \code{\link{fisher.test}}. -##' @return An object of class \code{CatTable}, which really is a \code{\link{by}} object with additional attributes. Each element of the \code{\link{by}} part is a matrix with rows representing variables, and columns representing summary statistics. +##' @param testApprox A function used to perform the large sample approximation based tests. The default is \code{chisq.test}. This is not recommended when some of the cell have small counts like fewer than 5. +##' @param argsApprox A named list of arguments passed to the function specified in testApprox. The default is \code{list(correct = TRUE)}, which turns on the continuity correction for \code{chisq.test}. +##' @param testExact A function used to perform the exact tests. The default is \code{fisher.test}. If the cells have large numbers, it will fail because of memory limitation. In this situation, the large sample approximation based should suffice. +##' @param argsExact A named list of arguments passed to the function specified in testExact. The default is \code{list(workspace = 2*10^5)}, which specifies the memory space allocated for \code{fisher.test}. +##' @param smd If TRUE, as in the default and there are more than two groups, standardized mean differences for all pairwise comparisons are calculated. +##' @return An object of class \code{CatTable}. ##' @author Kazuki Yoshida (based on \code{Deducer::frequencies()}) ##' @seealso -##' \code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}}, -##' \code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}}, -##' \code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}} +##' \code{\link{CreateTableOne}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}} ##' @examples ##' ##' ## Load @@ -61,7 +58,7 @@ ##' ##' ## print now includes p-values which are by default calculated by chisq.test. ##' ## It is formatted at the decimal place specified by the pDigits argument -##' ## (3 by default). It does <0.001 for you. +##' ## (3 by default). It is formatted like <0.001 if very small. ##' catTableBySexTrt ##' ##' ## The exact argument toggles the p-values to the exact test result from @@ -81,15 +78,17 @@ ##' ##' @export CreateCatTable <- - function(vars, # character vector of variable names - strata, # character vector of variable names - data, # data frame - test = TRUE, # whether to put p-values - testApprox = chisq.test, # function for approximation test - argsApprox = list(correct = TRUE), # arguments passed to testApprox - testExact = fisher.test, # function for exact test - argsExact = list(workspace = 2*10^5) # arguments passed to testExact - ) { +function(vars, # character vector of variable names + strata, # character vector of variable names + data, # data frame + includeNA = FALSE, # include NA as a category + test = TRUE, # whether to include p-values + testApprox = chisq.test, # function for approximation test + argsApprox = list(correct = TRUE), # arguments passed to testApprox + testExact = fisher.test, # function for exact test + argsExact = list(workspace = 2*10^5), # arguments passed to testExact + smd = TRUE # whether to include standardize mean differences + ) { ### Data check ## Check if the data given is a dataframe @@ -106,6 +105,10 @@ CreateCatTable <- ## Toggle test FALSE if no strata test <- ModuleReturnFalseIfNoStrata(strata, test) + smd <- ModuleReturnFalseIfNoStrata(strata, smd) + + ## Create strata data frame (data frame with only strata variables) + strata <- ModuleReturnStrata(strata, data) ## Convert to a factor if it is not a factor already. (categorical version only) ## Not done on factors, to avoid dropping zero levels. @@ -118,12 +121,12 @@ CreateCatTable <- dat[logiNotFactor] <- lapply(dat[logiNotFactor], factor) - ## Create strata data frame (data frame with only strata variables) - strata <- ModuleReturnStrata(strata, data) - + ## If including NA as a level, include NA as a factor level before subsetting + if (includeNA) { + dat <- ModuleIncludeNaAsLevel(dat) + } ### Actual descriptive statistics are calculated here. - ## strata--variable-CreateTableForOneVar structure ## Devide by strata result <- by(data = dat, INDICES = strata, # INDICES can be a multi-column data frame @@ -154,45 +157,38 @@ CreateCatTable <- pValues <- NULL listXtabs <- list() - ## Only when test is asked for - if (test == TRUE) { - - ## Create a single variable representation of multivariable stratification - strataVar <- ModuleCreateStrataVarAsFactor(result, strata) - - ## Loop over variables in dat, and create a list of xtabs - ## Empty strata are kept in the corss tables. Different behavior than the cont counterpart! - listXtabs <- sapply(X = names(dat), - FUN = function(var) { - ## Create a formula - formula <- paste0("~ ", var, " + ", "strataVar") - formula <- as.formula(formula) - - ## Create a 2-dimensional crosstable - xtabs(formula = formula, data = dat) - }, - simplify = FALSE) + ## Create a single variable representation of multivariable stratification + ## Respect ordering of levels in by() + strataVar <- ModuleCreateStrataVarAsFactor(result, strata) - ## Rename the second dimension of the xtabs with the newly create name. - for (i in seq_along(listXtabs)) { + ## Only when test is asked for + if (test) { + lstXtabsPVals <- + ModuleApproxExactTests(result = result, + strata = strata, + dat = dat, + strataVarName = strataVarName, + testApprox = testApprox, + argsApprox = argsApprox, + testExact = testExact, + argsExact = argsExact) + pValues <- lstXtabsPVals$pValues + listXtabs <- lstXtabsPVals$xtabs + } - names(dimnames(listXtabs[[i]]))[2] <- strataVarName - } - ## Loop over xtabs, and create p-values - pValues <- sapply(X = listXtabs, - FUN = function(xtabs) { - ## Perform tests and return the result as 1x2 DF - data.frame( - pApprox = ModuleTestSafe(xtabs, testApprox, argsApprox), - pExact = ModuleTestSafe(xtabs, testExact, argsExact) - ) - }, - simplify = FALSE) +### Perform SMD when requested + smds <- NULL - ## Create a single data frame (n x 2 (normal,nonormal)) - pValues <- do.call(rbind, pValues) - } # Conditional for test == TRUE ends here. + ## Only when SMD is asked for + if (smd) { + ## list of smds + smds <- sapply(dat, function(var) { + StdDiffMulti(variable = var, group = strataVar) + }, simplify = FALSE) + ## Give name and add mean column + smds <- FormatLstSmds(smds, nStrata = length(result)) + } ## Return object @@ -202,7 +198,8 @@ CreateCatTable <- ## Give additional attributes attributes(result) <- c(attributes(result), list(pValues = pValues), - list(xtabs = listXtabs)) + list(xtabs = listXtabs), + list(smd = smds)) ## Return return(result) diff --git a/R/CreateContTable.R b/R/CreateContTable.R index 27f6e1e..0e06371 100644 --- a/R/CreateContTable.R +++ b/R/CreateContTable.R @@ -1,9 +1,6 @@ ##' Create an object summarizing continous variables ##' -##' Create an object summarizing continous variables optionally stratifying by -##' one or more startifying variables and performing statistical tests. The -##' object gives a table that is easy to use in medical research papers. See -##' also \code{\link{print.ContTable}} and \code{\link{summary.ContTable}}. +##' Create an object summarizing continous variables optionally stratifying by one or more startifying variables and performing statistical tests. Usually, \code{\link{CreateTableOne}} should be used as the universal frontend for both continuous and categorical data. ##' ##' @param vars Variable(s) to be summarized given as a character vector. ##' @param strata Stratifying (grouping) variable name(s) given as a character vector. If omitted, the overall results are returned. @@ -11,16 +8,15 @@ ##' @param funcNames The functions to give the group size, number with missing values, proportion with missing values, mean, standard deviations, median, 25th percentile, 75th percentile, minimum, maximum, skewness (same definition as in SAS), kurtosis (same definition as in SAS). All of them can be seen in the summary method output. The print method uses subset of these. You can choose subset of them or reorder them. They are all configure to omit NA values (\code{na.rm = TRUE}). ##' @param funcAdditional Additional functions can be given as a named list. For example, \code{list(sum = sum)}. ##' @param test If TRUE, as in the default and there are more than two groups, groupwise comparisons are performed. Both tests that assume normality and tests that do not are performed. Either one of the result can be obtained from the print method. -##' @param testNormal A function used to perform the normal assumption based tests. The default is \code{\link{oneway.test}}. This is equivalent of the t-test when there are only two groups. +##' @param testNormal A function used to perform the normal assumption based tests. The default is \code{oneway.test}. This is equivalent of the t-test when there are only two groups. ##' @param argsNormal A named list of arguments passed to the function specified in \code{testNormal}. The default is \code{list(var.equal = TRUE)}, which makes it the ordinary ANOVA that assumes equal variance across groups. ##' @param testNonNormal A function used to perform the nonparametric tests. The default is \code{kruskal.test} (Kruskal-Wallis rank sum test). This is equivalent of the wilcox.test (Man-Whitney U test) when there are only two groups. ##' @param argsNonNormal A named list of arguments passed to the function specified in \code{testNonNormal}. The default is \code{list(NULL)}, which is just a placeholder. -##' @return An object of class \code{ContTable}, which really is a \code{\link{by}} object with additional attributes. Each element of the \code{\link{by}} part is a matrix with rows representing variables, and columns representing summary statistics. +##' @param smd If TRUE, as in the default and there are more than two groups, standardized mean differences for all pairwise comparisons are calculated. +##' @return An object of class \code{ContTable}. ##' @author Kazuki Yoshida (based on \code{Deducer::descriptive.table()}) ##' @seealso -##' \code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}}, -##' \code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}}, -##' \code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}} +##' \code{\link{CreateTableOne}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}} ##' @examples ##' ##' ## Load @@ -80,25 +76,22 @@ ##' ##' @export CreateContTable <- - function(vars, # character vector of variable names - strata, # character vector of variable names - data, # data frame - funcNames = c( # can pick a subset of them +function(vars, # character vector of variable names + strata, # character vector of variable names + data, # data frame + funcNames = c( # can pick a subset of them "n","miss","p.miss", "mean","sd", "median","p25","p75","min","max", - "skew","kurt" - ), - funcAdditional, # named list of additional functions - test = TRUE, # Whether to put p-values - testNormal = oneway.test, # test for normally distributed variables - argsNormal = list(var.equal = TRUE), # arguments passed to testNormal - testNonNormal = kruskal.test, # test for nonnormally distributed variables - argsNonNormal = list(NULL) # arguments passed to testNonNormal - ) { - - ## Require dependencies (DELETE before CRAN release. Use Depends in DESCRIPTION) - ## require(e1071) # for skewness and kurtosis + "skew","kurt"), + funcAdditional, # named list of additional functions + test = TRUE, # Whether to include p-values + testNormal = oneway.test, # test for normally distributed variables + argsNormal = list(var.equal = TRUE), # arguments passed to testNormal + testNonNormal = kruskal.test, # test for nonnormally distributed variables + argsNonNormal = list(NULL), # arguments passed to testNonNormal + smd = TRUE # whether to include standardize mean differences + ) { ### Data check ## Check if the data given is a dataframe @@ -115,6 +108,7 @@ CreateContTable <- ## Toggle test FALSE if no strata test <- ModuleReturnFalseIfNoStrata(strata, test) + smd <- ModuleReturnFalseIfNoStrata(strata, smd) ## Create strata data frame (data frame with only strata variables) strata <- ModuleReturnStrata(strata, data) @@ -208,15 +202,16 @@ CreateContTable <- ## Initialize to avoid error when it does not exist at the attribute assignment pValues <- NULL + ## Create a single variable representation of multivariable stratification + ## Respect ordering of levels in by() + strataVar <- ModuleCreateStrataVarAsFactor(result, strata) ## Only when test is asked for - if (test == TRUE) { - - ## Create a single variable representation of multivariable stratification - strataVar <- ModuleCreateStrataVarAsFactor(result, strata) + if (test) { ## Loop over variables in dat, and obtain p values for two tests - ## DF = 6 when there are 8 levels (one empty), i.e., empty strata dropped by oneway.test/kruskal.test + ## DF = 6 when there are 8 levels (one empty), + ## i.e., empty strata dropped by oneway.test/kruskal.test pValues <- sapply(X = dat, FUN = function(var) { @@ -233,13 +228,28 @@ CreateContTable <- } # Conditional for test == TRUE ends here. +### Perform SMD when requested + smds <- NULL + + ## Only when SMD is asked for + if (smd) { + ## list of smds + smds <- sapply(dat, function(var) { + StdDiff(variable = var, group = strataVar) + }, simplify = FALSE) + ## Give name and add mean column + smds <- FormatLstSmds(smds, nStrata = length(result)) + } + + ## Return object ## Give an S3 class class(result) <- c("ContTable", class(result)) ## Give additional attributes attributes(result) <- c(attributes(result), - list(pValues = pValues)) + list(pValues = pValues), + list(smd = smds)) ## Return return(result) diff --git a/R/CreateTableOne.R b/R/CreateTableOne.R index 39c5bc8..a3af750 100644 --- a/R/CreateTableOne.R +++ b/R/CreateTableOne.R @@ -1,31 +1,39 @@ -##' Create an object summarizing both categorical and continuous variables +##' Create an object summarizing both continuous and categorical variables ##' -##' Create an object summarizing all baseline variables optionally stratifying by one or more startifying variables and performing statistical tests. The object gives a table that is easy to use in medical research papers. See also \code{\link{print.TableOne}} and \code{\link{summary.TableOne}}. +##' Create an object summarizing all baseline variables (both continuous and categorical) optionally stratifying by one or more startifying variables and performing statistical tests. The object gives a table that is easy to use in medical research papers. ##' ##' @param vars Variables to be summarized given as a character vector. Factors are handled as categorical variables, whereas numeric variables are handled as continuous variables. If empty, all variables in the data frame specified in the data argument are used. ##' @param strata Stratifying (grouping) variable name(s) given as a character vector. If omitted, the overall results are returned. ##' @param data A data frame in which these variables exist. All variables (both vars and strata) must be in this data frame. ##' @param factorVars Numerically coded variables that should be handled as categorical variables given as a character vector. If omitted, only factors are considered categorical variables. If all categorical variables in the dataset are already factors, this option is not necessary. The variables specified here must also be specified in the \code{vars} argument. +##' @param includeNA If TRUE, NA is handled as a regular factor level rather than missing. NA is shown as the last factor level in the table. Only effective for categorical variables. ##' @param test If TRUE, as in the default and there are more than two groups, groupwise comparisons are performed. -##' @param testNormal A function used to perform the normal assumption based tests. The default is \code{\link{oneway.test}}. This is equivalent of the t-test when there are only two groups. +##' @param testNormal A function used to perform the normal assumption based tests. The default is \code{oneway.test}. This is equivalent of the t-test when there are only two groups. ##' @param argsNormal A named list of arguments passed to the function specified in \code{testNormal}. The default is \code{list(var.equal = TRUE)}, which makes it the ordinary ANOVA that assumes equal variance across groups. ##' @param testNonNormal A function used to perform the nonparametric tests. The default is \code{kruskal.test} (Kruskal-Wallis Rank Sum Test). This is equivalent of the wilcox.test (Man-Whitney U test) when there are only two groups. ##' @param argsNonNormal A named list of arguments passed to the function specified in \code{testNonNormal}. The default is \code{list(NULL)}, which is just a placeholder. -##' @param testApprox A function used to perform the large sample approximation based tests. The default is \code{\link{chisq.test}}. This is not recommended when some of the cell have small counts like fewer than 5. -##' @param argsApprox A named list of arguments passed to the function specified in testApprox. The default is \code{list(correct = TRUE)}, which turns on the continuity correction for \code{\link{chisq.test}}. -##' @param testExact A function used to perform the exact tests. The default is fisher.test. If the cells have large numbers, it will fail because of memory limitation. In this situation, the large sample approximation based should suffice. -##' @param argsExact A named list of arguments passed to the function specified in testExact. The default is \code{list(workspace = 2*10^5)}, which specifies the memory space allocated for \code{\link{fisher.test}}. -##' @return An object of class \code{TableOne}, which really is a list of three objects. -##' @return \item{TableOne}{a categorical-continuous mixture data formatted and printed by the \code{\link{print.TableOne}} method} -##' @return \item{ContTable}{an object of class \code{ContTable}, containing continuous variables only} -##' @return \item{CatTable}{ an object of class \code{CatTable}, containing categorical variables only} -##' @return The second and third objects can be then be examined with the \code{print} and \code{summary} method, for example, \code{summary(object$CatTable)} to examine the categorical variables in detail. -##' -##' @author Justin Bohn, Kazuki Yoshida +##' @param testApprox A function used to perform the large sample approximation based tests. The default is \code{chisq.test}. This is not recommended when some of the cell have small counts like fewer than 5. +##' @param argsApprox A named list of arguments passed to the function specified in testApprox. The default is \code{list(correct = TRUE)}, which turns on the continuity correction for \code{chisq.test}. +##' @param testExact A function used to perform the exact tests. The default is \code{fisher.test}. If the cells have large numbers, it will fail because of memory limitation. In this situation, the large sample approximation based should suffice. +##' @param argsExact A named list of arguments passed to the function specified in testExact. The default is \code{list(workspace = 2*10^5)}, which specifies the memory space allocated for \code{fisher.test}. +##' @param smd If TRUE, as in the default and there are more than two groups, standardized mean differences for all pairwise comparisons are calculated. +##' +##' @details The definitions of the standardized mean difference (SMD) are available in \href{http://www.tandfonline.com/doi/abs/10.1080/00031305.1986.10475403}{Flury \emph{et al} 1986} for the univariate case and the multivariate case (essentially the square root of the Mahalanobis distance). Extension to binary variables is discussed in \href{http://www.tandfonline.com/doi/abs/10.1080/03610910902859574}{Austin 2009} and extension to multinomival variables is suggested in \href{http://support.sas.com/resources/papers/proceedings12/335-2012.pdf}{Yang \emph{et al} 2012}. This multinomial extesion treats a single multinomial variable as multiple non-redundant dichotomous variables and use the Mahalanobis distance. The off diagonal elements of the covariance matrix on page 3 have an error, and need negation. In weighted data, the same definitions can be used except that the mean and standard deviation estimates are weighted estimates (\href{http://www.ncbi.nlm.nih.gov/pubmed/23902694}{Li \emph{et al} 2013} and \href{http://onlinelibrary.wiley.com/doi/10.1002/sim.6607/full}{Austin \emph{et al} 2015}). In tableone, all weighted estimates are calculated by weighted estimation functions in the \code{survey} package. +##' +##' @return An object of class \code{TableOne}, which is a list of three objects. +##' @return \item{ContTable}{object of class \code{ContTable}, containing continuous variables only} +##' @return \item{CatTable}{object of class \code{CatTable}, containing categorical variables only} +##' @return \item{MetaData}{list of metadata regarding variables} +##' +##' @references Flury, BK. and Riedwyl, H. (1986). Standard distance in univariate and multivariate analysis. \emph{The American Statistician}, \bold{40}, 249-251. +##' @references Austin, PC. (2009). Using the Standardized Difference to Compare the Prevalence of a Binary Variable Between Two Groups in Observational Research. \emph{Communications in Statistics - Simulation and Computation}, \bold{38}, 1228-1234. +##' @references Yang, D. and Dalton, JE. (2012). A unified approach to measuring the effect size between two groups using SAS. SAS Global Forum 2012, Paper 335-2012. +##' @references Li, L. and Greene, T. (2013). A weighting analogue to pair matching in propensity score analysis. \emph{International Journal of Biostatistics}, \bold{9}, 215-234. +##' @references Austin, PC. and Stuart, EA. (2015). Moving towards best practice when using inverse probability of treatment weighting (IPTW) using the propensity score to estimate causal treatment effects in observational studies. \emph{Statistics in Medicine}, Online on August 3, 2015. +##' +##' @author Kazuki Yoshida, Justin Bohn ##' @seealso -##' \code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}}, -##' \code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}}, -##' \code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}} +##' \code{\link{print.TableOne}}, \code{\link{summary.TableOne}} ##' @examples ##' ##' ## Load @@ -56,9 +64,10 @@ ##' ##' ## Specifying nonnormal variables will show the variables appropriately, ##' ## and show nonparametric test p-values. Specify variables in the exact -##' ## argument to obtain the exact test p-values. +##' ## argument to obtain the exact test p-values. cramVars can be used to +##' ## show both levels for a 2-level categorical variables. ##' print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), -##' exact = c("status","stage")) +##' exact = c("status","stage"), cramVars = "hepato", smd = TRUE) ##' ##' ## Use the summary.TableOne method for detailed summary ##' summary(tableOne) @@ -81,170 +90,159 @@ ##' print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), ##' exact = c("status","stage"), quote = TRUE, noSpaces = TRUE) ##' +##' ## If SMDs are needed as numericals, use ExtractSmd() +##' ExtractSmd(tableOne) +##' ##' @export CreateTableOne <- - function(vars, # character vector of variable names - strata, # character vector of variable names - data, # data frame - factorVars, # variables to be transformed to factors - test = TRUE, # whether to put p-values - ## Test configuration for categorical data - testApprox = chisq.test, # function for approximation test - argsApprox = list(correct = TRUE), # arguments passed to testApprox - testExact = fisher.test, # function for exact test - argsExact = list(workspace = 2*10^5), # arguments passed to testExact - ## Test configuration for continuous data - testNormal = oneway.test, # test for normally distributed variables - argsNormal = list(var.equal = TRUE), # arguments passed to testNormal - testNonNormal = kruskal.test, # test for nonnormally distributed variables - argsNonNormal = list(NULL) # arguments passed to testNonNormal - ) { +function(vars, # character vector of variable names + strata, # character vector of variable names + data, # data frame + factorVars, # variables to be transformed to factors + includeNA = FALSE, # include NA as a category (categoricals only) + test = TRUE, # whether to include p-values + ## Test configuration for categorical data + testApprox = chisq.test, # function for approximation test + argsApprox = list(correct = TRUE), # arguments passed to testApprox + testExact = fisher.test, # function for exact test + argsExact = list(workspace = 2*10^5), # arguments passed to testExact + ## Test configuration for continuous data + testNormal = oneway.test, # test for normally distributed variables + argsNormal = list(var.equal = TRUE), # arguments passed to testNormal + testNonNormal = kruskal.test, # test for nonnormally distributed variables + argsNonNormal = list(NULL), # arguments passed to testNonNormal + smd = TRUE # whether to include standardize mean differences + ) { ### Data check - ## Check if the data given is a dataframe - ModuleStopIfNotDataFrame(data) + ## Check if the data given is a dataframe + ModuleStopIfNotDataFrame(data) + + ## Check if vars argument is missing. If so, add all names in data. + if (missing(vars)) { + vars <- names(data) + } + + ## Check if variables exist. Drop them if not. + vars <- ModuleReturnVarsExist(vars, data) - ## Check if vars argument is missing. If so, add all names in data. - if (missing(vars)) { - vars <- names(data) - } + ## Abort if no variables exist at this point + ModuleStopIfNoVarsLeft(vars) + ## Factor conversions if the factorVars argument exist + if (!missing(factorVars)) { ## Check if variables exist. Drop them if not. - vars <- ModuleReturnVarsExist(vars, data) - - ## Abort if no variables exist at this point - ModuleStopIfNoVarsLeft(vars) - - ## Factor conversions if the factorVars argument exist - if (!missing(factorVars)) { - ## Check if variables exist. Drop them if not. - factorVars <- ModuleReturnVarsExist(factorVars, data) - ## Convert to factor - data[factorVars] <- lapply(data[factorVars], factor) - } - - ## Toggle test FALSE if no strata is given - test <- ModuleReturnFalseIfNoStrata(strata, test) - - ## Get the classes of the variables - varClasses <- lapply(data[vars], class) - - ## Classify as varFactors if any one of these classes are contained - varFactors <-sapply(varClasses, function(VEC) { - any(VEC %in% c("factor", "ordered", "logical", "character")) - }) - varFactors <- names(varFactors)[varFactors] - - ## Classify as varNumerics if any one of these classes are contained - varNumerics <-sapply(varClasses, function(VEC) { - any(VEC %in% c("numeric", "integer")) - }) - varNumerics <- names(varNumerics)[varNumerics] - - ## Drop variables that do not meet either because it is unsupported - varDrop <- setdiff(vars, c(varFactors, varNumerics)) - if (length(varDrop) > 0) { - warning("Dropping variable(s) ", paste0(varDrop, sep = " "), - " due to unsupported class.\n") - vars <- setdiff(vars, varDrop) - } - - ## Create a logical vector indicator for factors (vars in varFactors = TRUE) - logiFactors <- vars %in% varFactors - - ## Create lists of arguments - argsCreateContTable <- list(data = data, - test = test, - testNormal = testNormal, - argsNormal = argsNormal, - testNonNormal = testNonNormal, - argsNonNormal = argsNonNormal - ) - argsCreateCatTable <- list(data = data, - test = test, - testApprox = testApprox, - argsApprox = argsApprox, - testExact = testExact, - argsExact = argsExact - ) - ## Add strata = strata for argument only if strata is given - if (!missing(strata)) { - - ## Check strata. This returns a DF. Returns a "Overall" DF if strata is missing. - ## Must not be place outside if (!missing(strata)) { }. - dfStrata <- ModuleReturnStrata(strata, data) - ## Return variable names. Code inefficient in exchange for code simplicity. - strata <- names(dfStrata) - - ## Create lists of arguments including strata - argsCreateContTable <- c(list(strata = strata), argsCreateContTable) - argsCreateCatTable <- c(list(strata = strata), argsCreateCatTable) - } - - - ## Condition on the absence of factor/numeric - if (length(varNumerics) == 0) { - ## No numerics - cat('NOTE: no numeric/integer variables supplied, using CreateCatTable()\n') - CatTable <- do.call(CreateCatTable, - args = c(list(vars = varFactors), argsCreateCatTable)) - return(CatTable) - - } else if (length(varFactors) == 0) { - ## No factors - cat('NOTE: no factor/logical/character variables supplied, using CreateContTable()\n') - ContTable <- do.call(CreateContTable, - args = c(list(vars = varNumerics), argsCreateContTable)) - return(ContTable) - -### Proceed if both types of variables are present (both factors and numerics) - } else if ((length(varFactors) > 0) & (length(varNumerics) > 0)) { - - ## Create a list of constructors - listOfConstructors <- list(CreateContTable = CreateContTable, - CreateCatTable = CreateCatTable) - ## CreateCatTable for categorical. CreateContTable for continuous. - listOfConstructors <- listOfConstructors[logiFactors + 1] - ## Create a list of arguments - listOfArgs <- list(argsCreateContTable = argsCreateContTable, - argsCreateCatTable = argsCreateCatTable) - ## argsCreateCatTable for categorical. argsCreateContTable for continuous. - listOfArgs <- listOfArgs[logiFactors + 1] - - ## Create a list of tables by looping over variables/constructors/arguments - TableOne <- sapply(seq_along(listOfConstructors), - FUN = function(i) { - - args <- c(list(vars = vars[i]), # vector element - listOfArgs[[i]]) # list element - - do.call(listOfConstructors[[i]], # list element - args = args) - }, - simplify = FALSE) - - ## Give variable names to the result object - names(TableOne) <- vars - - - ## Create ContTable and CatTable objects (this is redundant, but easy) - ## Aggregated ContTable - ContTable <- do.call(CreateContTable, - args = c(list(vars = varNumerics), argsCreateContTable)) - ## Aggregated CatTable - CatTable <- do.call(CreateCatTable, - args = c(list(vars = varFactors), argsCreateCatTable)) - - ## Create a list for output - TableOneObject <- list(TableOne = TableOne, - ContTable = ContTable, - CatTable = CatTable - ) - - ## Give a class - class(TableOneObject) <- "TableOne" - - ## Return the object - return(TableOneObject) - } + factorVars <- ModuleReturnVarsExist(factorVars, data) + ## Convert to factor + data[factorVars] <- lapply(data[factorVars], factor) + } + + ## Toggle test FALSE if no strata is given + test <- ModuleReturnFalseIfNoStrata(strata, test) + smd <- ModuleReturnFalseIfNoStrata(strata, smd) + + ## Get the classes of the variables + varClasses <- lapply(data[vars], class) + + ## Classify as varFactors if any one of these classes are contained + varFactors <-sapply(varClasses, function(VEC) { + any(VEC %in% c("factor", "ordered", "logical", "character")) + }) + varFactors <- names(varFactors)[varFactors] + + ## Classify as varNumerics if any one of these classes are contained + varNumerics <-sapply(varClasses, function(VEC) { + any(VEC %in% c("numeric", "integer")) + }) + varNumerics <- names(varNumerics)[varNumerics] + + ## Drop variables that do not meet either because it is unsupported + varDrop <- setdiff(vars, c(varFactors, varNumerics)) + if (length(varDrop) > 0) { + warning("Dropping variable(s) ", paste0(varDrop, sep = " "), + " due to unsupported class.\n") + vars <- setdiff(vars, varDrop) + } + + ## Create a logical vector indicator for factors (vars in varFactors = TRUE) + logiFactors <- vars %in% varFactors + + ## Create lists of arguments + argsCreateContTable <- list(data = data, + test = test, + testNormal = testNormal, + argsNormal = argsNormal, + testNonNormal = testNonNormal, + argsNonNormal = argsNonNormal, + smd = smd) + argsCreateCatTable <- list(data = data, + includeNA = includeNA, + test = test, + testApprox = testApprox, + argsApprox = argsApprox, + testExact = testExact, + argsExact = argsExact, + smd = smd) + + ## Add strata = strata for argument only if strata is given + if (!missing(strata)) { + + ## Check strata. This returns a DF. Returns a "Overall" DF if strata is missing. + ## Must not be place outside if (!missing(strata)) { }. + dfStrata <- ModuleReturnStrata(strata, data) + ## Return variable names. Code inefficient in exchange for code simplicity. + strata <- names(dfStrata) + + ## Create lists of arguments including strata + argsCreateContTable <- c(list(strata = strata), argsCreateContTable) + argsCreateCatTable <- c(list(strata = strata), argsCreateCatTable) } + + +### If only varFactors/varNumerics are present, just call one constructor + if (length(varNumerics) == 0) { + ## No numerics + ContTable <- NULL + CatTable <- do.call(CreateCatTable, + args = c(list(vars = varFactors), argsCreateCatTable)) + + } else if (length(varFactors) == 0) { + ## No factors + ContTable <- do.call(CreateContTable, + args = c(list(vars = varNumerics), argsCreateContTable)) + CatTable <- NULL + +### Both types of variables are present, call both constructors + } else if ((length(varFactors) > 0) & (length(varNumerics) > 0)) { + + ## ContTable + ContTable <- do.call(CreateContTable, + args = c(list(vars = varNumerics), argsCreateContTable)) + ## CatTable + CatTable <- do.call(CreateCatTable, + args = c(list(vars = varFactors), argsCreateCatTable)) + } else { + + ## vars never empty by data check with ModuleStopIfNoVarsLeft() + ## Just to make sure + warning("No variables left to analyzed in vars.") + } + + + ## Create a list for output + ## Either one of the two tables may be NULL + TableOneObject <- list(ContTable = ContTable, + CatTable = CatTable, + MetaData = list(vars = vars, + ## describes which pos is vars is factor + logiFactors = logiFactors, + ## names of vars of each type + varFactors = varFactors, + varNumerics = varNumerics)) + + ## Give a class + class(TableOneObject) <- "TableOne" + + ## Return the object + return(TableOneObject) +} diff --git a/R/ExtractSmd.R b/R/ExtractSmd.R new file mode 100644 index 0000000..c5161ed --- /dev/null +++ b/R/ExtractSmd.R @@ -0,0 +1,36 @@ +##' Extract standardized mean differences from a (svy)TableOne object +##' +##' Extracts standardized mean differences data as a vector or matrix from a (svy)TableOne object +##' +##' +##' @param x A stratified (svy)TableOne object containing standardized mean differences. +##' @return A vector or matrix containing the average standardized mean differences (if more than two contrasts exist) as well as the all possible pairwise standardized mean differences. Variables are ordered in the same order as the printed table. +##' @author Kazuki Yoshida +##' @seealso +##' \code{\link{CreateTableOne}}, \code{\link{svyCreateTableOne}} +##' @examples +##' +##' ## See examples for CreateTableOne and svyCreateTableOne +##' +##' @export +ExtractSmd <- function(x) { + + if (class(x)[1] %in% c("TableOne","svyTableOne")) { + + ## Extract SMD from both continuous and categorical + matSmd <- rbind(attr(x$ContTable, "smd"), + attr(x$CatTable, "smd")) + + ## Order by table variable order + matSmd[x$MetaData$vars,] + + } else if (class(x)[1] %in% c("ContTable","svyContTable","CatTable","svyCatTable")) { + + ## If not a mixed table object, just get attribute + attr(x, "smd") + + } else { + + warning("Unsupported object of class: ", class(x)) + } +} diff --git a/R/ShowRegTable.R b/R/ShowRegTable.R index d569bd8..9434347 100644 --- a/R/ShowRegTable.R +++ b/R/ShowRegTable.R @@ -1,39 +1,41 @@ ##' Format regression results in medically decent format -##' +##' ##' It shows the regression result in the HR [95\% CI] p-value format, which is usually the form used in medical research papers. -##' -##' +##' +##' ##' @param model Regression model result objects that have the summary and confint methods. ##' @param exp TRUE by default. You need to specify exp = FALSE if your model is has the indentity link function (linear regression, etc). ##' @param digits Number of digits to print for the main part. ##' @param pDigits Number of digits to print for the p-values. ##' @param printToggle Whether to print the output. If FLASE, no output is created, and a matrix is invisibly returned. ##' @param quote Whether to show everything in quotes. The default is FALSE. If TRUE, everything including the row and column names are quoted so that you can copy it to Excel easily. +##' @param ciFun Function used for calculation. \code{confint} is the default. For generalized linear models this gives the profile likelihood-based calculation, which may take too much time for large models, use \code{confint.default} for simple normal approximation method (+/- 1.96 * standard error). ##' @return A matrix containing what you see is returned invisibly. You can capture it by assignment to an object. ##' @author Kazuki Yoshida ##' @examples -##' +##' ##' ## Load ##' library(tableone) -##' +##' ##' ## Load Mayo Clinic Primary Biliary Cirrhosis Data ##' library(survival) ##' data(pbc) ##' ## Check variables ##' head(pbc) -##' +##' ##' ## Fit a Cox regression model ##' objCoxph <- coxph(formula = Surv(time, status == 2) ~ trt + age + albumin + ascites, ##' data = pbc) -##' +##' ##' ## Show the simple table ##' ShowRegTable(objCoxph) -##' +##' ##' ## Show with quote to ease copy and paste ##' ShowRegTable(objCoxph, quote = TRUE) -##' +##' ##' @export -ShowRegTable <- function(model, exp = TRUE, digits = 2, pDigits = 3, printToggle = TRUE, quote = FALSE) { +ShowRegTable <- function(model, exp = TRUE, digits = 2, pDigits = 3, printToggle = TRUE, + quote = FALSE, ciFun = confint) { ## Create formats fmt1 <- paste0("%.", digits, "f") @@ -42,7 +44,7 @@ ShowRegTable <- function(model, exp = TRUE, digits = 2, pDigits = 3, printToggle ## Obtain necessary data ## The model must have summary and confint methods modelCoef <- coef(model) - modelConfInt <- suppressMessages(confint(model)) + modelConfInt <- suppressMessages(ciFun(model)) modelSummaryMat <- coef(summary(model)) modelP <- modelSummaryMat[,ncol(modelSummaryMat)] @@ -59,18 +61,19 @@ ShowRegTable <- function(model, exp = TRUE, digits = 2, pDigits = 3, printToggle pointEstimates <- sprintf(fmt = fmt1, resMat[,1]) pointEstimates <- format(pointEstimates, justify = "right") - + resString <- sprintf(fmt = paste0("%s", " [", fmt1, ", ", fmt1 ,"]"), - ## resMat[,1], # point estimate + ## point estimate pointEstimates, - resMat[,2], # lower - resMat[,3] # upper - ) + ## lower bound + resMat[,2], + ## upper bound + resMat[,3]) ## Format p-values pString <- sprintf(fmt = fmt2, modelP) - ## Create a string like <0.001 + ## Create a string like <0.001 smallPString <- paste0("<0.", paste0(rep("0", pDigits - 1), collapse = ""), "1") ## Check positions where it is all zero like 0.000 posAllZeros <- grepl("^0\\.0*$", pString) @@ -79,7 +82,7 @@ ShowRegTable <- function(model, exp = TRUE, digits = 2, pDigits = 3, printToggle ## Put a preceding " " where it is not all zero like 0.000 pString[!posAllZeros] <- paste0(" ", pString[!posAllZeros]) - + ## Combine with the result column. (Need to be after exponentiation) outMat <- cbind(resString, "p" = pString) @@ -89,15 +92,15 @@ ShowRegTable <- function(model, exp = TRUE, digits = 2, pDigits = 3, printToggle ## Change column names depending on the exponentiation status if (exp) { - colnames(outMat)[1] <- "exp(beta) [confint]" + colnames(outMat)[1] <- "exp(coef) [confint]" } else if (!exp) { - colnames(outMat)[1] <- "beta [confint]" + colnames(outMat)[1] <- "coef [confint]" } ## Add quotes if requested if (quote) { rownames(outMat) <- paste0('"', rownames(outMat), '"') - colnames(outMat) <- paste0('"', colnames(outMat), '"') + colnames(outMat) <- paste0('"', colnames(outMat), '"') } diff --git a/R/modules.R b/R/modules-constructors.R similarity index 56% rename from R/modules.R rename to R/modules-constructors.R index 0d4bce6..b6c642d 100644 --- a/R/modules.R +++ b/R/modules-constructors.R @@ -1,12 +1,15 @@ ################################################################################ -### Modules for tableone -## This file contains common modules used in the tableone package. -## Created on: 2014-02-10 +### Modules for contructors +## +## Created on: 2015-08-02 ## Author: Kazuki Yoshida ################################################################################ -### Modules intended for the constructors + +### +### Data check modules ################################################################################ + ## Check if the data given is a data frame ModuleStopIfNotDataFrame <- function(data) { @@ -122,6 +125,11 @@ ModuleReturnStrata <- function(strata, data) { # Give strata variable names return(strata) } + +### +### Modules for data creation +################################################################################ + ## Module to create a table for one categorical variable ## Taken from Deducer::frequencies() ModuleCreateTableForOneVar <- function(x) { # Give a vector @@ -157,13 +165,64 @@ ModuleCreateTableForOneVar <- function(x) { # Give a vector return(freq) } + +## Convert variables with NA to include NA as a level (for CatTable constructor) +ModuleIncludeNaAsLevel <- function(data) { + ## Logical vector for variables that have any NA + logiAnyNA <- (colSums(is.na(data)) > 0) + + ## Add NA as a new level unless already present + data[logiAnyNA] <- + lapply(data[logiAnyNA], + function(var) { + if (all(!is.na(levels(var)))) { + var <- factor(var, c(levels(var), NA), + exclude = NULL) + } + var + }) + data +} + + + +### +### Modules for stratification +################################################################################ + ## Create StrataVarName from multiple dimension headers, for example sex:trt ModuleCreateStrataVarName <- function(obj) { ## Combine variable names with : in between paste0(names(attr(obj, "dimnames")), collapse = ":") } -### ModuleTryCatchWE +## Create a single variable representation of multivariable stratification for individuals +## result: by object; strata: data frame of stratifying variable(s) +ModuleCreateStrataVarAsFactor <- function(result, strata) { + + ## Create all possible combinations of strata levels and collapse as a vector. + dfStrataLevels <- expand.grid(attr(result, "dimnames")) # 1st var cycles fastest, consistent with by() + ## Create a single variable representing all strata + strataLevels <- apply(X = dfStrataLevels, MARGIN = 1, FUN = paste0, collapse = ":") + ## The length is the number of potential combinations. Used for the levels argument in the next part. + + ## Create the actual variable from the observed levels. NA if any one of the variables is NA. + strataVar <- as.character(interaction(strata, sep = ":")) + ## Make it a factor (kruskal.test requires it). Use levels not to drop defined nonexisting levels. + strataVar <- factor(strataVar, levels = strataLevels) + + ## Return stratifying variable. The length is the number of observations in the dataset. + ## NA for subjects with NA for any of the stratifying variables. + return(strataVar) +} + + + +### +### Modules for safe hypothesis testing and numeric summaries +################################################################################ + +## ModuleTryCatchWE ## Try catch function # Taken from demo(error.catching) ## Used to define non-failing functions, that return NA when there is an error ModuleTryCatchWE <- function(expr) { @@ -177,7 +236,6 @@ ModuleTryCatchWE <- function(expr) { warning = W) } -### ModuleTestSafe ## Function to perform non-failing tests (obj should be xtabs or formula) ModuleTestSafe <- function(obj, testFunction, testArgs = NULL) { @@ -218,214 +276,47 @@ ModuleSasKurtosis <- function(x) { } -## Create a single variable representation of multivariable stratification for individuals -## result: by object; strata: data frame of stratifying variable(s) -ModuleCreateStrataVarAsFactor <- function(result, strata) { - - ## Create all possible combinations of strata levels and collapse as a vector. - dfStrataLevels <- expand.grid(attr(result, "dimnames")) # 1st var cycles fastest, consistent with by() - ## Create a single variable representing all strata - strataLevels <- apply(X = dfStrataLevels, MARGIN = 1, FUN = paste0, collapse = ":") - ## The length is the number of potential combinations. Used for the levels argument in the next part. - - ## Create the actual variable from the observed levels. NA if any one of the variables is NA. - strataVar <- as.character(interaction(strata, sep = ":")) - ## Make it a factor (kruskal.test requires it). Use levels not to drop defined nonexisting levels. - strataVar <- factor(strataVar, levels = strataLevels) - - ## Return stratifying variable. The length is the number of observations in the dataset. - ## NA for subjects with NA for any of the stratifying variables. - return(strataVar) -} - - - -### Modules intented for the print methods +### +### Module for testing multiple variables ################################################################################ -## Define a function to format a normal variable -ModuleConvertNormal <- function(rowMat, digits) { - - ## Format for SD - fmt <- paste0(" (%.", digits,"f",")") - - ## Create a DF with numeric mean column and character (SD) column - data.frame(col1 = rowMat[,"mean"], - col2 = sprintf(fmt = fmt, rowMat[,"sd"]), - stringsAsFactors = FALSE) -} - -## Define a function to format a nonnormal variable -ModuleConvertNonNormal <- function(rowMat, digits, minMax = FALSE) { - - ## Format for [p25, p75] - fmt <- paste0(" [%.", digits,"f, %.",digits,"f]") - - if (minMax == FALSE) { - ## Create a DF with numeric median column and character [p25, p75] column - out <- data.frame(col1 = rowMat[,"median"], - col2 = sprintf(fmt = fmt, rowMat[,"p25"], rowMat[,"p75"]), - stringsAsFactors = FALSE) - } else if (minMax == TRUE) { - ## Create a DF with numeric median column and character [p25, p75] column - out <- data.frame(col1 = rowMat[,"median"], - col2 = sprintf(fmt = fmt, rowMat[,"min"], rowMat[,"max"]), - stringsAsFactors = FALSE) - } else { - stop("minMax must be a logical vector of one: FALSE or TRUE") - } - - return(out) -} - +ModuleApproxExactTests <- function(result, strata, dat, strataVarName, + testApprox, argsApprox, + testExact, argsExact) { + ## Create a single variable representation of multivariable stratification + strataVar <- ModuleCreateStrataVarAsFactor(result, strata) -## Module to handle TRUE/FALSE or character vector of variable names -## Returns a numeric vector: 1 for default action variable; 2 for alternative action variable -ModuleHandleDefaultOrAlternative <- function(switchVec, nameOfSwitchVec, varNames) { + ## Loop over variables in dat, and create a list of xtabs + ## Empty strata are kept in the corss tables. Different behavior than the cont counterpart! + listXtabs <- sapply(X = names(dat), + FUN = function(var) { + ## Create a formula + formula <- as.formula(paste0("~ ", var, " + ", "strataVar")) - ## Check the number of variables - nVars <- length(varNames) + ## Create a 2-dimensional crosstable + xtabs(formula = formula, data = dat) + }, + simplify = FALSE) - ## If null, do default print/test for all variables - if (is.null(switchVec)) { - ## Give one as many as there are variables - switchVec <- rep(1, nVars) + ## Rename the second dimension of the xtabs with the newly create name. + for (i in seq_along(listXtabs)) { - } else { - ## If not null, it needs checking. - - ## Check the switchVec argument - if (!is.logical(switchVec) & !is.character(switchVec)) { - stop(paste0(nameOfSwitchVec, " argument has to be FALSE/TRUE or a character vector of variable names.")) - } - ## Extend if it is a logitcal vector with one element. - if (is.logical(switchVec)) { - - if (length(switchVec) != 1) { - stop(paste0(nameOfSwitchVec, " has to be a logical vector of length 1")) - } - - switchVec <- rep(switchVec, nVars) - } - ## Convert to a logical vector if it is a character vector - if (is.character(switchVec)) { - switchVec <- varNames %in% switchVec - } - ## Convert to numeric (1 for default action, 2 for alternative actions) - switchVec <- as.numeric(switchVec) + 1 + names(dimnames(listXtabs[[i]]))[2] <- strataVarName } - return(switchVec) -} - - -## Column name formatter -ModuleCreateStrataNames <- function(TableObject) { - - ## Create all combinations and collapse as strings - strataNames <- apply(expand.grid(attr(TableObject, "dimnames")), - MARGIN = 1, - paste0, collapse = ":") - - ## Return the names as a vector - return(strataNames) -} - - -## p-value picker/formatter -ModulePickAndFormatPValues <- function(TableObject, switchVec, pDigits) { - - ## nVarsiables x 2 (pNormal,pNonNormal) data frame - pValues <- attr(TableObject, "pValues") - - ## Pick ones specified in exact (a vector with 1s(approx) and 2s(exact)) - pValues <- sapply(seq_along(switchVec), # loop over exact - FUN = function(i) { - ## Pick from a matrix i-th row, exact[i]-th column - ## Logical NA must be converted to a numeric - as.numeric(pValues[i, switchVec[i]]) - }, - simplify = TRUE) - - ## Format p value - fmt <- paste0("%.", pDigits, "f") - pVec <- sprintf(fmt = fmt, pValues) - - ## Create a string like <0.001 - smallPString <- paste0("<0.", paste0(rep("0", pDigits - 1), collapse = ""), "1") - ## Check positions where it is all zero like 0.000 - posAllZeros <- grepl("^0\\.0*$", pVec) - ## Put the string where it is all zero like 0.000 - pVec[posAllZeros] <- smallPString - ## Put a preceding space where it is not like 0.000 - pVec[!posAllZeros] <- paste0(" ", pVec[!posAllZeros]) - - ## Return formatted p-values (as many as there are variables) - return(pVec) -} - - -## Module to return the dimention headers added to the out 2d matrix -ModuleReturnDimHeaders <- function(TableObject) { - - ## Add stratification information to the column header - if (length(TableObject) > 1 ) { - ## Create strata string - strataString <- paste0("Stratified by ", - paste0(names(attr(TableObject, "dimnames")), collapse = ":")) - - ## Name the row dimension with it. 1st dimension name should be empty. - dimHeaders <- c("", strataString) - - } else { - ## If no stratification, no name for the second dimension - dimHeaders <- c("", "") - } - - ## Return the dim header a vector of length 2 - return(dimHeaders) -} - - -## Module to remove spaces from the result matrix -ModuleRemoveSpaces <- function(mat, noSpaces) { - - ## Carry out these replacements to remove spaces if asked - if (noSpaces) { - mat <- gsub(pattern = "^ *| *$", replacement = "", x = mat) - mat <- gsub(pattern = "\\( *", replacement = "(", x = mat) - } - - ## Return the matrix - mat -} - - -### Modules by both print and summary methods -## ModuleQuoteAndPrintMat() -## Takes an matrix object format, print, and (invisibly) return it -## Requires quote and printToggle argument in the printToggle method -ModuleQuoteAndPrintMat <- function(matObj, quote = FALSE, printToggle = TRUE) { - - ## Add quotes for names if requested - if (quote) { - ## row and col names - rownames(matObj) <- paste0('"', rownames(matObj), '"') - colnames(matObj) <- paste0('"', colnames(matObj), '"') - ## dimension name - names(dimnames(matObj)) <- paste0('"', names(dimnames(matObj)), '"') - ## 1st (row) dimension needs a preceding space for best copy and paste - names(dimnames(matObj))[1] <- paste0(" ", names(dimnames(matObj))[1]) - } - - ## print if required and return - if (printToggle) { - - print(matObj, quote = quote) - return(matObj) - - } else if (!printToggle) { - - return(matObj) - } + ## Loop over xtabs, and create p-values + pValues <- + sapply(X = listXtabs, + FUN = function(xtabs) { + ## Perform tests and return the result as 1x2 DF + data.frame(pApprox = ModuleTestSafe(xtabs, testApprox, argsApprox), + pExact = ModuleTestSafe(xtabs, testExact, argsExact)) + }, + simplify = FALSE) + + ## Create a single data frame (n x 2 (normal,nonormal)) + pValues <- do.call(rbind, pValues) + + ## Return both xtabs and p value df + list(pValues = pValues, xtabs = listXtabs) } diff --git a/R/modules-print.R b/R/modules-print.R new file mode 100644 index 0000000..7637e79 --- /dev/null +++ b/R/modules-print.R @@ -0,0 +1,683 @@ +################################################################################ +### Modules for print methods +## +## Created on: 2015-08-02 +## Author: Kazuki Yoshida +################################################################################ + + +### +### Modules common to both continuous and categorical +################################################################################ + +## Module to handle TRUE/FALSE or character vector of variable names +## Used for nonnormal argument and exact argument which may be logical or character +## Returns a numeric vector: 1 for default action variable; 2 for alternative action variable +ModuleHandleDefaultOrAlternative <- function(switchVec, nameOfSwitchVec, varNames) { + + ## Check the number of variables + nVars <- length(varNames) + + ## If null, do default print/test for all variables + if (is.null(switchVec)) { + ## Give one as many as there are variables + switchVec <- rep(1, nVars) + + } else { + ## If not null, it needs checking. + + ## Check the switchVec argument + if (!is.logical(switchVec) & !is.character(switchVec)) { + stop(paste0(nameOfSwitchVec, " argument has to be FALSE/TRUE or a character vector of variable names.")) + } + ## Extend if it is a logitcal vector with one element. + if (is.logical(switchVec)) { + + if (length(switchVec) != 1) { + stop(paste0(nameOfSwitchVec, " has to be a logical vector of length 1")) + } + + switchVec <- rep(switchVec, nVars) + } + ## Convert to a logical vector if it is a character vector + if (is.character(switchVec)) { + switchVec <- varNames %in% switchVec + } + ## Convert to numeric (1 for default action, 2 for alternative actions) + switchVec <- as.numeric(switchVec) + 1 + } + + return(switchVec) +} + + +## Column name formatter +ModuleCreateStrataNames <- function(TableObject) { + + ## Create all combinations and collapse as strings + strataNames <- apply(expand.grid(attr(TableObject, "dimnames")), + MARGIN = 1, + paste0, collapse = ":") + + ## Return the names as a vector + return(strataNames) +} + + +## p-value formatter +ModuleFormatPValues <- function(pValues, pDigits) { + + ## Format p value + fmt <- paste0("%.", pDigits, "f") + pVec <- sprintf(fmt = fmt, pValues) + + ## Create a string like <0.001 + smallPString <- paste0("<0.", paste0(rep("0", pDigits - 1), collapse = ""), "1") + ## Check positions where it is all zero like 0.000 + posAllZeros <- grepl("^0\\.0*$", pVec) + ## Put the string where it is all zero like 0.000 + pVec[posAllZeros] <- smallPString + ## Put a preceding space where it is not like 0.000 + pVec[!posAllZeros] <- paste0(" ", pVec[!posAllZeros]) + + ## Return formatted p-values (as many as there are variables) + return(pVec) +} + + +## p-value picker/formatter +ModulePickAndFormatPValues <- function(TableObject, switchVec, pDigits) { + + ## nVarsiables x 2 (pNormal,pNonNormal) data frame + pValues <- attr(TableObject, "pValues") + + ## Pick ones specified in exact (a vector with 1s(approx) and 2s(exact)) + pValues <- sapply(seq_along(switchVec), # loop over exact + FUN = function(i) { + ## Pick from a matrix i-th row, exact[i]-th column + ## Logical NA must be converted to a numeric + as.numeric(pValues[i, switchVec[i]]) + }, + simplify = TRUE) + + ## Return formatted p-values (as many as there are variables) + ## e.g. <0.001 if too small to show + ModuleFormatPValues(pValues, pDigits) +} + + +## Module to return the dimention headers added to the out 2d matrix +ModuleReturnDimHeaders <- function(TableObject) { + + ## Add stratification information to the column header + if (length(TableObject) > 1) { + ## Create strata string + strataString <- paste0("Stratified by ", + paste0(names(attr(TableObject, "dimnames")), collapse = ":")) + + ## Name the row dimension with it. 1st dimension name should be empty. + dimHeaders <- c("", strataString) + + } else { + ## If no stratification, no name for the second dimension + dimHeaders <- c("", "") + } + + ## Return the dim header a vector of length 2 + return(dimHeaders) +} + + +## Module to remove spaces from the result matrix +ModuleRemoveSpaces <- function(mat, noSpaces) { + + ## Carry out these replacements to remove spaces if asked + if (noSpaces) { + mat <- gsub(pattern = "^ *| *$", replacement = "", x = mat) + mat <- gsub(pattern = "\\( *", replacement = "(", x = mat) + } + + ## Return the matrix + mat +} + + +## Takes an matrix object format, print, and (invisibly) return it +## Requires quote and printToggle argument in the printToggle method +ModuleQuoteAndPrintMat <- function(matObj, quote = FALSE, printToggle = TRUE) { + + ## Add quotes for names if requested + if (quote) { + ## row and col names + rownames(matObj) <- paste0('"', rownames(matObj), '"') + colnames(matObj) <- paste0('"', colnames(matObj), '"') + ## dimension name + names(dimnames(matObj)) <- paste0('"', names(dimnames(matObj)), '"') + ## 1st (row) dimension needs a preceding space for best copy and paste + names(dimnames(matObj))[1] <- paste0(" ", names(dimnames(matObj))[1]) + } + + ## print if required and return + if (printToggle) { + + print(matObj, quote = quote) + return(matObj) + + } else if (!printToggle) { + + return(matObj) + } +} + + + +### +### Continuous variable formatters +################################################################################ + +## Define a function to format a normal variable +ModuleConvertNormal <- function(rowMat, digits) { + + ## Format for SD + fmt <- paste0(" (%.", digits,"f",")") + + ## Create a DF with numeric mean column and character (SD) column + data.frame(col1 = rowMat[,"mean"], + col2 = sprintf(fmt = fmt, rowMat[,"sd"]), + stringsAsFactors = FALSE) +} + +## Define a function to format a nonnormal variable +ModuleConvertNonNormal <- function(rowMat, digits, minMax = FALSE) { + + ## Format for [p25, p75] + fmt <- paste0(" [%.", digits,"f, %.",digits,"f]") + + if (minMax == FALSE) { + ## Create a DF with numeric median column and character [p25, p75] column + out <- data.frame(col1 = rowMat[,"median"], + col2 = sprintf(fmt = fmt, rowMat[,"p25"], rowMat[,"p75"]), + stringsAsFactors = FALSE) + } else if (minMax == TRUE) { + ## Create a DF with numeric median column and character [p25, p75] column + out <- data.frame(col1 = rowMat[,"median"], + col2 = sprintf(fmt = fmt, rowMat[,"min"], rowMat[,"max"]), + stringsAsFactors = FALSE) + } else { + stop("minMax must be a logical vector of one: FALSE or TRUE") + } + + return(out) +} + + +## Module to loop over strata formatting continuous variables +## No variable level looping here as each stratum is a matrix of all variables +ModuleContFormatStrata <- function(ContTable, nVars, listOfFunctions, digits) { + + ## Return a formatted table looping over strata + sapply(ContTable, + ## Each stratum is a matrix containing summaries + ## One row is one variable + FUN = function(stratum) { + + ## In an empty stratum, return empty + if (is.null(stratum)) { + + out <- rep("-", nVars) + ## Give NA to the width of the mean/median column in characters + nCharMeanOrMedian <- NA + + } else { + + ## Apply row by row within each non-empty stratum + ## This row-by-row operation is necessary to handle mean (sd) and median [IQR] + out <- sapply(seq_len(nVars), + FUN = function(i) { + + ## Choose between normal or nonnormal function + fun <- listOfFunctions[[i]] + ## Convert a row matrix to 1x2 df (numeric, character) + fun(stratum[i, , drop = FALSE]) + + ## Create a 1-row DF (numeric, character) + }, + simplify = FALSE) + + ## nx2 data frame by row binding multiple 1-row data frames + out <- do.call(rbind, out) + + ## Format for decimals + out$col1 <- sprintf(fmt = paste0("%.", digits, "f"), out$col1) + + ## right justify by adding spaces (to align at the decimal point of mean/median) + out$col1 <- format(out$col1, justify = "right") + + ## Obtain the width of the mean/median column in characters + nCharMeanOrMedian <- nchar(out$col1[1]) + + ## Create mean (SD) or median [p25, p75] as a character vector + out <- do.call(paste0, out) + } + + ## Add attributes + attributes(out) <- c(attributes(out), + list(nCharMeanOrMedian = nCharMeanOrMedian)) + + ## Return + out + }, + simplify = FALSE) +} + + + +### +### Categorical variable formatters +################################################################################ + +## Module to loop over variables within a stratum formatting categorical variables +ModuleCatFormatVariables <- function(lstVars, varsToFormat, fmt, level, cramVars, showAllLevels) { + + ## Loop over variables within a stratum + ## Each list element is a data frame summarizing levels + sapply(X = seq_along(lstVars), + FUN = function(i) { + + ## Extract the data frame (list element) + DF <- lstVars[[i]] + + ## Extract the variable name + varName <- names(lstVars)[i] + + ## Check number of rows (levels) + nRow <- nrow(DF) + + ## Add a variable name to the left as a character vector + DF <- cbind(var = rep(varName, nRow), DF) + + ## Format percent and cum.percent as strings + DF[varsToFormat] <- lapply(X = DF[varsToFormat], + FUN = sprintf, + fmt = fmt) + + ## Make all variables strings (if freq is an integer, direct convert is ok) + DF[] <- lapply(X = DF, FUN = as.character) + + ## Add first row indicator column + DF$firstRowInd <- "" + ## Add crammed row indicator column + DF$crammedRowInd <- "" + + + ## Format based on the number of levels + if (!showAllLevels & nRow == 1) { + ## If showAllLevels is FALSE AND there are only ONE levels, + ## change variable name to "var = level" + DF$var <- with(DF, paste0(var, " = ", level)) + + } else if (!showAllLevels & nRow == 2) { + ## If showAllLevels is FALSE AND there are only TWO levels, + ## cram two levels in one row if requested + if (unique(DF$var) %in% cramVars) { + ## If cramVars includes var, cram into one line + ## Cram two freq and count with / in between + DF$freq <- paste0(DF$freq, collapse = "/") + DF$percent <- paste0(DF$percent, collapse = "/") + ## change variable name, and delete the first level. + DF$var <- paste0(DF$var, " = ", + paste0(DF$level, collapse = "/")) + ## Delete the first row + DF <- DF[-1, , drop = FALSE] + ## Add crammed row indicator (used for formatting) + DF[1,"crammedRowInd"] <- "crammed" + + } else { + ## Otherwise, keep the second level only + ## change variable name, and delete the first level. + DF$var <- sprintf("%s = %s", DF$var, DF$level) + DF <- DF[-1, , drop = FALSE] + } + + } else if (!showAllLevels & nRow > 2) { + ## If showAllLevels is FALSE AND there are MORE THAN two levels, + ## add an empty row and put the var name, then levels below. + DF <- rbind(rep("", ncol(DF)), DF) + ## Add variable name in the first row + DF[1,"var"] <- DF[2,"var"] + + ## 2nd to last have level names. (nrow has changed by +1) + secondToLastRows <- seq(from = 2,to = nrow(DF), by = 1) + DF[secondToLastRows, "var"] <- + paste0(" ", DF[secondToLastRows, "level"]) # preceding spaces + + } else if (showAllLevels) { + ## If showAllLevels is TRUE, clear these except in 1st row + DF[-1, c("var","n","miss","p.miss")] <- "" + } + + ## Add first row indicator (used to add (%)) + DF[1,"firstRowInd"] <- "first" + + ## Return a data frame + DF + }, + simplify = FALSE) # Looped over variables (list element is DF) +} + + +## Module to loop over strata formatting categorical variables +ModuleCatFormatStrata <- function(CatTable, digits, varsToFormat, cramVars, showAllLevels) { + + ## Create format for percent used in the loop + fmt1 <- paste0("%.", digits, "f") + + ## Obtain collpased result + CatTableCollapsed <- + ## Loop over strata extracting list of variables + sapply(X = CatTable, + FUN = function(lstVars) { + + ## Do the following formatting only if the stratum is non-null. Do nothing if null. + if (!is.null(lstVars)) { + ## Returns an empty list if the stratum is null (empty). + + ## Loop over list of variables formatting them + lstVarsFormatted <- + ModuleCatFormatVariables(lstVars = lstVars, + varsToFormat = varsToFormat, + fmt = fmt1, + cramVars = cramVars, + showAllLevels = showAllLevels) + + + ## Collapse DFs within each stratum + DF <- do.call(rbind, lstVarsFormatted) + + ## Justification should happen here after combining variable DFs into a stratum DF. + ## Check non-empty rows + posNonEmptyRows <- DF$freq != "" + + + ## Create freq to be added on to the right side within () + DF$freqAddOn <- DF$freq + ## Right justify frequency (crammed and non-crammed at once) + DF$freq <- format(DF$freq, justify = "right") + ## Right justify frequency (non-crammed only) + DF[DF$crammedRowInd == "","freqAddOn"] <- + format(DF[DF$crammedRowInd == "","freqAddOn"], justify = "right") + ## Obtain the max width of characters + nCharFreq <- max(nchar(DF$freq)) + + + ## Create percent to be added on to the right side within () + DF$percentAddOn <- DF$percent + ## Right justify percent (crammed and non-crammed at once) + DF$percent <- format(DF$percent, justify = "right") + ## Right justify percent (non-crammed only) + DF[DF$crammedRowInd == "","percentAddOn"] <- + format(DF[DF$crammedRowInd == "","percentAddOn"], justify = "right") + ## Obtain the max width of characters + nCharPercent <- max(nchar(DF$percent)) + + + ## Add freq (percent) column (only in non-empty rows) + DF$freqPer <- "" + DF[posNonEmptyRows,]$freqPer <- sprintf(fmt = "%s (%s) ", + DF[posNonEmptyRows,]$freq, + DF[posNonEmptyRows,]$percentAddOn) + + ## Add percent (freq) column (only in non-empty rows) + DF$perFreq <- "" + DF[posNonEmptyRows,]$perFreq <- sprintf(fmt = "%s (%s) ", + DF[posNonEmptyRows,]$percent, + DF[posNonEmptyRows,]$freqAddOn) + + ## Add aditional attributes + attributes(DF) <- c(attributes(DF), + list(nCharFreq = nCharFreq, + nCharPercent = nCharPercent)) + + ## Return a data frame (2014-02-12 sapply breaks attributes?) + DF + } # end of non-null condition (Null strata skip all this. No action.) + + }, simplify = FALSE) + + CatTableCollapsed +} + + +## Obtain a vector indictor showing n-th variable's +## correspondence row(s) in FmtCatTable +ModuleVarToRowFmtCatTable <- function(spcFmtEltTables) { + + ## If no categorical elements, return NULL + if (!("FmtCatTable" %in% names(spcFmtEltTables))) { + return(NULL) + } + + ## Extract logical vector of which rows are title rows + logiNameRows <- attr(spcFmtEltTables$FmtCatTable, "logiNameRows") + + ## Create a numeric representation of which row(s) belong to which variable + numNameRows <- as.numeric(logiNameRows) + numNameRows[logiNameRows] <- seq_len(sum(logiNameRows)) + numNameRows[!logiNameRows] <- NA + + ## LOCF for subheaders (some variables have multiple rows) + numNameRows <- zoo::na.locf(numNameRows, na.rm = FALSE) + + ## First element is always sample size and should be 0 to avoid NA, + ## which breaks == use + numNameRows[1] <- 0 + numNameRows +} + + +### +### Modules for unifying continuous and categorical tables +################################################################################ + +## Extract stratumSizesRow from vecColWidths attribute of a FmtTable object +## Used by print.(svy)TableOne() +ModuleStratumSizesRow <- function(FmtTable, showAllLevels) { + + ## Length of vecColWidths is number of strata + nStrata <- length(attr(FmtTable, "vecColWidths")) + + ## Extract the sample size row from Fmt*Table + stratumSizesRow <- FmtTable[1, , drop = FALSE] + + + ## showAllLevels indicates if level column exists + if (showAllLevels) { + + ## Teke first nStrata columns after level column (position 1) + vecColWidths <- nchar(stratumSizesRow[1, 1 + seq_len(nStrata)]) + } else { + + ## Teke first nStrata columns + vecColWidths <- nchar(stratumSizesRow[1, seq_len(nStrata)]) + } + + ## Add + attr(stratumSizesRow, "vecColWidths") <- vecColWidths + + ## Return a single row matrix with vecColWidths attribute + stratumSizesRow +} + + +## Given a list of tables with vecColWidths, +## return a strata-by-table df containing spaces to add +ModuleNSpacesToAdd <- function(FmtElementTables) { + ## + ## Get the column width information for each object + ## each object has widths as many as strata + colWidthInfo <- sapply(FmtElementTables, + FUN = function(matObj) { + + attributes(matObj)$vecColWidths + }, + simplify = FALSE) + ## list to df + colWidthInfo <- as.data.frame(colWidthInfo) + + ## Get the max values for each stratum + vecMaxValues <- do.call(function(...) {pmax(..., na.rm = TRUE)}, colWidthInfo) + + ## Get the difference (value - max. Must be negated) + nSpacesToAdd <- sweep(x = colWidthInfo, + MARGIN = 1, + STATS = vecMaxValues, + FUN = "-") + ## Make sure these negative numbers are made positive + nSpacesToAdd <- abs(nSpacesToAdd) + ## Get rid of NA, so that it does not cause problem in rep() as a times argument + nSpacesToAdd[is.na(nSpacesToAdd)] <- 0 + nSpacesToAdd +} + + +## Add spaces to table columns as specified in nSpacesToAdd and considering showAllLevels +ModuleAddSpacesToTable <- function(FmtElementTables, nSpacesToAdd, showAllLevels) { + ## For each matrix, add spaces + spaceFormattedTables <- + sapply(seq_along(FmtElementTables), + FUN = function(i) { + + ## For i-th table + matObj <- FmtElementTables[[i]] + nSpaces <- nSpacesToAdd[, i] + + ## For j-th stratum (column), add spaces. + ## Be aware of the p-value column (last. not included in first palce) + ## and level column (1st. explicitly ignore). + for (j in seq_along(nSpaces)) { + + ## If showAllLevels is requested, ignore the first column (level column). + if (showAllLevels) { + matObj[, (j + 1)] <- paste0(paste0(rep(" ", nSpaces[j]), collapse = ""), + matObj[, (j + 1)]) + + } else { + ## if not, no need to ignore the first column + matObj[, j] <- paste0(paste0(rep(" ", nSpaces[j]), collapse = ""), + matObj[, j]) + } + + } + + ## Return the adjusted table + matObj + }, + simplify = FALSE) + + ## Restore names for easy acces + names(spaceFormattedTables) <- names(FmtElementTables) + spaceFormattedTables +} + + +## Extract Cont/CatTable elements of x and dispatch print() appropriately +ModuleFormatTables <- function(x, catDigits, contDigits, + ## Generic argumetns passed + test, smd, + explain, pDigits, + ## print.CatTable arguments passed + format, exact, + showAllLevels, cramVars, + ## print.ContTable arguments passed + nonnormal, minMax, insertLevel + ) { + + ## Two-element list(ContTable, CatTable) + ## Cont first throughout this function + TableOne <- list(ContTable = x$ContTable, + CatTable = x$CatTable) + ## Drop NULL element + TableOne <- TableOne[!sapply(TableOne, is.null)] + if (length(TableOne) == 0) { + warning("This object does not have valid ContTable or CatTable") + } + + ## Get the Cont/Cat status (1st of classes) + ## Always (ContTable, CatTable) by new definition + classOfTables <- sapply(TableOne, class)[1,] + + ## Decimal point vector; pick appropriately depending on class + contCatDigits <- c(ContTable = contDigits, + CatTable = catDigits, + svyContTable = contDigits, + svyCatTable = catDigits)[classOfTables] + + ## Get the formatted tables (FmtContTable, FmtCatTable) + FmtTables <- + sapply(seq_along(TableOne), + ## loop over ContTable and CatTable + FUN = function(i) { + + ## print.CatTable or print.ContTable called depending on the class + print(TableOne[[i]], + ## Number of digits depends on Cont or CatTable + digits = contCatDigits[i], + + ## Do not print + printToggle = FALSE, + + ## The rests are just passed + ## generic arguments passed + test = test, smd = smd, + explain = explain, pDigits = pDigits, + + ## print.CatTable arguments passed + format = format, exact = exact, + showAllLevels = showAllLevels, # Returns one more column if TRUE + cramVars = cramVars, + + ## print.ContTable arguments passed + nonnormal = nonnormal, minMax = minMax, insertLevel = showAllLevels + ) # Method dispatch at work + }, + simplify = FALSE) + ## Name formatted tables for easier access (Cont first!) + names(FmtTables) <- paste0("Fmt", names(TableOne)) + + FmtTables +} + + +## Create a list of one variable tables excluding sample size row +ModuleListOfOneVarTables <- function(spcFmtEltTables, MetaData) { + + ## Obtain a vector indictor showing n-th variable's + ## correspondence row(s) in FmtCatTable + vecVarToRow <- ModuleVarToRowFmtCatTable(spcFmtEltTables) + + ## Pick elements and construct a list of rows to rbind + ## loop over vars picking elements from appropriate objects + lstOneVarTables <- lapply(seq_along(MetaData$vars), function(i) { + + ## Extract current elements + var <- MetaData$vars[i] + logiFactor <- MetaData$logiFactors[i] + + ## Conditional on if its logical + if (logiFactor) { + ## If cat + nthElt <- which(var == MetaData$varFactors) + rowsToPick <- which(nthElt == vecVarToRow) + + spcFmtEltTables$FmtCatTable[rowsToPick, , drop = FALSE] + + } else { + ## If Cont + nthElt <- which(var == MetaData$varNumerics) + + ## + 1 because of sample size row + spcFmtEltTables$FmtContTable[nthElt + 1, , drop = FALSE] + } + }) + lstOneVarTables +} diff --git a/R/modules-smd.R b/R/modules-smd.R new file mode 100644 index 0000000..14d574f --- /dev/null +++ b/R/modules-smd.R @@ -0,0 +1,325 @@ +################################################################################ +### Modules for standardized mean difference support +## +## Created on: 2015-07-29 +## Author: Kazuki Yoshida +################################################################################ + +### +### References +################################################################################ +## A unified approach to measuring the effect size between two groups using SAS +## http://support.sas.com/resources/papers/proceedings12/335-2012.pdf +## R multinomial distribution variance +## http://stackoverflow.com/questions/19960605/r-multinomial-distribution-variance + + +### +### Helpers common to both unweighted and weighed functions +################################################################################ + +## Dummy matrix to means (proportions for each dummy) +MultinomialMeans <- function(dummyMat, na.rm = TRUE) { + colMeans(dummyMat, na.rm = na.rm) +} + +## Proportions to variance-covariance matrix +## http://stackoverflow.com/questions/19960605/r-multinomial-distribution-variance +## diagonal p_k(1-p_k); off-diagonal -1 * p_k * p_l +MultinomialVar <- function(multinomialMeans) { + ## Mean for each + p <- multinomialMeans + ## Diagonal elements are p_i(1-p_i) + vars <- p * (1 - p) + ## Off-diagonal elements are - p_i p_j where i != j + covs <- - outer(p, p) + ## Put diagonal elements + diag(covs) <- vars + ## Reduce to a scalar if 1x1 + drop(covs) +} + +## List of mean vector of a multinomial variable to categorical SMD (Yang & Dalton 2012) +StdDiffFromLstMeans <- function(lstMeans) { + + ## list of variance-covariance matrices + ## http://stackoverflow.com/questions/19960605/r-multinomial-distribution-variance + ## diagonal p_k(1-p_k); off-diagonal -1 * p_k * p_l + lstCovs <- lapply(lstMeans, MultinomialVar) + + ## All possible mean vector differences + lstMeanDiffs <- lapply(lstMeans, function(x) { + lapply(lstMeans, function(y) { + x - y + }) + }) + + ## All possible covariance matrix means + lstCovMeans <- lapply(lstCovs, function(x) { + lapply(lstCovs, function(y) { + (x + y) / 2 + }) + }) + + ## Initialize a numeric vector object for capturing values + sqSmds <- vector(mode = "numeric") + + ## Add upper triangle elements (i < j) to output list + for (i in seq_along(lstMeans)) { + for (j in seq_along(lstMeans)) { + if (i < j) { + ## For upper triangle elements only + ## Squared Mahalanobis distance + ## meanDiffs^T (pooled vcov)^-1 meanDiffs + ## Generalized inverse for protection against singularity + ## Reduces to true inverse if non-singular + + ## Column mean difference vector + T_C <- t(t(lstMeanDiffs[[i]][[j]])) + ## Pooled vcov + S <- lstCovMeans[[i]][[j]] + + if (all(S == 0)) { + ## If S is a zero matrix, ginv is a zero matrix + ## which gives a zero SMD regardless of mean + ## difference. Such case should be NaN. + sqMD <- NaN + } else { + ## Squared Mahalanobis distance + sqMD <- t(T_C) %*% MASS::ginv(S) %*% T_C + } + + ## Add sqrt of MD to output + ## Not efficient; room for improvement + sqSmds <- c(sqSmds, sqMD) + } + } + } + ## We want it on the original scale + sqrt(sqSmds) +} + + +## Given strata x levels of variable table +## Return list of proportions of levels dropping redundant first level +LstMeansFromFullTable <- function(strataByLevels) { + ## Proportion within each stratum + ## Equivalent to mean of dummy variables + propTables <- prop.table(strataByLevels, margin = 1) + + ## Drop first level to eliminate dependence if more than two levels + ## Avoids errors with constant variable + if (ncol(propTables) > 1) { + propTables <- propTables[, -1, drop = FALSE] + } + + ## list of mean vectors (Missing values are discarded) + lstMeans <- lapply(seq_len(nrow(propTables)), function(i) { + + propTables[i,] + }) + lstMeans +} + + +## Create a matrix form +FormatLstSmds <- function(lstSmds, nStrata) { + + if (nStrata < 2) { + warning("nStrata has to be >= 2.") + } + + ## matrix form + matSmds <- do.call(rbind, lstSmds) + + ## Create contrast names + allCombns <- t(combn(nStrata, 2)) + combnNames <- apply(allCombns, MARGIN = 1, FUN = paste, collapse = " vs ") + colnames(matSmds) <- combnNames + + ## Add a mean column if more than two columns + if (ncol(matSmds) > 1) { + matSmds <- cbind(average = rowMeans(matSmds), + matSmds) + } + + matSmds +} + + +### +### Functions for unweighted data only +################################################################################ + +### Check strata for NA-only strata +CheckNaOnlyStrata <- function(variable, group) { + + unlist(lapply(split(variable, group), + function(var) { + ## TRUE if only NA's within stratum + all(is.na(var)) + })) +} + + +### Continuous/binary standardized mean differences +## Expects continuous or 0,1 binary variable +StdDiff <- function(variable, group, binary = FALSE, na.rm = TRUE) { + + ## Check strata for all NA strata + logiAllNaStrata <- CheckNaOnlyStrata(variable, group) + ## If ANY strata have only NA's do not remove NA's + if (any(logiAllNaStrata)) { + warning("Variable has only NA's in at least one stratum. na.rm turned off.") + na.rm = FALSE + } + + ## Proportion of 1 is the mean of variable + means <- tapply(variable, group, mean, na.rm = na.rm) + + ## Variance is p(1-p) + if (binary) { + vars <- means * (1 - means) + } else { + vars <- tapply(variable, group, var, na.rm = na.rm) + } + + ## Outer to obtain all pairwise differences + meanDiffs <- outer(X = means, Y = means, FUN = "-") + ## Outer to obtain all pairwise variance mean + varMeans <- outer(X = vars, Y = vars, FUN = "+") / 2 + + out <- meanDiffs / sqrt(varMeans) + + abs(out[lower.tri(out)]) +} + +### Categorical (including binary) standardizzed mean difference +StdDiffMulti <- function(variable, group) { + + ## Check strata for all NA strata + logiAllNaStrata <- CheckNaOnlyStrata(variable, group) + + ## If ALL strata have only NA's convert to a level + ## If any strata have valid levels, table is ok + ## Empty table breaks the function + if (all(logiAllNaStrata)) { + + warning("Variable has only NA's in all strata. Regarding NA as a level.") + + variable <- factor(variable, exclude = NULL) + } + + ## strata x variable table + strataByLevels <- table(group, variable) + lstMeans <- LstMeansFromFullTable(strataByLevels) + ## Return vector of SMDs + StdDiffFromLstMeans(lstMeans) +} + + +### Standardized mean differences for multiple variables +## Continuous or binary only +StdDiffs <- function(data, vars, groupVar, binaryVars) { + + lapply(vars, function(var) { + + StdDiff(variable = data[,var], group = data[,groupVar], + binary = (var %in% binaryVars)) + }) +} + + +### +### Functions for weighted data only +################################################################################ + +### Check strata for NA-only strata +svyCheckNaOnlyStrata <- function(varName, groupName, design) { + + unlist(lapply(split(design$variables[,varName], + design$variables[,groupName]), + function(var) { + ## TRUE if only NA's within stratum + all(is.na(var)) + })) +} + +### Continuous/binary standardized mean differences +## Expects continuous or 0,1 binary variable +svyStdDiff <- function(varName, groupName, design, binary = FALSE, na.rm = TRUE) { + + ## Check strata for all NA strata + logiAllNaStrata <- svyCheckNaOnlyStrata(varName, groupName, design) + ## If ANY strata have only NA's do not remove NA's + if (any(logiAllNaStrata)) { + warning(varName, " has only NA's in at least one stratum. na.rm turned off.") + na.rm = FALSE + } + + varFormula <- as.formula(paste("~", varName)) + groupFormula <- as.formula(paste("~", groupName)) + + means <- svyby(formula = varFormula, by = groupFormula, + FUN = svymean, design = design, na.rm = na.rm)[,2] + + if (binary) { + vars <- means * (1 - means) + } else { + vars <- svyby(formula = varFormula, by = groupFormula, + FUN = svyvar, design = design, na.rm = na.rm)[,2] + } + + ## Outer to obtain all pairwise differences + meanDiffs <- outer(X = means, Y = means, FUN = "-") + ## Outer to obtain all pairwise variance mean + varMeans <- outer(X = vars, Y = vars, FUN = "+") / 2 + + out <- meanDiffs / sqrt(varMeans) + + abs(out[lower.tri(out)]) +} + + +### Categorical (including binary) standardizzed mean difference +svyStdDiffMulti <- function(varName, groupName, design) { + + ## Check strata for all NA strata + logiAllNaStrata <- svyCheckNaOnlyStrata(varName, groupName, design) + + ## If ALL strata have only NA's convert to a level + ## If any strata have valid levels, table is ok + ## Empty table breaks the function + if (all(logiAllNaStrata)) { + + warning(varName, " has only NA's in all strata. Regarding NA as a level.") + + design$variables[,varName] <- factor(design$variables[,varName], + exclude = NULL) + } + + ## If same variable is used as a group, factor() to allow tabling + if (varName == groupName) { + groupName <- sprintf("factor(%s)", groupName) + } + + tabFormula <- as.formula(sprintf("~ %s + %s", groupName, varName)) + + ## strata x variable table + strataByLevels <- svytable(formula = tabFormula, design = design) + lstMeans <- LstMeansFromFullTable(strataByLevels) + ## Return vector of SMDs + StdDiffFromLstMeans(lstMeans) +} + + +### Standardized mean differences for multiple variables +## Continuous or binary only +svyStdDiffs <- function(data, vars, groupVar, binaryVars) { + + lapply(vars, function(var) { + + svyStdDiff(varName = var, groupName = groupVar, design = data, + binary = (var %in% binaryVars)) + }) +} diff --git a/R/modules-svy.R b/R/modules-svy.R new file mode 100644 index 0000000..9489a20 --- /dev/null +++ b/R/modules-svy.R @@ -0,0 +1,281 @@ +################################################################################ +### Modules for survey data support +## +## Created on: 2015-07-23 +## Author: Kazuki Yoshida +################################################################################ + + +### +### Generic helpers +################################################################################ + +## Function to return 1 as many as x elements +## Used for counting weighted sample size +one <- function(x) { + rep(1, length(x)) +} + + +## 0,1 indicator variable for missingness +miss <- function(x) { + as.numeric(is.na(x)) +} + + +## Return formula string having vars on RHS +FormulaString <- function(vars) { + paste0(" ~ ", paste0(vars, collapse = " + ")) +} + + +### +### Helpers for both types of data +################################################################################ + +## Check for survey data; fail if not +StopIfNotSurveyDesign <- function(data) { + + if (!("survey.design2" %in% class(data))) { + stop("The data argument needs to be a survey design object.") + } + +} + + +### +### Helpers for continuous variable summary +################################################################################ + +svyN <- function(vars, design) { + ## Same for all variables, just use first + form <- sprintf(" ~ one(%s)", vars[1]) + ## No missing handling as all values are 1 + res <- svytotal(x = as.formula(form), design = design) + ## Repeat for all variables + out <- rep(res[1], length(vars)) + names(out) <- vars + out +} + + +svyMiss <- function(vars, design) { + + ## Rewrite variables locally to missing 0,1 indicators + design$variables[vars] <- lapply(design$variables[vars], miss) + + ## Same for all variables, just use first + form <- FormulaString(vars) + + res <- svytotal(x = as.formula(form), design = design) + out <- as.vector(res) + names(out) <- vars + out +} + + +svyPMiss <- function(vars, design) { + svyMiss(vars, design) / svyN(vars, design) +} + + +svyMean <- function(vars, design) { + ## Same for all variables, just use first + form <- FormulaString(vars) + ## Remove missingness and mean + ## Bad behavior, but consistent with the unweighted version + res <- svymean(x = as.formula(form), design = design, na.rm = TRUE) + out <- as.vector(res) + names(out) <- vars + out +} + + +svySd <- function(vars, design) { + ## Same for all variables, just use first + form <- FormulaString(vars) + ## Remove missingness and var + ## Bad behavior, but consistent with the unweighted version + res <- svyvar(x = as.formula(form), design = design, na.rm = TRUE) + + + ## Diagnonal elements are variances given 2+ variables + if (length(vars) == 1) { + + out <- as.vector(sqrt(res)) + + } else if (length(vars) > 1) { + + ## Matrix if vars is of length 2+ + out <- sqrt(diag(res)) + } + + names(out) <- vars + out +} + + +svyQuant <- function(vars, design, q = 0.5) { + ## Same for all variables, just use first + form <- FormulaString(vars) + ## Remove missingness and mean + ## Bad behavior, but consistent with the unweighted version + ## Use only one quantile + res <- svyquantile(x = as.formula(form), quantiles = q[1], design = design, na.rm = TRUE) + out <- as.vector(res) + names(out) <- vars + out +} + + +svyContSummary <- function(vars, design) { + + ## Save for reuse + nVec <- svyN(vars, design) + missVec <- svyMiss(vars, design) + + cbind(n = nVec, + miss = missVec, + p.miss = missVec / nVec * 100, + mean = svyMean(vars, design), + sd = svySd(vars, design), + median = svyQuant(vars, design, q = 0.5), + p25 = svyQuant(vars, design, q = 0.25), + p75 = svyQuant(vars, design, q = 0.75), + min = svyQuant(vars, design, q = 0), + max = svyQuant(vars, design, q = 1)) +} + + +### +### Helpers for categorical variable summary +################################################################################ +## These work on one variable at a time + +svyTable <- function(var, design) { + form <- FormulaString(var) + out <- svytable(formula = as.formula(form), design = design) + out +} + + +svyLevel <- function(var, design) { + names(svyTable(var, design)) +} + + +svyPropTable <- function(var, design) { + prop.table(svyTable(var, design)) +} + + +## This one works at a single variable level within a stratum +svyCatSummaryForOneVar <- function(var, design) { + + ## Tables + freqTab <- svyTable(var, design) + propTab <- prop.table(freqTab) + nLevels <- length(freqTab) + ## Repeat as many as the levels + nVec <- rep(svyN(var, design), nLevels) + missVec <- rep(svyMiss(var, design), nLevels) + + data.frame(n = nVec, + miss = missVec, + p.miss = missVec / nVec * 100, + level = names(freqTab), + freq = as.vector(freqTab), + percent = as.vector(propTab) * 100, + cum.percent = cumsum(propTab) * 100, + ## To protect against, level having + row.names = NULL) +} + + +## This one can take multiple variables and return a list +svyCatSummary <- function(vars, design) { + + sapply(vars, function(var) { + + svyCatSummaryForOneVar(var, design) + + }, simplify = FALSE) +} + + +### +### Helpers for testing for p values +################################################################################ + +## Function to do Wald test on a multi-degree variable after linear regression +svyGlmTermTest <- function(formula, design, test.terms, method = "Wald") { + + ## Perform linear regression and perform + regTermTest(svyglm(formula, design), test.terms = test.terms, method = method) +} + + +## Given a formula string as its first argument, calls svyGlmTermTest correctly +svyTestNormal <- function(formulaString, design, test.terms, method = "Wald") { + + out <- svyGlmTermTest(formula = as.formula(formulaString), design = design, + test.terms = test.terms, method = method) + ## Give an appropriate name for consistent extraction + list(p.value = out$p[1,1]) +} + + +## Kruskal-Wallis test +svyTestNonNormal <- function(formulaString, design) { + + ## This returns an htest object that has a scalar $p.value element + svyranktest(formula = as.formula(formulaString), design = design) +} + + +svyTestChisq <- function(formulaString, design) { + + ## This returns an htest object that has a scalar $p.value element + svychisq(formula = as.formula(formulaString), design = design) +} + + +svyModuleApproxExactTests <- function(data, vars, strataVarName, + testApprox, argsApprox) { + + ## Loop over variables and create a list of xtabs + ## Empty strata are kept in the corss tables. Different behavior than the cont counterpart! + listXtabs <- sapply(X = vars, + FUN = function(var) { + ## Create a formula + formula <- as.formula(paste0("~ ", var, " + ", "..strataVar..")) + + ## Create a 2-dimensional crosstable + svytable(formula = formula, design = data) + }, + simplify = FALSE) + + ## Rename the second dimension of the xtabs with the newly create name. + for (i in seq_along(listXtabs)) { + + names(dimnames(listXtabs[[i]]))[2] <- strataVarName + } + + ## Loop over variables, and create p-values + pValues <- + sapply(X = vars, + FUN = function(var) { + formulaString <- paste0(" ~ ", var, " + ..strataVar..") + ## Perform tests and return the result as 1x2 DF + data.frame(pApprox = ModuleTestSafe(formulaString, testApprox, + c(list(design = data), argsApprox)), + pExact = NA) # Not available for survey data. Just fill in with NA + }, + simplify = FALSE) + + ## Create a single data frame (n x 2 (normal,nonormal)) + pValues <- do.call(rbind, pValues) + + ## Return both xtabs and p value df + list(pValues = pValues, xtabs = listXtabs) +} diff --git a/R/print.CatTable.R b/R/print.CatTable.R index 9b4c8de..f078e19 100644 --- a/R/print.CatTable.R +++ b/R/print.CatTable.R @@ -1,10 +1,10 @@ -##' Format and print the \code{CatTable} class objects +##' Format and print \code{CatTable} class objects ##' -##' This is the \code{print} method for the \code{CatTable} class objects created by \code{\link{CreateCatTable}} function. +##' \code{print} method for the \code{CatTable} class objects created by \code{\link{CreateCatTable}} function. ##' -##' @param x The result of a call to the \code{\link{CreateCatTable}} function. +##' @param x Object returned by \code{\link{CreateCatTable}} function. ##' @param digits Number of digits to print in the table. -##' @param pDigits Number of digits to print for p-values. +##' @param pDigits Number of digits to print for p-values (also used for standardized mean differences). ##' @param quote Whether to show everything in quotes. The default is FALSE. If TRUE, everything including the row and column names are quoted so that you can copy it to Excel easily. ##' @param missing Whether to show missing data information (not implemented yet, placeholder) ##' @param explain Whether to add explanation to the variable names, i.e., (\%) is added to the variable names when percentage is shown. @@ -13,16 +13,15 @@ ##' @param format The default is "fp" frequency (percentage). You can also choose from "f" frequency only, "p" percentage only, and "pf" percentage (frequency). ##' @param showAllLevels Whether to show all levels. FALSE by default, i.e., for 2-level categorical variables, only the higher level is shown to avoid redundant information. ##' @param cramVars A character vector to specify the two-level categorical variables, for which both levels should be shown in one row. -##' @param test Whether to show the p-values. TRUE by default. If FALSE, only the numerical summaries are shown. +##' @param test Whether to show p-values. TRUE by default. If FALSE, only the numerical summaries are shown. ##' @param exact A character vector to specify the variables for which the p-values should be those of exact tests. By default all p-values are from large sample approximation tests (chisq.test). +##' @param smd Whether to show standardized mean differences. FALSE by default. If there are more than one contrasts, the average of all possible standardized mean differences is shown. For individual contrasts, use \code{summary}. ##' @param CrossTable Whether to show the cross table objects held internally using gmodels::CrossTable function. This will give an output similar to the PROC FREQ in SAS. ##' @param ... For compatibility with generic. Ignored. -##' @return It is mainly for printing the result. But this function does return a matrix containing what you see in the output invisibly. You can assign it to an object to save it. +##' @return A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via \code{write.csv}. ##' @author Kazuki Yoshida ##' @seealso -##' \code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}}, -##' \code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}}, -##' \code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}} +##' \code{\link{CreateTableOne}}, \code{\link{CreateCatTable}}, \code{\link{summary.CatTable}} ##' @examples ##' ##' ## Load @@ -83,25 +82,28 @@ ##' print(catTableBySexTrt, exact = "ascites", quote = TRUE, noSpaces = TRUE) ##' ##' @export -print.CatTable <- function(x, # CatTable object - digits = 1, pDigits = 3, # Number of digits to show - quote = FALSE, # Whether to show quotes +print.CatTable <- +function(x, # CatTable object + digits = 1, pDigits = 3, # Number of digits to show + quote = FALSE, # Whether to show quotes - missing = FALSE, # Show missing values (not implemented yet) - explain = TRUE, # Whether to show explanation in variable names - printToggle = TRUE, # Whether to print the result visibly - noSpaces = FALSE, # Whether to remove spaces for alignments + missing = FALSE, # Show missing values (not implemented yet) + explain = TRUE, # Whether to show explanation in variable names + printToggle = TRUE, # Whether to print the result visibly + noSpaces = FALSE, # Whether to remove spaces for alignments - format = c("fp","f","p","pf")[1], # Format f_requency and/or p_ercent - showAllLevels = FALSE, - cramVars = NULL, # variables to be crammed into one row + format = c("fp","f","p","pf")[1], # Format f_requency and/or p_ercent + showAllLevels = FALSE, + cramVars = NULL, # variables to be crammed into one row - test = TRUE, # Whether to add p-values - exact = NULL, # Which variables should be tested with exact tests + test = TRUE, # Whether to add p-values + exact = NULL, # Which variables should be tested with exact tests - CrossTable = FALSE, # Whether to show gmodels::CrossTable + smd = FALSE, # Whether to add standardized mean differences - ...) { + CrossTable = FALSE, # Whether to show gmodels::CrossTable + + ...) { ## x and ... required to be consistent with generic print(x, ...) CatTable <- x @@ -147,166 +149,21 @@ print.CatTable <- function(x, # CatTable object }, simplify = TRUE) # vector with as many elements as strata - ## Provide indicators to show what columns were added. - wasLevelColumnAdded <- FALSE - wasPValueColumnAdded <- FALSE - wasExactColumnAdded <- FALSE - ### Formatting for printing - ## Create format for percent used in the loop - fmt1 <- paste0("%.", digits, "f") + ## Variables to format using digits option + ## Full list c("n","miss","p.miss","freq","percent","cum.percent") + varsToFormat <- c("p.miss","percent","cum.percent") - ## Obtain collpased result + ## Obtain collpased result by looping over strata + ## within each stratum, loop over variables CatTableCollapsed <- - sapply(X = CatTable, # Loop over strata - FUN = function(LIST) { - - ## Do the following formatting only if the stratum is non-null. Do nothing if null. - if (!is.null(LIST)) { - - ## Returns an empty list if the stratum is null (empty). - LIST <- - sapply(X = seq_along(LIST), # Loop over variables (list element is DF) - FUN = function(i) { - - ## Extract the data frame (list element) - DF <- LIST[[i]] - - ## Extract the variable name - varName <- names(LIST)[i] - - ## Check number of rows (levels) - nRow <- nrow(DF) - - ## Add a variable name to the left as a character vector - DF <- cbind(var = rep(varName, nRow), - DF) - - ## Format percent and cum.percent as strings - DF[c("p.miss","percent","cum.percent")] <- - lapply(X = DF[c("p.miss","percent","cum.percent")], - FUN = sprintf, - fmt = fmt1) - - - ## Make all variables strings (freq is an integer, so direct convert ok) - DF[] <- lapply(X = DF, FUN = as.character) - - ## Add first row indicator column - DF$firstRowInd <- "" - ## Add crammed row indicator column - DF$crammedRowInd <- "" - - ## Format based on the number of levels - if (!showAllLevels & nRow == 1) { - ## If showAllLevels is FALSE AND there are only ONE levels, - ## change variable name to "var = level" - DF$var <- with(DF, paste0(var, " = ", level)) - - } else if (!showAllLevels & nRow == 2) { - ## If showAllLevels is FALSE AND there are only TWO levels, - ## cram two levels in one row if requested - if (unique(DF$var) %in% cramVars) { - ## If cramVars includes var, cram into one line - ## Cram two freq and count with / in between - DF$freq <- paste0(DF$freq, collapse = "/") - DF$percent <- paste0(DF$percent, collapse = "/") - ## change variable name, and delete the first level. - DF$var <- paste0(DF$var, " = ", - paste0(DF$level, collapse = "/")) - ## Delete the first row - DF <- DF[-1, , drop = FALSE] - ## Add crammed row indicator (used for formatting) - DF[1,"crammedRowInd"] <- "crammed" - } else { - ## Otherwise, keep the second level only - ## change variable name, and delete the first level. - DF$var <- with(DF, paste0(var, " = ", level)) - DF <- DF[-1, , drop = FALSE] - } - - } else if (!showAllLevels & nRow > 2) { - ## If showAllLevels is FALSE AND there are MORE THAN two levels, - ## add an empty row and put the var name, then levels below. - DF <- rbind(rep("", ncol(DF)), - DF) - ## Add variable name in the first row - DF[1,"var"] <- DF[2,"var"] - - ## 2nd to last have level names. (nrow has changed by +1) - secondToLastRows <- seq(from = 2,to = nrow(DF), by = 1) - DF[secondToLastRows, "var"] <- - paste0(" ", DF[secondToLastRows, "level"]) # preceding spaces - - } else if (showAllLevels) { - ## If showAllLevels is TRUE, clear these except in 1st row - DF[-1, c("var","n","miss","p.miss")] <- "" - } - - ## Add first row indicator (used to add (%)) - DF[1,"firstRowInd"] <- "first" - - ## Return a data frame - DF - }, - simplify = FALSE) # Looped over variables (list element is DF) - - - ## Collapse DFs within each stratum - DF <- do.call(rbind, LIST) - - ## Justification should happen here after combining variable DFs into a stratum DF. - ## Check non-empty rows - posNonEmptyRows <- DF$freq != "" - - - ## Create freq to be added on to the right side within () - DF$freqAddOn <- DF$freq - ## Right justify frequency (crammed and non-crammed at once) - DF$freq <- format(DF$freq, justify = "right") - ## Right justify frequency (non-crammed only) - DF[DF$crammedRowInd == "","freqAddOn"] <- - format(DF[DF$crammedRowInd == "","freqAddOn"], justify = "right") - ## Obtain the max width of characters - nCharFreq <- max(nchar(DF$freq)) - - - ## Create percent to be added on to the right side within () - DF$percentAddOn <- DF$percent - ## Right justify percent (crammed and non-crammed at once) - DF$percent <- format(DF$percent, justify = "right") - ## Right justify percent (non-crammed only) - DF[DF$crammedRowInd == "","percentAddOn"] <- - format(DF[DF$crammedRowInd == "","percentAddOn"], justify = "right") - ## Obtain the max width of characters - nCharPercent <- max(nchar(DF$percent)) - - - ## Add freq (percent) column (only in non-empty rows) - DF$freqPer <- "" - DF[posNonEmptyRows,]$freqPer <- sprintf(fmt = "%s (%s) ", - DF[posNonEmptyRows,]$freq, - DF[posNonEmptyRows,]$percentAddOn) - - ## Add percent (freq) column (only in non-empty rows) - DF$perFreq <- "" - DF[posNonEmptyRows,]$perFreq <- sprintf(fmt = "%s (%s) ", - DF[posNonEmptyRows,]$percent, - DF[posNonEmptyRows,]$freqAddOn) - - ## Add aditional attributes - attributes(DF) <- c(attributes(DF), - list(nCharFreq = nCharFreq, - nCharPercent = nCharPercent) - ) - - ## Return a data frame (2014-02-12 sapply breaks attributes?) - DF - } # end of non-null condition (Null strata skip all this. No action.) - - }, simplify = FALSE) + ModuleCatFormatStrata(CatTable = CatTable, + digits = digits, + varsToFormat = varsToFormat, + cramVars = cramVars, + showAllLevels = showAllLevels) ### Obtain the original column width in characters for alignment in print.TableOne @@ -353,25 +210,16 @@ print.CatTable <- function(x, # CatTable object colnames(out) <- ModuleCreateStrataNames(CatTable) } - + ## Set the variables names rownames(out) <- CatTableCollapsed[[posFirstNonNullElement]][,"var"] ## Get positions of rows with variable names + ## Used for adding p values in place logiNonEmptyRowNames <- CatTableCollapsed[[posFirstNonNullElement]][, "firstRowInd"] != "" - - ## Add level names if showAllLevels is TRUE. This adds the level column. Need come after column naming. - if (showAllLevels) { - out <- cbind(level = CatTableCollapsed[[posFirstNonNullElement]][,"level"], # Cannot be DF - out) - ## Changed the indicator - wasLevelColumnAdded <- TRUE - } - - ## Add p-values when requested and available - if (test == TRUE & !is.null(attr(CatTable, "pValues"))) { + if (test & !is.null(attr(CatTable, "pValues"))) { ## Pick test types used (used for annonation) testTypes <- c("","exact")[exact] @@ -383,22 +231,29 @@ print.CatTable <- function(x, # CatTable object ## Create an empty p-value column and test column out <- cbind(out, - p = rep("", nrow(out))) # Column for p-values + p = rep("", nrow(out))) # Column for p-values ## Put the values at the non-empty positions out[logiNonEmptyRowNames,"p"] <- pVec - ## Change the indicator - wasPValueColumnAdded <- TRUE - - ## Create an empty test type column, and add test types out <- cbind(out, test = rep("", nrow(out))) # Column for test types ## Put the test types at the non-empty positions (all rows in continuous!) out[logiNonEmptyRowNames,"test"] <- testTypes - ## Change the indicator - wasExactColumnAdded <- TRUE + } + + + ## Add SMDs when requested and available + if (smd & !is.null(attr(CatTable, "smd"))) { + + ## Create an empty column + out <- cbind(out, + SMD = rep("", nrow(out))) # Column for p-values + ## Put the values at the non-empty positions + out[logiNonEmptyRowNames,"SMD"] <- + ModuleFormatPValues(attr(CatTable, "smd")[,1], + pDigits = pDigits) } @@ -411,18 +266,24 @@ print.CatTable <- function(x, # CatTable object explainString) } - ## Keep column names (strataN does not have correct names if stratification is by multiple variables) + ## Keep column names (strataN does not have correct names + ## if stratification is by multiple variables) outColNames <- colnames(out) - ## Add n at the correct location depending on the number of columns added (level and/or p) - out <- rbind(n = c(level = rep("", wasLevelColumnAdded), # Add "" padding if level added - strataN, - p = rep("", wasPValueColumnAdded), # Add "" padding if p-value added - test = rep("", wasExactColumnAdded) # Add "" padding if exact test used - ), - out) + ## rbind sample size row, padding necessary "" for p value, etc + nRow <- c(strataN, rep("", ncol(out) - length(strataN))) + out <- rbind(n = nRow, out) ## Put back the column names (overkill for non-multivariable cases) colnames(out) <- outColNames + ## Add level names if showAllLevels is TRUE. + ## This adds the level column to the left, thus, after nRow addition. + ## Need come after column naming. + if (showAllLevels) { + out <- + cbind(level = c("", CatTableCollapsed[[posFirstNonNullElement]][,"level"]), + out) + } + ## Add stratification information to the column header depending on the dimension names(dimnames(out)) <- ModuleReturnDimHeaders(CatTable) @@ -435,13 +296,14 @@ print.CatTable <- function(x, # CatTable object ## Print CrossTable() if requested if (CrossTable) { - junk <- lapply(attributes(CatTable)$xtabs, gmodels::CrossTable) } ## Add attributes for column widths in characters attributes(out) <- c(attributes(out), - list(vecColWidths = vecColWidths)) + list(vecColWidths = vecColWidths, + ## Add one FALSE for sample size row + logiNameRows = c(FALSE, logiNonEmptyRowNames))) ## return a matrix invisibly return(invisible(out)) diff --git a/R/print.ContTable.R b/R/print.ContTable.R index 46c251d..08962ed 100644 --- a/R/print.ContTable.R +++ b/R/print.ContTable.R @@ -1,10 +1,10 @@ -##' Format and print the \code{ContTable} class objects +##' Format and print \code{ContTable} class objects ##' -##' This is the \code{print} method for the \code{ContTable} class objects created by \code{\link{CreateContTable}} function. +##' \code{print} method for the \code{ContTable} class objects created by \code{\link{CreateContTable}} function. ##' -##' @param x The result of a call to the \code{\link{CreateContTable}} function. +##' @param x Object returned by \code{\link{CreateContTable}} function. ##' @param digits Number of digits to print in the table. -##' @param pDigits Number of digits to print for p-values. +##' @param pDigits Number of digits to print for p-values (also used for standardized mean differences). ##' @param quote Whether to show everything in quotes. The default is FALSE. If TRUE, everything including the row and column names are quoted so that you can copy it to Excel easily. ##' @param missing Whether to show missing data information (not implemented yet, placeholder) ##' @param explain Whether to add explanation to the variable names, i.e., (mean (sd) or median [IQR]) is added to the variable names. @@ -13,14 +13,13 @@ ##' @param nonnormal A character vector to specify the variables for which the p-values should be those of nonparametric tests. By default all p-values are from normal assumption-based tests (oneway.test). ##' @param minMax Whether to use [min,max] instead of [p25,p75] for nonnormal variables. The default is FALSE. ##' @param insertLevel Whether to add an empty level column to the left of strata. -##' @param test Whether to show the p-values. TRUE by default. If FALSE, only the numerical summaries are shown. +##' @param test Whether to show p-values. TRUE by default. If FALSE, only the numerical summaries are shown. +##' @param smd Whether to show standardized mean differences. FALSE by default. If there are more than one contrasts, the average of all possible standardized mean differences is shown. For individual contrasts, use \code{summary}. ##' @param ... For compatibility with generic. Ignored. -##' @return It is mainly for printing the result. But this function does return a matrix containing what you see in the output invisibly. You can assign it to an object to save it. +##' @return A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via \code{write.csv}. ##' @author Kazuki Yoshida ##' @seealso -##' \code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}}, -##' \code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}}, -##' \code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}} +##' \code{\link{CreateTableOne}}, \code{\link{CreateContTable}}, \code{\link{summary.ContTable}} ##' @examples ##' ##' ## Load @@ -80,24 +79,27 @@ ##' ##' ## If you want to center-align values in Word, use noSpaces option. ##' print(contTableBySexTrt, nonnormal = nonNormalVars, quote = TRUE, noSpaces = TRUE) -##' +##' ##' @export -print.ContTable <- function(x, # ContTable object - digits = 2, pDigits = 3, # Number of digits to show - quote = FALSE, # Whether to show quotes +print.ContTable <- +function(x, # ContTable object + digits = 2, pDigits = 3, # Number of digits to show + quote = FALSE, # Whether to show quotes + + missing = FALSE, # show missing values (not implemented yet) + explain = TRUE, # Whether to show explanation in variable names + printToggle = TRUE, # Whether to print the result visibly + noSpaces = FALSE, # Whether to remove spaces for alignments - missing = FALSE, # show missing values (not implemented yet) - explain = TRUE, # Whether to show explanation in variable names - printToggle = TRUE, # Whether to print the result visibly - noSpaces = FALSE, # Whether to remove spaces for alignments + nonnormal = NULL, # Which variables should be treated as nonnormal + minMax = FALSE, # median [range] instead of median [IQR] + insertLevel = FALSE, # insert the level column to match showAllLevels in print.CatTable - nonnormal = NULL, # Which variables should be treated as nonnormal - minMax = FALSE, # median [range] instead of median [IQR] - insertLevel = FALSE, # insert the level column to match showAllLevels in print.CatTable + test = TRUE, # Whether to add p-values - test = TRUE, # Whether to add p-values + smd = FALSE, # Whether to add standardized mean differences - ...) { + ...) { ## x and ... required to be consistent with generic print(x, ...) ContTable <- x @@ -143,10 +145,6 @@ print.ContTable <- function(x, # ContTable object }, simplify = TRUE) # vector with as many elements as strata - ## Provide indicators to show what columns were added. - wasPValueColumnAdded <- FALSE - wasNonNormalColumnAdded <- FALSE - ### Conversion of data for printing @@ -168,54 +166,11 @@ print.ContTable <- function(x, # ContTable object listOfFunctions <- listOfFunctions[nonnormal] ## Loop over strata (There may be just one) - out <- sapply(ContTable, - FUN = function(stratum) { - - ## In an empty stratum, return empty - if (is.null(stratum)) { - out <- rep("-", nVars) - ## Give NA to the width of the mean/median column in characters - nCharMeanOrMedian <- NA - } else { - - ## Apply row by row within each non-empty stratum - ## This row-by-row operation is necessary to handle mean (sd) and median [IQR] - out <- sapply(seq_len(nVars), - FUN = function(i) { - - ## Choose between normal or nonnormal function - fun <- listOfFunctions[[i]] - ## Convert a row matrix to 1x2 df (numeric, character) - fun(stratum[i, , drop = FALSE]) - - ## Create a 1-row DF (numeric, character) - }, - simplify = FALSE) - - ## nx2 data frame by row binding multiple 1-row data frames - out <- do.call(rbind, out) - - ## Format for decimals - out$col1 <- sprintf(fmt = paste0("%.", digits, "f"), out$col1) - - ## right justify by adding spaces (to align at the decimal point of mean/median) - out$col1 <- format(out$col1, justify = "right") - - ## Obtain the width of the mean/median column in characters - nCharMeanOrMedian <- nchar(out$col1[1]) + out <- ModuleContFormatStrata(ContTable = ContTable, + nVars = nVars, + listOfFunctions = listOfFunctions, + digits = digits) - ## Create mean (SD) or median [p25, p75] as a character vector - out <- do.call(paste0, out) - } - - ## Add attributes - attributes(out) <- c(attributes(out), - list(nCharMeanOrMedian = nCharMeanOrMedian)) - - ## Return - out - }, - simplify = FALSE) ### Obtain the original column width in characters for alignment in print.TableOne vecColWidths <- sapply(out, @@ -255,21 +210,28 @@ print.ContTable <- function(x, # ContTable object ## Column combine with the output out <- cbind(out, p = pVec) - ## Change the indicator - wasPValueColumnAdded <- TRUE - - ## Create an empty test type column, and add test types out <- cbind(out, test = rep("", nrow(out))) # Column for test types ## Put the test types at the non-empty positions (all rows in continuous!) out[ ,"test"] <- testTypes - ## Change the indicator - wasNonNormalColumnAdded <- TRUE } + ## Add SMDs when requested and available + if (smd & !is.null(attr(ContTable, "smd"))) { + + ## Create an empty column + out <- cbind(out, + SMD = rep("", nrow(out))) # Column for p-values + ## Put the values at the non-empty positions + out[,"SMD"] <- ModuleFormatPValues(attr(ContTable, "smd")[,1], + pDigits = pDigits) + } + + + ## Add mean (sd) or median [IQR]/median [range] explanation if requested if (explain) { ## Create a vector of explanations to be pasted @@ -285,11 +247,8 @@ print.ContTable <- function(x, # ContTable object ## Keep column names (strataN does not have correct names if stratification is by multiple variables) outColNames <- colnames(out) ## Add n at the correct location depending on the number of columns added (level and/or p) - out <- rbind(n = c(strataN, - p = rep("", wasPValueColumnAdded), # Add "" padding if p-value added - test = rep("", wasNonNormalColumnAdded) # Add "" padding if nonnormal test used - ), - out) + nRow <- c(strataN, rep("", ncol(out) - length(strataN))) # Additional padding to right + out <- rbind(n = nRow, out) ## Put back the column names (overkill for non-multivariable cases) colnames(out) <- outColNames diff --git a/R/print.TableOne.R b/R/print.TableOne.R index c400aa5..24fd757 100644 --- a/R/print.TableOne.R +++ b/R/print.TableOne.R @@ -1,16 +1,17 @@ -##' Format and print the \code{TableOne} class objects +##' Format and print \code{TableOne} class objects ##' -##' This is the \code{print} method for the \code{TableOne} class objects created by \code{\link{CreateTableOne}} function. +##' \code{print} method for the \code{TableOne} class objects created by \code{\link{CreateTableOne}} function. ##' -##' @param x The result of a call to the \code{\link{CreateTableOne}} function. +##' @param x Object returned by \code{\link{CreateTableOne}} function. ##' @param catDigits Number of digits to print for proportions. Default 1. ##' @param contDigits Number of digits to print for continuous variables. Default 2. -##' @param pDigits Number of digits to print for p-values. Default 3. +##' @param pDigits Number of digits to print for p-values (also used for standardized mean differences). Default 3. ##' @param quote Whether to show everything in quotes. The default is FALSE. If TRUE, everything including the row and column names are quoted so that you can copy it to Excel easily. ##' @param missing Whether to show missing data information (not implemented yet, placeholder) ##' @param explain Whether to add explanation to the variable names, i.e., (\%) is added to the variable names when percentage is shown. ##' @param printToggle Whether to print the output. If FLASE, no output is created, and a matrix is invisibly returned. -##' @param test Whether to show the p-values. TRUE by default. If FALSE, only the numerical summaries are shown. +##' @param test Whether to show p-values. TRUE by default. If FALSE, only the numerical summaries are shown. +##' @param smd Whether to show standardized mean differences. FALSE by default. If there are more than one contrasts, the average of all possible standardized mean differences is shown. For individual contrasts, use \code{summary}. ##' @param noSpaces Whether to remove spaces added for alignment. Use this option if you prefer to align numbers yourself in other software. ##' @param format The default is "fp" frequency (percentage). You can also choose from "f" frequency only, "p" percentage only, and "pf" percentage (frequency). ##' @param showAllLevels Whether to show all levels. FALSE by default, i.e., for 2-level categorical variables, only the higher level is shown to avoid redundant information. @@ -19,193 +20,112 @@ ##' @param nonnormal A character vector to specify the variables for which the p-values should be those of nonparametric tests. By default all p-values are from normal assumption-based tests (oneway.test). ##' @param minMax Whether to use [min,max] instead of [p25,p75] for nonnormal variables. The default is FALSE. ##' @param ... For compatibility with generic. Ignored. -##' @return It is mainly for printing the result. But this function does return a matrix containing what you see in the output invisibly. You can assign it to an object to save it. +##' @return A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via \code{write.csv}. ##' @author Kazuki Yoshida, Justin Bohn ##' @seealso -##' \code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}}, -##' \code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}}, -##' \code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}} +##' \code{\link{CreateTableOne}}, \code{\link{CreateTableOne}}, \code{\link{summary.TableOne}} ##' @examples ##' -##' ## Load -##' library(tableone) -##' -##' ## Load Mayo Clinic Primary Biliary Cirrhosis Data -##' library(survival) -##' data(pbc) -##' ## Check variables -##' head(pbc) -##' -##' ## Make categorical variables factors -##' varsToFactor <- c("status","trt","ascites","hepato","spiders","edema","stage") -##' pbc[varsToFactor] <- lapply(pbc[varsToFactor], factor) -##' -##' ## Create Table 1 stratified by sex and trt -##' tableOne <- CreateTableOne(vars = c("time","status","age","ascites","hepato", -##' "spiders","edema","bili","chol","albumin", -##' "copper","alk.phos","ast","trig","platelet", -##' "protime","stage"), -##' strata = c("sex","trt"), data = pbc) -##' -##' ## Just typing the object name will invoke the print.TableOne method -##' tableOne -##' -##' ## Specifying nonnormal variables will show the variables appropriately, -##' ## and show nonparametric test p-values. Specify variables in the exact -##' ## argument to obtain the exact test p-values. cramVars can be used to -##' ## show both levels for a 2-level categorical variables. -##' print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), -##' exact = c("status","stage"), cramVars = "hepato") -##' -##' ## Use the summary.TableOne method for detailed summary -##' summary(tableOne) -##' -##' ## See the categorical part only using $ operator -##' tableOne$CatTable -##' summary(tableOne$CatTable) -##' -##' ## See the continuous part only using $ operator -##' tableOne$ContTable -##' summary(tableOne$ContTable) -##' -##' ## If your work flow includes copying to Excel and Word when writing manuscripts, -##' ## you may benefit from the quote argument. This will quote everything so that -##' ## Excel does not mess up the cells. -##' print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), -##' exact = c("status","stage"), cramVars = "hepato", quote = TRUE) -##' -##' ## If you want to center-align values in Word, use noSpaces option. -##' print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), -##' exact = c("status","stage"), cramVars = "hepato", quote = TRUE, noSpaces = TRUE) +##' ## See examples for CreateTableOne and svyCreateTableOne ##' ##' @export -print.TableOne <- function(x, # TableOne object - catDigits = 1, contDigits = 2, pDigits = 3, # Number of digits to show - quote = FALSE, # Whether to show quotes - - ## Common options - missing = FALSE, # Not implemented yet - explain = TRUE, # Whether to show explanation in variable names - printToggle = TRUE, # Whether to print the result visibly - test = TRUE, # Whether to add p-values - noSpaces = FALSE, # Whether to remove spaces for alignments - - ## Categorical options - format = c("fp","f","p","pf")[1], # Format f_requency and/or p_ercent - showAllLevels = FALSE, # Show all levels of a categorical variable - cramVars = NULL, # Which 2-level variables to show both levels in one row - exact = NULL, # Which variables should be tested with exact tests - - ## Continuous options - nonnormal = NULL, # Which variables should be treated as nonnormal - minMax = FALSE, # Whether to show median - - ...) { - - ## Get the mixed element only - TableOne <- x$TableOne - - ## Get the Cont/Cat status (1st of classes) - classOfTables <- sapply(TableOne, class)[1,] - digits <- c(CatTable = catDigits, ContTable = contDigits)[classOfTables] - - - ## Get the formatted tables - formattedTables <- sapply(seq_along(TableOne), - FUN = function(i) { - - ## print.CatTable or print.ContTable called depending on the class - print(TableOne[[i]], printToggle = FALSE, test = test, explain = explain, - digits = digits[i], - - ## print.CatTable arguments - format = format, exact = exact, - showAllLevels = showAllLevels, # Returns one more column if TRUE - cramVars = cramVars, - - ## print.ContTable argument - nonnormal = nonnormal, minMax = minMax, insertLevel = showAllLevels - ) # Method dispatch at work - }, - simplify = FALSE) - - ## Get the column width information (strata x vars format) - columnWidthInfo <- sapply(formattedTables, - FUN = function(matObj) { - - attributes(matObj)$vecColWidths - }, - simplify = FALSE) - columnWidthInfo <- do.call(cbind, columnWidthInfo) - - ## Get the max values for each stratum - vecMaxValues <- apply(columnWidthInfo, MARGIN = 1, FUN = max, na.rm = TRUE) - - ## Get the difference (value - max. Must be negated) - nSpacesToAdd <- sweep(x = columnWidthInfo, - MARGIN = 1, - STATS = vecMaxValues, - FUN = "-" - ) - nSpacesToAdd <- -1 * nSpacesToAdd - ## Get rid of NA, so that it does not cause problem in rep() as a times argument - nSpacesToAdd[is.na(nSpacesToAdd)] <- 0 - - ## For each matrix, add spaces - spaceFormattedTables <- sapply(seq_along(formattedTables), - FUN = function(i) { - - ## For i-th variable - matObj <- formattedTables[[i]] - nSpaces <- nSpacesToAdd[, i] - - ## For j-th stratum (column), add spaces. - ## Be aware of the p-value column (last. not included in first palce) - ## and level column (1st. explicitly ignore). - for (j in seq_along(nSpaces)) { - - ## If showAllLevels is requested, ignore the first column (level column). - if (showAllLevels) { - matObj[, (j + 1)] <- paste0(paste0(rep(" ", nSpaces[j]), collapse = ""), - matObj[, (j + 1)]) - - } else { - ## if not, no need to ignore the first column - matObj[, j] <- paste0(paste0(rep(" ", nSpaces[j]), collapse = ""), - matObj[, j]) - } - - } - - ## Return the adjusted table - matObj - }, - simplify = FALSE) - - ## Set aside the n row (stratum sizes). 1st element, 1st row - stratumSizesRow <- spaceFormattedTables[[1]][1, , drop = FALSE] - - ## Remove 1st rows from each table (stratum sizes) - spaceFormattedTables <- sapply(spaceFormattedTables, - FUN = function(matObj) { - - matObj[-1, , drop = FALSE] - }, - simplify = FALSE) +print.TableOne <- +function(x, # TableOne object + catDigits = 1, contDigits = 2, pDigits = 3, # Number of digits to show + quote = FALSE, # Whether to show quotes + + ## Common options + missing = FALSE, # Not implemented yet + explain = TRUE, # Whether to show explanation in variable names + printToggle = TRUE, # Whether to print the result visibly + test = TRUE, # Whether to add p-values + smd = FALSE, # Whether to add standardized mean differences + noSpaces = FALSE, # Whether to remove spaces for alignments + + ## Categorical options + format = c("fp","f","p","pf")[1], # Format f_requency and/or p_ercent + showAllLevels = FALSE, # Show all levels of a categorical variable + cramVars = NULL, # Which 2-level variables to show both levels in one row + exact = NULL, # Which variables should be tested with exact tests + + ## Continuous options + nonnormal = NULL, # Which variables should be treated as nonnormal + minMax = FALSE, # Whether to show median + + ...) { + + ## Extract Cont/CatTable elements of x and dispatch print() appropriately + FmtTables <- ModuleFormatTables(x, + catDigits = catDigits, contDigits = contDigits, + test = test, smd = smd, + explain = explain, pDigits = pDigits, + + ## print.CatTable arguments passed + format = format, exact = exact, + ## Returns one more column if TRUE + showAllLevels = showAllLevels, + cramVars = cramVars, + + ## print.ContTable arguments passed + nonnormal = nonnormal, minMax = minMax, + insertLevel = showAllLevels) + + ## List of stratum sample size row only tables + FmtStratumSizesTables <- sapply(FmtTables, + FUN = ModuleStratumSizesRow, + showAllLevels = showAllLevels, + simplify = FALSE) + names(FmtStratumSizesTables) <- paste0(names(FmtStratumSizesTables), "N") + + ## Combine as a list of necessary table elements + FmtElementTables <- c(FmtTables, FmtStratumSizesTables) + + + ## Add space paddings + ## Given a list of tables with vecColWidths, + ## return a strata-by-table df containing spaces to add + nSpacesToAdd <- ModuleNSpacesToAdd(FmtElementTables) + ## Actually add spaces to tables + spcFmtEltTables <- ModuleAddSpacesToTable(FmtElementTables, nSpacesToAdd, showAllLevels) + + + ## Create a list of one variable tables excluding sample size row + lstOneVarTables <- ModuleListOfOneVarTables(spcFmtEltTables, + MetaData = x$MetaData) + + + ## Check if the first row is CatTable element + ## if so, pick sample size row from space-padded CatTable element + ## if not, pick sample size row from space-padded ContTable element + ## Intentionally a one-element list + lstStratumSizesRow <- ifelse(x$MetaData$logiFactors[1], + list(spcFmtEltTables$FmtCatTableN), + list(spcFmtEltTables$FmtContTableN)) ## Row-combin n and all variables - out <- do.call(rbind, c(list(stratumSizesRow), spaceFormattedTables)) + out <- do.call(rbind, + ## List concatenation (both are lists) + c(lstStratumSizesRow, lstOneVarTables)) - ## Add stratification information to the column header (This is also in the constructor) - if (length(TableOne[[1]]) > 1 ) { - ## Combine variable names with : in between - strataVarName <- attributes(TableOne[[1]])$strataVarName + ## Add stratification information to the column header + ## NULL element has a length of zero + if (length(x$ContTable) > 1) { + ## Combine variable names with : in between + strataVarName <- attributes(x$ContTable)$strataVarName ## Create strata string strataString <- paste0("Stratified by ", strataVarName) + ## Name the row dimension with it. 1st dimension name should be empty. + names(dimnames(out)) <- c("", strataString) + } else if (length(x$CatTable) > 1) { + ## Combine variable names with : in between + strataVarName <- attributes(x$CatTable)$strataVarName + ## Create strata string + strataString <- paste0("Stratified by ", strataVarName) ## Name the row dimension with it. 1st dimension name should be empty. names(dimnames(out)) <- c("", strataString) + } else { names(dimnames(out)) <- c("", "") diff --git a/R/print.svyCatTable.R b/R/print.svyCatTable.R new file mode 100644 index 0000000..4d5b3a1 --- /dev/null +++ b/R/print.svyCatTable.R @@ -0,0 +1,255 @@ +##' Format and print \code{svyCatTable} class objects +##' +##' \code{print} method for the \code{svyCatTable} class objects created by \code{\link{svyCreateCatTable}} function. +##' +##' @param x The result of a call to the \code{\link{svyCreateCatTable}} function. +##' @param digits Number of digits to print in the table. +##' @param pDigits Number of digits to print for p-values (also used for standardized mean differences). +##' @param quote Whether to show everything in quotes. The default is FALSE. If TRUE, everything including the row and column names are quoted so that you can copy it to Excel easily. +##' @param missing Whether to show missing data information (not implemented yet, placeholder) +##' @param explain Whether to add explanation to the variable names, i.e., (\%) is added to the variable names when percentage is shown. +##' @param printToggle Whether to print the output. If FLASE, no output is created, and a matrix is invisibly returned. +##' @param noSpaces Whether to remove spaces added for alignment. Use this option if you prefer to align numbers yourself in other software. +##' @param format The default is "fp" frequency (percentage). You can also choose from "f" frequency only, "p" percentage only, and "pf" percentage (frequency). +##' @param showAllLevels Whether to show all levels. FALSE by default, i.e., for 2-level categorical variables, only the higher level is shown to avoid redundant information. +##' @param cramVars A character vector to specify the two-level categorical variables, for which both levels should be shown in one row. +##' @param test Whether to show p-values. TRUE by default. If FALSE, only the numerical summaries are shown. +##' @param exact This option is not available for tables from weighted data. +##' @param smd Whether to show standardized mean differences. FALSE by default. If there are more than one contrasts, the average of all possible standardized mean differences is shown. For individual contrasts, use \code{summary}. +##' @param CrossTable Whether to show the cross table objects held internally using gmodels::CrossTable function. This will give an output similar to the PROC FREQ in SAS. +##' @param ... For compatibility with generic. Ignored. +##' @return A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via \code{write.csv}. +##' @author Kazuki Yoshida +##' @seealso +##' \code{\link{svyCreateTableOne}}, \code{\link{svyCreateCatTable}}, \code{\link{summary.svyCatTable}} +##' @examples +##' +##' ## See the examples for svyCreateTableOne() +##' +##' @export +print.svyCatTable <- +function(x, # CatTable object + digits = 1, pDigits = 3, # Number of digits to show + quote = FALSE, # Whether to show quotes + + missing = FALSE, # Show missing values (not implemented yet) + explain = TRUE, # Whether to show explanation in variable names + printToggle = TRUE, # Whether to print the result visibly + noSpaces = FALSE, # Whether to remove spaces for alignments + + format = c("fp","f","p","pf")[1], # Format f_requency and/or p_ercent + showAllLevels = FALSE, + cramVars = NULL, # variables to be crammed into one row + + test = TRUE, # Whether to add p-values + exact = NULL, # Which variables should be tested with exact tests + + smd = FALSE, # Whether to add standardized mean differences + + CrossTable = FALSE, # Whether to show gmodels::CrossTable + + ...) { + + ## x and ... required to be consistent with generic print(x, ...) + CatTable <- x + +### Check the data structure first + + ## CatTable has a strata(list)-variable(list)-table(dataframe) structure + ## Get the position of the non-null element + logiNonNullElement <- !sapply(CatTable, is.null) + ## Stop if all elements are null. + if (sum(logiNonNullElement) == 0) {stop("All strata are null strata. Check data.")} + ## Get the first non-null position + posFirstNonNullElement <- which(logiNonNullElement)[1] + ## Save variable names using the first non-null element + varNames <- names(CatTable[[posFirstNonNullElement]]) + ## Check the number of variables (list length) + nVars <- length(varNames) + + + ## Returns a numeric vector: 1 for approx test variable; 2 for exact test variable + exact <- ModuleHandleDefaultOrAlternative(switchVec = exact, + nameOfSwitchVec = "exact", + varNames = varNames) + + + ## Check format argument. If it is broken, choose "fp" for frequency (percent) + if (!length(format) == 1 | !format %in% c("fp","f","p","pf")) { + warning("format only accepts one of fp, f, p, or pf. Choosing fp.") + format <- "fp" + } + + ## Obtain the strata sizes in a character vector. This has to be obtained from the original data + ## Added as the top row later + strataN <- sapply(CatTable, + FUN = function(stratum) { # loop over strata + ## each stratum is a list of one data frame for each variable + ## Obtain n from all variables and all levels (list of data frames) + n <- unlist(sapply(stratum, getElement, "n")) + ## Pick the first non-null element + n[!is.null(n)][1] + ## Convert NULL to 0 + ifelse(is.null(n), + "0", + sprintf(fmt = paste0("%.", digits, "f"), n)) + }, + simplify = TRUE) # vector with as many elements as strata + + +### Formatting for printing + + ## Variables to format using digits option + varsToFormat <- c("n","miss","p.miss","freq","percent","cum.percent") + + ## Obtain collpased result by looping over strata + ## within each stratum, loop over variables + CatTableCollapsed <- + ModuleCatFormatStrata(CatTable = CatTable, + digits = digits, + varsToFormat = varsToFormat, + cramVars = cramVars, + showAllLevels = showAllLevels) + + +### Obtain the original column width in characters for alignment in print.TableOne + ## Name of the column to keep + widthCol <- c("nCharFreq","nCharFreq","nCharPercent","nCharPercent")[format == c("fp","f","p","pf")] + vecColWidths <- sapply(CatTableCollapsed, + FUN = function(LIST) { + + ## Get the width of the column (freq or percent, whichever comes left) + out <- attributes(LIST)[widthCol] + ## Return NA if null + if (is.null(out)) { + return(NA) + } else { + return(as.numeric(out)) + } + }, + simplify = TRUE) + + + ## Fill the null element using the first non-null element's dimension (Make sure to erase data) + CatTableCollapsed[!logiNonNullElement] <- CatTableCollapsed[posFirstNonNullElement] + ## Access the filled-in data frames, and erase them with place holders. + for (i in which(!logiNonNullElement)) { + ## Replace all elements with a place holder variable by variable + CatTableCollapsed[[i]][] <- lapply(CatTableCollapsed[[i]][], + function(var) { + + var <- rep("-", length(var)) + }) + } + + ## Choose the column name for the right format + nameResCol <- c("freqPer","freq","percent","perFreq")[format == c("fp","f","p","pf")] + + + ## Create output matrix without variable names with the right format + out <- do.call(cbind, lapply(CatTableCollapsed, getElement, nameResCol)) + out <- as.matrix(out) + + ## Add column names if multivariable stratification is used. (No column names added automatically) + if (length(attr(CatTable, "dimnames")) > 1) { + + colnames(out) <- ModuleCreateStrataNames(CatTable) + } + + + ## Set the variables names + rownames(out) <- CatTableCollapsed[[posFirstNonNullElement]][,"var"] + ## Get positions of rows with variable names + logiNonEmptyRowNames <- CatTableCollapsed[[posFirstNonNullElement]][, "firstRowInd"] != "" + + + ## Add p-values when requested and available + if (test == TRUE & !is.null(attr(CatTable, "pValues"))) { + + ## Pick test types used (used for annonation) + testTypes <- c("","exact")[exact] + + ## Pick the p-values requested, and format like <0.001 + pVec <- ModulePickAndFormatPValues(TableObject = CatTable, + switchVec = exact, + pDigits = pDigits) + + ## Create an empty p-value column and test column + out <- cbind(out, + p = rep("", nrow(out))) # Column for p-values + ## Put the values at the non-empty positions + out[logiNonEmptyRowNames,"p"] <- pVec + + ## Create an empty test type column, and add test types + out <- cbind(out, + test = rep("", nrow(out))) # Column for test types + ## Put the test types at the non-empty positions (all rows in continuous!) + out[logiNonEmptyRowNames,"test"] <- testTypes + + } + + + ## Add SMDs when requested and available + if (smd & !is.null(attr(CatTable, "smd"))) { + + ## Create an empty column + out <- cbind(out, + SMD = rep("", nrow(out))) # Column for p-values + ## Put the values at the non-empty positions + out[logiNonEmptyRowNames,"SMD"] <- + ModuleFormatPValues(attr(CatTable, "smd")[,1], pDigits = pDigits) + } + + + ## Add freq () explanation if requested + if (explain) { + ## Choose the format of the explanation string + explainString <- c(" (%)", "", " (%)", " % (freq)")[format == c("fp","f","p","pf")] + ## Only for rows with row names + rownames(out)[logiNonEmptyRowNames] <- paste0(rownames(out)[logiNonEmptyRowNames], + explainString) + } + + ## Keep column names (strataN does not have correct names + ## if stratification is by multiple variables) + outColNames <- colnames(out) + ## rbind sample size row, padding necessary "" for p value, etc + nRow <- c(strataN, rep("", ncol(out) - length(strataN))) + out <- rbind(n = nRow, out) + ## Put back the column names (overkill for non-multivariable cases) + colnames(out) <- outColNames + + ## Add level names if showAllLevels is TRUE. + ## This adds the level column to the left, thus, after nRow addition. + ## Need come after column naming. + if (showAllLevels) { + out <- + cbind(level = c("", CatTableCollapsed[[posFirstNonNullElement]][,"level"]), + out) + } + + ## Add stratification information to the column header depending on the dimension + names(dimnames(out)) <- c("", paste0("Stratified by ", + attr(CatTable, "strataVarName"))) + + ## Remove spaces if asked. + out <- ModuleRemoveSpaces(mat = out, noSpaces = noSpaces) + + ## Modular version of quote/print toggle. + out <- ModuleQuoteAndPrintMat(matObj = out, + quote = quote, printToggle = printToggle) + + ## Print CrossTable() if requested + if (CrossTable) { + junk <- lapply(attributes(CatTable)$xtabs, gmodels::CrossTable) + } + + ## Add attributes for column widths in characters + attributes(out) <- c(attributes(out), + list(vecColWidths = vecColWidths, + ## Add one FALSE for sample size row + logiNameRows = c(FALSE, logiNonEmptyRowNames))) + + ## return a matrix invisibly + return(invisible(out)) +} diff --git a/R/print.svyContTable.R b/R/print.svyContTable.R new file mode 100644 index 0000000..36c2ea3 --- /dev/null +++ b/R/print.svyContTable.R @@ -0,0 +1,223 @@ +##' Format and print \code{svyContTable} class objects +##' +##' \code{print} method for the \code{svyContTable} class objects created by \code{\link{CreateContTable}} function. +##' +##' @param x Object returned by \code{\link{svyCreateContTable}} function. +##' @param digits Number of digits to print in the table. +##' @param pDigits Number of digits to print for p-values (also used for standardized mean differences). +##' @param quote Whether to show everything in quotes. The default is FALSE. If TRUE, everything including the row and column names are quoted so that you can copy it to Excel easily. +##' @param missing Whether to show missing data information (not implemented yet, placeholder) +##' @param explain Whether to add explanation to the variable names, i.e., (mean (sd) or median [IQR]) is added to the variable names. +##' @param printToggle Whether to print the output. If FLASE, no output is created, and a matrix is invisibly returned. +##' @param noSpaces Whether to remove spaces added for alignment. Use this option if you prefer to align numbers yourself in other software. +##' @param nonnormal A character vector to specify the variables for which the p-values should be those of nonparametric tests. By default all p-values are from normal assumption-based tests (oneway.test). +##' @param minMax Whether to use [min,max] instead of [p25,p75] for nonnormal variables. The default is FALSE. +##' @param insertLevel Whether to add an empty level column to the left of strata. +##' @param test Whether to show p-values. TRUE by default. If FALSE, only the numerical summaries are shown. +##' @param smd Whether to show standardized mean differences. FALSE by default. If there are more than one contrasts, the average of all possible standardized mean differences is shown. For individual contrasts, use \code{summary}. +##' @param ... For compatibility with generic. Ignored. +##' @return A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via \code{write.csv}. +##' @author Kazuki Yoshida +##' @seealso +##' \code{\link{svyCreateTableOne}}, \code{\link{svyCreateCatTable}}, \code{\link{summary.svyCatTable}} +##' @examples +##' +##' ## See the examples for svyCreateTableOne() +##' +##' @export +print.svyContTable <- +function(x, # ContTable object + digits = 2, pDigits = 3, # Number of digits to show + quote = FALSE, # Whether to show quotes + + missing = FALSE, # show missing values (not implemented yet) + explain = TRUE, # Whether to show explanation in variable names + printToggle = TRUE, # Whether to print the result visibly + noSpaces = FALSE, # Whether to remove spaces for alignments + + nonnormal = NULL, # Which variables should be treated as nonnormal + minMax = FALSE, # median [range] instead of median [IQR] + insertLevel = FALSE, # insert the level column to match showAllLevels in print.CatTable + + test = TRUE, # Whether to add p-values + + smd = FALSE, # Whether to add standardized mean differences + + ...) { + + ## x and ... required to be consistent with generic print(x, ...) + ContTable <- x + +### Check data structure first + + ## ContTable is by() object + ## Get the position of the first non-null element + posFirstNonNullElement <- which(!sapply(ContTable, is.null))[1] + ## Save variable names using the first non-null element + varNames <- rownames(ContTable[[posFirstNonNullElement]]) + ## Check the number of variables + nVars <- length(varNames) + + + ## Returns a numeric vector: 1 for normal variable; 2 for nonnormal variable + nonnormal <- ModuleHandleDefaultOrAlternative(switchVec = nonnormal, + nameOfSwitchVec = "nonnormal", + varNames = varNames) + + + ## Check the statistics. If necessary statistics are lacking abort + statNames <- colnames(ContTable[[posFirstNonNullElement]]) + funcDefault <- c("n","miss","mean","sd","median","p25","p75") + if (any(!funcDefault %in% statNames)) { + + ## summary(ContTable) + stop("The object does not contain all necessary statistics. Use summary() method.") + } + + + ## Obtain the strata sizes in a character vector. This has to be obtained from the original data + ## Added as the top row later + strataN <- sapply(ContTable, + FUN = function(stratum) { # loop over strata + ## each strutum is a data frame with one row for each variable + ## Obtain n from all variables (matrix) + n <- stratum[,"n"] + ## Pick the first non-null element + n[!is.null(n)][1] + ## Convert NULL to 0 + ifelse(is.null(n), + "0", + sprintf(fmt = paste0("%.", digits, "f"), n)) + }, + simplify = TRUE) # vector with as many elements as strata + + +### Conversion of data for printing + + ## Define the nonnormal formatter depending on the minMax status + ConvertNormal <- function(rowMat) { + ## Take minMax value from outside (NOT A STANDALONE FUNCTION!!) + ModuleConvertNormal(rowMat, digits) + } + ## Define the nonnormal formatter depending on the minMax status + ConvertNonNormal <- function(rowMat) { + ## Take minMax value from outside (NOT A STANDALONE FUNCTION!!) + ModuleConvertNonNormal(rowMat, digits, minMax = minMax) + } + + ## Create a list of these two functions + listOfFunctions <- list(normal = ConvertNormal, nonnormal = ConvertNonNormal) + + ## Take functions from the 2-element list, and convert to an nVars-length list + listOfFunctions <- listOfFunctions[nonnormal] + + ## Loop over strata (There may be just one) + out <- ModuleContFormatStrata(ContTable = ContTable, + nVars = nVars, + listOfFunctions = listOfFunctions, + digits = digits) + + +### Obtain the original column width in characters for alignment in print.TableOne + vecColWidths <- sapply(out, + FUN = function(LIST) { + + ## Get the width of the column + attributes(LIST)$nCharMeanOrMedian + }, + simplify = TRUE) + + + ## The outer sapply should not simplify to avoid a vector + ## Column-bind to create variables x strata matrix + out <- do.call(cbind, out) + + ## Put the variables names back (looping over rows can solve this) + rownames(out) <- varNames + + ## Add column names if multivariable stratification is used. + if (length(attr(ContTable, "dimnames")) > 1) { + + colnames(out) <- ModuleCreateStrataNames(ContTable) + } + + + ## Add p-values when requested and available + if (test == TRUE & !is.null(attr(ContTable, "pValues"))) { + + ## Pick test types used (used for annonation) + testTypes <- c("","nonnorm")[nonnormal] + + ## Pick the p-values requested, and format like <0.001 + pVec <- ModulePickAndFormatPValues(TableObject = ContTable, + switchVec = nonnormal, + pDigits = pDigits) + + ## Column combine with the output + out <- cbind(out, p = pVec) + + ## Create an empty test type column, and add test types + out <- cbind(out, + test = rep("", nrow(out))) # Column for test types + ## Put the test types at the non-empty positions (all rows in continuous!) + out[ ,"test"] <- testTypes + + } + + + ## Add SMDs when requested and available + if (smd & !is.null(attr(ContTable, "smd"))) { + + ## Create an empty column + out <- cbind(out, + SMD = rep("", nrow(out))) # Column for p-values + ## Put the values at the non-empty positions + out[,"SMD"] <- ModuleFormatPValues(attr(ContTable, "smd")[,1], + pDigits = pDigits) + } + + + ## Add mean (sd) or median [IQR]/median [range] explanation if requested + if (explain) { + ## Create a vector of explanations to be pasted + if (minMax == FALSE) { + what <- c(" (mean (sd))"," (median [IQR])")[nonnormal] + } else if (minMax == TRUE) { + what <- c(" (mean (sd))"," (median [range])")[nonnormal] + } + ## Paste to the rownames + rownames(out) <- paste0(rownames(out), what) + } + + ## Keep column names (strataN does not have correct names if stratification is by multiple variables) + outColNames <- colnames(out) + ## Add n at the correct location depending on the number of columns added (level and/or p) + nRow <- c(strataN, rep("", ncol(out) - length(strataN))) # Additional padding to right + out <- rbind(n = nRow, out) + ## Put back the column names (overkill for non-multivariable cases) + colnames(out) <- outColNames + + ## Add the level column if requested + if (insertLevel) { + out <- cbind(level = rep("", nrow(out)), + out) + } + + ## Add stratification information to the column header depending on the dimension + names(dimnames(out)) <- c("", paste0("Stratified by ", + attr(ContTable, "strataVarName"))) + + ## Remove spaces if asked. + out <- ModuleRemoveSpaces(mat = out, noSpaces = noSpaces) + + ## (module) Takes an matrix object format, print if requested + out <- ModuleQuoteAndPrintMat(matObj = out, + quote = quote, printToggle = printToggle) + + ## Add attributes for column widths in characters + attributes(out) <- c(attributes(out), + list(vecColWidths = vecColWidths)) + + ## return a matrix invisibly + return(invisible(out)) +} diff --git a/R/summary.CatTable.R b/R/summary.CatTable.R index 052b18f..b59bde2 100644 --- a/R/summary.CatTable.R +++ b/R/summary.CatTable.R @@ -1,39 +1,17 @@ ##' Shows all results in a \code{CatTable} class object ##' -##' This method shows all the data a \code{CatTable} class object has. This includes the (optionally stratified) part with summary statistics and , if available, p-values from the approximation method test (\code{\link{chisq.test}} by default) and exact method test (\code{\link{fisher.test}} by default). +##' Shows all data a \code{CatTable} class object has. This includes the (optionally stratified) part with summary statistics and, if available, p-values from the approximation method test (\code{chisq.test} by default) and exact method test (\code{fisher.test} by default) and standardized mean differences of all possible pairwise contrasts. ##' ##' @param object An object that has the \code{CatTable} class to be shown. ##' @param digits Number of digits to print. ##' @param ... For compatibility with generic. Ignored. -##' @return It will print the results. +##' @return None. Results are printed. ##' @author Kazuki Yoshida ##' @seealso -##' \code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}}, -##' \code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}}, -##' \code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}} +##' \code{\link{CreateTableOne}}, \code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, ##' @examples ##' -##' ## Load -##' library(tableone) -##' -##' ## Load Mayo Clinic Primary Biliary Cirrhosis Data -##' library(survival) -##' data(pbc) -##' ## Check variables -##' head(pbc) -##' -##' ## Create an overall table for categorical variables -##' catVars <- c("status","ascites","hepato","spiders","edema","stage") -##' catTableOverall <- CreateCatTable(vars = catVars, data = pbc) -##' -##' ## Simply typing the object name will invoke the print.CatTable method, -##' ## which will show the sample size, frequencies and percentages. -##' ## For 2-level variables, only the higher level is shown for simplicity. -##' catTableOverall -##' -##' ## To further examine the variables, use the summary.CatTable method, -##' ## which will show more details. -##' summary(catTableOverall) +##' ## See examples for CreateTableOne ##' ##' @export summary.CatTable <- function(object, digits = 1, ...) { @@ -106,4 +84,10 @@ summary.CatTable <- function(object, digits = 1, ...) { cat("\np-values\n") print(attributes(CatTable)$pValues) } + + ## Print SMDs if it exist + if (!is.null(attributes(CatTable)$smd)) { + cat("\nStandardize mean differences\n") + print(attributes(CatTable)$smd) + } } diff --git a/R/summary.ContTable.R b/R/summary.ContTable.R index 869e0aa..58630f0 100644 --- a/R/summary.ContTable.R +++ b/R/summary.ContTable.R @@ -1,39 +1,17 @@ ##' Shows all results in a \code{ContTable} class object ##' -##' This method shows all the data a \code{ContTable} class object has. This includes the (optionally stratified) part with summary statistics and , if available, p-values from the normal assupmtion-based test (\code{\link{oneway.test}} by default) and nonparametric test (\code{\link{kruskal.test}} by default). +##' Shows all data a \code{ContTable} class object has. This includes the (optionally stratified) part with summary statistics and, if available, p-values from the normal assupmtion-based test (\code{oneway.test} by default) and nonparametric test (\code{kruskal.test} by default) and standardized mean differences of all possible pairwise contrasts. ##' ##' @param object An object that has the \code{ContTable} class to be shown. ##' @param digits Number of digits to print. ##' @param ... For compatibility with generic. Ignored. -##' @return It will print the results. +##' @return None. Results are printed. ##' @author Kazuki Yoshida ##' @seealso -##' \code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}}, -##' \code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}}, -##' \code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}} +##' \code{\link{CreateTableOne}}, \code{\link{CreateContTable}}, \code{\link{print.ContTable}} ##' @examples ##' -##' ## Load -##' library(tableone) -##' -##' ## Load Mayo Clinic Primary Biliary Cirrhosis Data -##' library(survival) -##' data(pbc) -##' ## Check variables -##' head(pbc) -##' -##' ## Create an overall table for continuous variables -##' contVars <- c("time","age","bili","chol","albumin","copper", -##' "alk.phos","ast","trig","platelet","protime") -##' contTableOverall <- CreateContTable(vars = contVars, data = pbc) -##' -##' ## Simply typing the object name will invoke the print.ContTable method, -##' ## which will show the sample size, means and standard deviations. -##' contTableOverall -##' -##' ## To further examine the variables, use the summary.ContTable method, -##' ## which will show more details. -##' summary(contTableOverall) +##' ## See examples for CreateTableOne ##' ##' @export summary.ContTable <- function(object, digits = 2, ...) { @@ -46,4 +24,10 @@ summary.ContTable <- function(object, digits = 2, ...) { cat("\np-values\n") print(attributes(object)$pValues) } + + ## Print SMDs if it exist + if (!is.null(attributes(object)$smd)) { + cat("\nStandardize mean differences\n") + print(attributes(object)$smd) + } } diff --git a/R/summary.TableOne.R b/R/summary.TableOne.R index 6dcfed6..678b45c 100644 --- a/R/summary.TableOne.R +++ b/R/summary.TableOne.R @@ -1,62 +1,47 @@ -##' Shows all results in a \code{TableOne} class object +##' Shows all results in a \code{(svy)TableOne} class object ##' -##' This method shows all the data a CatTable class object has. This includes -##' the (optionally stratified) part with summary statistics and p-values from -##' the approximation method test (chisq.test by default) and exact method test -##' (fisher.test by default). +##' Shows all data a \code{(svy)TableOne} class object has. This includes the (optionally stratified) part with summary statistics and p-values and/or standardized mean differences. ##' ##' -##' @param object An object that has the \code{CatTable} class to be shown. +##' @param object An object that has the \code{(svy)TableOne} class to be shown. ##' @param digits Number of digits to print. ##' @param ... For compatibility with generic. Ignored. -##' @return It will print the results. +##' @return None. Results are printed. ##' @author Kazuki Yoshida ##' @seealso -##' \code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}}, -##' \code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}}, -##' \code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}} +##' \code{\link{CreateTableOne}}, \code{\link{svyCreateCatTable}} ##' @examples ##' -##' ## Load -##' library(tableone) -##' -##' ## Load Mayo Clinic Primary Biliary Cirrhosis Data -##' library(survival) -##' data(pbc) -##' ## Check variables -##' head(pbc) -##' -##' ## Make categorical variables factors -##' varsToFactor <- c("status","trt","ascites","hepato","spiders","edema","stage") -##' pbc[varsToFactor] <- lapply(pbc[varsToFactor], factor) -##' -##' ## Create Table 1 stratified by sex and trt -##' tableOne <- CreateTableOne(vars = c("time","status","age","ascites","hepato", -##' "spiders","edema","bili","chol","albumin", -##' "copper","alk.phos","ast","trig","platelet", -##' "protime","stage"), -##' strata = c("sex","trt"), data = pbc) -##' -##' ## Just typing the object name will invoke the print.TableOne method -##' tableOne -##' -##' ## Use the summary.TableOne method for detailed summary -##' summary(tableOne) +##' ## See examples for CreateTableOne and svyCreateTableOne ##' ##' @export summary.TableOne <- function(object, digits = 1, ...) { - ## object and ... required to be consistent with generic summary(object, ...) - listCatContTables <- object - ## Continuous - cat("\n ### Summary of continuous variables ###\n\n") - summary(listCatContTables$ContTable, digits = digits) + if (!is.null(object$ContTable)) { + cat("\n ### Summary of continuous variables ###\n\n") + summary(object$ContTable, digits = digits) + } ## Separator - cat("\n=======================================================================================\n") - - ## Categorical - cat("\n ### Summary of categorical variables ### \n\n") - summary(listCatContTables$CatTable, digits = digits) + if ((!is.null(object$ContTable)) & !is.null(object$CatTable)) { + cat("\n=======================================================================================\n") + } + + ## Categorical + if (!is.null(object$CatTable)) { + cat("\n ### Summary of categorical variables ### \n\n") + summary(object$CatTable, digits = digits) + } } + + + + + + + + + + + diff --git a/R/summary.svyCatTable.R b/R/summary.svyCatTable.R new file mode 100644 index 0000000..7a30321 --- /dev/null +++ b/R/summary.svyCatTable.R @@ -0,0 +1,95 @@ +##' Shows all results in a \code{svyCatTable} class object +##' +##' Shows all data a \code{svyCatTable} class object has. This includes the (optionally stratified) part with summary statistics and, if available, p-values from the approximation method test (\code{chisq.test} by default) and exact method test (\code{fisher.test} by default) and standardized mean differences of all possible pairwise contrasts. +##' +##' @param object An object that has the \code{svyCatTable} class to be shown. +##' @param digits Number of digits to print. +##' @param ... For compatibility with generic. Ignored. +##' @return None. Results are printed. +##' @author Kazuki Yoshida +##' @seealso +##' \code{\link{svyCreateTableOne}}, \code{\link{svyCreateCatTable}}, \code{\link{print.svyCatTable}} +##' @examples +##' +##' ## See the examples for svyCreateTableOne() +##' +##' @export +summary.svyCatTable <- function(object, digits = 1, ...) { + + ## object and ... required to be consistent with generic summary(object, ...) + CatTable <- object + + ## Create format + fmt <- paste0("%.", digits, "f") + varsNumeric <- c("n","miss","p.miss","freq","percent","cum.percent") + + ## Obtain collpased result within each stratum + CatTableCollapsed <- + sapply(X = CatTable, # Loop over strata + FUN = function(LIST) { + + LIST <- sapply(X = seq_along(LIST), # Loop over variables + FUN = function(i) { + + ## Extract the data frame + DF <- LIST[[i]] + + ## Extract the variable name + varName <- names(LIST)[i] + + ## Check number of rows (levels) + nRow <- nrow(DF) + + ## Add a variable name to the left as a character vector + DF <- cbind(var = rep(varName, nRow), + DF) + + ## Format percent and cum.percent + DF[varsNumeric] <- lapply(X = DF[varsNumeric], + FUN = sprintf, + fmt = fmt) + + ## Make var and level a string + DF[c("var","level")] <- + lapply(X = DF[c("var","level")], + FUN = as.character) + + ## Delete n and miss except in the first row + DF[-1, c("var","n","miss","p.miss")] <- "" + + ## row bind an empty row + DF <- rbind(DF, + rep("", ncol(DF))) + + ## Return a data frame + DF + }, + simplify = FALSE) + + ## Collapse DFs within each stratum + DF <- do.call(rbind, LIST) + + ## Return a data frame + DF + }, simplify = FALSE) + + ## Change to an array to use print.by() + CatTableCollapsed <- as.array(CatTableCollapsed) + ## Force dimname label as the strataVarName + names(dimnames(CatTableCollapsed)) <- attr(CatTable, "strataVarName") + + ## Print forcing the print.by method. Do not show row names. + print.by(CatTableCollapsed, digits = digits, row.names = FALSE) + + ## Print p-values if it exist + if (!is.null(attributes(CatTable)$pValues)) { + cat("\np-values\n") + print(attributes(CatTable)$pValues) + } + + ## Print SMDs if it exist + if (!is.null(attributes(CatTable)$smd)) { + cat("\nStandardize mean differences\n") + print(attributes(CatTable)$smd) + } +} diff --git a/R/summary.svyContTable.R b/R/summary.svyContTable.R new file mode 100644 index 0000000..4622795 --- /dev/null +++ b/R/summary.svyContTable.R @@ -0,0 +1,40 @@ +##' Shows all results in a \code{svyContTable} class object +##' +##' Shows all data a \code{svyContTable} class object has. This includes the (optionally stratified) part with summary statistics and, if available, p-values from the normal assupmtion-based test (\code{regTermTest} with \code{svyglm} by default) and nonparametric test (\code{svyranktest} by default) and standardized mean differences of all possible pairwise contrasts. +##' +##' @param object An object that has the \code{svyContTable} class to be shown. +##' @param digits Number of digits to print. +##' @param ... For compatibility with generic. Ignored. +##' @return None. Results are printed. +##' @author Kazuki Yoshida +##' @seealso +##' \code{\link{svyCreateTableOne}}, \code{\link{svyCreateContTable}}, \code{\link{print.svyContTable}} +##' @examples +##' +##' ## See the examples for svyCreateTableOne() +##' +##' @export +summary.svyContTable <- function(object, digits = 2, ...) { + + ## Save the initial object + ContTable <- object + + ## Force an 1-dimensional array + object <- as.array(object) + names(dimnames(object)) <- attr(ContTable, "strataVarName") + + ## Just print as a list + print.by(object, digits = digits) + + ## Print p-values if it exist + if (!is.null(attributes(object)$pValues)) { + cat("\np-values\n") + print(attributes(object)$pValues) + } + + ## Print SMDs if it exist + if (!is.null(attributes(object)$smd)) { + cat("\nStandardize mean differences\n") + print(attributes(object)$smd) + } +} diff --git a/R/svyCreateCatTable.R b/R/svyCreateCatTable.R new file mode 100644 index 0000000..24351f6 --- /dev/null +++ b/R/svyCreateCatTable.R @@ -0,0 +1,140 @@ +##' Create an object summarizing categorical variables for weighted data +##' +##' Create an object summarizing categorical variables optionally stratifying by one or more startifying variables and performing statistical tests. Usually, \code{\link{svyCreateTableOne}} should be used as the universal frontend for both continuous and categorical data. +##' +##' @param vars Variable(s) to be summarized given as a character vector. +##' @param strata Stratifying (grouping) variable name(s) given as a character vector. If omitted, the overall results are returned. +##' @param data A survey design object in which these variables exist. All variables (both vars and strata) must be in this survey design object. It is created with the \code{svydesign} function in the \code{survey} package. +##' @param includeNA If TRUE, NA is handled as a regular factor level rather than missing. NA is shown as the last factor level in the table. Only effective for categorical variables. +##' @param test If TRUE, as in the default and there are more than two groups, groupwise comparisons are performed. Both tests that require the large sample approximation and exact tests are performed. Either one of the result can be obtained from the print method. +##' @param testApprox A function used to perform the large sample approximation based tests. The default is \code{svychisq}. +##' @param argsApprox A named list of arguments passed to the function specified in testApprox. +##' @param smd If TRUE, as in the default and there are more than two groups, standardized mean differences for all pairwise comparisons are calculated. +##' @return An object of class \code{svyCatTable}. +##' @author Kazuki Yoshida +##' @seealso +##' \code{\link{svyCreateTableOne}}, \code{\link{print.svyCatTable}}, \code{\link{summary.svyCatTable}}, +##' @examples +##' +##' ## See the examples for svyCreateTableOne() +##' +##' @export +svyCreateCatTable <- +function(vars, # character vector of variable names + strata, # character vector of variable names + data, # survey design object + includeNA = FALSE, # include NA as a category + test = TRUE, # whether to include p-values + testApprox = svyTestChisq, # function for approximation test (only choice) + argsApprox = NULL, # arguments passed to testApprox + smd = TRUE # whether to include standardize mean differences + ) { + +### Data check + ## Check if the data given is a dataframe + StopIfNotSurveyDesign(data) + + ## Check if variables exist. Drop them if not. + vars <- ModuleReturnVarsExist(vars, data$variables) + + ## Abort if no variables exist at this point + ModuleStopIfNoVarsLeft(vars) + + ## Toggle test FALSE if no strata + test <- ModuleReturnFalseIfNoStrata(strata, test) + smd <- ModuleReturnFalseIfNoStrata(strata, smd) + + ## Create strata data frame (data frame with only strata variables) + ## FIXME: This changes type of strata; not a good practice + strata <- ModuleReturnStrata(strata, data$variables) + + ## Create a single stratification variable + ## Keeps non-existing levels + data$variables$..strataVar.. <- interaction(strata, sep = ":") + strataVarLevels <- levels(data$variables$..strataVar..) + ## Dummy and dumb object to avoid CRAN check "no visible binding for global variable" + ..strataVar.. <- NULL + + ## Convert to a factor if it is not a factor already. (categorical version only) + ## Not done on factors, to avoid dropping zero levels. + logiNotFactor <- sapply(data$variables, function(v) { + ## Return TRUE if classes for a vector does NOT contain class "factor" + ## v is a vector of a variable in the data$variables data frame, use class + ## Ordered factor has c("ordered", "factor"), thus, %in% is necessary + !("factor" %in% class(v)) + }) + + data$variables[logiNotFactor] <- lapply(data$variables[logiNotFactor], factor) + + ## If including NA as a level, include NA as a factor level before subsetting + if (includeNA) { + data$variables <- ModuleIncludeNaAsLevel(data$variables) + } + +### Actual descriptive statistics are calculated here. + + ## Return a list of summary matrices + result <- sapply(strataVarLevels, function(level) { + + ## Create a matrix including vars X c(n,miss,...) matrix + svyCatSummary(vars, subset(data, ..strataVar.. %in% level)) + + }, simplify = FALSE) + + ## Make it a by object + class(result) <- "by" + + ## strataVarName from dimension headers + strataVarName <- paste0(names(strata), collapse = ":") + ## Add stratification variable information as an attribute + if (length(result) > 1) { + ## Add an attribute for the stratifying variable name + attributes(result) <- c(attributes(result), + list(strataVarName = strataVarName)) + } + + +### Perform tests when necessary + ## Initialize + pValues <- NULL + listXtabs <- list() + + ## Only when test is asked for + if (test) { + + lstXtabsPVals <- + svyModuleApproxExactTests(data = data, + vars = vars, + strataVarName = strataVarName, + testApprox = testApprox, + argsApprox = argsApprox) + pValues <- lstXtabsPVals$pValues + listXtabs <- lstXtabsPVals$xtabs + } + +### Perform SMD when requested + smds <- NULL + + ## Only when SMD is asked for + if (smd) { + ## list of smds + smds <- sapply(vars, function(var) { + svyStdDiffMulti(varName = var, groupName = "..strataVar..", design = data) + }, simplify = FALSE) + ## Give name and add mean column + smds <- FormatLstSmds(smds, nStrata = length(result)) + } + + ## Return object + ## Give an S3 class + class(result) <- c("svyCatTable", "CatTable", class(result)) + + ## Give additional attributes + attributes(result) <- c(attributes(result), + list(pValues = pValues), + list(xtabs = listXtabs), + list(smd = smds)) + + ## Return + return(result) +} diff --git a/R/svyCreateContTable.R b/R/svyCreateContTable.R new file mode 100644 index 0000000..dfe46d7 --- /dev/null +++ b/R/svyCreateContTable.R @@ -0,0 +1,160 @@ +##' Create an object summarizing continous variables for weighted dataset +##' +##' Create an object summarizing continous variables optionally stratifying by one or more startifying variables and performing statistical tests. Usually, \code{\link{svyCreateTableOne}} should be used as the universal frontend for both continuous and categorical data. +##' +##' @param vars Variable(s) to be summarized given as a character vector. +##' @param strata Stratifying (grouping) variable name(s) given as a character vector. If omitted, the overall results are returned. +##' @param data A survey design object in which these variables exist. All variables (both vars and strata) must be in this survey design object. It is created with the \code{svydesign} function in the \code{survey} package. +##' @param test If TRUE, as in the default and there are more than two groups, groupwise comparisons are performed. Both tests that assume normality and tests that do not are performed. Either one of the result can be obtained from the print method. +##' @param testNormal A function used to perform the normal assumption based tests. The default is multiple degrees of freedom test using \code{svyglm} and \code{regTermTest}. This is equivalent of the \code{svyttest} when there are only two groups. +##' @param argsNormal A named list of arguments passed to the function specified in \code{testNormal}. +##' @param testNonNormal A function used to perform the nonparametric tests. The default is \code{svyranktest}. +##' @param argsNonNormal A named list of arguments passed to the function specified in \code{testNonNormal}. +##' @param smd If TRUE, as in the default and there are more than two groups, standardized mean differences for all pairwise comparisons are calculated. +##' @return An object of class \code{svyContTable}. +##' @author Kazuki Yoshida +##' @seealso +##' \code{\link{svyCreateTableOne}}, \code{\link{print.svyContTable}}, \code{\link{summary.svyContTable}}, +##' @examples +##' +##' ## See the examples for svyCreateTableOne() +##' +##' @export +svyCreateContTable <- +function(vars, # character vector of variable names + strata, # character vector of variable names + data, # survey design data + test = TRUE, # Whether to include p-values + testNormal = svyTestNormal, # test for normally distributed variables + argsNormal = list(method = "Wald"), # arguments passed to testNormal + testNonNormal = svyTestNonNormal, # test for nonnormally distributed variables + argsNonNormal = NULL, # arguments passed to testNonNormal + smd = TRUE # whether to include standardize mean differences + ) { + +### Data check + ## Check if the data given is a survey design object + StopIfNotSurveyDesign(data) + + ## Check if variables exist. Drop them if not. + ## survey.design$variables holds original data frame + vars <- ModuleReturnVarsExist(vars, data$variables) + + ## Abort if no variables exist at this point + ModuleStopIfNoVarsLeft(vars) + + ## Toggle test FALSE if no strata + test <- ModuleReturnFalseIfNoStrata(strata, test) + smd <- ModuleReturnFalseIfNoStrata(strata, smd) + + ## Create strata data frame (data frame with only strata variables) + ## FIXME: This changes type of strata; not a good practice + strata <- ModuleReturnStrata(strata, data$variables) + + ## Create a single stratification variable + ## Keeps non-existing levels + data$variables$..strataVar.. <- interaction(strata, sep = ":") + strataVarLevels <- levels(data$variables$..strataVar..) + ## Dummy and dumb object to avoid CRAN check "no visible binding for global variable" + ..strataVar.. <- NULL + + ## Handle non-numeric elements (intergers give TRUE, and pass) + if(any(!sapply(data$variables[vars], is.numeric))){ + + ## If there is any non-numeric variables + vars <- vars[sapply(data$variables[vars], is.numeric)] + warning("Non-numeric variables dropped") + } + + ## Check if all the variables are continuous, and stop if not + if(!all(sapply(data$variables[vars], is.numeric))) { + stop("Can only be run on numeric variables") + } + + +### Actual descriptive statistics are calculated here. + + ## To implement + ## Create a single grouping variable from strata variables + ## Create a list of subgroup data by the grouping variable + ## Loop over each stratum with matrix forming function + + result <- sapply(strataVarLevels, function(level) { + + ## Create a matrix including vars X c(n,miss,...) matrix + svyContSummary(vars, subset(data, ..strataVar.. %in% level)) + + }, simplify = FALSE) + + ## Make it a by object + class(result) <- "by" + + + ## Add stratification variable information as an attribute + if (length(result) > 1) { + ## strataVarName from dimension headers + strataVarName <- paste0(names(strata), collapse = ":") + ## Add an attribute for the stratifying variable name + attributes(result) <- c(attributes(result), + list(strataVarName = strataVarName)) + } + + +### Perform tests when necessary + ## Initialize to avoid error when it does not exist at the attribute assignment + pValues <- NULL + + + ## Only when test is asked FOR + if (test) { + + ## Loop over variables in dat, and obtain p values for two tests + ## DF = 6 when there are 8 levels (one empty), i.e., empty strata dropped by oneway.test/kruskal.test + pValues <- + sapply(X = vars, + FUN = function(var) { + + ## Create a formula as a string + formulaString <- paste0(var, " ~ ..strataVar..") + + ## Perform tests and return the result as 1x2 DF + ## The test functions should take a formula string as their first argument. + data.frame(pNormal = ModuleTestSafe(formulaString, testNormal, + c(list(design = data, test.terms = "..strataVar.."), + argsNormal)), + pNonNormal = ModuleTestSafe(formulaString, testNonNormal, + c(list(design = data), argsNonNormal))) + }, + simplify = FALSE) + + ## Create a single data frame (n x 2 (normal,nonormal)) + pValues <- do.call(rbind, pValues) + } # Conditional for test == TRUE ends here. + + +### Perform SMD when requested + smds <- NULL + + ## Only when SMD is asked for + if (smd) { + ## list of smds + smds <- sapply(vars, function(var) { + svyStdDiff(varName = var, groupName = "..strataVar..", design = data) + }, simplify = FALSE) + ## Give name and add mean column + smds <- FormatLstSmds(smds, nStrata = length(result)) + } + + + ## Return object + ## Give an S3 class + class(result) <- c("svyContTable", "ContTable", class(result)) + + ## Give additional attributes + attributes(result) <- c(attributes(result), + list(pValues = pValues), + list(smd = smds)) + + ## Return + return(result) +} diff --git a/R/svyCreateTableOne.R b/R/svyCreateTableOne.R new file mode 100644 index 0000000..ed5b8dc --- /dev/null +++ b/R/svyCreateTableOne.R @@ -0,0 +1,224 @@ +##' Create an object summarizing both continuous and categorical variables for weighted data +##' +##' Create an object summarizing all baseline variables (both continuous and categorical) optionally stratifying by one or more startifying variables and performing statistical tests. The object gives a table that is easy to use in medical research papers. +##' +##' @param vars Variables to be summarized given as a character vector. Factors are handled as categorical variables, whereas numeric variables are handled as continuous variables. If empty, all variables in the survey design object specified in the data argument are used. +##' @param strata Stratifying (grouping) variable name(s) given as a character vector. If omitted, the overall results are returned. +##' @param data A survey design object in which these variables exist. All variables (both vars and strata) must be in this survey design object. It is created with the \code{svydesign} function in the \code{survey} package. +##' @param factorVars Numerically coded variables that should be handled as categorical variables given as a character vector. If omitted, only factors are considered categorical variables. If all categorical variables in the dataset are already factors, this option is not necessary. The variables specified here must also be specified in the \code{vars} argument. +##' @param includeNA If TRUE, NA is handled as a regular factor level rather than missing. NA is shown as the last factor level in the table. Only effective for categorical variables. +##' @param test If TRUE, as in the default and there are more than two groups, groupwise comparisons are performed. +##' @param testNormal A function used to perform the normal assumption based tests. The default is multiple degrees of freedom test using \code{svyglm} and \code{regTermTest}. This is equivalent of the \code{svyttest} when there are only two groups. +##' @param argsNormal A named list of arguments passed to the function specified in \code{testNormal}. +##' @param testNonNormal A function used to perform the nonparametric tests. The default is \code{svyranktest}. +##' @param argsNonNormal A named list of arguments passed to the function specified in \code{testNonNormal}. +##' @param testApprox A function used to perform the large sample approximation based tests. The default is \code{svychisq}. +##' @param argsApprox A named list of arguments passed to the function specified in testApprox. +##' @param smd If TRUE, as in the default and there are more than two groups, standardized mean differences for all pairwise comparisons are calculated. +##' +##' @details See the details for \code{\link{CreateTableOne}}. +##' +##' @return An object of class \code{svyTableOne}, which is a list of three objects. +##' @return \item{ContTable}{an object of class \code{svyContTable}, containing continuous variables only} +##' @return \item{CatTable}{ an object of class \code{svyCatTable}, containing categorical variables only} +##' @return \item{MetaData}{list of metadata regarding variables} +##' +##' @author Kazuki Yoshida +##' @seealso +##' \code{\link{print.TableOne}}, \code{\link{summary.TableOne}} +##' @examples +##' +##' ## Load packages +##' library(tableone) +##' library(survey) +##' +##' ## Create a weighted survey design object +##' data(nhanes) +##' nhanesSvy <- svydesign(ids = ~ SDMVPSU, strata = ~ SDMVSTRA, weights = ~ WTMEC2YR, +##' nest = TRUE, data = nhanes) +##' +##' ## Create a table object +##' ## factorVars are converted to factors; no need for variables already factors +##' ## strata will stratify summaries; leave it unspecified for overall summaries +##' tab1 <- svyCreateTableOne(vars = c("HI_CHOL","race","agecat","RIAGENDR"), +##' strata = "RIAGENDR", data = nhanesSvy, +##' factorVars = c("race","RIAGENDR")) +##' +##' ## Detailed output +##' summary(tab1) +##' +##' ## Default formatted printing +##' tab1 +##' +##' ## nonnormal specifies variables to be shown as median [IQR] +##' print(tab1, nonnormal = "HI_CHOL", contDigits = 3, catDigits = 2, +##' pDigits = 4, smd = TRUE) +##' +##' ## minMax changes it to median [min, max] +##' print(tab1, nonnormal = "HI_CHOL", minMax = TRUE, contDigits = 3, +##' catDigits = 2, pDigits = 4, smd = TRUE) +##' +##' ## showAllLevels can be used tow show levels for all categorical variables +##' print(tab1, showAllLevels = TRUE, smd = TRUE) +##' +##' ## To see all printing options +##' ?print.TableOne +##' +##' ## To examine categorical variables only +##' tab1$CatTable +##' +##' ## To examine continuous variables only +##' tab1$ContTable +##' +##' ## If SMDs are needed as numericals, use ExtractSmd() +##' ExtractSmd(tab1) +##' +##' @export +svyCreateTableOne <- +function(vars, # character vector of variable names + strata, # character vector of variable names + data, # data frame + factorVars, # variables to be transformed to factors + includeNA = FALSE, # include NA as a category (categoricals only) + test = TRUE, # whether to include p-values + ## Test configuration for categorical data + testApprox = svyTestChisq, # function for approximation test (only choice) + argsApprox = NULL, # arguments passed to testApprox + ## Test configuration for continuous data + testNormal = svyTestNormal, # test for normally distributed variables + argsNormal = list(method = "Wald"), # arguments passed to testNormal + testNonNormal = svyTestNonNormal, # test for nonnormally distributed variables + argsNonNormal = NULL, # arguments passed to testNonNormal + smd = TRUE # whether to include standardize mean differences + ) { + +### Data check + ## Check if the data given is a dataframe + StopIfNotSurveyDesign(data) + + ## Check if vars argument is missing. If so, add all names in data. + if (missing(vars)) { + vars <- names(data$variables) + } + + ## Check if variables exist. Drop them if not. + vars <- ModuleReturnVarsExist(vars, data$variables) + + ## Abort if no variables exist at this point + ModuleStopIfNoVarsLeft(vars) + + ## Factor conversions if the factorVars argument exist + if (!missing(factorVars)) { + ## Check if variables exist. Drop them if not. + factorVars <- ModuleReturnVarsExist(factorVars, data$variables) + ## Convert to factor + data$variables[factorVars] <- lapply(data$variables[factorVars], factor) + } + + ## Toggle test FALSE if no strata is given + test <- ModuleReturnFalseIfNoStrata(strata, test) + smd <- ModuleReturnFalseIfNoStrata(strata, smd) + + ## Get the classes of the variables + varClasses <- lapply(data$variables[vars], class) + + ## Classify as varFactors if any one of these classes are contained + varFactors <- sapply(varClasses, function(VEC) { + any(VEC %in% c("factor", "ordered", "logical", "character")) + }) + varFactors <- names(varFactors)[varFactors] + + ## Classify as varNumerics if any one of these classes are contained + varNumerics <- sapply(varClasses, function(VEC) { + any(VEC %in% c("numeric", "integer")) + }) + varNumerics <- names(varNumerics)[varNumerics] + + ## Drop variables that do not meet either because it is unsupported + varDrop <- setdiff(vars, c(varFactors, varNumerics)) + if (length(varDrop) > 0) { + warning("Dropping variable(s) ", paste0(varDrop, sep = " "), + " due to unsupported class.\n") + vars <- setdiff(vars, varDrop) + } + + ## Create a logical vector indicator for factors (vars in varFactors = TRUE) + logiFactors <- vars %in% varFactors + + ## Create lists of arguments + argsCreateContTable <- list(data = data, + test = test, + testNormal = testNormal, + argsNormal = argsNormal, + testNonNormal = testNonNormal, + argsNonNormal = argsNonNormal, + smd = smd) + argsCreateCatTable <- list(data = data, + includeNA = includeNA, + test = test, + testApprox = testApprox, + argsApprox = argsApprox, + smd = smd) + + ## Add strata = strata for argument only if strata is given + if (!missing(strata)) { + + ## Check strata. This returns a DF. Returns a "Overall" DF if strata is missing. + ## Must not be place outside if (!missing(strata)) { }. + dfStrata <- ModuleReturnStrata(strata, data$variable) + ## Return variable names. Code inefficient in exchange for code simplicity. + strata <- names(dfStrata) + + ## Create lists of arguments including strata + argsCreateContTable <- c(list(strata = strata), argsCreateContTable) + argsCreateCatTable <- c(list(strata = strata), argsCreateCatTable) + } + + +### If only varFactors/varNumerics are present, just call one constructor + if (length(varNumerics) == 0) { + ## No numerics + ContTable <- NULL + CatTable <- do.call(svyCreateCatTable, + args = c(list(vars = varFactors), argsCreateCatTable)) + + } else if (length(varFactors) == 0) { + ## No factors + ContTable <- do.call(svyCreateContTable, + args = c(list(vars = varNumerics), argsCreateContTable)) + CatTable <- NULL + +### Both types of variables are present, call both constructors + } else if ((length(varFactors) > 0) & (length(varNumerics) > 0)) { + + ## ContTable + ContTable <- do.call(svyCreateContTable, + args = c(list(vars = varNumerics), argsCreateContTable)) + ## CatTable + CatTable <- do.call(svyCreateCatTable, + args = c(list(vars = varFactors), argsCreateCatTable)) + } else { + + ## vars never empty by data check with ModuleStopIfNoVarsLeft() + ## Just to make sure + warning("No variables left to analyzed in vars.") + } + + + ## Create a list for output + ## Either one of the two tables may be NULL + TableOneObject <- list(ContTable = ContTable, + CatTable = CatTable, + MetaData = list(vars = vars, + ## describes which pos is vars is factor + logiFactors = logiFactors, + ## names of vars of each type + varFactors = varFactors, + varNumerics = varNumerics)) + + ## Give a class + class(TableOneObject) <- c("svyTableOne", "TableOne") + + ## Return the object + return(TableOneObject) +} diff --git a/R/tableone-package.R b/R/tableone-package.R index 698448c..e0e1e5e 100644 --- a/R/tableone-package.R +++ b/R/tableone-package.R @@ -1,94 +1,39 @@ ##' Create "Table 1" to describe baseline characteristics ##' -##' This package creates "Table 1", i.e., description of baseline patient characteristics, which is essential in every medical research. This package provides functions to create such summaries for continuous and categorical variables, optionally with subgroup comparisons. The package was insipired by and based on descriptive statistics functions in Deducer, a Java-based GUI package by Ian Fellows. This package does not require GUI or Java, and intended for command-line users. +##' Creates "Table 1", i.e., description of baseline patient characteristics, which is essential in every medical research. Supports both continuous and categorical variables, as well as p-values and standardized mean differences. Weighted data are supported via the survey package. See github for a screencast. tableone was inspired by descriptive statistics functions in Deducer , a Java-based GUI package by Ian Fellows. This package does not require GUI or Java, and intended for command-line users. Most important functions are \code{\link{CreateTableOne}} and \code{\link{svyCreateTableOne}}. ##' ##' @name tableone-package ##' @aliases tableone-package tableone ##' @docType package -##' @import e1071 gmodels -##' @note Special Thanks: +##' @import survey +##' @importFrom stats as.formula chisq.test coef confint fisher.test kruskal.test median oneway.test quantile sd var xtabs +##' @importFrom utils combn +##' @note Acknowledgement: ##' -##' Ian Fellows for developing the Deducer package, which this package is based on. +##' Ian Fellows for developing the \code{deducer} package, which this package is based on. ##' -##' Hadley Wickham for packaging advice and for creating tools this package was made with (roxygen2, devtools, testthat). +##' Hadley Wickham for packaging advice and for creating tools this package was made with (\code{roxygen2}, \code{devtools}, \code{testthat}). ##' ##' Yoshinobu Kanda for design advice and for integration into \code{RcmdrPlugin.EZR}. ##' -##' jomuller for a bug report and fix suggestion regarding handling of ordered factors. +##' H.Tachibana and Hiroki Matsui for inputs regarding standardized mean differences. ##' -##' Raja Sriswan Mamidi for suggestions regarding alignment. +##' jomuller, Raja Sriswan Mamidi, and Atsushi Shiraishi for bug reports and/or feature suggestions. ##' ##' Members of the Facebook Organization of R Users for Medical Statistics in Japan (FORUMS-J) for testing pre-release versions and suggestions. ##' +##' ##' Developmental repository is on github. Your contributions are appreciated. ##' -##' https://github.com/kaz-yos/tableone +##' \url{https://github.com/kaz-yos/tableone} ##' ##' @author Kazuki Yoshida, Justin Bohn ##' ##' Maintainer: Kazuki Yoshida ##' @seealso -##' \code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}}, -##' \code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}}, -##' \code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}}, -##' \code{\link{ShowRegTable}} +##' \code{\link{CreateTableOne}}, \code{\link{svyCreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}}, \code{\link{ShowRegTable}} ##' @examples ##' -##' ## Load -##' library(tableone) -##' -##' ## Load Mayo Clinic Primary Biliary Cirrhosis Data -##' library(survival) -##' data(pbc) -##' ## Check variables -##' head(pbc) -##' -##' ## List numerically coded categorical variables for later conversion. -##' ## Factor variables are automatically handled as categorical variables. -##' factorVars <- c("status","trt","ascites","hepato","spiders","edema","stage") -##' -##' ## Create a variable list -##' dput(names(pbc)) # This shows a character vector-creating syntax. -##' vars <- c("time","status","age","sex","ascites","hepato", -##' "spiders","edema","bili","chol","albumin", -##' "copper","alk.phos","ast","trig","platelet", -##' "protime","stage") -##' -##' ## Create Table 1 stratified by trt. Use factorVars to convert numerically -##' ## coded categorical variables as factors without changing the dataset. -##' tableOne <- CreateTableOne(vars = vars, strata = c("trt"), data = pbc, -##' factorVars = factorVars) -##' -##' ## Just typing the object name will invoke the print.TableOne method -##' tableOne -##' -##' ## Specifying nonnormal variables will show the variables appropriately, -##' ## and show nonparametric test p-values. Specify variables in the exact -##' ## argument to obtain the exact test p-values. For two-level categorical -##' ## variables specified in cramVars, both levels are shown. Use minMax -##' ## argument to show median [min, max] for nonnormal variables. -##' print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), -##' exact = c("status","stage"), cramVars = "sex") -##' -##' ## Use the summary.TableOne method for detailed summary -##' summary(tableOne) -##' -##' ## See the categorical part only using $ operator -##' tableOne$CatTable -##' summary(tableOne$CatTable) -##' -##' ## See the continuous part only using $ operator -##' tableOne$ContTable -##' summary(tableOne$ContTable) -##' -##' ## If your work flow includes copying to Excel and Word when writing manuscripts, -##' ## you may benefit from the quote argument. This will quote everything so that -##' ## Excel does not mess up the cells. -##' print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), -##' exact = c("status","stage"), cramVars = "sex", quote = TRUE) -##' -##' ## If you want to center-align values in Word, use noSpaces option. -##' print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), -##' exact = c("status","stage"), cramVars = "sex", quote = TRUE, noSpaces = TRUE) +##' ## See examples for CreateTableOne and svyCreateTableOne ##' NULL diff --git a/README.md b/README.md index 41ccd33..54f3b2d 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,13 @@ tableone (release repo) =============================================================================== +[![Travis-CI Build Status](https://travis-ci.org/kaz-yos/tableone.svg?branch=develop)](https://travis-ci.org/kaz-yos/tableone) + **An R package to create "Table 1", description of baseline characteristics** -This package creates "Table 1", i.e., description of baseline patient characteristics, which is essential every medical research. This package provides functions to create such summaries for continuous and categorical variables, optionally with subgroup comparisons. The package was inspired by and based on descriptive statistics functions in Deducer, a Java-based GUI package by Ian Fellows. This package does not require GUI or Java, and intended for command-line users. +Creates "Table 1", i.e., description of baseline patient characteristics, which is essential in every medical research. Supports both continuous and categorical variables, as well as p-values and standardized mean differences. Weighted data are supported via the survey package. + +tableone was inspired by descriptive statistics functions in Deducer , a Java-based GUI package by Ian Fellows. This package does not require GUI or Java, and intended for command-line users. tableone in action @@ -19,44 +23,57 @@ A higher quality version is available at YouTube: https://www.youtube.com/watch? tableone code example ------------------------------------------------------------------------------- -In this table, continuous and categorical variables can be ordered however you like. The p-valeus are from exact tests for pre-specified variables. For nonnormal variables, it shows median and IQR instead of mean and SD, and p-values are from nonparametric tests. In this case, the dataset has been transformed so that categorical variables are factors. +In this table, continuous and categorical variables can be placed in any order. The p-valeus are from exact tests for pre-specified variables. For nonnormal variables, it shows median and IQR instead of mean and SD, and p-values are from nonparametric tests. Numerically coded categorical variables can be transformed on the fly with factorVars. SMD stands for standardized mean differences. For weighted data, first created a svydesign object, and use the svyCreateTableOne() function. Most other options remain the same. ``` -> tableOne <- CreateTableOne(vars = vars, strata = "trt", data = pbc) + +> ## Load data +> library(survival); data(pbc) +> # drop ID from variable list +> vars <- names(pbc)[-1] +> ## Create Table 1 stratified by trt (can add more stratifying variables) +> tableOne <- CreateTableOne(vars = vars, strata = c("trt"), data = pbc, ++ factorVars = c("status","edema","stage")) +> ## Specifying nonnormal variables will show the variables appropriately, +> ## and show nonparametric test p-values. Specify variables in the exact +> ## argument to obtain the exact test p-values. > print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), -+ exact = c("status","stage"), cramVars = "sex") - - Stratified by trt - 1 2 p test - n 158 154 - time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883 - status (%) 0.884 exact - 0 83 (52.5) 85 (55.2) - 1 10 ( 6.3) 9 ( 5.8) - 2 65 (41.1) 60 (39.0) - age (mean (sd)) 51.42 (11.01) 48.58 (9.96) 0.018 - sex = m/f (%) 21/137 (13.3/86.7) 15/139 (9.7/90.3) 0.421 - ascites = 1 (%) 14 (8.9) 10 (6.5) 0.567 - hepato = 1 (%) 73 (46.2) 87 (56.5) 0.088 - spiders = 1 (%) 45 (28.5) 45 (29.2) 0.985 - edema (%) 0.877 - 0 132 (83.5) 131 (85.1) - 0.5 16 (10.1) 13 ( 8.4) - 1 10 ( 6.3) 10 ( 6.5) - bili (median [IQR]) 1.40 [0.80, 3.20] 1.30 [0.72, 3.60] 0.842 nonnorm - chol (median [IQR]) 315.50 [247.75, 417.00] 303.50 [254.25, 377.00] 0.544 nonnorm - albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) 0.874 - copper (median [IQR]) 73.00 [40.00, 121.00] 73.00 [43.00, 139.00] 0.717 nonnorm - alk.phos (median [IQR]) 1214.50 [840.75, 2028.00] 1283.00 [922.50, 1949.75] 0.812 nonnorm - ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) 0.460 - trig (median [IQR]) 106.00 [84.50, 146.00] 113.00 [84.50, 155.00] 0.370 nonnorm - platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) 0.555 - protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) 0.197 - stage (%) 0.205 exact - 1 12 ( 7.6) 4 ( 2.6) - 2 35 (22.2) 32 (20.8) - 3 56 (35.4) 64 (41.6) - 4 55 (34.8) 54 (35.1) ++ exact = c("status","stage"), smd = TRUE) + + + Stratified by trt + 1 2 p test SMD +n 158 154 +time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883 0.017 +status (%) 0.884 exact 0.054 + 0 83 (52.5) 85 (55.2) + 1 10 ( 6.3) 9 ( 5.8) + 2 65 (41.1) 60 (39.0) +trt (mean (sd)) 1.00 (0.00) 2.00 (0.00) <0.001 Inf +age (mean (sd)) 51.42 (11.01) 48.58 (9.96) 0.018 0.270 +sex = f (%) 137 (86.7) 139 (90.3) 0.421 0.111 +ascites (mean (sd)) 0.09 (0.29) 0.06 (0.25) 0.434 0.089 +hepato (mean (sd)) 0.46 (0.50) 0.56 (0.50) 0.069 0.206 +spiders (mean (sd)) 0.28 (0.45) 0.29 (0.46) 0.886 0.016 +edema (%) 0.877 0.058 + 0 132 (83.5) 131 (85.1) + 0.5 16 (10.1) 13 ( 8.4) + 1 10 ( 6.3) 10 ( 6.5) +bili (median [IQR]) 1.40 [0.80, 3.20] 1.30 [0.72, 3.60] 0.842 nonnorm 0.171 +chol (median [IQR]) 315.50 [247.75, 417.00] 303.50 [254.25, 377.00] 0.544 nonnorm 0.038 +albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) 0.874 0.018 +copper (median [IQR]) 73.00 [40.00, 121.00] 73.00 [43.00, 139.00] 0.717 nonnorm <0.001 +alk.phos (median [IQR]) 1214.50 [840.75, 2028.00] 1283.00 [922.50, 1949.75] 0.812 nonnorm 0.037 +ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) 0.460 0.084 +trig (median [IQR]) 106.00 [84.50, 146.00] 113.00 [84.50, 155.00] 0.370 nonnorm 0.017 +platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) 0.555 0.067 +protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) 0.197 0.146 +stage (%) 0.205 exact 0.246 + 1 12 ( 7.6) 4 ( 2.6) + 2 35 (22.2) 32 (20.8) + 3 56 (35.4) 64 (41.6) + 4 55 (34.8) 54 (35.1) + ``` @@ -65,8 +82,7 @@ Installation This is the release version of the tableone package either to be released on CRAN or is already on CRAN. The installation can be done by the usual package installation via CRAN. -- http://cran.r-project.org/web/packages/tableone/index.html - +- https://cran.r-project.org/package=tableone If you prefer to follow the latest development, please see the developmetal repo: diff --git a/cran-check.txt b/cran-check.txt new file mode 100644 index 0000000..80df09b --- /dev/null +++ b/cran-check.txt @@ -0,0 +1,65 @@ +* using log directory ‘/Users/kazuki/Documents/programming/r/tableone/tableone.Rcheck’ +* using R version 3.2.1 (2015-06-18) +* using platform: x86_64-apple-darwin13.4.0 (64-bit) +* using session charset: UTF-8 +* using option ‘--as-cran’ +* checking for file ‘tableone/DESCRIPTION’ ... OK +* checking extension type ... Package +* this is package ‘tableone’ version ‘0.7.0’ +* checking CRAN incoming feasibility ... Note_to_CRAN_maintainers +Maintainer: ‘Kazuki Yoshida ’ +* checking package namespace information ... OK +* checking package dependencies ... OK +* checking if this is a source package ... OK +* checking if there is a namespace ... OK +* checking for executable files ... OK +* checking for hidden files and directories ... OK +* checking for portable file names ... OK +* checking for sufficient/correct file permissions ... OK +* checking whether package ‘tableone’ can be installed ... OK +* checking installed package size ... OK +* checking package directory ... OK +* checking ‘build’ directory ... OK +* checking DESCRIPTION meta-information ... OK +* checking top-level files ... OK +* checking for left-over files ... OK +* checking index information ... OK +* checking package subdirectories ... OK +* checking R files for non-ASCII characters ... OK +* checking R files for syntax errors ... OK +* checking whether the package can be loaded ... OK +* checking whether the package can be loaded with stated dependencies ... OK +* checking whether the package can be unloaded cleanly ... OK +* checking whether the namespace can be loaded with stated dependencies ... OK +* checking whether the namespace can be unloaded cleanly ... OK +* checking use of S3 registration ... OK +* checking dependencies in R code ... OK +* checking S3 generic/method consistency ... OK +* checking replacement functions ... OK +* checking foreign function calls ... OK +* checking R code for possible problems ... OK +* checking Rd files ... OK +* checking Rd metadata ... OK +* checking Rd line widths ... OK +* checking Rd cross-references ... OK +* checking for missing documentation entries ... OK +* checking for code/documentation mismatches ... OK +* checking Rd \usage sections ... OK +* checking Rd contents ... OK +* checking for unstated dependencies in examples ... OK +* checking installed files from ‘inst/doc’ ... OK +* checking files in ‘vignettes’ ... OK +* checking examples ... OK +* checking for unstated dependencies in ‘tests’ ... OK +* checking tests ... + OK +* checking for unstated dependencies in vignettes ... OK +* checking package vignettes in ‘inst/doc’ ... OK +* checking running R code from vignettes ... + ‘introduction.Rmd’ using ‘UTF-8’ ... OK + ‘smd.Rmd’ using ‘UTF-8’ ... OK + OK +* checking re-building of vignette outputs ... OK +* checking PDF version of manual ... OK +* DONE +Status: OK diff --git a/cran-comment.md b/cran-comment.md new file mode 100644 index 0000000..65791de --- /dev/null +++ b/cran-comment.md @@ -0,0 +1,18 @@ +## What's new +* Weighted data support +* Standardized mean difference support + +## Test environments +* Local OS X 10.10.4, R 3.2.1; R-devel (2015-08-06 r68871) +* Ubuntu Linux (on Travis-CI), R 3.2.1 +* winb-builder (devel and release) + +## R CMD check results +* ERRORs: None +* WARNINGs: None +* NOTEs: + - Package author e-mail confirmation. + - "No repository set, so cyclic dependency check skipped" on R 3.2.1 on win-builder (not a package problem) + +## Downstream dependencies +RcmdrPlugin.EZR GUI frontend: Manually examined "Summary table of sample characteristics" for compatibility; EZR author also examined compatibility. diff --git a/man/CreateCatTable.Rd b/man/CreateCatTable.Rd index 54b1faf..72988c1 100644 --- a/man/CreateCatTable.Rd +++ b/man/CreateCatTable.Rd @@ -1,12 +1,13 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/CreateCatTable.R \name{CreateCatTable} \alias{CreateCatTable} \title{Create an object summarizing categorical variables} \usage{ -CreateCatTable(vars, strata, data, test = TRUE, testApprox = chisq.test, - argsApprox = list(correct = TRUE), testExact = fisher.test, - argsExact = list(workspace = 2 * 10^5)) +CreateCatTable(vars, strata, data, includeNA = FALSE, test = TRUE, + testApprox = chisq.test, argsApprox = list(correct = TRUE), + testExact = fisher.test, argsExact = list(workspace = 2 * 10^5), + smd = TRUE) } \arguments{ \item{vars}{Variable(s) to be summarized given as a character vector.} @@ -15,24 +16,25 @@ CreateCatTable(vars, strata, data, test = TRUE, testApprox = chisq.test, \item{data}{A data frame in which these variables exist. All variables (both vars and strata) must be in this data frame.} +\item{includeNA}{If TRUE, NA is handled as a regular factor level rather than missing. NA is shown as the last factor level in the table. Only effective for categorical variables.} + \item{test}{If TRUE, as in the default and there are more than two groups, groupwise comparisons are performed. Both tests that require the large sample approximation and exact tests are performed. Either one of the result can be obtained from the print method.} -\item{testApprox}{A function used to perform the large sample approximation based tests. The default is \code{\link{chisq.test}}. This is not recommended when some of the cell have small counts like fewer than 5.} +\item{testApprox}{A function used to perform the large sample approximation based tests. The default is \code{chisq.test}. This is not recommended when some of the cell have small counts like fewer than 5.} + +\item{argsApprox}{A named list of arguments passed to the function specified in testApprox. The default is \code{list(correct = TRUE)}, which turns on the continuity correction for \code{chisq.test}.} -\item{argsApprox}{A named list of arguments passed to the function specified in testApprox. The default is \code{list(correct = TRUE)}, which turns on the continuity correction for \code{\link{chisq.test}}.} +\item{testExact}{A function used to perform the exact tests. The default is \code{fisher.test}. If the cells have large numbers, it will fail because of memory limitation. In this situation, the large sample approximation based should suffice.} -\item{testExact}{A function used to perform the exact tests. The default is fisher.test. If the cells have large numbers, it will fail because of memory limitation. In this situation, the large sample approximation based should suffice.} +\item{argsExact}{A named list of arguments passed to the function specified in testExact. The default is \code{list(workspace = 2*10^5)}, which specifies the memory space allocated for \code{fisher.test}.} -\item{argsExact}{A named list of arguments passed to the function specified in testExact. The default is \code{list(workspace = 2*10^5)}, which specifies the memory space allocated for \code{\link{fisher.test}}.} +\item{smd}{If TRUE, as in the default and there are more than two groups, standardized mean differences for all pairwise comparisons are calculated.} } \value{ -An object of class \code{CatTable}, which really is a \code{\link{by}} object with additional attributes. Each element of the \code{\link{by}} part is a matrix with rows representing variables, and columns representing summary statistics. +An object of class \code{CatTable}. } \description{ -Create an object summarizing categorical variables optionally stratifying -by one or more startifying variables and performing statistical tests. The -object gives a table that is easy to use in medical research papers. See -also \code{\link{print.CatTable}} and \code{\link{summary.CatTable}}. +Create an object summarizing categorical variables optionally stratifying by one or more startifying variables and performing statistical tests. Usually, \code{\link{CreateTableOne}} should be used as the universal frontend for both continuous and categorical data. } \examples{ ## Load @@ -75,7 +77,7 @@ catTableBySexTrt <- CreateCatTable(vars = catVars, ## print now includes p-values which are by default calculated by chisq.test. ## It is formatted at the decimal place specified by the pDigits argument -## (3 by default). It does <0.001 for you. +## (3 by default). It is formatted like <0.001 if very small. catTableBySexTrt ## The exact argument toggles the p-values to the exact test result from @@ -97,8 +99,6 @@ print(catTableBySexTrt, exact = "ascites", quote = TRUE, noSpaces = TRUE) Kazuki Yoshida (based on \code{Deducer::frequencies()}) } \seealso{ -\code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}}, -\code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}}, -\code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}} +\code{\link{CreateTableOne}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}} } diff --git a/man/CreateContTable.Rd b/man/CreateContTable.Rd index 494feb1..6ceb6a9 100644 --- a/man/CreateContTable.Rd +++ b/man/CreateContTable.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/CreateContTable.R \name{CreateContTable} \alias{CreateContTable} @@ -8,7 +8,7 @@ CreateContTable(vars, strata, data, funcNames = c("n", "miss", "p.miss", "mean", "sd", "median", "p25", "p75", "min", "max", "skew", "kurt"), funcAdditional, test = TRUE, testNormal = oneway.test, argsNormal = list(var.equal = TRUE), testNonNormal = kruskal.test, - argsNonNormal = list(NULL)) + argsNonNormal = list(NULL), smd = TRUE) } \arguments{ \item{vars}{Variable(s) to be summarized given as a character vector.} @@ -23,22 +23,21 @@ CreateContTable(vars, strata, data, funcNames = c("n", "miss", "p.miss", \item{test}{If TRUE, as in the default and there are more than two groups, groupwise comparisons are performed. Both tests that assume normality and tests that do not are performed. Either one of the result can be obtained from the print method.} -\item{testNormal}{A function used to perform the normal assumption based tests. The default is \code{\link{oneway.test}}. This is equivalent of the t-test when there are only two groups.} +\item{testNormal}{A function used to perform the normal assumption based tests. The default is \code{oneway.test}. This is equivalent of the t-test when there are only two groups.} \item{argsNormal}{A named list of arguments passed to the function specified in \code{testNormal}. The default is \code{list(var.equal = TRUE)}, which makes it the ordinary ANOVA that assumes equal variance across groups.} \item{testNonNormal}{A function used to perform the nonparametric tests. The default is \code{kruskal.test} (Kruskal-Wallis rank sum test). This is equivalent of the wilcox.test (Man-Whitney U test) when there are only two groups.} \item{argsNonNormal}{A named list of arguments passed to the function specified in \code{testNonNormal}. The default is \code{list(NULL)}, which is just a placeholder.} + +\item{smd}{If TRUE, as in the default and there are more than two groups, standardized mean differences for all pairwise comparisons are calculated.} } \value{ -An object of class \code{ContTable}, which really is a \code{\link{by}} object with additional attributes. Each element of the \code{\link{by}} part is a matrix with rows representing variables, and columns representing summary statistics. +An object of class \code{ContTable}. } \description{ -Create an object summarizing continous variables optionally stratifying by -one or more startifying variables and performing statistical tests. The -object gives a table that is easy to use in medical research papers. See -also \code{\link{print.ContTable}} and \code{\link{summary.ContTable}}. +Create an object summarizing continous variables optionally stratifying by one or more startifying variables and performing statistical tests. Usually, \code{\link{CreateTableOne}} should be used as the universal frontend for both continuous and categorical data. } \examples{ ## Load @@ -100,8 +99,6 @@ print(contTableBySexTrt, nonnormal = nonNormalVars, quote = TRUE, noSpaces = TRU Kazuki Yoshida (based on \code{Deducer::descriptive.table()}) } \seealso{ -\code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}}, -\code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}}, -\code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}} +\code{\link{CreateTableOne}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}} } diff --git a/man/CreateTableOne.Rd b/man/CreateTableOne.Rd index 373ad68..04f5cdb 100644 --- a/man/CreateTableOne.Rd +++ b/man/CreateTableOne.Rd @@ -1,14 +1,14 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/CreateTableOne.R \name{CreateTableOne} \alias{CreateTableOne} -\title{Create an object summarizing both categorical and continuous variables} +\title{Create an object summarizing both continuous and categorical variables} \usage{ -CreateTableOne(vars, strata, data, factorVars, test = TRUE, - testApprox = chisq.test, argsApprox = list(correct = TRUE), +CreateTableOne(vars, strata, data, factorVars, includeNA = FALSE, + test = TRUE, testApprox = chisq.test, argsApprox = list(correct = TRUE), testExact = fisher.test, argsExact = list(workspace = 2 * 10^5), testNormal = oneway.test, argsNormal = list(var.equal = TRUE), - testNonNormal = kruskal.test, argsNonNormal = list(NULL)) + testNonNormal = kruskal.test, argsNonNormal = list(NULL), smd = TRUE) } \arguments{ \item{vars}{Variables to be summarized given as a character vector. Factors are handled as categorical variables, whereas numeric variables are handled as continuous variables. If empty, all variables in the data frame specified in the data argument are used.} @@ -19,37 +19,42 @@ CreateTableOne(vars, strata, data, factorVars, test = TRUE, \item{factorVars}{Numerically coded variables that should be handled as categorical variables given as a character vector. If omitted, only factors are considered categorical variables. If all categorical variables in the dataset are already factors, this option is not necessary. The variables specified here must also be specified in the \code{vars} argument.} +\item{includeNA}{If TRUE, NA is handled as a regular factor level rather than missing. NA is shown as the last factor level in the table. Only effective for categorical variables.} + \item{test}{If TRUE, as in the default and there are more than two groups, groupwise comparisons are performed.} -\item{testApprox}{A function used to perform the large sample approximation based tests. The default is \code{\link{chisq.test}}. This is not recommended when some of the cell have small counts like fewer than 5.} +\item{testApprox}{A function used to perform the large sample approximation based tests. The default is \code{chisq.test}. This is not recommended when some of the cell have small counts like fewer than 5.} -\item{argsApprox}{A named list of arguments passed to the function specified in testApprox. The default is \code{list(correct = TRUE)}, which turns on the continuity correction for \code{\link{chisq.test}}.} +\item{argsApprox}{A named list of arguments passed to the function specified in testApprox. The default is \code{list(correct = TRUE)}, which turns on the continuity correction for \code{chisq.test}.} -\item{testExact}{A function used to perform the exact tests. The default is fisher.test. If the cells have large numbers, it will fail because of memory limitation. In this situation, the large sample approximation based should suffice.} +\item{testExact}{A function used to perform the exact tests. The default is \code{fisher.test}. If the cells have large numbers, it will fail because of memory limitation. In this situation, the large sample approximation based should suffice.} -\item{argsExact}{A named list of arguments passed to the function specified in testExact. The default is \code{list(workspace = 2*10^5)}, which specifies the memory space allocated for \code{\link{fisher.test}}.} +\item{argsExact}{A named list of arguments passed to the function specified in testExact. The default is \code{list(workspace = 2*10^5)}, which specifies the memory space allocated for \code{fisher.test}.} -\item{testNormal}{A function used to perform the normal assumption based tests. The default is \code{\link{oneway.test}}. This is equivalent of the t-test when there are only two groups.} +\item{testNormal}{A function used to perform the normal assumption based tests. The default is \code{oneway.test}. This is equivalent of the t-test when there are only two groups.} \item{argsNormal}{A named list of arguments passed to the function specified in \code{testNormal}. The default is \code{list(var.equal = TRUE)}, which makes it the ordinary ANOVA that assumes equal variance across groups.} \item{testNonNormal}{A function used to perform the nonparametric tests. The default is \code{kruskal.test} (Kruskal-Wallis Rank Sum Test). This is equivalent of the wilcox.test (Man-Whitney U test) when there are only two groups.} \item{argsNonNormal}{A named list of arguments passed to the function specified in \code{testNonNormal}. The default is \code{list(NULL)}, which is just a placeholder.} + +\item{smd}{If TRUE, as in the default and there are more than two groups, standardized mean differences for all pairwise comparisons are calculated.} } \value{ -An object of class \code{TableOne}, which really is a list of three objects. - -\item{TableOne}{a categorical-continuous mixture data formatted and printed by the \code{\link{print.TableOne}} method} +An object of class \code{TableOne}, which is a list of three objects. -\item{ContTable}{an object of class \code{ContTable}, containing continuous variables only} +\item{ContTable}{object of class \code{ContTable}, containing continuous variables only} -\item{CatTable}{ an object of class \code{CatTable}, containing categorical variables only} +\item{CatTable}{object of class \code{CatTable}, containing categorical variables only} -The second and third objects can be then be examined with the \code{print} and \code{summary} method, for example, \code{summary(object$CatTable)} to examine the categorical variables in detail. +\item{MetaData}{list of metadata regarding variables} } \description{ -Create an object summarizing all baseline variables optionally stratifying by one or more startifying variables and performing statistical tests. The object gives a table that is easy to use in medical research papers. See also \code{\link{print.TableOne}} and \code{\link{summary.TableOne}}. +Create an object summarizing all baseline variables (both continuous and categorical) optionally stratifying by one or more startifying variables and performing statistical tests. The object gives a table that is easy to use in medical research papers. +} +\details{ +The definitions of the standardized mean difference (SMD) are available in \href{http://www.tandfonline.com/doi/abs/10.1080/00031305.1986.10475403}{Flury \emph{et al} 1986} for the univariate case and the multivariate case (essentially the square root of the Mahalanobis distance). Extension to binary variables is discussed in \href{http://www.tandfonline.com/doi/abs/10.1080/03610910902859574}{Austin 2009} and extension to multinomival variables is suggested in \href{http://support.sas.com/resources/papers/proceedings12/335-2012.pdf}{Yang \emph{et al} 2012}. This multinomial extesion treats a single multinomial variable as multiple non-redundant dichotomous variables and use the Mahalanobis distance. The off diagonal elements of the covariance matrix on page 3 have an error, and need negation. In weighted data, the same definitions can be used except that the mean and standard deviation estimates are weighted estimates (\href{http://www.ncbi.nlm.nih.gov/pubmed/23902694}{Li \emph{et al} 2013} and \href{http://onlinelibrary.wiley.com/doi/10.1002/sim.6607/full}{Austin \emph{et al} 2015}). In tableone, all weighted estimates are calculated by weighted estimation functions in the \code{survey} package. } \examples{ ## Load @@ -80,9 +85,10 @@ tableOne ## Specifying nonnormal variables will show the variables appropriately, ## and show nonparametric test p-values. Specify variables in the exact -## argument to obtain the exact test p-values. +## argument to obtain the exact test p-values. cramVars can be used to +## show both levels for a 2-level categorical variables. print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), - exact = c("status","stage")) + exact = c("status","stage"), cramVars = "hepato", smd = TRUE) ## Use the summary.TableOne method for detailed summary summary(tableOne) @@ -104,13 +110,25 @@ print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), ## If you want to center-align values in Word, use noSpaces option. print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), exact = c("status","stage"), quote = TRUE, noSpaces = TRUE) + +## If SMDs are needed as numericals, use ExtractSmd() +ExtractSmd(tableOne) } \author{ -Justin Bohn, Kazuki Yoshida +Kazuki Yoshida, Justin Bohn +} +\references{ +Flury, BK. and Riedwyl, H. (1986). Standard distance in univariate and multivariate analysis. \emph{The American Statistician}, \bold{40}, 249-251. + +Austin, PC. (2009). Using the Standardized Difference to Compare the Prevalence of a Binary Variable Between Two Groups in Observational Research. \emph{Communications in Statistics - Simulation and Computation}, \bold{38}, 1228-1234. + +Yang, D. and Dalton, JE. (2012). A unified approach to measuring the effect size between two groups using SAS. SAS Global Forum 2012, Paper 335-2012. + +Li, L. and Greene, T. (2013). A weighting analogue to pair matching in propensity score analysis. \emph{International Journal of Biostatistics}, \bold{9}, 215-234. + +Austin, PC. and Stuart, EA. (2015). Moving towards best practice when using inverse probability of treatment weighting (IPTW) using the propensity score to estimate causal treatment effects in observational studies. \emph{Statistics in Medicine}, Online on August 3, 2015. } \seealso{ -\code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}}, -\code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}}, -\code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}} +\code{\link{print.TableOne}}, \code{\link{summary.TableOne}} } diff --git a/man/ExtractSmd.Rd b/man/ExtractSmd.Rd new file mode 100644 index 0000000..e0c610f --- /dev/null +++ b/man/ExtractSmd.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/ExtractSmd.R +\name{ExtractSmd} +\alias{ExtractSmd} +\title{Extract standardized mean differences from a (svy)TableOne object} +\usage{ +ExtractSmd(x) +} +\arguments{ +\item{x}{A stratified (svy)TableOne object containing standardized mean differences.} +} +\value{ +A vector or matrix containing the average standardized mean differences (if more than two contrasts exist) as well as the all possible pairwise standardized mean differences. Variables are ordered in the same order as the printed table. +} +\description{ +Extracts standardized mean differences data as a vector or matrix from a (svy)TableOne object +} +\examples{ +## See examples for CreateTableOne and svyCreateTableOne +} +\author{ +Kazuki Yoshida +} +\seealso{ +\code{\link{CreateTableOne}}, \code{\link{svyCreateTableOne}} +} + diff --git a/man/ShowRegTable.Rd b/man/ShowRegTable.Rd index ddca8f8..7261094 100644 --- a/man/ShowRegTable.Rd +++ b/man/ShowRegTable.Rd @@ -1,11 +1,11 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/ShowRegTable.R \name{ShowRegTable} \alias{ShowRegTable} \title{Format regression results in medically decent format} \usage{ ShowRegTable(model, exp = TRUE, digits = 2, pDigits = 3, - printToggle = TRUE, quote = FALSE) + printToggle = TRUE, quote = FALSE, ciFun = confint) } \arguments{ \item{model}{Regression model result objects that have the summary and confint methods.} @@ -19,6 +19,8 @@ ShowRegTable(model, exp = TRUE, digits = 2, pDigits = 3, \item{printToggle}{Whether to print the output. If FLASE, no output is created, and a matrix is invisibly returned.} \item{quote}{Whether to show everything in quotes. The default is FALSE. If TRUE, everything including the row and column names are quoted so that you can copy it to Excel easily.} + +\item{ciFun}{Function used for calculation. \code{confint} is the default. For generalized linear models this gives the profile likelihood-based calculation, which may take too much time for large models, use \code{confint.default} for simple normal approximation method (+/- 1.96 * standard error).} } \value{ A matrix containing what you see is returned invisibly. You can capture it by assignment to an object. diff --git a/man/print.CatTable.Rd b/man/print.CatTable.Rd index 5a0c957..a1be6bd 100644 --- a/man/print.CatTable.Rd +++ b/man/print.CatTable.Rd @@ -1,20 +1,21 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/print.CatTable.R \name{print.CatTable} \alias{print.CatTable} -\title{Format and print the \code{CatTable} class objects} +\title{Format and print \code{CatTable} class objects} \usage{ \method{print}{CatTable}(x, digits = 1, pDigits = 3, quote = FALSE, missing = FALSE, explain = TRUE, printToggle = TRUE, noSpaces = FALSE, format = c("fp", "f", "p", "pf")[1], showAllLevels = FALSE, - cramVars = NULL, test = TRUE, exact = NULL, CrossTable = FALSE, ...) + cramVars = NULL, test = TRUE, exact = NULL, smd = FALSE, + CrossTable = FALSE, ...) } \arguments{ -\item{x}{The result of a call to the \code{\link{CreateCatTable}} function.} +\item{x}{Object returned by \code{\link{CreateCatTable}} function.} \item{digits}{Number of digits to print in the table.} -\item{pDigits}{Number of digits to print for p-values.} +\item{pDigits}{Number of digits to print for p-values (also used for standardized mean differences).} \item{quote}{Whether to show everything in quotes. The default is FALSE. If TRUE, everything including the row and column names are quoted so that you can copy it to Excel easily.} @@ -32,19 +33,21 @@ \item{cramVars}{A character vector to specify the two-level categorical variables, for which both levels should be shown in one row.} -\item{test}{Whether to show the p-values. TRUE by default. If FALSE, only the numerical summaries are shown.} +\item{test}{Whether to show p-values. TRUE by default. If FALSE, only the numerical summaries are shown.} \item{exact}{A character vector to specify the variables for which the p-values should be those of exact tests. By default all p-values are from large sample approximation tests (chisq.test).} +\item{smd}{Whether to show standardized mean differences. FALSE by default. If there are more than one contrasts, the average of all possible standardized mean differences is shown. For individual contrasts, use \code{summary}.} + \item{CrossTable}{Whether to show the cross table objects held internally using gmodels::CrossTable function. This will give an output similar to the PROC FREQ in SAS.} \item{...}{For compatibility with generic. Ignored.} } \value{ -It is mainly for printing the result. But this function does return a matrix containing what you see in the output invisibly. You can assign it to an object to save it. +A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via \code{write.csv}. } \description{ -This is the \code{print} method for the \code{CatTable} class objects created by \code{\link{CreateCatTable}} function. +\code{print} method for the \code{CatTable} class objects created by \code{\link{CreateCatTable}} function. } \examples{ ## Load @@ -108,8 +111,6 @@ print(catTableBySexTrt, exact = "ascites", quote = TRUE, noSpaces = TRUE) Kazuki Yoshida } \seealso{ -\code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}}, -\code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}}, -\code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}} +\code{\link{CreateTableOne}}, \code{\link{CreateCatTable}}, \code{\link{summary.CatTable}} } diff --git a/man/print.ContTable.Rd b/man/print.ContTable.Rd index 51a7c78..5a472ca 100644 --- a/man/print.ContTable.Rd +++ b/man/print.ContTable.Rd @@ -1,20 +1,20 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/print.ContTable.R \name{print.ContTable} \alias{print.ContTable} -\title{Format and print the \code{ContTable} class objects} +\title{Format and print \code{ContTable} class objects} \usage{ \method{print}{ContTable}(x, digits = 2, pDigits = 3, quote = FALSE, missing = FALSE, explain = TRUE, printToggle = TRUE, noSpaces = FALSE, nonnormal = NULL, minMax = FALSE, insertLevel = FALSE, test = TRUE, - ...) + smd = FALSE, ...) } \arguments{ -\item{x}{The result of a call to the \code{\link{CreateContTable}} function.} +\item{x}{Object returned by \code{\link{CreateContTable}} function.} \item{digits}{Number of digits to print in the table.} -\item{pDigits}{Number of digits to print for p-values.} +\item{pDigits}{Number of digits to print for p-values (also used for standardized mean differences).} \item{quote}{Whether to show everything in quotes. The default is FALSE. If TRUE, everything including the row and column names are quoted so that you can copy it to Excel easily.} @@ -32,15 +32,17 @@ \item{insertLevel}{Whether to add an empty level column to the left of strata.} -\item{test}{Whether to show the p-values. TRUE by default. If FALSE, only the numerical summaries are shown.} +\item{test}{Whether to show p-values. TRUE by default. If FALSE, only the numerical summaries are shown.} + +\item{smd}{Whether to show standardized mean differences. FALSE by default. If there are more than one contrasts, the average of all possible standardized mean differences is shown. For individual contrasts, use \code{summary}.} \item{...}{For compatibility with generic. Ignored.} } \value{ -It is mainly for printing the result. But this function does return a matrix containing what you see in the output invisibly. You can assign it to an object to save it. +A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via \code{write.csv}. } \description{ -This is the \code{print} method for the \code{ContTable} class objects created by \code{\link{CreateContTable}} function. +\code{print} method for the \code{ContTable} class objects created by \code{\link{CreateContTable}} function. } \examples{ ## Load @@ -105,8 +107,6 @@ print(contTableBySexTrt, nonnormal = nonNormalVars, quote = TRUE, noSpaces = TRU Kazuki Yoshida } \seealso{ -\code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}}, -\code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}}, -\code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}} +\code{\link{CreateTableOne}}, \code{\link{CreateContTable}}, \code{\link{summary.ContTable}} } diff --git a/man/print.TableOne.Rd b/man/print.TableOne.Rd index 2aa6913..d8d057a 100644 --- a/man/print.TableOne.Rd +++ b/man/print.TableOne.Rd @@ -1,23 +1,23 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/print.TableOne.R \name{print.TableOne} \alias{print.TableOne} -\title{Format and print the \code{TableOne} class objects} +\title{Format and print \code{TableOne} class objects} \usage{ \method{print}{TableOne}(x, catDigits = 1, contDigits = 2, pDigits = 3, quote = FALSE, missing = FALSE, explain = TRUE, printToggle = TRUE, - test = TRUE, noSpaces = FALSE, format = c("fp", "f", "p", "pf")[1], - showAllLevels = FALSE, cramVars = NULL, exact = NULL, + test = TRUE, smd = FALSE, noSpaces = FALSE, format = c("fp", "f", "p", + "pf")[1], showAllLevels = FALSE, cramVars = NULL, exact = NULL, nonnormal = NULL, minMax = FALSE, ...) } \arguments{ -\item{x}{The result of a call to the \code{\link{CreateTableOne}} function.} +\item{x}{Object returned by \code{\link{CreateTableOne}} function.} \item{catDigits}{Number of digits to print for proportions. Default 1.} \item{contDigits}{Number of digits to print for continuous variables. Default 2.} -\item{pDigits}{Number of digits to print for p-values. Default 3.} +\item{pDigits}{Number of digits to print for p-values (also used for standardized mean differences). Default 3.} \item{quote}{Whether to show everything in quotes. The default is FALSE. If TRUE, everything including the row and column names are quoted so that you can copy it to Excel easily.} @@ -27,7 +27,9 @@ \item{printToggle}{Whether to print the output. If FLASE, no output is created, and a matrix is invisibly returned.} -\item{test}{Whether to show the p-values. TRUE by default. If FALSE, only the numerical summaries are shown.} +\item{test}{Whether to show p-values. TRUE by default. If FALSE, only the numerical summaries are shown.} + +\item{smd}{Whether to show standardized mean differences. FALSE by default. If there are more than one contrasts, the average of all possible standardized mean differences is shown. For individual contrasts, use \code{summary}.} \item{noSpaces}{Whether to remove spaces added for alignment. Use this option if you prefer to align numbers yourself in other software.} @@ -46,69 +48,18 @@ \item{...}{For compatibility with generic. Ignored.} } \value{ -It is mainly for printing the result. But this function does return a matrix containing what you see in the output invisibly. You can assign it to an object to save it. +A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via \code{write.csv}. } \description{ -This is the \code{print} method for the \code{TableOne} class objects created by \code{\link{CreateTableOne}} function. +\code{print} method for the \code{TableOne} class objects created by \code{\link{CreateTableOne}} function. } \examples{ -## Load -library(tableone) - -## Load Mayo Clinic Primary Biliary Cirrhosis Data -library(survival) -data(pbc) -## Check variables -head(pbc) - -## Make categorical variables factors -varsToFactor <- c("status","trt","ascites","hepato","spiders","edema","stage") -pbc[varsToFactor] <- lapply(pbc[varsToFactor], factor) - -## Create Table 1 stratified by sex and trt -tableOne <- CreateTableOne(vars = c("time","status","age","ascites","hepato", - "spiders","edema","bili","chol","albumin", - "copper","alk.phos","ast","trig","platelet", - "protime","stage"), - strata = c("sex","trt"), data = pbc) - -## Just typing the object name will invoke the print.TableOne method -tableOne - -## Specifying nonnormal variables will show the variables appropriately, -## and show nonparametric test p-values. Specify variables in the exact -## argument to obtain the exact test p-values. cramVars can be used to -## show both levels for a 2-level categorical variables. -print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), - exact = c("status","stage"), cramVars = "hepato") - -## Use the summary.TableOne method for detailed summary -summary(tableOne) - -## See the categorical part only using $ operator -tableOne$CatTable -summary(tableOne$CatTable) - -## See the continuous part only using $ operator -tableOne$ContTable -summary(tableOne$ContTable) - -## If your work flow includes copying to Excel and Word when writing manuscripts, -## you may benefit from the quote argument. This will quote everything so that -## Excel does not mess up the cells. -print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), - exact = c("status","stage"), cramVars = "hepato", quote = TRUE) - -## If you want to center-align values in Word, use noSpaces option. -print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), - exact = c("status","stage"), cramVars = "hepato", quote = TRUE, noSpaces = TRUE) +## See examples for CreateTableOne and svyCreateTableOne } \author{ Kazuki Yoshida, Justin Bohn } \seealso{ -\code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}}, -\code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}}, -\code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}} +\code{\link{CreateTableOne}}, \code{\link{CreateTableOne}}, \code{\link{summary.TableOne}} } diff --git a/man/print.svyCatTable.Rd b/man/print.svyCatTable.Rd new file mode 100644 index 0000000..1d18baf --- /dev/null +++ b/man/print.svyCatTable.Rd @@ -0,0 +1,61 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/print.svyCatTable.R +\name{print.svyCatTable} +\alias{print.svyCatTable} +\title{Format and print \code{svyCatTable} class objects} +\usage{ +\method{print}{svyCatTable}(x, digits = 1, pDigits = 3, quote = FALSE, + missing = FALSE, explain = TRUE, printToggle = TRUE, noSpaces = FALSE, + format = c("fp", "f", "p", "pf")[1], showAllLevels = FALSE, + cramVars = NULL, test = TRUE, exact = NULL, smd = FALSE, + CrossTable = FALSE, ...) +} +\arguments{ +\item{x}{The result of a call to the \code{\link{svyCreateCatTable}} function.} + +\item{digits}{Number of digits to print in the table.} + +\item{pDigits}{Number of digits to print for p-values (also used for standardized mean differences).} + +\item{quote}{Whether to show everything in quotes. The default is FALSE. If TRUE, everything including the row and column names are quoted so that you can copy it to Excel easily.} + +\item{missing}{Whether to show missing data information (not implemented yet, placeholder)} + +\item{explain}{Whether to add explanation to the variable names, i.e., (\%) is added to the variable names when percentage is shown.} + +\item{printToggle}{Whether to print the output. If FLASE, no output is created, and a matrix is invisibly returned.} + +\item{noSpaces}{Whether to remove spaces added for alignment. Use this option if you prefer to align numbers yourself in other software.} + +\item{format}{The default is "fp" frequency (percentage). You can also choose from "f" frequency only, "p" percentage only, and "pf" percentage (frequency).} + +\item{showAllLevels}{Whether to show all levels. FALSE by default, i.e., for 2-level categorical variables, only the higher level is shown to avoid redundant information.} + +\item{cramVars}{A character vector to specify the two-level categorical variables, for which both levels should be shown in one row.} + +\item{test}{Whether to show p-values. TRUE by default. If FALSE, only the numerical summaries are shown.} + +\item{exact}{This option is not available for tables from weighted data.} + +\item{smd}{Whether to show standardized mean differences. FALSE by default. If there are more than one contrasts, the average of all possible standardized mean differences is shown. For individual contrasts, use \code{summary}.} + +\item{CrossTable}{Whether to show the cross table objects held internally using gmodels::CrossTable function. This will give an output similar to the PROC FREQ in SAS.} + +\item{...}{For compatibility with generic. Ignored.} +} +\value{ +A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via \code{write.csv}. +} +\description{ +\code{print} method for the \code{svyCatTable} class objects created by \code{\link{svyCreateCatTable}} function. +} +\examples{ +## See the examples for svyCreateTableOne() +} +\author{ +Kazuki Yoshida +} +\seealso{ +\code{\link{svyCreateTableOne}}, \code{\link{svyCreateCatTable}}, \code{\link{summary.svyCatTable}} +} + diff --git a/man/print.svyContTable.Rd b/man/print.svyContTable.Rd new file mode 100644 index 0000000..8d7ec64 --- /dev/null +++ b/man/print.svyContTable.Rd @@ -0,0 +1,56 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/print.svyContTable.R +\name{print.svyContTable} +\alias{print.svyContTable} +\title{Format and print \code{svyContTable} class objects} +\usage{ +\method{print}{svyContTable}(x, digits = 2, pDigits = 3, quote = FALSE, + missing = FALSE, explain = TRUE, printToggle = TRUE, noSpaces = FALSE, + nonnormal = NULL, minMax = FALSE, insertLevel = FALSE, test = TRUE, + smd = FALSE, ...) +} +\arguments{ +\item{x}{Object returned by \code{\link{svyCreateContTable}} function.} + +\item{digits}{Number of digits to print in the table.} + +\item{pDigits}{Number of digits to print for p-values (also used for standardized mean differences).} + +\item{quote}{Whether to show everything in quotes. The default is FALSE. If TRUE, everything including the row and column names are quoted so that you can copy it to Excel easily.} + +\item{missing}{Whether to show missing data information (not implemented yet, placeholder)} + +\item{explain}{Whether to add explanation to the variable names, i.e., (mean (sd) or median [IQR]) is added to the variable names.} + +\item{printToggle}{Whether to print the output. If FLASE, no output is created, and a matrix is invisibly returned.} + +\item{noSpaces}{Whether to remove spaces added for alignment. Use this option if you prefer to align numbers yourself in other software.} + +\item{nonnormal}{A character vector to specify the variables for which the p-values should be those of nonparametric tests. By default all p-values are from normal assumption-based tests (oneway.test).} + +\item{minMax}{Whether to use [min,max] instead of [p25,p75] for nonnormal variables. The default is FALSE.} + +\item{insertLevel}{Whether to add an empty level column to the left of strata.} + +\item{test}{Whether to show p-values. TRUE by default. If FALSE, only the numerical summaries are shown.} + +\item{smd}{Whether to show standardized mean differences. FALSE by default. If there are more than one contrasts, the average of all possible standardized mean differences is shown. For individual contrasts, use \code{summary}.} + +\item{...}{For compatibility with generic. Ignored.} +} +\value{ +A matrix object containing what you see is also invisibly returned. This can be assinged a name and exported via \code{write.csv}. +} +\description{ +\code{print} method for the \code{svyContTable} class objects created by \code{\link{CreateContTable}} function. +} +\examples{ +## See the examples for svyCreateTableOne() +} +\author{ +Kazuki Yoshida +} +\seealso{ +\code{\link{svyCreateTableOne}}, \code{\link{svyCreateCatTable}}, \code{\link{summary.svyCatTable}} +} + diff --git a/man/summary.CatTable.Rd b/man/summary.CatTable.Rd index 047f985..41ebe64 100644 --- a/man/summary.CatTable.Rd +++ b/man/summary.CatTable.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/summary.CatTable.R \name{summary.CatTable} \alias{summary.CatTable} @@ -14,40 +14,18 @@ \item{...}{For compatibility with generic. Ignored.} } \value{ -It will print the results. +None. Results are printed. } \description{ -This method shows all the data a \code{CatTable} class object has. This includes the (optionally stratified) part with summary statistics and , if available, p-values from the approximation method test (\code{\link{chisq.test}} by default) and exact method test (\code{\link{fisher.test}} by default). +Shows all data a \code{CatTable} class object has. This includes the (optionally stratified) part with summary statistics and, if available, p-values from the approximation method test (\code{chisq.test} by default) and exact method test (\code{fisher.test} by default) and standardized mean differences of all possible pairwise contrasts. } \examples{ -## Load -library(tableone) - -## Load Mayo Clinic Primary Biliary Cirrhosis Data -library(survival) -data(pbc) -## Check variables -head(pbc) - -## Create an overall table for categorical variables -catVars <- c("status","ascites","hepato","spiders","edema","stage") -catTableOverall <- CreateCatTable(vars = catVars, data = pbc) - -## Simply typing the object name will invoke the print.CatTable method, -## which will show the sample size, frequencies and percentages. -## For 2-level variables, only the higher level is shown for simplicity. -catTableOverall - -## To further examine the variables, use the summary.CatTable method, -## which will show more details. -summary(catTableOverall) +## See examples for CreateTableOne } \author{ Kazuki Yoshida } \seealso{ -\code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}}, -\code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}}, -\code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}} +\code{\link{CreateTableOne}}, \code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, } diff --git a/man/summary.ContTable.Rd b/man/summary.ContTable.Rd index 4589dbe..71523b1 100644 --- a/man/summary.ContTable.Rd +++ b/man/summary.ContTable.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/summary.ContTable.R \name{summary.ContTable} \alias{summary.ContTable} @@ -14,40 +14,18 @@ \item{...}{For compatibility with generic. Ignored.} } \value{ -It will print the results. +None. Results are printed. } \description{ -This method shows all the data a \code{ContTable} class object has. This includes the (optionally stratified) part with summary statistics and , if available, p-values from the normal assupmtion-based test (\code{\link{oneway.test}} by default) and nonparametric test (\code{\link{kruskal.test}} by default). +Shows all data a \code{ContTable} class object has. This includes the (optionally stratified) part with summary statistics and, if available, p-values from the normal assupmtion-based test (\code{oneway.test} by default) and nonparametric test (\code{kruskal.test} by default) and standardized mean differences of all possible pairwise contrasts. } \examples{ -## Load -library(tableone) - -## Load Mayo Clinic Primary Biliary Cirrhosis Data -library(survival) -data(pbc) -## Check variables -head(pbc) - -## Create an overall table for continuous variables -contVars <- c("time","age","bili","chol","albumin","copper", - "alk.phos","ast","trig","platelet","protime") -contTableOverall <- CreateContTable(vars = contVars, data = pbc) - -## Simply typing the object name will invoke the print.ContTable method, -## which will show the sample size, means and standard deviations. -contTableOverall - -## To further examine the variables, use the summary.ContTable method, -## which will show more details. -summary(contTableOverall) +## See examples for CreateTableOne } \author{ Kazuki Yoshida } \seealso{ -\code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}}, -\code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}}, -\code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}} +\code{\link{CreateTableOne}}, \code{\link{CreateContTable}}, \code{\link{print.ContTable}} } diff --git a/man/summary.TableOne.Rd b/man/summary.TableOne.Rd index aff87be..047ba04 100644 --- a/man/summary.TableOne.Rd +++ b/man/summary.TableOne.Rd @@ -1,60 +1,31 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/summary.TableOne.R \name{summary.TableOne} \alias{summary.TableOne} -\title{Shows all results in a \code{TableOne} class object} +\title{Shows all results in a \code{(svy)TableOne} class object} \usage{ \method{summary}{TableOne}(object, digits = 1, ...) } \arguments{ -\item{object}{An object that has the \code{CatTable} class to be shown.} +\item{object}{An object that has the \code{(svy)TableOne} class to be shown.} \item{digits}{Number of digits to print.} \item{...}{For compatibility with generic. Ignored.} } \value{ -It will print the results. +None. Results are printed. } \description{ -This method shows all the data a CatTable class object has. This includes -the (optionally stratified) part with summary statistics and p-values from -the approximation method test (chisq.test by default) and exact method test -(fisher.test by default). +Shows all data a \code{(svy)TableOne} class object has. This includes the (optionally stratified) part with summary statistics and p-values and/or standardized mean differences. } \examples{ -## Load -library(tableone) - -## Load Mayo Clinic Primary Biliary Cirrhosis Data -library(survival) -data(pbc) -## Check variables -head(pbc) - -## Make categorical variables factors -varsToFactor <- c("status","trt","ascites","hepato","spiders","edema","stage") -pbc[varsToFactor] <- lapply(pbc[varsToFactor], factor) - -## Create Table 1 stratified by sex and trt -tableOne <- CreateTableOne(vars = c("time","status","age","ascites","hepato", - "spiders","edema","bili","chol","albumin", - "copper","alk.phos","ast","trig","platelet", - "protime","stage"), - strata = c("sex","trt"), data = pbc) - -## Just typing the object name will invoke the print.TableOne method -tableOne - -## Use the summary.TableOne method for detailed summary -summary(tableOne) +## See examples for CreateTableOne and svyCreateTableOne } \author{ Kazuki Yoshida } \seealso{ -\code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}}, -\code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}}, -\code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}} +\code{\link{CreateTableOne}}, \code{\link{svyCreateCatTable}} } diff --git a/man/summary.svyCatTable.Rd b/man/summary.svyCatTable.Rd new file mode 100644 index 0000000..7f3a73b --- /dev/null +++ b/man/summary.svyCatTable.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/summary.svyCatTable.R +\name{summary.svyCatTable} +\alias{summary.svyCatTable} +\title{Shows all results in a \code{svyCatTable} class object} +\usage{ +\method{summary}{svyCatTable}(object, digits = 1, ...) +} +\arguments{ +\item{object}{An object that has the \code{svyCatTable} class to be shown.} + +\item{digits}{Number of digits to print.} + +\item{...}{For compatibility with generic. Ignored.} +} +\value{ +None. Results are printed. +} +\description{ +Shows all data a \code{svyCatTable} class object has. This includes the (optionally stratified) part with summary statistics and, if available, p-values from the approximation method test (\code{chisq.test} by default) and exact method test (\code{fisher.test} by default) and standardized mean differences of all possible pairwise contrasts. +} +\examples{ +## See the examples for svyCreateTableOne() +} +\author{ +Kazuki Yoshida +} +\seealso{ +\code{\link{svyCreateTableOne}}, \code{\link{svyCreateCatTable}}, \code{\link{print.svyCatTable}} +} + diff --git a/man/summary.svyContTable.Rd b/man/summary.svyContTable.Rd new file mode 100644 index 0000000..871c532 --- /dev/null +++ b/man/summary.svyContTable.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/summary.svyContTable.R +\name{summary.svyContTable} +\alias{summary.svyContTable} +\title{Shows all results in a \code{svyContTable} class object} +\usage{ +\method{summary}{svyContTable}(object, digits = 2, ...) +} +\arguments{ +\item{object}{An object that has the \code{svyContTable} class to be shown.} + +\item{digits}{Number of digits to print.} + +\item{...}{For compatibility with generic. Ignored.} +} +\value{ +None. Results are printed. +} +\description{ +Shows all data a \code{svyContTable} class object has. This includes the (optionally stratified) part with summary statistics and, if available, p-values from the normal assupmtion-based test (\code{regTermTest} with \code{svyglm} by default) and nonparametric test (\code{svyranktest} by default) and standardized mean differences of all possible pairwise contrasts. +} +\examples{ +## See the examples for svyCreateTableOne() +} +\author{ +Kazuki Yoshida +} +\seealso{ +\code{\link{svyCreateTableOne}}, \code{\link{svyCreateContTable}}, \code{\link{print.svyContTable}} +} + diff --git a/man/svyCreateCatTable.Rd b/man/svyCreateCatTable.Rd new file mode 100644 index 0000000..6c77446 --- /dev/null +++ b/man/svyCreateCatTable.Rd @@ -0,0 +1,42 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/svyCreateCatTable.R +\name{svyCreateCatTable} +\alias{svyCreateCatTable} +\title{Create an object summarizing categorical variables for weighted data} +\usage{ +svyCreateCatTable(vars, strata, data, includeNA = FALSE, test = TRUE, + testApprox = svyTestChisq, argsApprox = NULL, smd = TRUE) +} +\arguments{ +\item{vars}{Variable(s) to be summarized given as a character vector.} + +\item{strata}{Stratifying (grouping) variable name(s) given as a character vector. If omitted, the overall results are returned.} + +\item{data}{A survey design object in which these variables exist. All variables (both vars and strata) must be in this survey design object. It is created with the \code{svydesign} function in the \code{survey} package.} + +\item{includeNA}{If TRUE, NA is handled as a regular factor level rather than missing. NA is shown as the last factor level in the table. Only effective for categorical variables.} + +\item{test}{If TRUE, as in the default and there are more than two groups, groupwise comparisons are performed. Both tests that require the large sample approximation and exact tests are performed. Either one of the result can be obtained from the print method.} + +\item{testApprox}{A function used to perform the large sample approximation based tests. The default is \code{svychisq}.} + +\item{argsApprox}{A named list of arguments passed to the function specified in testApprox.} + +\item{smd}{If TRUE, as in the default and there are more than two groups, standardized mean differences for all pairwise comparisons are calculated.} +} +\value{ +An object of class \code{svyCatTable}. +} +\description{ +Create an object summarizing categorical variables optionally stratifying by one or more startifying variables and performing statistical tests. Usually, \code{\link{svyCreateTableOne}} should be used as the universal frontend for both continuous and categorical data. +} +\examples{ +## See the examples for svyCreateTableOne() +} +\author{ +Kazuki Yoshida +} +\seealso{ +\code{\link{svyCreateTableOne}}, \code{\link{print.svyCatTable}}, \code{\link{summary.svyCatTable}}, +} + diff --git a/man/svyCreateContTable.Rd b/man/svyCreateContTable.Rd new file mode 100644 index 0000000..6537f33 --- /dev/null +++ b/man/svyCreateContTable.Rd @@ -0,0 +1,45 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/svyCreateContTable.R +\name{svyCreateContTable} +\alias{svyCreateContTable} +\title{Create an object summarizing continous variables for weighted dataset} +\usage{ +svyCreateContTable(vars, strata, data, test = TRUE, + testNormal = svyTestNormal, argsNormal = list(method = "Wald"), + testNonNormal = svyTestNonNormal, argsNonNormal = NULL, smd = TRUE) +} +\arguments{ +\item{vars}{Variable(s) to be summarized given as a character vector.} + +\item{strata}{Stratifying (grouping) variable name(s) given as a character vector. If omitted, the overall results are returned.} + +\item{data}{A survey design object in which these variables exist. All variables (both vars and strata) must be in this survey design object. It is created with the \code{svydesign} function in the \code{survey} package.} + +\item{test}{If TRUE, as in the default and there are more than two groups, groupwise comparisons are performed. Both tests that assume normality and tests that do not are performed. Either one of the result can be obtained from the print method.} + +\item{testNormal}{A function used to perform the normal assumption based tests. The default is multiple degrees of freedom test using \code{svyglm} and \code{regTermTest}. This is equivalent of the \code{svyttest} when there are only two groups.} + +\item{argsNormal}{A named list of arguments passed to the function specified in \code{testNormal}.} + +\item{testNonNormal}{A function used to perform the nonparametric tests. The default is \code{svyranktest}.} + +\item{argsNonNormal}{A named list of arguments passed to the function specified in \code{testNonNormal}.} + +\item{smd}{If TRUE, as in the default and there are more than two groups, standardized mean differences for all pairwise comparisons are calculated.} +} +\value{ +An object of class \code{svyContTable}. +} +\description{ +Create an object summarizing continous variables optionally stratifying by one or more startifying variables and performing statistical tests. Usually, \code{\link{svyCreateTableOne}} should be used as the universal frontend for both continuous and categorical data. +} +\examples{ +## See the examples for svyCreateTableOne() +} +\author{ +Kazuki Yoshida +} +\seealso{ +\code{\link{svyCreateTableOne}}, \code{\link{print.svyContTable}}, \code{\link{summary.svyContTable}}, +} + diff --git a/man/svyCreateTableOne.Rd b/man/svyCreateTableOne.Rd new file mode 100644 index 0000000..d425eef --- /dev/null +++ b/man/svyCreateTableOne.Rd @@ -0,0 +1,106 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/svyCreateTableOne.R +\name{svyCreateTableOne} +\alias{svyCreateTableOne} +\title{Create an object summarizing both continuous and categorical variables for weighted data} +\usage{ +svyCreateTableOne(vars, strata, data, factorVars, includeNA = FALSE, + test = TRUE, testApprox = svyTestChisq, argsApprox = NULL, + testNormal = svyTestNormal, argsNormal = list(method = "Wald"), + testNonNormal = svyTestNonNormal, argsNonNormal = NULL, smd = TRUE) +} +\arguments{ +\item{vars}{Variables to be summarized given as a character vector. Factors are handled as categorical variables, whereas numeric variables are handled as continuous variables. If empty, all variables in the survey design object specified in the data argument are used.} + +\item{strata}{Stratifying (grouping) variable name(s) given as a character vector. If omitted, the overall results are returned.} + +\item{data}{A survey design object in which these variables exist. All variables (both vars and strata) must be in this survey design object. It is created with the \code{svydesign} function in the \code{survey} package.} + +\item{factorVars}{Numerically coded variables that should be handled as categorical variables given as a character vector. If omitted, only factors are considered categorical variables. If all categorical variables in the dataset are already factors, this option is not necessary. The variables specified here must also be specified in the \code{vars} argument.} + +\item{includeNA}{If TRUE, NA is handled as a regular factor level rather than missing. NA is shown as the last factor level in the table. Only effective for categorical variables.} + +\item{test}{If TRUE, as in the default and there are more than two groups, groupwise comparisons are performed.} + +\item{testApprox}{A function used to perform the large sample approximation based tests. The default is \code{svychisq}.} + +\item{argsApprox}{A named list of arguments passed to the function specified in testApprox.} + +\item{testNormal}{A function used to perform the normal assumption based tests. The default is multiple degrees of freedom test using \code{svyglm} and \code{regTermTest}. This is equivalent of the \code{svyttest} when there are only two groups.} + +\item{argsNormal}{A named list of arguments passed to the function specified in \code{testNormal}.} + +\item{testNonNormal}{A function used to perform the nonparametric tests. The default is \code{svyranktest}.} + +\item{argsNonNormal}{A named list of arguments passed to the function specified in \code{testNonNormal}.} + +\item{smd}{If TRUE, as in the default and there are more than two groups, standardized mean differences for all pairwise comparisons are calculated.} +} +\value{ +An object of class \code{svyTableOne}, which is a list of three objects. + +\item{ContTable}{an object of class \code{svyContTable}, containing continuous variables only} + +\item{CatTable}{ an object of class \code{svyCatTable}, containing categorical variables only} + +\item{MetaData}{list of metadata regarding variables} +} +\description{ +Create an object summarizing all baseline variables (both continuous and categorical) optionally stratifying by one or more startifying variables and performing statistical tests. The object gives a table that is easy to use in medical research papers. +} +\details{ +See the details for \code{\link{CreateTableOne}}. +} +\examples{ +## Load packages +library(tableone) +library(survey) + +## Create a weighted survey design object +data(nhanes) +nhanesSvy <- svydesign(ids = ~ SDMVPSU, strata = ~ SDMVSTRA, weights = ~ WTMEC2YR, + nest = TRUE, data = nhanes) + +## Create a table object +## factorVars are converted to factors; no need for variables already factors +## strata will stratify summaries; leave it unspecified for overall summaries +tab1 <- svyCreateTableOne(vars = c("HI_CHOL","race","agecat","RIAGENDR"), + strata = "RIAGENDR", data = nhanesSvy, + factorVars = c("race","RIAGENDR")) + +## Detailed output +summary(tab1) + +## Default formatted printing +tab1 + +## nonnormal specifies variables to be shown as median [IQR] +print(tab1, nonnormal = "HI_CHOL", contDigits = 3, catDigits = 2, + pDigits = 4, smd = TRUE) + +## minMax changes it to median [min, max] +print(tab1, nonnormal = "HI_CHOL", minMax = TRUE, contDigits = 3, + catDigits = 2, pDigits = 4, smd = TRUE) + +## showAllLevels can be used tow show levels for all categorical variables +print(tab1, showAllLevels = TRUE, smd = TRUE) + +## To see all printing options +?print.TableOne + +## To examine categorical variables only +tab1$CatTable + +## To examine continuous variables only +tab1$ContTable + +## If SMDs are needed as numericals, use ExtractSmd() +ExtractSmd(tab1) +} +\author{ +Kazuki Yoshida +} +\seealso{ +\code{\link{print.TableOne}}, \code{\link{summary.TableOne}} +} + diff --git a/man/tableone-package.Rd b/man/tableone-package.Rd index 50addc7..81a6868 100644 --- a/man/tableone-package.Rd +++ b/man/tableone-package.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/tableone-package.R \docType{package} \name{tableone-package} @@ -6,84 +6,30 @@ \alias{tableone-package} \title{Create "Table 1" to describe baseline characteristics} \description{ -This package creates "Table 1", i.e., description of baseline patient characteristics, which is essential in every medical research. This package provides functions to create such summaries for continuous and categorical variables, optionally with subgroup comparisons. The package was insipired by and based on descriptive statistics functions in Deducer, a Java-based GUI package by Ian Fellows. This package does not require GUI or Java, and intended for command-line users. +Creates "Table 1", i.e., description of baseline patient characteristics, which is essential in every medical research. Supports both continuous and categorical variables, as well as p-values and standardized mean differences. Weighted data are supported via the survey package. See github for a screencast. tableone was inspired by descriptive statistics functions in Deducer , a Java-based GUI package by Ian Fellows. This package does not require GUI or Java, and intended for command-line users. Most important functions are \code{\link{CreateTableOne}} and \code{\link{svyCreateTableOne}}. } \note{ -Special Thanks: +Acknowledgement: -Ian Fellows for developing the Deducer package, which this package is based on. +Ian Fellows for developing the \code{deducer} package, which this package is based on. -Hadley Wickham for packaging advice and for creating tools this package was made with (roxygen2, devtools, testthat). +Hadley Wickham for packaging advice and for creating tools this package was made with (\code{roxygen2}, \code{devtools}, \code{testthat}). Yoshinobu Kanda for design advice and for integration into \code{RcmdrPlugin.EZR}. -jomuller for a bug report and fix suggestion regarding handling of ordered factors. +H.Tachibana and Hiroki Matsui for inputs regarding standardized mean differences. -Raja Sriswan Mamidi for suggestions regarding alignment. +jomuller, Raja Sriswan Mamidi, and Atsushi Shiraishi for bug reports and/or feature suggestions. Members of the Facebook Organization of R Users for Medical Statistics in Japan (FORUMS-J) for testing pre-release versions and suggestions. + Developmental repository is on github. Your contributions are appreciated. -https://github.com/kaz-yos/tableone +\url{https://github.com/kaz-yos/tableone} } \examples{ -## Load -library(tableone) - -## Load Mayo Clinic Primary Biliary Cirrhosis Data -library(survival) -data(pbc) -## Check variables -head(pbc) - -## List numerically coded categorical variables for later conversion. -## Factor variables are automatically handled as categorical variables. -factorVars <- c("status","trt","ascites","hepato","spiders","edema","stage") - -## Create a variable list -dput(names(pbc)) # This shows a character vector-creating syntax. -vars <- c("time","status","age","sex","ascites","hepato", - "spiders","edema","bili","chol","albumin", - "copper","alk.phos","ast","trig","platelet", - "protime","stage") - -## Create Table 1 stratified by trt. Use factorVars to convert numerically -## coded categorical variables as factors without changing the dataset. -tableOne <- CreateTableOne(vars = vars, strata = c("trt"), data = pbc, - factorVars = factorVars) - -## Just typing the object name will invoke the print.TableOne method -tableOne - -## Specifying nonnormal variables will show the variables appropriately, -## and show nonparametric test p-values. Specify variables in the exact -## argument to obtain the exact test p-values. For two-level categorical -## variables specified in cramVars, both levels are shown. Use minMax -## argument to show median [min, max] for nonnormal variables. -print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), - exact = c("status","stage"), cramVars = "sex") - -## Use the summary.TableOne method for detailed summary -summary(tableOne) - -## See the categorical part only using $ operator -tableOne$CatTable -summary(tableOne$CatTable) - -## See the continuous part only using $ operator -tableOne$ContTable -summary(tableOne$ContTable) - -## If your work flow includes copying to Excel and Word when writing manuscripts, -## you may benefit from the quote argument. This will quote everything so that -## Excel does not mess up the cells. -print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), - exact = c("status","stage"), cramVars = "sex", quote = TRUE) - -## If you want to center-align values in Word, use noSpaces option. -print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), - exact = c("status","stage"), cramVars = "sex", quote = TRUE, noSpaces = TRUE) +## See examples for CreateTableOne and svyCreateTableOne } \author{ Kazuki Yoshida, Justin Bohn @@ -91,9 +37,6 @@ Kazuki Yoshida, Justin Bohn Maintainer: Kazuki Yoshida } \seealso{ -\code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}}, -\code{\link{CreateContTable}}, \code{\link{print.ContTable}}, \code{\link{summary.ContTable}}, -\code{\link{CreateCatTable}}, \code{\link{print.CatTable}}, \code{\link{summary.CatTable}}, -\code{\link{ShowRegTable}} +\code{\link{CreateTableOne}}, \code{\link{svyCreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}}, \code{\link{ShowRegTable}} } diff --git a/revdep_check.txt b/revdep_check.txt new file mode 100644 index 0000000..e7eff2b --- /dev/null +++ b/revdep_check.txt @@ -0,0 +1,55 @@ +$check_dir +[1] "/var/folders/r8/dhc1ygjd6332_fbgphmsbm800000gn/T//Rtmp5vf49d/check_cran6b7316c7d6f" + +$libpath +[1] "/var/folders/r8/dhc1ygjd6332_fbgphmsbm800000gn/T//Rtmp5vf49d/R-lib" + +$pkg +$package +[1] "tableone" + +$type +[1] "Package" + +$title +[1] "Create \"Table 1\" to Describe Baseline Characteristics" + +$version +[1] "0.7.0" + +$date +[1] "2015-08-07" + +$author +[1] "Kazuki Yoshida, Justin Bohn." + +$maintainer +[1] "Kazuki Yoshida " + +$description +[1] "Creates \"Table 1\", i.e., description of baseline patient\ncharacteristics, which is essential in every medical research.\nSupports both continuous and categorical variables, as well as\np-values and standardized mean differences. Weighted data are\nsupported via the survey package. See github for a screencast.\ntableone was inspired by descriptive statistics functions in\nDeducer , a Java-based GUI package by Ian Fellows. This package\ndoes not require GUI or Java, and intended for command-line users." + +$license +[1] "GPL-2" + +$imports +[1] "\nsurvey,\nMASS,\ne1071,\nzoo,\ngmodels" + +$suggests +[1] "\nsurvival,\ntestthat,\nMatrix,\ndummies,\nMatching,\nreshape2,\nggplot2,\nknitr" + +$url +[1] "https://github.com/kaz-yos/tableone" + +$vignettebuilder +[1] "knitr" + +$path +[1] "/Users/kazuki/Documents/programming/r/tableone" + +attr(,"class") +[1] "package" + +$deps +[1] "RcmdrPlugin.EZR" + diff --git a/tableone.Rcheck/tableone-Ex.Rout b/tableone.Rcheck/tableone-Ex.Rout index 485d6fe..2d42704 100644 --- a/tableone.Rcheck/tableone-Ex.Rout +++ b/tableone.Rcheck/tableone-Ex.Rout @@ -1,6 +1,6 @@ -R version 3.1.2 (2014-10-31) -- "Pumpkin Helmet" -Copyright (C) 2014 The R Foundation for Statistical Computing +R version 3.2.1 (2015-06-18) -- "World-Famous Astronaut" +Copyright (C) 2015 The R Foundation for Statistical Computing Platform: x86_64-apple-darwin13.4.0 (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. @@ -53,7 +53,6 @@ Type 'q()' to quit R. > > ## Load Mayo Clinic Primary Biliary Cirrhosis Data > library(survival) -Loading required package: splines > data(pbc) > ## Check variables > head(pbc) @@ -222,7 +221,7 @@ strata: Overall > > ## print now includes p-values which are by default calculated by chisq.test. > ## It is formatted at the decimal place specified by the pDigits argument -> ## (3 by default). It does <0.001 for you. +> ## (3 by default). It is formatted like <0.001 if very small. > catTableBySexTrt Stratified by sex:trt m:1 f:1 m:2 f:2 p test @@ -381,6 +380,22 @@ hepato 0.20805680 0.20480121 spiders 0.08898575 0.08569292 edema 0.90584197 0.84833223 stage 0.64630048 NA + +Standardize mean differences + average 1 vs 2 1 vs 3 1 vs 4 2 vs 3 2 vs 4 +status 0.5770647 0.8748771 0.79644734 0.83097631 0.4375075 0.06043662 +ascites 0.1755851 0.1845375 0.30219043 0.04452096 0.1211310 0.14105454 +hepato 0.1598214 0.2544151 0.05802589 0.02073917 0.3135916 0.23336861 +spiders 0.4018377 0.3999925 0.25073566 0.42200838 0.6471968 0.02154469 +edema 0.2122338 0.1624118 0.37625250 0.18063879 0.2420277 0.04705137 +stage 0.2773411 0.1333844 0.21945509 0.35632272 0.3000785 0.26275030 + 3 vs 4 +status 0.46214324 +ascites 0.26007640 +hepato 0.07878788 +spiders 0.66954801 +edema 0.26502087 +stage 0.39205566 > > ## If your work flow includes copying to Excel and Word when writing manuscripts, > ## you may benefit from the quote argument. This will quote everything so that @@ -472,7 +487,7 @@ stage 0.64630048 NA > base::cat("CreateCatTable", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") > cleanEx() -detaching ‘package:survival’, ‘package:splines’ +detaching ‘package:survival’ > nameEx("CreateContTable") > ### * CreateContTable @@ -491,7 +506,6 @@ detaching ‘package:survival’, ‘package:splines’ > > ## Load Mayo Clinic Primary Biliary Cirrhosis Data > library(survival) -Loading required package: splines > data(pbc) > ## Check variables > head(pbc) @@ -805,6 +819,32 @@ ast 5.982898e-01 5.086616e-01 trig 3.701073e-01 5.827726e-02 platelet 3.624740e-01 4.451277e-01 protime 1.365463e-01 8.114383e-02 + +Standardize mean differences + average 1 vs 2 1 vs 3 1 vs 4 2 vs 3 2 vs 4 +time 0.1556365 0.22071813 0.27083134 0.15677410 0.08450703 0.06369595 +age 0.5214974 0.40993004 0.13353519 0.70700091 0.60864596 0.30853876 +bili 0.1352025 0.04023105 0.11373946 0.18444097 0.04265701 0.18855913 +chol 0.2950579 0.21756422 0.62108339 0.09219212 0.33996564 0.09881382 +albumin 0.1637683 0.30878937 0.28262444 0.26228013 0.01074465 0.06658036 +copper 0.5416574 0.94378898 0.50727571 0.86116098 0.46259003 0.11048709 +alk.phos 0.1633259 0.23603622 0.30907333 0.23332162 0.09290687 0.00733728 +ast 0.1818681 0.18797132 0.34614452 0.04399580 0.12602823 0.12777729 +trig 0.2940632 0.38944815 0.63360898 0.33242327 0.09194315 0.09085446 +platelet 0.2340433 0.30251429 0.08655951 0.37226635 0.25448008 0.05557853 +protime 0.3283721 0.24596092 0.40162254 0.08476002 0.66430044 0.12924857 + 3 vs 4 +time 0.13729269 +age 0.96133382 +bili 0.24158721 +chol 0.40072828 +albumin 0.05159074 +copper 0.36464167 +alk.phos 0.10128012 +ast 0.25929154 +trig 0.22610097 +platelet 0.33286083 +protime 0.44433997 > > ## If your work flow includes copying to Excel and Word when writing manuscripts, > ## you may benefit from the quote argument. This will quote everything so that @@ -932,7 +972,7 @@ protime 1.365463e-01 8.114383e-02 > base::cat("CreateContTable", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") > cleanEx() -detaching ‘package:survival’, ‘package:splines’ +detaching ‘package:survival’ > nameEx("CreateTableOne") > ### * CreateTableOne @@ -941,7 +981,7 @@ detaching ‘package:survival’, ‘package:splines’ > > base::assign(".ptime", proc.time(), pos = "CheckExEnv") > ### Name: CreateTableOne -> ### Title: Create an object summarizing both categorical and continuous +> ### Title: Create an object summarizing both continuous and categorical > ### variables > ### Aliases: CreateTableOne > @@ -952,7 +992,6 @@ detaching ‘package:survival’, ‘package:splines’ > > ## Load Mayo Clinic Primary Biliary Cirrhosis Data > library(survival) -Loading required package: splines > data(pbc) > ## Check variables > head(pbc) @@ -992,7 +1031,7 @@ c("id", "time", "status", "trt", "age", "sex", "ascites", "hepato", > tableOne Stratified by trt 1 2 p test - n 158 154 + n 158 154 time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883 status (%) 0.894 0 83 (52.5) 85 (55.2) @@ -1000,7 +1039,7 @@ c("id", "time", "status", "trt", "age", "sex", "ascites", "hepato", 2 65 (41.1) 60 (39.0) age (mean (sd)) 51.42 (11.01) 48.58 (9.96) 0.018 sex = f (%) 137 (86.7) 139 (90.3) 0.421 - ascites = 1 (%) 14 (8.9) 10 (6.5) 0.567 + ascites = 1 (%) 14 ( 8.9) 10 ( 6.5) 0.567 hepato = 1 (%) 73 (46.2) 87 (56.5) 0.088 spiders = 1 (%) 45 (28.5) 45 (29.2) 0.985 edema (%) 0.877 @@ -1024,12 +1063,13 @@ c("id", "time", "status", "trt", "age", "sex", "ascites", "hepato", > > ## Specifying nonnormal variables will show the variables appropriately, > ## and show nonparametric test p-values. Specify variables in the exact -> ## argument to obtain the exact test p-values. +> ## argument to obtain the exact test p-values. cramVars can be used to +> ## show both levels for a 2-level categorical variables. > print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), -+ exact = c("status","stage")) ++ exact = c("status","stage"), cramVars = "hepato", smd = TRUE) Stratified by trt 1 2 - n 158 154 + n 158 154 time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) status (%) 0 83 (52.5) 85 (55.2) @@ -1037,8 +1077,8 @@ c("id", "time", "status", "trt", "age", "sex", "ascites", "hepato", 2 65 (41.1) 60 (39.0) age (mean (sd)) 51.42 (11.01) 48.58 (9.96) sex = f (%) 137 (86.7) 139 (90.3) - ascites = 1 (%) 14 (8.9) 10 (6.5) - hepato = 1 (%) 73 (46.2) 87 (56.5) + ascites = 1 (%) 14 ( 8.9) 10 ( 6.5) + hepato = 0/1 (%) 85/73 (53.8/46.2) 67/87 (43.5/56.5) spiders = 1 (%) 45 (28.5) 45 (29.2) edema (%) 0 132 (83.5) 131 (85.1) @@ -1059,36 +1099,36 @@ c("id", "time", "status", "trt", "age", "sex", "ascites", "hepato", 3 56 (35.4) 64 (41.6) 4 55 (34.8) 54 (35.1) Stratified by trt - p test - n - time (mean (sd)) 0.883 - status (%) 0.884 exact - 0 - 1 - 2 - age (mean (sd)) 0.018 - sex = f (%) 0.421 - ascites = 1 (%) 0.567 - hepato = 1 (%) 0.088 - spiders = 1 (%) 0.985 - edema (%) 0.877 - 0 - 0.5 - 1 - bili (median [IQR]) 0.842 nonnorm - chol (median [IQR]) 0.544 nonnorm - albumin (mean (sd)) 0.874 - copper (median [IQR]) 0.717 nonnorm - alk.phos (median [IQR]) 0.812 nonnorm - ast (mean (sd)) 0.460 - trig (median [IQR]) 0.370 nonnorm - platelet (mean (sd)) 0.555 - protime (mean (sd)) 0.197 - stage (%) 0.205 exact - 1 - 2 - 3 - 4 + p test SMD + n + time (mean (sd)) 0.883 0.017 + status (%) 0.884 exact 0.054 + 0 + 1 + 2 + age (mean (sd)) 0.018 0.270 + sex = f (%) 0.421 0.111 + ascites = 1 (%) 0.567 0.089 + hepato = 0/1 (%) 0.088 0.207 + spiders = 1 (%) 0.985 0.016 + edema (%) 0.877 0.058 + 0 + 0.5 + 1 + bili (median [IQR]) 0.842 nonnorm 0.171 + chol (median [IQR]) 0.544 nonnorm 0.038 + albumin (mean (sd)) 0.874 0.018 + copper (median [IQR]) 0.717 nonnorm <0.001 + alk.phos (median [IQR]) 0.812 nonnorm 0.037 + ast (mean (sd)) 0.460 0.084 + trig (median [IQR]) 0.370 nonnorm 0.017 + platelet (mean (sd)) 0.555 0.067 + protime (mean (sd)) 0.197 0.146 + stage (%) 0.205 exact 0.246 + 1 + 2 + 3 + 4 > > ## Use the summary.TableOne method for detailed summary > summary(tableOne) @@ -1137,6 +1177,20 @@ trig 0.88604213 0.36980434 platelet 0.55451136 0.45482564 protime 0.19714026 0.58802048 +Standardize mean differences + 1 vs 2 +time 0.0166658751 +age 0.2702619258 +bili 0.1710905651 +chol 0.0382210537 +albumin 0.0180021838 +copper 0.0001200022 +alk.phos 0.0365323630 +ast 0.0837836058 +trig 0.0170615337 +platelet 0.0674763888 +protime 0.1460939117 + ======================================================================================= ### Summary of categorical variables ### @@ -1206,6 +1260,16 @@ hepato 0.08820884 0.07137522 spiders 0.98466036 0.90113734 edema 0.87681949 0.89370131 stage 0.20129629 0.20455558 + +Standardize mean differences + 1 vs 2 +status 0.05375763 +sex 0.11141161 +ascites 0.08900618 +hepato 0.20699413 +spiders 0.01632844 +edema 0.05811659 +stage 0.24600834 > > ## See the categorical part only using $ operator > tableOne$CatTable @@ -1295,6 +1359,16 @@ hepato 0.08820884 0.07137522 spiders 0.98466036 0.90113734 edema 0.87681949 0.89370131 stage 0.20129629 0.20455558 + +Standardize mean differences + 1 vs 2 +status 0.05375763 +sex 0.11141161 +ascites 0.08900618 +hepato 0.20699413 +spiders 0.01632844 +edema 0.05811659 +stage 0.24600834 > > ## See the continuous part only using $ operator > tableOne$ContTable @@ -1378,6 +1452,20 @@ ast 0.45969842 0.45892358 trig 0.88604213 0.36980434 platelet 0.55451136 0.45482564 protime 0.19714026 0.58802048 + +Standardize mean differences + 1 vs 2 +time 0.0166658751 +age 0.2702619258 +bili 0.1710905651 +chol 0.0382210537 +albumin 0.0180021838 +copper 0.0001200022 +alk.phos 0.0365323630 +ast 0.0837836058 +trig 0.0170615337 +platelet 0.0674763888 +protime 0.1460939117 > > ## If your work flow includes copying to Excel and Word when writing manuscripts, > ## you may benefit from the quote argument. This will quote everything so that @@ -1386,15 +1474,15 @@ protime 0.19714026 0.58802048 + exact = c("status","stage"), quote = TRUE) "Stratified by trt" "" "1" - "n" "158" + "n" " 158" "time (mean (sd))" "2015.62 (1094.12)" - "status (%)" " " + "status (%)" " " " 0" " 83 (52.5) " " 1" " 10 ( 6.3) " " 2" " 65 (41.1) " "age (mean (sd))" " 51.42 (11.01)" "sex = f (%)" " 137 (86.7) " - "ascites = 1 (%)" " 14 (8.9) " + "ascites = 1 (%)" " 14 ( 8.9) " "hepato = 1 (%)" " 73 (46.2) " "spiders = 1 (%)" " 45 (28.5) " "edema (%)" " " @@ -1410,22 +1498,22 @@ protime 0.19714026 0.58802048 "trig (median [IQR])" " 106.00 [84.50, 146.00]" "platelet (mean (sd))" " 258.75 (100.32)" "protime (mean (sd))" " 10.65 (0.85)" - "stage (%)" " " + "stage (%)" " " " 1" " 12 ( 7.6) " " 2" " 35 (22.2) " " 3" " 56 (35.4) " " 4" " 55 (34.8) " "Stratified by trt" "" "2" "p" "test" - "n" "154" "" "" + "n" " 154" "" "" "time (mean (sd))" "1996.86 (1155.93)" " 0.883" "" - "status (%)" " " " 0.884" "exact" + "status (%)" " " " 0.884" "exact" " 0" " 85 (55.2) " "" "" " 1" " 9 ( 5.8) " "" "" " 2" " 60 (39.0) " "" "" "age (mean (sd))" " 48.58 (9.96)" " 0.018" "" "sex = f (%)" " 139 (90.3) " " 0.421" "" - "ascites = 1 (%)" " 10 (6.5) " " 0.567" "" + "ascites = 1 (%)" " 10 ( 6.5) " " 0.567" "" "hepato = 1 (%)" " 87 (56.5) " " 0.088" "" "spiders = 1 (%)" " 45 (29.2) " " 0.985" "" "edema (%)" " " " 0.877" "" @@ -1441,7 +1529,7 @@ protime 0.19714026 0.58802048 "trig (median [IQR])" " 113.00 [84.50, 155.00]" " 0.370" "nonnorm" "platelet (mean (sd))" " 265.20 (90.73)" " 0.555" "" "protime (mean (sd))" " 10.80 (1.14)" " 0.197" "" - "stage (%)" " " " 0.205" "exact" + "stage (%)" " " " 0.205" "exact" " 1" " 4 ( 2.6) " "" "" " 2" " 32 (20.8) " "" "" " 3" " 64 (41.6) " "" "" @@ -1513,14 +1601,43 @@ protime 0.19714026 0.58802048 " 3" "64 (41.6)" "" "" " 4" "54 (35.1)" "" "" > +> ## If SMDs are needed as numericals, use ExtractSmd() +> ExtractSmd(tableOne) + time status age sex ascites hepato +0.0166658751 0.0537576290 0.2702619258 0.1114116116 0.0890061759 0.2069941350 + spiders edema bili chol albumin copper +0.0163284440 0.0581165880 0.1710905651 0.0382210537 0.0180021838 0.0001200022 + alk.phos ast trig platelet protime stage +0.0365323630 0.0837836058 0.0170615337 0.0674763888 0.1460939117 0.2460083410 +> > > > base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") > base::cat("CreateTableOne", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") > cleanEx() -detaching ‘package:survival’, ‘package:splines’ +detaching ‘package:survival’ +> nameEx("ExtractSmd") +> ### * ExtractSmd +> +> flush(stderr()); flush(stdout()) +> +> base::assign(".ptime", proc.time(), pos = "CheckExEnv") +> ### Name: ExtractSmd +> ### Title: Extract standardized mean differences from a (svy)TableOne +> ### object +> ### Aliases: ExtractSmd +> +> ### ** Examples +> +> ## See examples for CreateTableOne and svyCreateTableOne +> +> +> +> base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") +> base::cat("ExtractSmd", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") +> cleanEx() > nameEx("ShowRegTable") > ### * ShowRegTable > @@ -1538,7 +1655,6 @@ detaching ‘package:survival’, ‘package:splines’ > > ## Load Mayo Clinic Primary Biliary Cirrhosis Data > library(survival) -Loading required package: splines > data(pbc) > ## Check variables > head(pbc) @@ -1563,7 +1679,7 @@ Loading required package: splines > > ## Show the simple table > ShowRegTable(objCoxph) - exp(beta) [confint] p + exp(coef) [confint] p trt 1.03 [0.72, 1.47] 0.891 age 1.03 [1.01, 1.05] 0.005 albumin 0.26 [0.17, 0.41] <0.001 @@ -1571,7 +1687,7 @@ ascites 2.93 [1.68, 5.11] <0.001 > > ## Show with quote to ease copy and paste > ShowRegTable(objCoxph, quote = TRUE) - "exp(beta) [confint]" "p" + "exp(coef) [confint]" "p" "trt" "1.03 [0.72, 1.47]" " 0.891" "age" "1.03 [1.01, 1.05]" " 0.005" "albumin" "0.26 [0.17, 0.41]" "<0.001" @@ -1583,7 +1699,7 @@ ascites 2.93 [1.68, 5.11] <0.001 > base::cat("ShowRegTable", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") > cleanEx() -detaching ‘package:survival’, ‘package:splines’ +detaching ‘package:survival’ > nameEx("print.CatTable") > ### * print.CatTable @@ -1592,7 +1708,7 @@ detaching ‘package:survival’, ‘package:splines’ > > base::assign(".ptime", proc.time(), pos = "CheckExEnv") > ### Name: print.CatTable -> ### Title: Format and print the 'CatTable' class objects +> ### Title: Format and print 'CatTable' class objects > ### Aliases: print.CatTable > > ### ** Examples @@ -1602,7 +1718,6 @@ detaching ‘package:survival’, ‘package:splines’ > > ## Load Mayo Clinic Primary Biliary Cirrhosis Data > library(survival) -Loading required package: splines > data(pbc) > ## Check variables > head(pbc) @@ -1929,6 +2044,22 @@ hepato 0.20805680 0.20480121 spiders 0.08898575 0.08569292 edema 0.90584197 0.84833223 stage 0.64630048 NA + +Standardize mean differences + average 1 vs 2 1 vs 3 1 vs 4 2 vs 3 2 vs 4 +status 0.5770647 0.8748771 0.79644734 0.83097631 0.4375075 0.06043662 +ascites 0.1755851 0.1845375 0.30219043 0.04452096 0.1211310 0.14105454 +hepato 0.1598214 0.2544151 0.05802589 0.02073917 0.3135916 0.23336861 +spiders 0.4018377 0.3999925 0.25073566 0.42200838 0.6471968 0.02154469 +edema 0.2122338 0.1624118 0.37625250 0.18063879 0.2420277 0.04705137 +stage 0.2773411 0.1333844 0.21945509 0.35632272 0.3000785 0.26275030 + 3 vs 4 +status 0.46214324 +ascites 0.26007640 +hepato 0.07878788 +spiders 0.66954801 +edema 0.26502087 +stage 0.39205566 > > ## If your work flow includes copying to Excel and Word when writing manuscripts, > ## you may benefit from the quote argument. This will quote everything so that @@ -2020,7 +2151,7 @@ stage 0.64630048 NA > base::cat("print.CatTable", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") > cleanEx() -detaching ‘package:survival’, ‘package:splines’ +detaching ‘package:survival’ > nameEx("print.ContTable") > ### * print.ContTable @@ -2029,7 +2160,7 @@ detaching ‘package:survival’, ‘package:splines’ > > base::assign(".ptime", proc.time(), pos = "CheckExEnv") > ### Name: print.ContTable -> ### Title: Format and print the 'ContTable' class objects +> ### Title: Format and print 'ContTable' class objects > ### Aliases: print.ContTable > > ### ** Examples @@ -2039,7 +2170,6 @@ detaching ‘package:survival’, ‘package:splines’ > > ## Load Mayo Clinic Primary Biliary Cirrhosis Data > library(survival) -Loading required package: splines > data(pbc) > ## Check variables > head(pbc) @@ -2412,6 +2542,32 @@ ast 5.982898e-01 5.086616e-01 trig 3.701073e-01 5.827726e-02 platelet 3.624740e-01 4.451277e-01 protime 1.365463e-01 8.114383e-02 + +Standardize mean differences + average 1 vs 2 1 vs 3 1 vs 4 2 vs 3 2 vs 4 +time 0.1556365 0.22071813 0.27083134 0.15677410 0.08450703 0.06369595 +age 0.5214974 0.40993004 0.13353519 0.70700091 0.60864596 0.30853876 +bili 0.1352025 0.04023105 0.11373946 0.18444097 0.04265701 0.18855913 +chol 0.2950579 0.21756422 0.62108339 0.09219212 0.33996564 0.09881382 +albumin 0.1637683 0.30878937 0.28262444 0.26228013 0.01074465 0.06658036 +copper 0.5416574 0.94378898 0.50727571 0.86116098 0.46259003 0.11048709 +alk.phos 0.1633259 0.23603622 0.30907333 0.23332162 0.09290687 0.00733728 +ast 0.1818681 0.18797132 0.34614452 0.04399580 0.12602823 0.12777729 +trig 0.2940632 0.38944815 0.63360898 0.33242327 0.09194315 0.09085446 +platelet 0.2340433 0.30251429 0.08655951 0.37226635 0.25448008 0.05557853 +protime 0.3283721 0.24596092 0.40162254 0.08476002 0.66430044 0.12924857 + 3 vs 4 +time 0.13729269 +age 0.96133382 +bili 0.24158721 +chol 0.40072828 +albumin 0.05159074 +copper 0.36464167 +alk.phos 0.10128012 +ast 0.25929154 +trig 0.22610097 +platelet 0.33286083 +protime 0.44433997 > > ## If your work flow includes copying to Excel and Word when writing manuscripts, > ## you may benefit from the quote argument. This will quote everything so that @@ -2539,7 +2695,7 @@ protime 1.365463e-01 8.114383e-02 > base::cat("print.ContTable", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") > cleanEx() -detaching ‘package:survival’, ‘package:splines’ +detaching ‘package:survival’ > nameEx("print.TableOne") > ### * print.TableOne @@ -2548,948 +2704,56 @@ detaching ‘package:survival’, ‘package:splines’ > > base::assign(".ptime", proc.time(), pos = "CheckExEnv") > ### Name: print.TableOne -> ### Title: Format and print the 'TableOne' class objects +> ### Title: Format and print 'TableOne' class objects > ### Aliases: print.TableOne > > ### ** Examples > -> ## Load -> library(tableone) +> ## See examples for CreateTableOne and svyCreateTableOne > -> ## Load Mayo Clinic Primary Biliary Cirrhosis Data -> library(survival) -Loading required package: splines -> data(pbc) -> ## Check variables -> head(pbc) - id time status trt age sex ascites hepato spiders edema bili chol -1 1 400 2 1 58.76523 f 1 1 1 1.0 14.5 261 -2 2 4500 0 1 56.44627 f 0 1 1 0.0 1.1 302 -3 3 1012 2 1 70.07255 m 0 0 0 0.5 1.4 176 -4 4 1925 2 1 54.74059 f 0 1 1 0.5 1.8 244 -5 5 1504 1 2 38.10541 f 0 1 1 0.0 3.4 279 -6 6 2503 2 2 66.25873 f 0 1 0 0.0 0.8 248 - albumin copper alk.phos ast trig platelet protime stage -1 2.60 156 1718.0 137.95 172 190 12.2 4 -2 4.14 54 7394.8 113.52 88 221 10.6 3 -3 3.48 210 516.0 96.10 55 151 12.0 4 -4 2.54 64 6121.8 60.63 92 183 10.3 4 -5 3.53 143 671.0 113.15 72 136 10.9 3 -6 3.98 50 944.0 93.00 63 NA 11.0 3 > -> ## Make categorical variables factors -> varsToFactor <- c("status","trt","ascites","hepato","spiders","edema","stage") -> pbc[varsToFactor] <- lapply(pbc[varsToFactor], factor) > -> ## Create Table 1 stratified by sex and trt -> tableOne <- CreateTableOne(vars = c("time","status","age","ascites","hepato", -+ "spiders","edema","bili","chol","albumin", -+ "copper","alk.phos","ast","trig","platelet", -+ "protime","stage"), -+ strata = c("sex","trt"), data = pbc) +> base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") +> base::cat("print.TableOne", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") +> cleanEx() +> nameEx("print.svyCatTable") +> ### * print.svyCatTable > -> ## Just typing the object name will invoke the print.TableOne method -> tableOne - Stratified by sex:trt - m:1 f:1 m:2 - n 21 137 15 - time (mean (sd)) 1793.48 (1244.70) 2049.67 (1070.20) 2156.33 (1428.56) - status (%) - 0 4 (19.0) 79 (57.7) 7 (46.7) - 1 3 (14.3) 7 ( 5.1) 0 ( 0.0) - 2 14 (66.7) 51 (37.2) 8 (53.3) - age (mean (sd)) 55.57 (12.61) 50.78 (10.65) 57.09 (10.07) - ascites = 1 (%) 1 (4.8) 13 (9.5) 2 (13.3) - hepato = 1 (%) 12 (57.1) 61 (44.5) 9 (60.0) - spiders = 1 (%) 3 (14.3) 42 (30.7) 1 (6.7) - edema (%) - 0 17 (81.0) 115 (83.9) 12 (80.0) - 0.5 3 (14.3) 13 ( 9.5) 1 ( 6.7) - 1 1 ( 4.8) 9 ( 6.6) 2 (13.3) - bili (mean (sd)) 2.98 (2.11) 2.86 (3.81) 2.72 (2.46) - chol (mean (sd)) 403.43 (204.95) 358.24 (210.46) 301.00 (111.32) - albumin (mean (sd)) 3.63 (0.43) 3.50 (0.44) 3.50 (0.49) - copper (mean (sd)) 174.90 (105.33) 85.71 (82.27) 125.40 (89.18) - alk.phos (mean (sd)) 2486.16 (2385.32) 1950.04 (2151.35) 1734.45 (2478.07) - ast (mean (sd)) 128.71 (48.74) 118.91 (55.40) 112.58 (44.38) - trig (mean (sd)) 145.81 (56.62) 120.28 (73.41) 114.86 (39.59) - platelet (mean (sd)) 232.65 (97.38) 262.59 (100.53) 240.13 (73.93) - protime (mean (sd)) 10.84 (0.94) 10.62 (0.84) 11.23 (0.97) - stage (%) - 1 2 ( 9.5) 10 ( 7.3) 1 ( 6.7) - 2 4 (19.0) 31 (22.6) 2 (13.3) - 3 7 (33.3) 49 (35.8) 5 (33.3) - 4 8 (38.1) 47 (34.3) 7 (46.7) - Stratified by sex:trt - f:2 p test - n 139 - time (mean (sd)) 1979.65 (1127.52) 0.730 - status (%) 0.033 - 0 78 (56.1) - 1 9 ( 6.5) - 2 52 (37.4) - age (mean (sd)) 47.66 (9.54) <0.001 - ascites = 1 (%) 8 (5.8) 0.516 - hepato = 1 (%) 78 (56.1) 0.208 - spiders = 1 (%) 44 (31.7) 0.089 - edema (%) 0.906 - 0 119 (85.6) - 0.5 12 ( 8.6) - 1 8 ( 5.8) - bili (mean (sd)) 3.75 (5.50) 0.394 - chol (mean (sd)) 381.73 (262.26) 0.512 - albumin (mean (sd)) 3.53 (0.39) 0.585 - copper (mean (sd)) 94.64 (79.25) <0.001 - alk.phos (mean (sd)) 1965.52 (2066.15) 0.706 - ast (mean (sd)) 126.30 (60.27) 0.598 - trig (mean (sd)) 126.38 (60.22) 0.370 - platelet (mean (sd)) 267.95 (92.20) 0.362 - protime (mean (sd)) 10.75 (1.15) 0.137 - stage (%) 0.646 - 1 3 ( 2.2) - 2 30 (21.6) - 3 59 (42.4) - 4 47 (33.8) +> flush(stderr()); flush(stdout()) > -> ## Specifying nonnormal variables will show the variables appropriately, -> ## and show nonparametric test p-values. Specify variables in the exact -> ## argument to obtain the exact test p-values. cramVars can be used to -> ## show both levels for a 2-level categorical variables. -> print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), -+ exact = c("status","stage"), cramVars = "hepato") - Stratified by sex:trt - m:1 f:1 - n 21 137 - time (mean (sd)) 1793.48 (1244.70) 2049.67 (1070.20) - status (%) - 0 4 (19.0) 79 (57.7) - 1 3 (14.3) 7 ( 5.1) - 2 14 (66.7) 51 (37.2) - age (mean (sd)) 55.57 (12.61) 50.78 (10.65) - ascites = 1 (%) 1 (4.8) 13 (9.5) - hepato = 0/1 (%) 9/12 (42.9/57.1) 76/61 (55.5/44.5) - spiders = 1 (%) 3 (14.3) 42 (30.7) - edema (%) - 0 17 (81.0) 115 (83.9) - 0.5 3 (14.3) 13 ( 9.5) - 1 1 ( 4.8) 9 ( 6.6) - bili (median [IQR]) 2.30 [1.40, 4.00] 1.30 [0.80, 3.20] - chol (median [IQR]) 376.00 [251.00, 456.00] 309.00 [247.50, 399.50] - albumin (mean (sd)) 3.63 (0.43) 3.50 (0.44) - copper (median [IQR]) 158.00 [100.00, 225.00] 67.00 [36.75, 105.00] - alk.phos (median [IQR]) 1664.00 [983.00, 2496.00] 1212.00 [823.00, 1877.00] - ast (mean (sd)) 128.71 (48.74) 118.91 (55.40) - trig (median [IQR]) 142.00 [107.00, 194.00] 101.00 [84.00, 134.00] - platelet (mean (sd)) 232.65 (97.38) 262.59 (100.53) - protime (mean (sd)) 10.84 (0.94) 10.62 (0.84) - stage (%) - 1 2 ( 9.5) 10 ( 7.3) - 2 4 (19.0) 31 (22.6) - 3 7 (33.3) 49 (35.8) - 4 8 (38.1) 47 (34.3) - Stratified by sex:trt - m:2 f:2 - n 15 139 - time (mean (sd)) 2156.33 (1428.56) 1979.65 (1127.52) - status (%) - 0 7 (46.7) 78 (56.1) - 1 0 ( 0.0) 9 ( 6.5) - 2 8 (53.3) 52 (37.4) - age (mean (sd)) 57.09 (10.07) 47.66 (9.54) - ascites = 1 (%) 2 (13.3) 8 (5.8) - hepato = 0/1 (%) 6/9 (40.0/60.0) 61/78 (43.9/56.1) - spiders = 1 (%) 1 (6.7) 44 (31.7) - edema (%) - 0 12 (80.0) 119 (85.6) - 0.5 1 ( 6.7) 12 ( 8.6) - 1 2 (13.3) 8 ( 5.8) - bili (median [IQR]) 1.90 [1.10, 2.85] 1.30 [0.70, 3.60] - chol (median [IQR]) 259.00 [235.75, 342.25] 306.50 [258.00, 381.00] - albumin (mean (sd)) 3.50 (0.49) 3.53 (0.39) - copper (median [IQR]) 84.00 [52.50, 198.00] 69.50 [42.50, 127.25] - alk.phos (median [IQR]) 1070.00 [737.00, 1211.50] 1345.00 [941.00, 1992.00] - ast (mean (sd)) 112.58 (44.38) 126.30 (60.27) - trig (median [IQR]) 108.50 [90.25, 137.00] 113.00 [84.00, 157.00] - platelet (mean (sd)) 240.13 (73.93) 267.95 (92.20) - protime (mean (sd)) 11.23 (0.97) 10.75 (1.15) - stage (%) - 1 1 ( 6.7) 3 ( 2.2) - 2 2 (13.3) 30 (21.6) - 3 5 (33.3) 59 (42.4) - 4 7 (46.7) 47 (33.8) - Stratified by sex:trt - p test - n - time (mean (sd)) 0.730 - status (%) 0.024 exact - 0 - 1 - 2 - age (mean (sd)) <0.001 - ascites = 1 (%) 0.516 - hepato = 0/1 (%) 0.208 - spiders = 1 (%) 0.089 - edema (%) 0.906 - 0 - 0.5 - 1 - bili (median [IQR]) 0.205 nonnorm - chol (median [IQR]) 0.458 nonnorm - albumin (mean (sd)) 0.585 - copper (median [IQR]) <0.001 nonnorm - alk.phos (median [IQR]) 0.180 nonnorm - ast (mean (sd)) 0.598 - trig (median [IQR]) 0.058 nonnorm - platelet (mean (sd)) 0.362 - protime (mean (sd)) 0.137 - stage (%) NA exact - 1 - 2 - 3 - 4 +> base::assign(".ptime", proc.time(), pos = "CheckExEnv") +> ### Name: print.svyCatTable +> ### Title: Format and print 'svyCatTable' class objects +> ### Aliases: print.svyCatTable > -> ## Use the summary.TableOne method for detailed summary -> summary(tableOne) - - ### Summary of continuous variables ### - -sex: m -trt: 1 - n miss p.miss mean sd median p25 p75 min max skew kurt -time 21 0 0 1793 1e+03 1302 999 2386 140.0 4459 1.04 0.1 -age 21 0 0 56 1e+01 56 46 66 33.5 78 -0.20 -0.7 -bili 21 0 0 3 2e+00 2 1 4 0.6 7 1.02 -0.1 -chol 21 0 0 403 2e+02 376 251 456 168.0 1000 1.33 2.2 -albumin 21 0 0 4 4e-01 4 3 4 2.6 4 -0.97 0.3 -copper 21 0 0 175 1e+02 158 100 225 25.0 444 1.08 1.0 -alk.phos 21 0 0 2486 2e+03 1664 983 2496 516.0 10165 2.05 4.5 -ast 21 0 0 129 5e+01 127 86 158 56.8 222 0.31 -0.9 -trig 21 0 0 146 6e+01 142 107 194 55.0 242 0.16 -1.0 -platelet 21 1 5 233 1e+02 228 148 314 70.0 394 -0.05 -1.2 -protime 21 0 0 11 9e-01 11 10 11 9.7 14 2.11 6.8 ------------------------------------------------------------- -sex: f -trt: 1 - n miss p.miss mean sd median p25 p75 min max skew kurt -time 137 0 0.0 2050 1e+03 1945 1e+03 2644 41.0 4556 0.32 -0.4 -age 137 0 0.0 51 1e+01 51 4e+01 57 26.3 77 0.05 -0.5 -bili 137 0 0.0 3 4e+00 1 8e-01 3 0.3 20 2.65 7.1 -chol 137 18 13.1 358 2e+02 309 2e+02 400 127.0 1712 4.28 23.8 -albumin 137 0 0.0 3 4e-01 4 3e+00 4 2.1 5 -0.33 0.4 -copper 137 1 0.7 86 8e+01 67 4e+01 105 9.0 588 3.18 13.9 -alk.phos 137 0 0.0 1950 2e+03 1212 8e+02 1877 369.0 11552 2.88 8.4 -ast 137 0 0.0 119 6e+01 108 7e+01 150 26.4 338 1.20 1.9 -trig 137 19 13.9 120 7e+01 101 8e+01 134 33.0 598 3.28 16.0 -platelet 137 1 0.7 263 1e+02 258 2e+02 322 62.0 563 0.57 0.3 -protime 137 0 0.0 11 8e-01 11 1e+01 11 9.0 13 0.92 0.6 ------------------------------------------------------------- -sex: m -trt: 2 - n miss p.miss mean sd median p25 p75 min max skew kurt -time 15 0 0 2156 1e+03 1656 1054 3420 191.0 4427 0.3 -1.4 -age 15 0 0 57 1e+01 53 49 66 43.9 75 0.5 -1.3 -bili 15 0 0 3 2e+00 2 1 3 0.6 9 1.5 1.4 -chol 15 1 7 301 1e+02 259 236 342 151.0 546 0.9 0.4 -albumin 15 0 0 4 5e-01 4 3 4 2.3 4 -1.0 1.9 -copper 15 0 0 125 9e+01 84 52 198 13.0 281 0.5 -1.1 -alk.phos 15 0 0 1734 2e+03 1070 737 1212 577.0 10397 3.5 12.7 -ast 15 0 0 113 4e+01 110 74 129 46.5 188 0.3 -0.8 -trig 15 1 7 115 4e+01 108 90 137 49.0 188 0.4 -0.4 -platelet 15 0 0 240 7e+01 214 190 294 119.0 360 0.2 -1.0 -protime 15 0 0 11 1e+00 11 11 12 10.0 13 0.7 0.2 ------------------------------------------------------------- -sex: f -trt: 2 - n miss p.miss mean sd median p25 p75 min max skew kurt -time 139 0 0.0 1980 1e+03 1832 1e+03 2717 51.0 4523 0.3 -0.6 -age 139 0 0.0 48 1e+01 47 4e+01 55 30.6 71 0.2 -0.7 -bili 139 0 0.0 4 5e+00 1 7e-01 4 0.3 28 2.6 6.5 -chol 139 9 6.5 382 3e+02 306 3e+02 381 120.0 1775 3.0 10.1 -albumin 139 0 0.0 4 4e-01 4 3e+00 4 2.0 4 -0.8 2.1 -copper 139 1 0.7 95 8e+01 70 4e+01 127 4.0 558 2.2 8.3 -alk.phos 139 0 0.0 1966 2e+03 1345 9e+02 1992 289.0 13862 3.4 13.6 -ast 139 0 0.0 126 6e+01 118 8e+01 153 28.4 457 1.8 6.2 -trig 139 10 7.2 126 6e+01 113 8e+01 157 44.0 432 1.7 5.3 -platelet 139 2 1.4 268 9e+01 265 2e+02 324 71.0 487 0.2 -0.4 -protime 139 0 0.0 11 1e+00 11 1e+01 11 9.2 17 2.0 7.2 - -p-values - pNormal pNonNormal -time 7.300496e-01 5.566440e-01 -age 1.286381e-04 6.517437e-04 -bili 3.937430e-01 2.049525e-01 -chol 5.122223e-01 4.578773e-01 -albumin 5.853926e-01 4.041206e-01 -copper 6.237989e-05 4.712924e-05 -alk.phos 7.063812e-01 1.801480e-01 -ast 5.982898e-01 5.086616e-01 -trig 3.701073e-01 5.827726e-02 -platelet 3.624740e-01 4.451277e-01 -protime 1.365463e-01 8.114383e-02 - -======================================================================================= - - ### Summary of categorical variables ### - -sex: m -trt: 1 - var n miss p.miss level freq percent cum.percent - status 21 0 0.0 0 4 19.0 19.0 - 1 3 14.3 33.3 - 2 14 66.7 100.0 - - ascites 21 0 0.0 0 20 95.2 95.2 - 1 1 4.8 100.0 - - hepato 21 0 0.0 0 9 42.9 42.9 - 1 12 57.1 100.0 - - spiders 21 0 0.0 0 18 85.7 85.7 - 1 3 14.3 100.0 - - edema 21 0 0.0 0 17 81.0 81.0 - 0.5 3 14.3 95.2 - 1 1 4.8 100.0 - - stage 21 0 0.0 1 2 9.5 9.5 - 2 4 19.0 28.6 - 3 7 33.3 61.9 - 4 8 38.1 100.0 - ------------------------------------------------------------- -sex: f -trt: 1 - var n miss p.miss level freq percent cum.percent - status 137 0 0.0 0 79 57.7 57.7 - 1 7 5.1 62.8 - 2 51 37.2 100.0 - - ascites 137 0 0.0 0 124 90.5 90.5 - 1 13 9.5 100.0 - - hepato 137 0 0.0 0 76 55.5 55.5 - 1 61 44.5 100.0 - - spiders 137 0 0.0 0 95 69.3 69.3 - 1 42 30.7 100.0 - - edema 137 0 0.0 0 115 83.9 83.9 - 0.5 13 9.5 93.4 - 1 9 6.6 100.0 - - stage 137 0 0.0 1 10 7.3 7.3 - 2 31 22.6 29.9 - 3 49 35.8 65.7 - 4 47 34.3 100.0 - ------------------------------------------------------------- -sex: m -trt: 2 - var n miss p.miss level freq percent cum.percent - status 15 0 0.0 0 7 46.7 46.7 - 1 0 0.0 46.7 - 2 8 53.3 100.0 - - ascites 15 0 0.0 0 13 86.7 86.7 - 1 2 13.3 100.0 - - hepato 15 0 0.0 0 6 40.0 40.0 - 1 9 60.0 100.0 - - spiders 15 0 0.0 0 14 93.3 93.3 - 1 1 6.7 100.0 - - edema 15 0 0.0 0 12 80.0 80.0 - 0.5 1 6.7 86.7 - 1 2 13.3 100.0 - - stage 15 0 0.0 1 1 6.7 6.7 - 2 2 13.3 20.0 - 3 5 33.3 53.3 - 4 7 46.7 100.0 - ------------------------------------------------------------- -sex: f -trt: 2 - var n miss p.miss level freq percent cum.percent - status 139 0 0.0 0 78 56.1 56.1 - 1 9 6.5 62.6 - 2 52 37.4 100.0 - - ascites 139 0 0.0 0 131 94.2 94.2 - 1 8 5.8 100.0 - - hepato 139 0 0.0 0 61 43.9 43.9 - 1 78 56.1 100.0 - - spiders 139 0 0.0 0 95 68.3 68.3 - 1 44 31.7 100.0 - - edema 139 0 0.0 0 119 85.6 85.6 - 0.5 12 8.6 94.2 - 1 8 5.8 100.0 - - stage 139 0 0.0 1 3 2.2 2.2 - 2 30 21.6 23.7 - 3 59 42.4 66.2 - 4 47 33.8 100.0 - - -p-values - pApprox pExact -status 0.03269881 0.02420814 -ascites 0.51569424 0.43540746 -hepato 0.20805680 0.20480121 -spiders 0.08898575 0.08569292 -edema 0.90584197 0.84833223 -stage 0.64630048 NA -> -> ## See the categorical part only using $ operator -> tableOne$CatTable - Stratified by sex:trt - m:1 f:1 m:2 f:2 p test - n 21 137 15 139 - status (%) 0.033 - 0 4 (19.0) 79 (57.7) 7 (46.7) 78 (56.1) - 1 3 (14.3) 7 ( 5.1) 0 ( 0.0) 9 ( 6.5) - 2 14 (66.7) 51 (37.2) 8 (53.3) 52 (37.4) - ascites = 1 (%) 1 ( 4.8) 13 ( 9.5) 2 (13.3) 8 ( 5.8) 0.516 - hepato = 1 (%) 12 (57.1) 61 (44.5) 9 (60.0) 78 (56.1) 0.208 - spiders = 1 (%) 3 (14.3) 42 (30.7) 1 ( 6.7) 44 (31.7) 0.089 - edema (%) 0.906 - 0 17 (81.0) 115 (83.9) 12 (80.0) 119 (85.6) - 0.5 3 (14.3) 13 ( 9.5) 1 ( 6.7) 12 ( 8.6) - 1 1 ( 4.8) 9 ( 6.6) 2 (13.3) 8 ( 5.8) - stage (%) 0.646 - 1 2 ( 9.5) 10 ( 7.3) 1 ( 6.7) 3 ( 2.2) - 2 4 (19.0) 31 (22.6) 2 (13.3) 30 (21.6) - 3 7 (33.3) 49 (35.8) 5 (33.3) 59 (42.4) - 4 8 (38.1) 47 (34.3) 7 (46.7) 47 (33.8) -> summary(tableOne$CatTable) -sex: m -trt: 1 - var n miss p.miss level freq percent cum.percent - status 21 0 0.0 0 4 19.0 19.0 - 1 3 14.3 33.3 - 2 14 66.7 100.0 - - ascites 21 0 0.0 0 20 95.2 95.2 - 1 1 4.8 100.0 - - hepato 21 0 0.0 0 9 42.9 42.9 - 1 12 57.1 100.0 - - spiders 21 0 0.0 0 18 85.7 85.7 - 1 3 14.3 100.0 - - edema 21 0 0.0 0 17 81.0 81.0 - 0.5 3 14.3 95.2 - 1 1 4.8 100.0 - - stage 21 0 0.0 1 2 9.5 9.5 - 2 4 19.0 28.6 - 3 7 33.3 61.9 - 4 8 38.1 100.0 - ------------------------------------------------------------- -sex: f -trt: 1 - var n miss p.miss level freq percent cum.percent - status 137 0 0.0 0 79 57.7 57.7 - 1 7 5.1 62.8 - 2 51 37.2 100.0 - - ascites 137 0 0.0 0 124 90.5 90.5 - 1 13 9.5 100.0 - - hepato 137 0 0.0 0 76 55.5 55.5 - 1 61 44.5 100.0 - - spiders 137 0 0.0 0 95 69.3 69.3 - 1 42 30.7 100.0 - - edema 137 0 0.0 0 115 83.9 83.9 - 0.5 13 9.5 93.4 - 1 9 6.6 100.0 - - stage 137 0 0.0 1 10 7.3 7.3 - 2 31 22.6 29.9 - 3 49 35.8 65.7 - 4 47 34.3 100.0 - ------------------------------------------------------------- -sex: m -trt: 2 - var n miss p.miss level freq percent cum.percent - status 15 0 0.0 0 7 46.7 46.7 - 1 0 0.0 46.7 - 2 8 53.3 100.0 - - ascites 15 0 0.0 0 13 86.7 86.7 - 1 2 13.3 100.0 - - hepato 15 0 0.0 0 6 40.0 40.0 - 1 9 60.0 100.0 - - spiders 15 0 0.0 0 14 93.3 93.3 - 1 1 6.7 100.0 - - edema 15 0 0.0 0 12 80.0 80.0 - 0.5 1 6.7 86.7 - 1 2 13.3 100.0 - - stage 15 0 0.0 1 1 6.7 6.7 - 2 2 13.3 20.0 - 3 5 33.3 53.3 - 4 7 46.7 100.0 - ------------------------------------------------------------- -sex: f -trt: 2 - var n miss p.miss level freq percent cum.percent - status 139 0 0.0 0 78 56.1 56.1 - 1 9 6.5 62.6 - 2 52 37.4 100.0 - - ascites 139 0 0.0 0 131 94.2 94.2 - 1 8 5.8 100.0 - - hepato 139 0 0.0 0 61 43.9 43.9 - 1 78 56.1 100.0 - - spiders 139 0 0.0 0 95 68.3 68.3 - 1 44 31.7 100.0 - - edema 139 0 0.0 0 119 85.6 85.6 - 0.5 12 8.6 94.2 - 1 8 5.8 100.0 - - stage 139 0 0.0 1 3 2.2 2.2 - 2 30 21.6 23.7 - 3 59 42.4 66.2 - 4 47 33.8 100.0 - - -p-values - pApprox pExact -status 0.03269881 0.02420814 -ascites 0.51569424 0.43540746 -hepato 0.20805680 0.20480121 -spiders 0.08898575 0.08569292 -edema 0.90584197 0.84833223 -stage 0.64630048 NA -> -> ## See the continuous part only using $ operator -> tableOne$ContTable - Stratified by sex:trt - m:1 f:1 m:2 - n 21 137 15 - time (mean (sd)) 1793.48 (1244.70) 2049.67 (1070.20) 2156.33 (1428.56) - age (mean (sd)) 55.57 (12.61) 50.78 (10.65) 57.09 (10.07) - bili (mean (sd)) 2.98 (2.11) 2.86 (3.81) 2.72 (2.46) - chol (mean (sd)) 403.43 (204.95) 358.24 (210.46) 301.00 (111.32) - albumin (mean (sd)) 3.63 (0.43) 3.50 (0.44) 3.50 (0.49) - copper (mean (sd)) 174.90 (105.33) 85.71 (82.27) 125.40 (89.18) - alk.phos (mean (sd)) 2486.16 (2385.32) 1950.04 (2151.35) 1734.45 (2478.07) - ast (mean (sd)) 128.71 (48.74) 118.91 (55.40) 112.58 (44.38) - trig (mean (sd)) 145.81 (56.62) 120.28 (73.41) 114.86 (39.59) - platelet (mean (sd)) 232.65 (97.38) 262.59 (100.53) 240.13 (73.93) - protime (mean (sd)) 10.84 (0.94) 10.62 (0.84) 11.23 (0.97) - Stratified by sex:trt - f:2 p test - n 139 - time (mean (sd)) 1979.65 (1127.52) 0.730 - age (mean (sd)) 47.66 (9.54) <0.001 - bili (mean (sd)) 3.75 (5.50) 0.394 - chol (mean (sd)) 381.73 (262.26) 0.512 - albumin (mean (sd)) 3.53 (0.39) 0.585 - copper (mean (sd)) 94.64 (79.25) <0.001 - alk.phos (mean (sd)) 1965.52 (2066.15) 0.706 - ast (mean (sd)) 126.30 (60.27) 0.598 - trig (mean (sd)) 126.38 (60.22) 0.370 - platelet (mean (sd)) 267.95 (92.20) 0.362 - protime (mean (sd)) 10.75 (1.15) 0.137 -> summary(tableOne$ContTable) -sex: m -trt: 1 - n miss p.miss mean sd median p25 p75 min max skew -time 21 0 0.0 1793.5 1244.70 1302.0 999.0 2386.0 140.0 4459.0 1.042 -age 21 0 0.0 55.6 12.61 56.0 46.5 65.8 33.5 78.4 -0.200 -bili 21 0 0.0 3.0 2.11 2.3 1.4 4.0 0.6 7.3 1.019 -chol 21 0 0.0 403.4 204.95 376.0 251.0 456.0 168.0 1000.0 1.329 -albumin 21 0 0.0 3.6 0.43 3.8 3.4 3.9 2.6 4.2 -0.966 -copper 21 0 0.0 174.9 105.33 158.0 100.0 225.0 25.0 444.0 1.082 -alk.phos 21 0 0.0 2486.2 2385.32 1664.0 983.0 2496.0 516.0 10165.0 2.051 -ast 21 0 0.0 128.7 48.74 127.0 86.0 158.1 56.8 221.9 0.309 -trig 21 0 0.0 145.8 56.62 142.0 107.0 194.0 55.0 242.0 0.164 -platelet 21 1 4.8 232.7 97.38 227.5 148.2 313.8 70.0 394.0 -0.045 -protime 21 0 0.0 10.8 0.94 10.8 10.3 11.0 9.7 14.1 2.113 - kurt -time 0.10 -age -0.71 -bili -0.12 -chol 2.15 -albumin 0.34 -copper 0.97 -alk.phos 4.49 -ast -0.86 -trig -1.04 -platelet -1.16 -protime 6.84 ------------------------------------------------------------- -sex: f -trt: 1 - n miss p.miss mean sd median p25 p75 min max -time 137 0 0.00 2049.7 1070.20 1945.0 1293.0 2644.0 41.0 4556.0 -age 137 0 0.00 50.8 10.65 51.2 42.5 57.0 26.3 76.7 -bili 137 0 0.00 2.9 3.81 1.3 0.8 3.2 0.3 20.0 -chol 137 18 13.14 358.2 210.46 309.0 247.5 399.5 127.0 1712.0 -albumin 137 0 0.00 3.5 0.44 3.5 3.2 3.8 2.1 4.6 -copper 137 1 0.73 85.7 82.27 67.0 36.8 105.0 9.0 588.0 -alk.phos 137 0 0.00 1950.0 2151.35 1212.0 823.0 1877.0 369.0 11552.0 -ast 137 0 0.00 118.9 55.40 108.5 74.4 150.0 26.4 338.0 -trig 137 19 13.87 120.3 73.41 101.0 84.0 134.0 33.0 598.0 -platelet 137 1 0.73 262.6 100.53 258.5 194.2 322.0 62.0 563.0 -protime 137 0 0.00 10.6 0.84 10.6 10.0 11.0 9.0 13.0 - skew kurt -time 0.318 -0.41 -age 0.045 -0.48 -bili 2.652 7.10 -chol 4.281 23.79 -albumin -0.327 0.42 -copper 3.181 13.92 -alk.phos 2.876 8.39 -ast 1.196 1.91 -trig 3.278 16.03 -platelet 0.565 0.34 -protime 0.916 0.57 ------------------------------------------------------------- -sex: m -trt: 2 - n miss p.miss mean sd median p25 p75 min max skew -time 15 0 0.0 2156.3 1428.56 1656.0 1053.5 3420.0 191.0 4427.0 0.30 -age 15 0 0.0 57.1 10.07 52.8 49.4 65.8 43.9 74.5 0.45 -bili 15 0 0.0 2.7 2.46 1.9 1.1 2.9 0.6 8.6 1.54 -chol 15 1 6.7 301.0 111.32 259.0 235.8 342.2 151.0 546.0 0.91 -albumin 15 0 0.0 3.5 0.49 3.6 3.3 3.8 2.3 4.3 -0.96 -copper 15 0 0.0 125.4 89.18 84.0 52.5 198.0 13.0 281.0 0.50 -alk.phos 15 0 0.0 1734.5 2478.07 1070.0 737.0 1211.5 577.0 10396.8 3.49 -ast 15 0 0.0 112.6 44.38 110.0 74.2 129.4 46.5 188.3 0.32 -trig 15 1 6.7 114.9 39.59 108.5 90.2 137.0 49.0 188.0 0.44 -platelet 15 0 0.0 240.1 73.93 214.0 189.5 294.5 119.0 360.0 0.18 -protime 15 0 0.0 11.2 0.97 11.2 10.6 11.6 10.0 13.2 0.72 - kurt -time -1.39 -age -1.29 -bili 1.43 -chol 0.36 -albumin 1.88 -copper -1.13 -alk.phos 12.68 -ast -0.80 -trig -0.36 -platelet -1.02 -protime 0.18 ------------------------------------------------------------- -sex: f -trt: 2 - n miss p.miss mean sd median p25 p75 min max -time 139 0 0.00 1979.7 1127.52 1832.0 1157.0 2717.0 51.0 4523.0 -age 139 0 0.00 47.7 9.54 47.2 41.3 55.5 30.6 70.6 -bili 139 0 0.00 3.7 5.50 1.3 0.7 3.6 0.3 28.0 -chol 139 9 6.47 381.7 262.26 306.5 258.0 381.0 120.0 1775.0 -albumin 139 0 0.00 3.5 0.39 3.5 3.3 3.8 2.0 4.4 -copper 139 1 0.72 94.6 79.25 69.5 42.5 127.2 4.0 558.0 -alk.phos 139 0 0.00 1965.5 2066.15 1345.0 941.0 1992.0 289.0 13862.4 -ast 139 0 0.00 126.3 60.27 117.8 84.5 152.7 28.4 457.2 -trig 139 10 7.19 126.4 60.22 113.0 84.0 157.0 44.0 432.0 -platelet 139 2 1.44 267.9 92.20 265.0 213.0 324.0 71.0 487.0 -protime 139 0 0.00 10.8 1.15 10.6 9.9 11.2 9.2 17.1 - skew kurt -time 0.34 -0.64 -age 0.16 -0.74 -bili 2.57 6.54 -chol 3.01 10.13 -albumin -0.83 2.06 -copper 2.25 8.30 -alk.phos 3.39 13.57 -ast 1.76 6.24 -trig 1.71 5.27 -platelet 0.21 -0.36 -protime 2.02 7.15 - -p-values - pNormal pNonNormal -time 7.300496e-01 5.566440e-01 -age 1.286381e-04 6.517437e-04 -bili 3.937430e-01 2.049525e-01 -chol 5.122223e-01 4.578773e-01 -albumin 5.853926e-01 4.041206e-01 -copper 6.237989e-05 4.712924e-05 -alk.phos 7.063812e-01 1.801480e-01 -ast 5.982898e-01 5.086616e-01 -trig 3.701073e-01 5.827726e-02 -platelet 3.624740e-01 4.451277e-01 -protime 1.365463e-01 8.114383e-02 -> -> ## If your work flow includes copying to Excel and Word when writing manuscripts, -> ## you may benefit from the quote argument. This will quote everything so that -> ## Excel does not mess up the cells. -> print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), -+ exact = c("status","stage"), cramVars = "hepato", quote = TRUE) - "Stratified by sex:trt" - "" "m:1" - "n" "21" - "time (mean (sd))" "1793.48 (1244.70)" - "status (%)" " " - " 0" " 4 (19.0) " - " 1" " 3 (14.3) " - " 2" " 14 (66.7) " - "age (mean (sd))" " 55.57 (12.61)" - "ascites = 1 (%)" " 1 (4.8) " - "hepato = 0/1 (%)" " 9/12 (42.9/57.1) " - "spiders = 1 (%)" " 3 (14.3) " - "edema (%)" " " - " 0" " 17 (81.0) " - " 0.5" " 3 (14.3) " - " 1" " 1 ( 4.8) " - "bili (median [IQR])" " 2.30 [1.40, 4.00]" - "chol (median [IQR])" " 376.00 [251.00, 456.00]" - "albumin (mean (sd))" " 3.63 (0.43)" - "copper (median [IQR])" " 158.00 [100.00, 225.00]" - "alk.phos (median [IQR])" "1664.00 [983.00, 2496.00]" - "ast (mean (sd))" " 128.71 (48.74)" - "trig (median [IQR])" " 142.00 [107.00, 194.00]" - "platelet (mean (sd))" " 232.65 (97.38)" - "protime (mean (sd))" " 10.84 (0.94)" - "stage (%)" " " - " 1" " 2 ( 9.5) " - " 2" " 4 (19.0) " - " 3" " 7 (33.3) " - " 4" " 8 (38.1) " - "Stratified by sex:trt" - "" "f:1" - "n" "137" - "time (mean (sd))" "2049.67 (1070.20)" - "status (%)" " " - " 0" " 79 (57.7) " - " 1" " 7 ( 5.1) " - " 2" " 51 (37.2) " - "age (mean (sd))" " 50.78 (10.65)" - "ascites = 1 (%)" " 13 (9.5) " - "hepato = 0/1 (%)" " 76/61 (55.5/44.5) " - "spiders = 1 (%)" " 42 (30.7) " - "edema (%)" " " - " 0" " 115 (83.9) " - " 0.5" " 13 ( 9.5) " - " 1" " 9 ( 6.6) " - "bili (median [IQR])" " 1.30 [0.80, 3.20]" - "chol (median [IQR])" " 309.00 [247.50, 399.50]" - "albumin (mean (sd))" " 3.50 (0.44)" - "copper (median [IQR])" " 67.00 [36.75, 105.00]" - "alk.phos (median [IQR])" "1212.00 [823.00, 1877.00]" - "ast (mean (sd))" " 118.91 (55.40)" - "trig (median [IQR])" " 101.00 [84.00, 134.00]" - "platelet (mean (sd))" " 262.59 (100.53)" - "protime (mean (sd))" " 10.62 (0.84)" - "stage (%)" " " - " 1" " 10 ( 7.3) " - " 2" " 31 (22.6) " - " 3" " 49 (35.8) " - " 4" " 47 (34.3) " - "Stratified by sex:trt" - "" "m:2" - "n" "15" - "time (mean (sd))" "2156.33 (1428.56)" - "status (%)" " " - " 0" " 7 (46.7) " - " 1" " 0 ( 0.0) " - " 2" " 8 (53.3) " - "age (mean (sd))" " 57.09 (10.07)" - "ascites = 1 (%)" " 2 (13.3) " - "hepato = 0/1 (%)" " 6/9 (40.0/60.0) " - "spiders = 1 (%)" " 1 (6.7) " - "edema (%)" " " - " 0" " 12 (80.0) " - " 0.5" " 1 ( 6.7) " - " 1" " 2 (13.3) " - "bili (median [IQR])" " 1.90 [1.10, 2.85]" - "chol (median [IQR])" " 259.00 [235.75, 342.25]" - "albumin (mean (sd))" " 3.50 (0.49)" - "copper (median [IQR])" " 84.00 [52.50, 198.00]" - "alk.phos (median [IQR])" "1070.00 [737.00, 1211.50]" - "ast (mean (sd))" " 112.58 (44.38)" - "trig (median [IQR])" " 108.50 [90.25, 137.00]" - "platelet (mean (sd))" " 240.13 (73.93)" - "protime (mean (sd))" " 11.23 (0.97)" - "stage (%)" " " - " 1" " 1 ( 6.7) " - " 2" " 2 (13.3) " - " 3" " 5 (33.3) " - " 4" " 7 (46.7) " - "Stratified by sex:trt" - "" "f:2" "p" "test" - "n" "139" "" "" - "time (mean (sd))" "1979.65 (1127.52)" " 0.730" "" - "status (%)" " " " 0.024" "exact" - " 0" " 78 (56.1) " "" "" - " 1" " 9 ( 6.5) " "" "" - " 2" " 52 (37.4) " "" "" - "age (mean (sd))" " 47.66 (9.54)" "<0.001" "" - "ascites = 1 (%)" " 8 (5.8) " " 0.516" "" - "hepato = 0/1 (%)" " 61/78 (43.9/56.1) " " 0.208" "" - "spiders = 1 (%)" " 44 (31.7) " " 0.089" "" - "edema (%)" " " " 0.906" "" - " 0" " 119 (85.6) " "" "" - " 0.5" " 12 ( 8.6) " "" "" - " 1" " 8 ( 5.8) " "" "" - "bili (median [IQR])" " 1.30 [0.70, 3.60]" " 0.205" "nonnorm" - "chol (median [IQR])" " 306.50 [258.00, 381.00]" " 0.458" "nonnorm" - "albumin (mean (sd))" " 3.53 (0.39)" " 0.585" "" - "copper (median [IQR])" " 69.50 [42.50, 127.25]" "<0.001" "nonnorm" - "alk.phos (median [IQR])" "1345.00 [941.00, 1992.00]" " 0.180" "nonnorm" - "ast (mean (sd))" " 126.30 (60.27)" " 0.598" "" - "trig (median [IQR])" " 113.00 [84.00, 157.00]" " 0.058" "nonnorm" - "platelet (mean (sd))" " 267.95 (92.20)" " 0.362" "" - "protime (mean (sd))" " 10.75 (1.15)" " 0.137" "" - "stage (%)" " " " NA" "exact" - " 1" " 3 ( 2.2) " "" "" - " 2" " 30 (21.6) " "" "" - " 3" " 59 (42.4) " "" "" - " 4" " 47 (33.8) " "" "" -> -> ## If you want to center-align values in Word, use noSpaces option. -> print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), -+ exact = c("status","stage"), cramVars = "hepato", quote = TRUE, noSpaces = TRUE) - "Stratified by sex:trt" - "" "m:1" - "n" "21" - "time (mean (sd))" "1793.48 (1244.70)" - "status (%)" "" - " 0" "4 (19.0)" - " 1" "3 (14.3)" - " 2" "14 (66.7)" - "age (mean (sd))" "55.57 (12.61)" - "ascites = 1 (%)" "1 (4.8)" - "hepato = 0/1 (%)" "9/12 (42.9/57.1)" - "spiders = 1 (%)" "3 (14.3)" - "edema (%)" "" - " 0" "17 (81.0)" - " 0.5" "3 (14.3)" - " 1" "1 (4.8)" - "bili (median [IQR])" "2.30 [1.40, 4.00]" - "chol (median [IQR])" "376.00 [251.00, 456.00]" - "albumin (mean (sd))" "3.63 (0.43)" - "copper (median [IQR])" "158.00 [100.00, 225.00]" - "alk.phos (median [IQR])" "1664.00 [983.00, 2496.00]" - "ast (mean (sd))" "128.71 (48.74)" - "trig (median [IQR])" "142.00 [107.00, 194.00]" - "platelet (mean (sd))" "232.65 (97.38)" - "protime (mean (sd))" "10.84 (0.94)" - "stage (%)" "" - " 1" "2 (9.5)" - " 2" "4 (19.0)" - " 3" "7 (33.3)" - " 4" "8 (38.1)" - "Stratified by sex:trt" - "" "f:1" - "n" "137" - "time (mean (sd))" "2049.67 (1070.20)" - "status (%)" "" - " 0" "79 (57.7)" - " 1" "7 (5.1)" - " 2" "51 (37.2)" - "age (mean (sd))" "50.78 (10.65)" - "ascites = 1 (%)" "13 (9.5)" - "hepato = 0/1 (%)" "76/61 (55.5/44.5)" - "spiders = 1 (%)" "42 (30.7)" - "edema (%)" "" - " 0" "115 (83.9)" - " 0.5" "13 (9.5)" - " 1" "9 (6.6)" - "bili (median [IQR])" "1.30 [0.80, 3.20]" - "chol (median [IQR])" "309.00 [247.50, 399.50]" - "albumin (mean (sd))" "3.50 (0.44)" - "copper (median [IQR])" "67.00 [36.75, 105.00]" - "alk.phos (median [IQR])" "1212.00 [823.00, 1877.00]" - "ast (mean (sd))" "118.91 (55.40)" - "trig (median [IQR])" "101.00 [84.00, 134.00]" - "platelet (mean (sd))" "262.59 (100.53)" - "protime (mean (sd))" "10.62 (0.84)" - "stage (%)" "" - " 1" "10 (7.3)" - " 2" "31 (22.6)" - " 3" "49 (35.8)" - " 4" "47 (34.3)" - "Stratified by sex:trt" - "" "m:2" - "n" "15" - "time (mean (sd))" "2156.33 (1428.56)" - "status (%)" "" - " 0" "7 (46.7)" - " 1" "0 (0.0)" - " 2" "8 (53.3)" - "age (mean (sd))" "57.09 (10.07)" - "ascites = 1 (%)" "2 (13.3)" - "hepato = 0/1 (%)" "6/9 (40.0/60.0)" - "spiders = 1 (%)" "1 (6.7)" - "edema (%)" "" - " 0" "12 (80.0)" - " 0.5" "1 (6.7)" - " 1" "2 (13.3)" - "bili (median [IQR])" "1.90 [1.10, 2.85]" - "chol (median [IQR])" "259.00 [235.75, 342.25]" - "albumin (mean (sd))" "3.50 (0.49)" - "copper (median [IQR])" "84.00 [52.50, 198.00]" - "alk.phos (median [IQR])" "1070.00 [737.00, 1211.50]" - "ast (mean (sd))" "112.58 (44.38)" - "trig (median [IQR])" "108.50 [90.25, 137.00]" - "platelet (mean (sd))" "240.13 (73.93)" - "protime (mean (sd))" "11.23 (0.97)" - "stage (%)" "" - " 1" "1 (6.7)" - " 2" "2 (13.3)" - " 3" "5 (33.3)" - " 4" "7 (46.7)" - "Stratified by sex:trt" - "" "f:2" "p" "test" - "n" "139" "" "" - "time (mean (sd))" "1979.65 (1127.52)" "0.730" "" - "status (%)" "" "0.024" "exact" - " 0" "78 (56.1)" "" "" - " 1" "9 (6.5)" "" "" - " 2" "52 (37.4)" "" "" - "age (mean (sd))" "47.66 (9.54)" "<0.001" "" - "ascites = 1 (%)" "8 (5.8)" "0.516" "" - "hepato = 0/1 (%)" "61/78 (43.9/56.1)" "0.208" "" - "spiders = 1 (%)" "44 (31.7)" "0.089" "" - "edema (%)" "" "0.906" "" - " 0" "119 (85.6)" "" "" - " 0.5" "12 (8.6)" "" "" - " 1" "8 (5.8)" "" "" - "bili (median [IQR])" "1.30 [0.70, 3.60]" "0.205" "nonnorm" - "chol (median [IQR])" "306.50 [258.00, 381.00]" "0.458" "nonnorm" - "albumin (mean (sd))" "3.53 (0.39)" "0.585" "" - "copper (median [IQR])" "69.50 [42.50, 127.25]" "<0.001" "nonnorm" - "alk.phos (median [IQR])" "1345.00 [941.00, 1992.00]" "0.180" "nonnorm" - "ast (mean (sd))" "126.30 (60.27)" "0.598" "" - "trig (median [IQR])" "113.00 [84.00, 157.00]" "0.058" "nonnorm" - "platelet (mean (sd))" "267.95 (92.20)" "0.362" "" - "protime (mean (sd))" "10.75 (1.15)" "0.137" "" - "stage (%)" "" "NA" "exact" - " 1" "3 (2.2)" "" "" - " 2" "30 (21.6)" "" "" - " 3" "59 (42.4)" "" "" - " 4" "47 (33.8)" "" "" +> ### ** Examples +> +> ## See the examples for svyCreateTableOne() > > > > base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") -> base::cat("print.TableOne", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") +> base::cat("print.svyCatTable", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") +> cleanEx() +> nameEx("print.svyContTable") +> ### * print.svyContTable +> +> flush(stderr()); flush(stdout()) +> +> base::assign(".ptime", proc.time(), pos = "CheckExEnv") +> ### Name: print.svyContTable +> ### Title: Format and print 'svyContTable' class objects +> ### Aliases: print.svyContTable +> +> ### ** Examples +> +> ## See the examples for svyCreateTableOne() +> +> +> +> base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") +> base::cat("print.svyContTable", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") > cleanEx() - -detaching ‘package:survival’, ‘package:splines’ - > nameEx("summary.CatTable") > ### * summary.CatTable > @@ -3502,94 +2766,13 @@ detaching ‘package:survival’, ‘package:splines’ > > ### ** Examples > -> ## Load -> library(tableone) -> -> ## Load Mayo Clinic Primary Biliary Cirrhosis Data -> library(survival) -Loading required package: splines -> data(pbc) -> ## Check variables -> head(pbc) - id time status trt age sex ascites hepato spiders edema bili chol -1 1 400 2 1 58.76523 f 1 1 1 1.0 14.5 261 -2 2 4500 0 1 56.44627 f 0 1 1 0.0 1.1 302 -3 3 1012 2 1 70.07255 m 0 0 0 0.5 1.4 176 -4 4 1925 2 1 54.74059 f 0 1 1 0.5 1.8 244 -5 5 1504 1 2 38.10541 f 0 1 1 0.0 3.4 279 -6 6 2503 2 2 66.25873 f 0 1 0 0.0 0.8 248 - albumin copper alk.phos ast trig platelet protime stage -1 2.60 156 1718.0 137.95 172 190 12.2 4 -2 4.14 54 7394.8 113.52 88 221 10.6 3 -3 3.48 210 516.0 96.10 55 151 12.0 4 -4 2.54 64 6121.8 60.63 92 183 10.3 4 -5 3.53 143 671.0 113.15 72 136 10.9 3 -6 3.98 50 944.0 93.00 63 NA 11.0 3 -> -> ## Create an overall table for categorical variables -> catVars <- c("status","ascites","hepato","spiders","edema","stage") -> catTableOverall <- CreateCatTable(vars = catVars, data = pbc) -> -> ## Simply typing the object name will invoke the print.CatTable method, -> ## which will show the sample size, frequencies and percentages. -> ## For 2-level variables, only the higher level is shown for simplicity. -> catTableOverall - - Overall - n 418 - status (%) - 0 232 (55.5) - 1 25 ( 6.0) - 2 161 (38.5) - ascites = 1 (%) 24 ( 7.7) - hepato = 1 (%) 160 (51.3) - spiders = 1 (%) 90 (28.8) - edema (%) - 0 354 (84.7) - 0.5 44 (10.5) - 1 20 ( 4.8) - stage (%) - 1 21 ( 5.1) - 2 92 (22.3) - 3 155 (37.6) - 4 144 (35.0) -> -> ## To further examine the variables, use the summary.CatTable method, -> ## which will show more details. -> summary(catTableOverall) -strata: Overall - var n miss p.miss level freq percent cum.percent - status 418 0 0.0 0 232 55.5 55.5 - 1 25 6.0 61.5 - 2 161 38.5 100.0 - - ascites 418 106 25.4 0 288 92.3 92.3 - 1 24 7.7 100.0 - - hepato 418 106 25.4 0 152 48.7 48.7 - 1 160 51.3 100.0 - - spiders 418 106 25.4 0 222 71.2 71.2 - 1 90 28.8 100.0 - - edema 418 0 0.0 0 354 84.7 84.7 - 0.5 44 10.5 95.2 - 1 20 4.8 100.0 - - stage 418 6 1.4 1 21 5.1 5.1 - 2 92 22.3 27.4 - 3 155 37.6 65.0 - 4 144 35.0 100.0 - +> ## See examples for CreateTableOne > > > > base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") > base::cat("summary.CatTable", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") > cleanEx() - -detaching ‘package:survival’, ‘package:splines’ - > nameEx("summary.ContTable") > ### * summary.ContTable > @@ -3602,90 +2785,13 @@ detaching ‘package:survival’, ‘package:splines’ > > ### ** Examples > -> ## Load -> library(tableone) -> -> ## Load Mayo Clinic Primary Biliary Cirrhosis Data -> library(survival) -Loading required package: splines -> data(pbc) -> ## Check variables -> head(pbc) - id time status trt age sex ascites hepato spiders edema bili chol -1 1 400 2 1 58.76523 f 1 1 1 1.0 14.5 261 -2 2 4500 0 1 56.44627 f 0 1 1 0.0 1.1 302 -3 3 1012 2 1 70.07255 m 0 0 0 0.5 1.4 176 -4 4 1925 2 1 54.74059 f 0 1 1 0.5 1.8 244 -5 5 1504 1 2 38.10541 f 0 1 1 0.0 3.4 279 -6 6 2503 2 2 66.25873 f 0 1 0 0.0 0.8 248 - albumin copper alk.phos ast trig platelet protime stage -1 2.60 156 1718.0 137.95 172 190 12.2 4 -2 4.14 54 7394.8 113.52 88 221 10.6 3 -3 3.48 210 516.0 96.10 55 151 12.0 4 -4 2.54 64 6121.8 60.63 92 183 10.3 4 -5 3.53 143 671.0 113.15 72 136 10.9 3 -6 3.98 50 944.0 93.00 63 NA 11.0 3 -> -> ## Create an overall table for continuous variables -> contVars <- c("time","age","bili","chol","albumin","copper", -+ "alk.phos","ast","trig","platelet","protime") -> contTableOverall <- CreateContTable(vars = contVars, data = pbc) -> -> ## Simply typing the object name will invoke the print.ContTable method, -> ## which will show the sample size, means and standard deviations. -> contTableOverall - - Overall - n 418 - time (mean (sd)) 1917.78 (1104.67) - age (mean (sd)) 50.74 (10.45) - bili (mean (sd)) 3.22 (4.41) - chol (mean (sd)) 369.51 (231.94) - albumin (mean (sd)) 3.50 (0.42) - copper (mean (sd)) 97.65 (85.61) - alk.phos (mean (sd)) 1982.66 (2140.39) - ast (mean (sd)) 122.56 (56.70) - trig (mean (sd)) 124.70 (65.15) - platelet (mean (sd)) 257.02 (98.33) - protime (mean (sd)) 10.73 (1.02) -> -> ## To further examine the variables, use the summary.ContTable method, -> ## which will show more details. -> summary(contTableOverall) -strata: Overall - n miss p.miss mean sd median p25 p75 min max -time 418 0 0.00 1917.8 1104.67 1730.0 1092.8 2613.5 41.0 4795.0 -age 418 0 0.00 50.7 10.45 51.0 42.8 58.2 26.3 78.4 -bili 418 0 0.00 3.2 4.41 1.4 0.8 3.4 0.3 28.0 -chol 418 134 32.06 369.5 231.94 309.5 249.5 400.0 120.0 1775.0 -albumin 418 0 0.00 3.5 0.42 3.5 3.2 3.8 2.0 4.6 -copper 418 108 25.84 97.6 85.61 73.0 41.2 123.0 4.0 588.0 -alk.phos 418 106 25.36 1982.7 2140.39 1259.0 871.5 1980.0 289.0 13862.4 -ast 418 106 25.36 122.6 56.70 114.7 80.6 151.9 26.4 457.2 -trig 418 136 32.54 124.7 65.15 108.0 84.2 151.0 33.0 598.0 -platelet 418 11 2.63 257.0 98.33 251.0 188.5 318.0 62.0 721.0 -protime 418 2 0.48 10.7 1.02 10.6 10.0 11.1 9.0 18.0 - skew kurt -time 0.473 -0.48 -age 0.087 -0.62 -bili 2.718 8.07 -chol 3.409 14.34 -albumin -0.468 0.57 -copper 2.304 7.62 -alk.phos 2.993 9.66 -ast 1.449 4.31 -trig 2.524 11.80 -platelet 0.627 0.86 -protime 2.223 10.04 +> ## See examples for CreateTableOne > > > > base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") > base::cat("summary.ContTable", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") > cleanEx() - -detaching ‘package:survival’, ‘package:splines’ - > nameEx("summary.TableOne") > ### * summary.TableOne > @@ -3693,894 +2799,471 @@ detaching ‘package:survival’, ‘package:splines’ > > base::assign(".ptime", proc.time(), pos = "CheckExEnv") > ### Name: summary.TableOne -> ### Title: Shows all results in a 'TableOne' class object +> ### Title: Shows all results in a '(svy)TableOne' class object > ### Aliases: summary.TableOne > -> ### ** Examples -> -> ## Load -> library(tableone) -> -> ## Load Mayo Clinic Primary Biliary Cirrhosis Data -> library(survival) -Loading required package: splines -> data(pbc) -> ## Check variables -> head(pbc) - id time status trt age sex ascites hepato spiders edema bili chol -1 1 400 2 1 58.76523 f 1 1 1 1.0 14.5 261 -2 2 4500 0 1 56.44627 f 0 1 1 0.0 1.1 302 -3 3 1012 2 1 70.07255 m 0 0 0 0.5 1.4 176 -4 4 1925 2 1 54.74059 f 0 1 1 0.5 1.8 244 -5 5 1504 1 2 38.10541 f 0 1 1 0.0 3.4 279 -6 6 2503 2 2 66.25873 f 0 1 0 0.0 0.8 248 - albumin copper alk.phos ast trig platelet protime stage -1 2.60 156 1718.0 137.95 172 190 12.2 4 -2 4.14 54 7394.8 113.52 88 221 10.6 3 -3 3.48 210 516.0 96.10 55 151 12.0 4 -4 2.54 64 6121.8 60.63 92 183 10.3 4 -5 3.53 143 671.0 113.15 72 136 10.9 3 -6 3.98 50 944.0 93.00 63 NA 11.0 3 -> -> ## Make categorical variables factors -> varsToFactor <- c("status","trt","ascites","hepato","spiders","edema","stage") -> pbc[varsToFactor] <- lapply(pbc[varsToFactor], factor) -> -> ## Create Table 1 stratified by sex and trt -> tableOne <- CreateTableOne(vars = c("time","status","age","ascites","hepato", -+ "spiders","edema","bili","chol","albumin", -+ "copper","alk.phos","ast","trig","platelet", -+ "protime","stage"), -+ strata = c("sex","trt"), data = pbc) -> -> ## Just typing the object name will invoke the print.TableOne method -> tableOne - Stratified by sex:trt - m:1 f:1 m:2 - n 21 137 15 - time (mean (sd)) 1793.48 (1244.70) 2049.67 (1070.20) 2156.33 (1428.56) - status (%) - 0 4 (19.0) 79 (57.7) 7 (46.7) - 1 3 (14.3) 7 ( 5.1) 0 ( 0.0) - 2 14 (66.7) 51 (37.2) 8 (53.3) - age (mean (sd)) 55.57 (12.61) 50.78 (10.65) 57.09 (10.07) - ascites = 1 (%) 1 (4.8) 13 (9.5) 2 (13.3) - hepato = 1 (%) 12 (57.1) 61 (44.5) 9 (60.0) - spiders = 1 (%) 3 (14.3) 42 (30.7) 1 (6.7) - edema (%) - 0 17 (81.0) 115 (83.9) 12 (80.0) - 0.5 3 (14.3) 13 ( 9.5) 1 ( 6.7) - 1 1 ( 4.8) 9 ( 6.6) 2 (13.3) - bili (mean (sd)) 2.98 (2.11) 2.86 (3.81) 2.72 (2.46) - chol (mean (sd)) 403.43 (204.95) 358.24 (210.46) 301.00 (111.32) - albumin (mean (sd)) 3.63 (0.43) 3.50 (0.44) 3.50 (0.49) - copper (mean (sd)) 174.90 (105.33) 85.71 (82.27) 125.40 (89.18) - alk.phos (mean (sd)) 2486.16 (2385.32) 1950.04 (2151.35) 1734.45 (2478.07) - ast (mean (sd)) 128.71 (48.74) 118.91 (55.40) 112.58 (44.38) - trig (mean (sd)) 145.81 (56.62) 120.28 (73.41) 114.86 (39.59) - platelet (mean (sd)) 232.65 (97.38) 262.59 (100.53) 240.13 (73.93) - protime (mean (sd)) 10.84 (0.94) 10.62 (0.84) 11.23 (0.97) - stage (%) - 1 2 ( 9.5) 10 ( 7.3) 1 ( 6.7) - 2 4 (19.0) 31 (22.6) 2 (13.3) - 3 7 (33.3) 49 (35.8) 5 (33.3) - 4 8 (38.1) 47 (34.3) 7 (46.7) - Stratified by sex:trt - f:2 p test - n 139 - time (mean (sd)) 1979.65 (1127.52) 0.730 - status (%) 0.033 - 0 78 (56.1) - 1 9 ( 6.5) - 2 52 (37.4) - age (mean (sd)) 47.66 (9.54) <0.001 - ascites = 1 (%) 8 (5.8) 0.516 - hepato = 1 (%) 78 (56.1) 0.208 - spiders = 1 (%) 44 (31.7) 0.089 - edema (%) 0.906 - 0 119 (85.6) - 0.5 12 ( 8.6) - 1 8 ( 5.8) - bili (mean (sd)) 3.75 (5.50) 0.394 - chol (mean (sd)) 381.73 (262.26) 0.512 - albumin (mean (sd)) 3.53 (0.39) 0.585 - copper (mean (sd)) 94.64 (79.25) <0.001 - alk.phos (mean (sd)) 1965.52 (2066.15) 0.706 - ast (mean (sd)) 126.30 (60.27) 0.598 - trig (mean (sd)) 126.38 (60.22) 0.370 - platelet (mean (sd)) 267.95 (92.20) 0.362 - protime (mean (sd)) 10.75 (1.15) 0.137 - stage (%) 0.646 - 1 3 ( 2.2) - 2 30 (21.6) - 3 59 (42.4) - 4 47 (33.8) +> ### ** Examples > -> ## Use the summary.TableOne method for detailed summary -> summary(tableOne) - - ### Summary of continuous variables ### - -sex: m -trt: 1 - n miss p.miss mean sd median p25 p75 min max skew kurt -time 21 0 0 1793 1e+03 1302 999 2386 140.0 4459 1.04 0.1 -age 21 0 0 56 1e+01 56 46 66 33.5 78 -0.20 -0.7 -bili 21 0 0 3 2e+00 2 1 4 0.6 7 1.02 -0.1 -chol 21 0 0 403 2e+02 376 251 456 168.0 1000 1.33 2.2 -albumin 21 0 0 4 4e-01 4 3 4 2.6 4 -0.97 0.3 -copper 21 0 0 175 1e+02 158 100 225 25.0 444 1.08 1.0 -alk.phos 21 0 0 2486 2e+03 1664 983 2496 516.0 10165 2.05 4.5 -ast 21 0 0 129 5e+01 127 86 158 56.8 222 0.31 -0.9 -trig 21 0 0 146 6e+01 142 107 194 55.0 242 0.16 -1.0 -platelet 21 1 5 233 1e+02 228 148 314 70.0 394 -0.05 -1.2 -protime 21 0 0 11 9e-01 11 10 11 9.7 14 2.11 6.8 ------------------------------------------------------------- -sex: f -trt: 1 - n miss p.miss mean sd median p25 p75 min max skew kurt -time 137 0 0.0 2050 1e+03 1945 1e+03 2644 41.0 4556 0.32 -0.4 -age 137 0 0.0 51 1e+01 51 4e+01 57 26.3 77 0.05 -0.5 -bili 137 0 0.0 3 4e+00 1 8e-01 3 0.3 20 2.65 7.1 -chol 137 18 13.1 358 2e+02 309 2e+02 400 127.0 1712 4.28 23.8 -albumin 137 0 0.0 3 4e-01 4 3e+00 4 2.1 5 -0.33 0.4 -copper 137 1 0.7 86 8e+01 67 4e+01 105 9.0 588 3.18 13.9 -alk.phos 137 0 0.0 1950 2e+03 1212 8e+02 1877 369.0 11552 2.88 8.4 -ast 137 0 0.0 119 6e+01 108 7e+01 150 26.4 338 1.20 1.9 -trig 137 19 13.9 120 7e+01 101 8e+01 134 33.0 598 3.28 16.0 -platelet 137 1 0.7 263 1e+02 258 2e+02 322 62.0 563 0.57 0.3 -protime 137 0 0.0 11 8e-01 11 1e+01 11 9.0 13 0.92 0.6 ------------------------------------------------------------- -sex: m -trt: 2 - n miss p.miss mean sd median p25 p75 min max skew kurt -time 15 0 0 2156 1e+03 1656 1054 3420 191.0 4427 0.3 -1.4 -age 15 0 0 57 1e+01 53 49 66 43.9 75 0.5 -1.3 -bili 15 0 0 3 2e+00 2 1 3 0.6 9 1.5 1.4 -chol 15 1 7 301 1e+02 259 236 342 151.0 546 0.9 0.4 -albumin 15 0 0 4 5e-01 4 3 4 2.3 4 -1.0 1.9 -copper 15 0 0 125 9e+01 84 52 198 13.0 281 0.5 -1.1 -alk.phos 15 0 0 1734 2e+03 1070 737 1212 577.0 10397 3.5 12.7 -ast 15 0 0 113 4e+01 110 74 129 46.5 188 0.3 -0.8 -trig 15 1 7 115 4e+01 108 90 137 49.0 188 0.4 -0.4 -platelet 15 0 0 240 7e+01 214 190 294 119.0 360 0.2 -1.0 -protime 15 0 0 11 1e+00 11 11 12 10.0 13 0.7 0.2 ------------------------------------------------------------- -sex: f -trt: 2 - n miss p.miss mean sd median p25 p75 min max skew kurt -time 139 0 0.0 1980 1e+03 1832 1e+03 2717 51.0 4523 0.3 -0.6 -age 139 0 0.0 48 1e+01 47 4e+01 55 30.6 71 0.2 -0.7 -bili 139 0 0.0 4 5e+00 1 7e-01 4 0.3 28 2.6 6.5 -chol 139 9 6.5 382 3e+02 306 3e+02 381 120.0 1775 3.0 10.1 -albumin 139 0 0.0 4 4e-01 4 3e+00 4 2.0 4 -0.8 2.1 -copper 139 1 0.7 95 8e+01 70 4e+01 127 4.0 558 2.2 8.3 -alk.phos 139 0 0.0 1966 2e+03 1345 9e+02 1992 289.0 13862 3.4 13.6 -ast 139 0 0.0 126 6e+01 118 8e+01 153 28.4 457 1.8 6.2 -trig 139 10 7.2 126 6e+01 113 8e+01 157 44.0 432 1.7 5.3 -platelet 139 2 1.4 268 9e+01 265 2e+02 324 71.0 487 0.2 -0.4 -protime 139 0 0.0 11 1e+00 11 1e+01 11 9.2 17 2.0 7.2 - -p-values - pNormal pNonNormal -time 7.300496e-01 5.566440e-01 -age 1.286381e-04 6.517437e-04 -bili 3.937430e-01 2.049525e-01 -chol 5.122223e-01 4.578773e-01 -albumin 5.853926e-01 4.041206e-01 -copper 6.237989e-05 4.712924e-05 -alk.phos 7.063812e-01 1.801480e-01 -ast 5.982898e-01 5.086616e-01 -trig 3.701073e-01 5.827726e-02 -platelet 3.624740e-01 4.451277e-01 -protime 1.365463e-01 8.114383e-02 - -======================================================================================= - - ### Summary of categorical variables ### - -sex: m -trt: 1 - var n miss p.miss level freq percent cum.percent - status 21 0 0.0 0 4 19.0 19.0 - 1 3 14.3 33.3 - 2 14 66.7 100.0 - - ascites 21 0 0.0 0 20 95.2 95.2 - 1 1 4.8 100.0 - - hepato 21 0 0.0 0 9 42.9 42.9 - 1 12 57.1 100.0 - - spiders 21 0 0.0 0 18 85.7 85.7 - 1 3 14.3 100.0 - - edema 21 0 0.0 0 17 81.0 81.0 - 0.5 3 14.3 95.2 - 1 1 4.8 100.0 - - stage 21 0 0.0 1 2 9.5 9.5 - 2 4 19.0 28.6 - 3 7 33.3 61.9 - 4 8 38.1 100.0 - ------------------------------------------------------------- -sex: f -trt: 1 - var n miss p.miss level freq percent cum.percent - status 137 0 0.0 0 79 57.7 57.7 - 1 7 5.1 62.8 - 2 51 37.2 100.0 - - ascites 137 0 0.0 0 124 90.5 90.5 - 1 13 9.5 100.0 - - hepato 137 0 0.0 0 76 55.5 55.5 - 1 61 44.5 100.0 - - spiders 137 0 0.0 0 95 69.3 69.3 - 1 42 30.7 100.0 - - edema 137 0 0.0 0 115 83.9 83.9 - 0.5 13 9.5 93.4 - 1 9 6.6 100.0 - - stage 137 0 0.0 1 10 7.3 7.3 - 2 31 22.6 29.9 - 3 49 35.8 65.7 - 4 47 34.3 100.0 - ------------------------------------------------------------- -sex: m -trt: 2 - var n miss p.miss level freq percent cum.percent - status 15 0 0.0 0 7 46.7 46.7 - 1 0 0.0 46.7 - 2 8 53.3 100.0 - - ascites 15 0 0.0 0 13 86.7 86.7 - 1 2 13.3 100.0 - - hepato 15 0 0.0 0 6 40.0 40.0 - 1 9 60.0 100.0 - - spiders 15 0 0.0 0 14 93.3 93.3 - 1 1 6.7 100.0 - - edema 15 0 0.0 0 12 80.0 80.0 - 0.5 1 6.7 86.7 - 1 2 13.3 100.0 - - stage 15 0 0.0 1 1 6.7 6.7 - 2 2 13.3 20.0 - 3 5 33.3 53.3 - 4 7 46.7 100.0 - ------------------------------------------------------------- -sex: f -trt: 2 - var n miss p.miss level freq percent cum.percent - status 139 0 0.0 0 78 56.1 56.1 - 1 9 6.5 62.6 - 2 52 37.4 100.0 - - ascites 139 0 0.0 0 131 94.2 94.2 - 1 8 5.8 100.0 - - hepato 139 0 0.0 0 61 43.9 43.9 - 1 78 56.1 100.0 - - spiders 139 0 0.0 0 95 68.3 68.3 - 1 44 31.7 100.0 - - edema 139 0 0.0 0 119 85.6 85.6 - 0.5 12 8.6 94.2 - 1 8 5.8 100.0 - - stage 139 0 0.0 1 3 2.2 2.2 - 2 30 21.6 23.7 - 3 59 42.4 66.2 - 4 47 33.8 100.0 - - -p-values - pApprox pExact -status 0.03269881 0.02420814 -ascites 0.51569424 0.43540746 -hepato 0.20805680 0.20480121 -spiders 0.08898575 0.08569292 -edema 0.90584197 0.84833223 -stage 0.64630048 NA +> ## See examples for CreateTableOne and svyCreateTableOne > > > > base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") > base::cat("summary.TableOne", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") > cleanEx() - -detaching ‘package:survival’, ‘package:splines’ - -> nameEx("tableone-package") -> ### * tableone-package +> nameEx("summary.svyCatTable") +> ### * summary.svyCatTable > > flush(stderr()); flush(stdout()) > > base::assign(".ptime", proc.time(), pos = "CheckExEnv") -> ### Name: tableone-package -> ### Title: Create "Table 1" to describe baseline characteristics -> ### Aliases: tableone tableone-package +> ### Name: summary.svyCatTable +> ### Title: Shows all results in a 'svyCatTable' class object +> ### Aliases: summary.svyCatTable > > ### ** Examples > -> ## Load -> library(tableone) -> -> ## Load Mayo Clinic Primary Biliary Cirrhosis Data -> library(survival) -Loading required package: splines -> data(pbc) -> ## Check variables -> head(pbc) - id time status trt age sex ascites hepato spiders edema bili chol -1 1 400 2 1 58.76523 f 1 1 1 1.0 14.5 261 -2 2 4500 0 1 56.44627 f 0 1 1 0.0 1.1 302 -3 3 1012 2 1 70.07255 m 0 0 0 0.5 1.4 176 -4 4 1925 2 1 54.74059 f 0 1 1 0.5 1.8 244 -5 5 1504 1 2 38.10541 f 0 1 1 0.0 3.4 279 -6 6 2503 2 2 66.25873 f 0 1 0 0.0 0.8 248 - albumin copper alk.phos ast trig platelet protime stage -1 2.60 156 1718.0 137.95 172 190 12.2 4 -2 4.14 54 7394.8 113.52 88 221 10.6 3 -3 3.48 210 516.0 96.10 55 151 12.0 4 -4 2.54 64 6121.8 60.63 92 183 10.3 4 -5 3.53 143 671.0 113.15 72 136 10.9 3 -6 3.98 50 944.0 93.00 63 NA 11.0 3 +> ## See the examples for svyCreateTableOne() > -> ## List numerically coded categorical variables for later conversion. -> ## Factor variables are automatically handled as categorical variables. -> factorVars <- c("status","trt","ascites","hepato","spiders","edema","stage") > -> ## Create a variable list -> dput(names(pbc)) # This shows a character vector-creating syntax. -c("id", "time", "status", "trt", "age", "sex", "ascites", "hepato", -"spiders", "edema", "bili", "chol", "albumin", "copper", "alk.phos", -"ast", "trig", "platelet", "protime", "stage") -> vars <- c("time","status","age","sex","ascites","hepato", -+ "spiders","edema","bili","chol","albumin", -+ "copper","alk.phos","ast","trig","platelet", -+ "protime","stage") > -> ## Create Table 1 stratified by trt. Use factorVars to convert numerically -> ## coded categorical variables as factors without changing the dataset. -> tableOne <- CreateTableOne(vars = vars, strata = c("trt"), data = pbc, -+ factorVars = factorVars) +> base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") +> base::cat("summary.svyCatTable", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") +> cleanEx() +> nameEx("summary.svyContTable") +> ### * summary.svyContTable > -> ## Just typing the object name will invoke the print.TableOne method -> tableOne - Stratified by trt - 1 2 p test - n 158 154 - time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883 - status (%) 0.894 - 0 83 (52.5) 85 (55.2) - 1 10 ( 6.3) 9 ( 5.8) - 2 65 (41.1) 60 (39.0) - age (mean (sd)) 51.42 (11.01) 48.58 (9.96) 0.018 - sex = f (%) 137 (86.7) 139 (90.3) 0.421 - ascites = 1 (%) 14 (8.9) 10 (6.5) 0.567 - hepato = 1 (%) 73 (46.2) 87 (56.5) 0.088 - spiders = 1 (%) 45 (28.5) 45 (29.2) 0.985 - edema (%) 0.877 - 0 132 (83.5) 131 (85.1) - 0.5 16 (10.1) 13 ( 8.4) - 1 10 ( 6.3) 10 ( 6.5) - bili (mean (sd)) 2.87 (3.63) 3.65 (5.28) 0.131 - chol (mean (sd)) 365.01 (209.54) 373.88 (252.48) 0.748 - albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) 0.874 - copper (mean (sd)) 97.64 (90.59) 97.65 (80.49) 0.999 - alk.phos (mean (sd)) 2021.30 (2183.44) 1943.01 (2101.69) 0.747 - ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) 0.460 - trig (mean (sd)) 124.14 (71.54) 125.25 (58.52) 0.886 - platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) 0.555 - protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) 0.197 - stage (%) 0.201 - 1 12 ( 7.6) 4 ( 2.6) - 2 35 (22.2) 32 (20.8) - 3 56 (35.4) 64 (41.6) - 4 55 (34.8) 54 (35.1) +> flush(stderr()); flush(stdout()) > -> ## Specifying nonnormal variables will show the variables appropriately, -> ## and show nonparametric test p-values. Specify variables in the exact -> ## argument to obtain the exact test p-values. For two-level categorical -> ## variables specified in cramVars, both levels are shown. Use minMax -> ## argument to show median [min, max] for nonnormal variables. -> print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), -+ exact = c("status","stage"), cramVars = "sex") - Stratified by trt - 1 2 - n 158 154 - time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) - status (%) - 0 83 (52.5) 85 (55.2) - 1 10 ( 6.3) 9 ( 5.8) - 2 65 (41.1) 60 (39.0) - age (mean (sd)) 51.42 (11.01) 48.58 (9.96) - sex = m/f (%) 21/137 (13.3/86.7) 15/139 (9.7/90.3) - ascites = 1 (%) 14 (8.9) 10 (6.5) - hepato = 1 (%) 73 (46.2) 87 (56.5) - spiders = 1 (%) 45 (28.5) 45 (29.2) - edema (%) - 0 132 (83.5) 131 (85.1) - 0.5 16 (10.1) 13 ( 8.4) - 1 10 ( 6.3) 10 ( 6.5) - bili (median [IQR]) 1.40 [0.80, 3.20] 1.30 [0.72, 3.60] - chol (median [IQR]) 315.50 [247.75, 417.00] 303.50 [254.25, 377.00] - albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) - copper (median [IQR]) 73.00 [40.00, 121.00] 73.00 [43.00, 139.00] - alk.phos (median [IQR]) 1214.50 [840.75, 2028.00] 1283.00 [922.50, 1949.75] - ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) - trig (median [IQR]) 106.00 [84.50, 146.00] 113.00 [84.50, 155.00] - platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) - protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) - stage (%) - 1 12 ( 7.6) 4 ( 2.6) - 2 35 (22.2) 32 (20.8) - 3 56 (35.4) 64 (41.6) - 4 55 (34.8) 54 (35.1) - Stratified by trt - p test - n - time (mean (sd)) 0.883 - status (%) 0.884 exact - 0 - 1 - 2 - age (mean (sd)) 0.018 - sex = m/f (%) 0.421 - ascites = 1 (%) 0.567 - hepato = 1 (%) 0.088 - spiders = 1 (%) 0.985 - edema (%) 0.877 - 0 - 0.5 - 1 - bili (median [IQR]) 0.842 nonnorm - chol (median [IQR]) 0.544 nonnorm - albumin (mean (sd)) 0.874 - copper (median [IQR]) 0.717 nonnorm - alk.phos (median [IQR]) 0.812 nonnorm - ast (mean (sd)) 0.460 - trig (median [IQR]) 0.370 nonnorm - platelet (mean (sd)) 0.555 - protime (mean (sd)) 0.197 - stage (%) 0.205 exact - 1 - 2 - 3 - 4 +> base::assign(".ptime", proc.time(), pos = "CheckExEnv") +> ### Name: summary.svyContTable +> ### Title: Shows all results in a 'svyContTable' class object +> ### Aliases: summary.svyContTable > -> ## Use the summary.TableOne method for detailed summary -> summary(tableOne) - - ### Summary of continuous variables ### - -trt: 1 - n miss p.miss mean sd median p25 p75 min max skew kurt -time 158 0 0.0 2016 1e+03 1895 1e+03 2632 41.0 4556 0.41 -0.4 -age 158 0 0.0 51 1e+01 52 4e+01 59 26.3 78 0.06 -0.5 -bili 158 0 0.0 3 4e+00 1 8e-01 3 0.3 20 2.67 7.6 -chol 158 18 11.4 365 2e+02 316 2e+02 417 127.0 1712 3.83 20.2 -albumin 158 0 0.0 4 4e-01 4 3e+00 4 2.1 5 -0.40 0.3 -copper 158 1 0.6 98 9e+01 73 4e+01 121 9.0 588 2.50 8.2 -alk.phos 158 0 0.0 2021 2e+03 1214 8e+02 2028 369.0 11552 2.71 7.4 -ast 158 0 0.0 120 5e+01 112 8e+01 152 26.4 338 1.09 1.6 -trig 158 19 12.0 124 7e+01 106 8e+01 146 33.0 598 2.95 14.3 -platelet 158 2 1.3 259 1e+02 255 2e+02 322 62.0 563 0.50 0.2 -protime 158 0 0.0 11 9e-01 11 1e+01 11 9.0 14 1.10 1.6 ------------------------------------------------------------- -trt: 2 - n miss p.miss mean sd median p25 p75 min max skew kurt -time 154 0 0.0 1997 1e+03 1811 1e+03 2771 51.0 4523 0.4 -0.7 -age 154 0 0.0 49 1e+01 48 4e+01 56 30.6 75 0.2 -0.5 -bili 154 0 0.0 4 5e+00 1 7e-01 4 0.3 28 2.7 7.3 -chol 154 10 6.5 374 3e+02 304 3e+02 377 120.0 1775 3.1 11.1 -albumin 154 0 0.0 4 4e-01 4 3e+00 4 2.0 4 -0.8 2.0 -copper 154 1 0.6 98 8e+01 73 4e+01 139 4.0 558 2.0 6.6 -alk.phos 154 0 0.0 1943 2e+03 1283 9e+02 1950 289.0 13862 3.3 12.8 -ast 154 0 0.0 125 6e+01 117 8e+01 152 28.4 457 1.7 6.3 -trig 154 11 7.1 125 6e+01 113 8e+01 155 44.0 432 1.7 5.5 -platelet 154 2 1.3 265 9e+01 260 2e+02 322 71.0 487 0.2 -0.3 -protime 154 0 0.0 11 1e+00 11 1e+01 11 9.2 17 1.9 6.4 - -p-values - pNormal pNonNormal -time 0.88304691 0.82661809 -age 0.01767247 0.01962155 -bili 0.13093942 0.84168460 -chol 0.74799072 0.54433899 -albumin 0.87388074 0.95045176 -copper 0.99915849 0.71745444 -alk.phos 0.74726165 0.81198200 -ast 0.45969842 0.45892358 -trig 0.88604213 0.36980434 -platelet 0.55451136 0.45482564 -protime 0.19714026 0.58802048 - -======================================================================================= - - ### Summary of categorical variables ### - -trt: 1 - var n miss p.miss level freq percent cum.percent - status 158 0 0.0 0 83 52.5 52.5 - 1 10 6.3 58.9 - 2 65 41.1 100.0 - - sex 158 0 0.0 m 21 13.3 13.3 - f 137 86.7 100.0 - - ascites 158 0 0.0 0 144 91.1 91.1 - 1 14 8.9 100.0 - - hepato 158 0 0.0 0 85 53.8 53.8 - 1 73 46.2 100.0 - - spiders 158 0 0.0 0 113 71.5 71.5 - 1 45 28.5 100.0 - - edema 158 0 0.0 0 132 83.5 83.5 - 0.5 16 10.1 93.7 - 1 10 6.3 100.0 - - stage 158 0 0.0 1 12 7.6 7.6 - 2 35 22.2 29.7 - 3 56 35.4 65.2 - 4 55 34.8 100.0 - ------------------------------------------------------------- -trt: 2 - var n miss p.miss level freq percent cum.percent - status 154 0 0.0 0 85 55.2 55.2 - 1 9 5.8 61.0 - 2 60 39.0 100.0 - - sex 154 0 0.0 m 15 9.7 9.7 - f 139 90.3 100.0 - - ascites 154 0 0.0 0 144 93.5 93.5 - 1 10 6.5 100.0 - - hepato 154 0 0.0 0 67 43.5 43.5 - 1 87 56.5 100.0 - - spiders 154 0 0.0 0 109 70.8 70.8 - 1 45 29.2 100.0 - - edema 154 0 0.0 0 131 85.1 85.1 - 0.5 13 8.4 93.5 - 1 10 6.5 100.0 - - stage 154 0 0.0 1 4 2.6 2.6 - 2 32 20.8 23.4 - 3 64 41.6 64.9 - 4 54 35.1 100.0 - - -p-values - pApprox pExact -status 0.89350975 0.88422188 -sex 0.42122610 0.37743235 -ascites 0.56728647 0.52558267 -hepato 0.08820884 0.07137522 -spiders 0.98466036 0.90113734 -edema 0.87681949 0.89370131 -stage 0.20129629 0.20455558 +> ### ** Examples > -> ## See the categorical part only using $ operator -> tableOne$CatTable - Stratified by trt - 1 2 p test - n 158 154 - status (%) 0.894 - 0 83 (52.5) 85 (55.2) - 1 10 ( 6.3) 9 ( 5.8) - 2 65 (41.1) 60 (39.0) - sex = f (%) 137 (86.7) 139 (90.3) 0.421 - ascites = 1 (%) 14 ( 8.9) 10 ( 6.5) 0.567 - hepato = 1 (%) 73 (46.2) 87 (56.5) 0.088 - spiders = 1 (%) 45 (28.5) 45 (29.2) 0.985 - edema (%) 0.877 - 0 132 (83.5) 131 (85.1) - 0.5 16 (10.1) 13 ( 8.4) - 1 10 ( 6.3) 10 ( 6.5) - stage (%) 0.201 - 1 12 ( 7.6) 4 ( 2.6) - 2 35 (22.2) 32 (20.8) - 3 56 (35.4) 64 (41.6) - 4 55 (34.8) 54 (35.1) -> summary(tableOne$CatTable) -trt: 1 - var n miss p.miss level freq percent cum.percent - status 158 0 0.0 0 83 52.5 52.5 - 1 10 6.3 58.9 - 2 65 41.1 100.0 - - sex 158 0 0.0 m 21 13.3 13.3 - f 137 86.7 100.0 - - ascites 158 0 0.0 0 144 91.1 91.1 - 1 14 8.9 100.0 - - hepato 158 0 0.0 0 85 53.8 53.8 - 1 73 46.2 100.0 - - spiders 158 0 0.0 0 113 71.5 71.5 - 1 45 28.5 100.0 - - edema 158 0 0.0 0 132 83.5 83.5 - 0.5 16 10.1 93.7 - 1 10 6.3 100.0 - - stage 158 0 0.0 1 12 7.6 7.6 - 2 35 22.2 29.7 - 3 56 35.4 65.2 - 4 55 34.8 100.0 - ------------------------------------------------------------- -trt: 2 - var n miss p.miss level freq percent cum.percent - status 154 0 0.0 0 85 55.2 55.2 - 1 9 5.8 61.0 - 2 60 39.0 100.0 - - sex 154 0 0.0 m 15 9.7 9.7 - f 139 90.3 100.0 - - ascites 154 0 0.0 0 144 93.5 93.5 - 1 10 6.5 100.0 - - hepato 154 0 0.0 0 67 43.5 43.5 - 1 87 56.5 100.0 - - spiders 154 0 0.0 0 109 70.8 70.8 - 1 45 29.2 100.0 - - edema 154 0 0.0 0 131 85.1 85.1 - 0.5 13 8.4 93.5 - 1 10 6.5 100.0 - - stage 154 0 0.0 1 4 2.6 2.6 - 2 32 20.8 23.4 - 3 64 41.6 64.9 - 4 54 35.1 100.0 - - -p-values - pApprox pExact -status 0.89350975 0.88422188 -sex 0.42122610 0.37743235 -ascites 0.56728647 0.52558267 -hepato 0.08820884 0.07137522 -spiders 0.98466036 0.90113734 -edema 0.87681949 0.89370131 -stage 0.20129629 0.20455558 +> ## See the examples for svyCreateTableOne() > -> ## See the continuous part only using $ operator -> tableOne$ContTable - Stratified by trt - 1 2 p test - n 158 154 - time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883 - age (mean (sd)) 51.42 (11.01) 48.58 (9.96) 0.018 - bili (mean (sd)) 2.87 (3.63) 3.65 (5.28) 0.131 - chol (mean (sd)) 365.01 (209.54) 373.88 (252.48) 0.748 - albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) 0.874 - copper (mean (sd)) 97.64 (90.59) 97.65 (80.49) 0.999 - alk.phos (mean (sd)) 2021.30 (2183.44) 1943.01 (2101.69) 0.747 - ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) 0.460 - trig (mean (sd)) 124.14 (71.54) 125.25 (58.52) 0.886 - platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) 0.555 - protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) 0.197 -> summary(tableOne$ContTable) -trt: 1 - n miss p.miss mean sd median p25 p75 min max -time 158 0 0.00 2015.6 1094.12 1895.0 1231.0 2632.5 41.0 4556.0 -age 158 0 0.00 51.4 11.01 51.9 43.0 58.9 26.3 78.4 -bili 158 0 0.00 2.9 3.63 1.4 0.8 3.2 0.3 20.0 -chol 158 18 11.39 365.0 209.54 315.5 247.8 417.0 127.0 1712.0 -albumin 158 0 0.00 3.5 0.44 3.6 3.2 3.8 2.1 4.6 -copper 158 1 0.63 97.6 90.59 73.0 40.0 121.0 9.0 588.0 -alk.phos 158 0 0.00 2021.3 2183.44 1214.5 840.8 2028.0 369.0 11552.0 -ast 158 0 0.00 120.2 54.52 111.6 76.7 151.5 26.4 338.0 -trig 158 19 12.03 124.1 71.54 106.0 84.5 146.0 33.0 598.0 -platelet 158 2 1.27 258.8 100.32 255.0 189.5 322.0 62.0 563.0 -protime 158 0 0.00 10.7 0.85 10.6 10.0 11.0 9.0 14.1 - skew kurt -time 0.412 -0.44 -age 0.056 -0.53 -bili 2.675 7.61 -chol 3.831 20.21 -albumin -0.395 0.30 -copper 2.503 8.21 -alk.phos 2.707 7.36 -ast 1.095 1.60 -trig 2.947 14.29 -platelet 0.498 0.25 -protime 1.103 1.62 +> +> +> base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") +> base::cat("summary.svyContTable", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") +> cleanEx() +> nameEx("svyCreateCatTable") +> ### * svyCreateCatTable +> +> flush(stderr()); flush(stdout()) +> +> base::assign(".ptime", proc.time(), pos = "CheckExEnv") +> ### Name: svyCreateCatTable +> ### Title: Create an object summarizing categorical variables for weighted +> ### data +> ### Aliases: svyCreateCatTable +> +> ### ** Examples +> +> ## See the examples for svyCreateTableOne() +> +> +> +> base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") +> base::cat("svyCreateCatTable", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") +> cleanEx() +> nameEx("svyCreateContTable") +> ### * svyCreateContTable +> +> flush(stderr()); flush(stdout()) +> +> base::assign(".ptime", proc.time(), pos = "CheckExEnv") +> ### Name: svyCreateContTable +> ### Title: Create an object summarizing continous variables for weighted +> ### dataset +> ### Aliases: svyCreateContTable +> +> ### ** Examples +> +> ## See the examples for svyCreateTableOne() +> +> +> +> base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") +> base::cat("svyCreateContTable", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") +> cleanEx() +> nameEx("svyCreateTableOne") +> ### * svyCreateTableOne +> +> flush(stderr()); flush(stdout()) +> +> base::assign(".ptime", proc.time(), pos = "CheckExEnv") +> ### Name: svyCreateTableOne +> ### Title: Create an object summarizing both continuous and categorical +> ### variables for weighted data +> ### Aliases: svyCreateTableOne +> +> ### ** Examples +> +> ## Load packages +> library(tableone) +> library(survey) +Loading required package: grid + +Attaching package: ‘survey’ + +The following object is masked from ‘package:graphics’: + + dotchart + +> +> ## Create a weighted survey design object +> data(nhanes) +> nhanesSvy <- svydesign(ids = ~ SDMVPSU, strata = ~ SDMVSTRA, weights = ~ WTMEC2YR, ++ nest = TRUE, data = nhanes) +> +> ## Create a table object +> ## factorVars are converted to factors; no need for variables already factors +> ## strata will stratify summaries; leave it unspecified for overall summaries +> tab1 <- svyCreateTableOne(vars = c("HI_CHOL","race","agecat","RIAGENDR"), ++ strata = "RIAGENDR", data = nhanesSvy, ++ factorVars = c("race","RIAGENDR")) +> +> ## Detailed output +> summary(tab1) + + ### Summary of continuous variables ### + +RIAGENDR: 1 + n miss p.miss mean sd median p25 p75 min max +HI_CHOL 1e+08 1e+07 7 0.1 0.3 0 0 0 0 1 ------------------------------------------------------------ -trt: 2 - n miss p.miss mean sd median p25 p75 min max -time 154 0 0.00 1996.9 1155.9 1811.0 1153.00 2771.2 51.0 4523.0 -age 154 0 0.00 48.6 10.0 48.1 41.43 55.8 30.6 74.5 -bili 154 0 0.00 3.6 5.3 1.3 0.72 3.6 0.3 28.0 -chol 154 10 6.49 373.9 252.5 303.5 254.25 377.0 120.0 1775.0 -albumin 154 0 0.00 3.5 0.4 3.5 3.34 3.8 2.0 4.4 -copper 154 1 0.65 97.7 80.5 73.0 43.00 139.0 4.0 558.0 -alk.phos 154 0 0.00 1943.0 2101.7 1283.0 922.50 1949.8 289.0 13862.4 -ast 154 0 0.00 125.0 58.9 117.4 83.78 151.9 28.4 457.2 -trig 154 11 7.14 125.3 58.5 113.0 84.50 155.0 44.0 432.0 -platelet 154 2 1.30 265.2 90.7 259.5 206.75 322.5 71.0 487.0 -protime 154 0 0.00 10.8 1.1 10.6 10.00 11.4 9.2 17.1 - skew kurt -time 0.35 -0.72 -age 0.24 -0.53 -bili 2.67 7.27 -chol 3.12 11.14 -albumin -0.85 1.96 -copper 2.01 6.65 -alk.phos 3.35 12.81 -ast 1.73 6.31 -trig 1.72 5.51 -platelet 0.23 -0.35 -protime 1.87 6.39 +RIAGENDR: 2 + n miss p.miss mean sd median p25 p75 min max +HI_CHOL 1e+08 1e+07 8 0.1 0.3 0 0 0 0 1 p-values - pNormal pNonNormal -time 0.88304691 0.82661809 -age 0.01767247 0.01962155 -bili 0.13093942 0.84168460 -chol 0.74799072 0.54433899 -albumin 0.87388074 0.95045176 -copper 0.99915849 0.71745444 -alk.phos 0.74726165 0.81198200 -ast 0.45969842 0.45892358 -trig 0.88604213 0.36980434 -platelet 0.55451136 0.45482564 -protime 0.19714026 0.58802048 + pNormal pNonNormal +HI_CHOL 0.009221135 0.009221135 + +Standardize mean differences + 1 vs 2 +HI_CHOL 0.07093003 + +======================================================================================= + + ### Summary of categorical variables ### + +RIAGENDR: 1 + var n miss p.miss level freq percent cum.percent + race 134944553.9 0.0 0.0 1 21381884.2 15.8 15.8 + 2 89315751.4 66.2 82.0 + 3 15045455.5 11.1 93.2 + 4 9201462.9 6.8 100.0 + + agecat 134944553.9 0.0 0.0 (0,19] 29299546.1 21.7 21.7 + (19,39] 40497613.1 30.0 51.7 + (39,59] 41053579.4 30.4 82.1 + (59,Inf] 24093815.3 17.9 100.0 + + RIAGENDR 134944553.9 0.0 0.0 1 134944553.9 100.0 100.0 + 2 0.0 0.0 100.0 + +------------------------------------------------------------ +RIAGENDR: 2 + var n miss p.miss level freq percent cum.percent + race 141591892.0 0.0 0.0 1 20251367.4 14.3 14.3 + 2 92486945.1 65.3 79.6 + 3 17967228.3 12.7 92.3 + 4 10886351.1 7.7 100.0 + + agecat 141591892.0 0.0 0.0 (0,19] 28150760.5 19.9 19.9 + (19,39] 40640361.5 28.7 48.6 + (39,59] 42817044.0 30.2 78.8 + (59,Inf] 29983725.9 21.2 100.0 + + RIAGENDR 141591892.0 0.0 0.0 1 0.0 0.0 0.0 + 2 141591892.0 100.0 100.0 + + +p-values + pApprox pExact +race 4.162884e-02 NA +agecat 1.175703e-03 NA +RIAGENDR 2.688704e-49 NA + +Standardize mean differences + 1 vs 2 +race 0.06878329 +agecat 0.08873652 +RIAGENDR NaN +> +> ## Default formatted printing +> tab1 + Stratified by RIAGENDR + 1 2 p test + n 134944553.92 141591892.00 + HI_CHOL (mean (sd)) 0.10 (0.30) 0.12 (0.33) 0.009 + race (%) 0.042 + 1 21381884.2 (15.8) 20251367.4 ( 14.3) + 2 89315751.4 (66.2) 92486945.1 ( 65.3) + 3 15045455.5 (11.1) 17967228.3 ( 12.7) + 4 9201462.9 ( 6.8) 10886351.1 ( 7.7) + agecat (%) 0.001 + (0,19] 29299546.1 (21.7) 28150760.5 ( 19.9) + (19,39] 40497613.1 (30.0) 40640361.5 ( 28.7) + (39,59] 41053579.4 (30.4) 42817044.0 ( 30.2) + (59,Inf] 24093815.3 (17.9) 29983725.9 ( 21.2) + RIAGENDR = 2 (%) 0.0 ( 0.0) 141591892.0 (100.0) <0.001 +> +> ## nonnormal specifies variables to be shown as median [IQR] +> print(tab1, nonnormal = "HI_CHOL", contDigits = 3, catDigits = 2, ++ pDigits = 4, smd = TRUE) + Stratified by RIAGENDR + 1 + n 134944553.923 + HI_CHOL (median [IQR]) 0.000 [0.000, 0.000] + race (%) + 1 21381884.19 (15.84) + 2 89315751.41 (66.19) + 3 15045455.46 (11.15) + 4 9201462.86 ( 6.82) + agecat (%) + (0,19] 29299546.11 (21.71) + (19,39] 40497613.07 (30.01) + (39,59] 41053579.41 (30.42) + (59,Inf] 24093815.33 (17.85) + RIAGENDR = 2 (%) 0.00 ( 0.00) + Stratified by RIAGENDR + 2 p test SMD + n 141591891.998 + HI_CHOL (median [IQR]) 0.000 [0.000, 0.000] 0.0092 nonnorm 0.0709 + race (%) 0.0416 0.0688 + 1 20251367.39 ( 14.30) + 2 92486945.14 ( 65.32) + 3 17967228.32 ( 12.69) + 4 10886351.15 ( 7.69) + agecat (%) 0.0012 0.0887 + (0,19] 28150760.54 ( 19.88) + (19,39] 40640361.53 ( 28.70) + (39,59] 42817044.01 ( 30.24) + (59,Inf] 29983725.90 ( 21.18) + RIAGENDR = 2 (%) 141591892.00 (100.00) <0.0001 NaN +> +> ## minMax changes it to median [min, max] +> print(tab1, nonnormal = "HI_CHOL", minMax = TRUE, contDigits = 3, ++ catDigits = 2, pDigits = 4, smd = TRUE) + Stratified by RIAGENDR + 1 + n 134944553.923 + HI_CHOL (median [range]) 0.000 [0.000, 1.000] + race (%) + 1 21381884.19 (15.84) + 2 89315751.41 (66.19) + 3 15045455.46 (11.15) + 4 9201462.86 ( 6.82) + agecat (%) + (0,19] 29299546.11 (21.71) + (19,39] 40497613.07 (30.01) + (39,59] 41053579.41 (30.42) + (59,Inf] 24093815.33 (17.85) + RIAGENDR = 2 (%) 0.00 ( 0.00) + Stratified by RIAGENDR + 2 p test SMD + n 141591891.998 + HI_CHOL (median [range]) 0.000 [0.000, 1.000] 0.0092 nonnorm 0.0709 + race (%) 0.0416 0.0688 + 1 20251367.39 ( 14.30) + 2 92486945.14 ( 65.32) + 3 17967228.32 ( 12.69) + 4 10886351.15 ( 7.69) + agecat (%) 0.0012 0.0887 + (0,19] 28150760.54 ( 19.88) + (19,39] 40640361.53 ( 28.70) + (39,59] 42817044.01 ( 30.24) + (59,Inf] 29983725.90 ( 21.18) + RIAGENDR = 2 (%) 141591892.00 (100.00) <0.0001 NaN +> +> ## showAllLevels can be used tow show levels for all categorical variables +> print(tab1, showAllLevels = TRUE, smd = TRUE) + Stratified by RIAGENDR + level 1 2 + n 134944553.92 141591892.00 + HI_CHOL (mean (sd)) 0.10 (0.30) 0.12 (0.33) + race (%) 1 21381884.2 ( 15.8) 20251367.4 ( 14.3) + 2 89315751.4 ( 66.2) 92486945.1 ( 65.3) + 3 15045455.5 ( 11.1) 17967228.3 ( 12.7) + 4 9201462.9 ( 6.8) 10886351.1 ( 7.7) + agecat (%) (0,19] 29299546.1 ( 21.7) 28150760.5 ( 19.9) + (19,39] 40497613.1 ( 30.0) 40640361.5 ( 28.7) + (39,59] 41053579.4 ( 30.4) 42817044.0 ( 30.2) + (59,Inf] 24093815.3 ( 17.9) 29983725.9 ( 21.2) + RIAGENDR (%) 1 134944553.9 (100.0) 0.0 ( 0.0) + 2 0.0 ( 0.0) 141591892.0 (100.0) + Stratified by RIAGENDR + p test SMD + n + HI_CHOL (mean (sd)) 0.009 0.071 + race (%) 0.042 0.069 + + + + agecat (%) 0.001 0.089 + + + + RIAGENDR (%) <0.001 NaN + > -> ## If your work flow includes copying to Excel and Word when writing manuscripts, -> ## you may benefit from the quote argument. This will quote everything so that -> ## Excel does not mess up the cells. -> print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), -+ exact = c("status","stage"), cramVars = "sex", quote = TRUE) - "Stratified by trt" - "" "1" - "n" "158" - "time (mean (sd))" "2015.62 (1094.12)" - "status (%)" " " - " 0" " 83 (52.5) " - " 1" " 10 ( 6.3) " - " 2" " 65 (41.1) " - "age (mean (sd))" " 51.42 (11.01)" - "sex = m/f (%)" " 21/137 (13.3/86.7) " - "ascites = 1 (%)" " 14 (8.9) " - "hepato = 1 (%)" " 73 (46.2) " - "spiders = 1 (%)" " 45 (28.5) " - "edema (%)" " " - " 0" " 132 (83.5) " - " 0.5" " 16 (10.1) " - " 1" " 10 ( 6.3) " - "bili (median [IQR])" " 1.40 [0.80, 3.20]" - "chol (median [IQR])" " 315.50 [247.75, 417.00]" - "albumin (mean (sd))" " 3.52 (0.44)" - "copper (median [IQR])" " 73.00 [40.00, 121.00]" - "alk.phos (median [IQR])" "1214.50 [840.75, 2028.00]" - "ast (mean (sd))" " 120.21 (54.52)" - "trig (median [IQR])" " 106.00 [84.50, 146.00]" - "platelet (mean (sd))" " 258.75 (100.32)" - "protime (mean (sd))" " 10.65 (0.85)" - "stage (%)" " " - " 1" " 12 ( 7.6) " - " 2" " 35 (22.2) " - " 3" " 56 (35.4) " - " 4" " 55 (34.8) " - "Stratified by trt" - "" "2" "p" "test" - "n" "154" "" "" - "time (mean (sd))" "1996.86 (1155.93)" " 0.883" "" - "status (%)" " " " 0.884" "exact" - " 0" " 85 (55.2) " "" "" - " 1" " 9 ( 5.8) " "" "" - " 2" " 60 (39.0) " "" "" - "age (mean (sd))" " 48.58 (9.96)" " 0.018" "" - "sex = m/f (%)" " 15/139 (9.7/90.3) " " 0.421" "" - "ascites = 1 (%)" " 10 (6.5) " " 0.567" "" - "hepato = 1 (%)" " 87 (56.5) " " 0.088" "" - "spiders = 1 (%)" " 45 (29.2) " " 0.985" "" - "edema (%)" " " " 0.877" "" - " 0" " 131 (85.1) " "" "" - " 0.5" " 13 ( 8.4) " "" "" - " 1" " 10 ( 6.5) " "" "" - "bili (median [IQR])" " 1.30 [0.72, 3.60]" " 0.842" "nonnorm" - "chol (median [IQR])" " 303.50 [254.25, 377.00]" " 0.544" "nonnorm" - "albumin (mean (sd))" " 3.52 (0.40)" " 0.874" "" - "copper (median [IQR])" " 73.00 [43.00, 139.00]" " 0.717" "nonnorm" - "alk.phos (median [IQR])" "1283.00 [922.50, 1949.75]" " 0.812" "nonnorm" - "ast (mean (sd))" " 124.97 (58.93)" " 0.460" "" - "trig (median [IQR])" " 113.00 [84.50, 155.00]" " 0.370" "nonnorm" - "platelet (mean (sd))" " 265.20 (90.73)" " 0.555" "" - "protime (mean (sd))" " 10.80 (1.14)" " 0.197" "" - "stage (%)" " " " 0.205" "exact" - " 1" " 4 ( 2.6) " "" "" - " 2" " 32 (20.8) " "" "" - " 3" " 64 (41.6) " "" "" - " 4" " 54 (35.1) " "" "" +> ## To see all printing options +> ?print.TableOne +print.TableOne package:tableone R Documentation + +_F_o_r_m_a_t _a_n_d _p_r_i_n_t '_T_a_b_l_e_O_n_e' _c_l_a_s_s _o_b_j_e_c_t_s + +_D_e_s_c_r_i_p_t_i_o_n: + + ‘print’ method for the ‘TableOne’ class objects created by + ‘CreateTableOne’ function. + +_U_s_a_g_e: + + ## S3 method for class 'TableOne' + print(x, catDigits = 1, contDigits = 2, pDigits = 3, + quote = FALSE, missing = FALSE, explain = TRUE, printToggle = TRUE, + test = TRUE, smd = FALSE, noSpaces = FALSE, format = c("fp", "f", "p", + "pf")[1], showAllLevels = FALSE, cramVars = NULL, exact = NULL, + nonnormal = NULL, minMax = FALSE, ...) + +_A_r_g_u_m_e_n_t_s: + + x: Object returned by ‘CreateTableOne’ function. + +catDigits: Number of digits to print for proportions. Default 1. + +contDigits: Number of digits to print for continuous variables. Default + 2. + + pDigits: Number of digits to print for p-values (also used for + standardized mean differences). Default 3. + + quote: Whether to show everything in quotes. The default is FALSE. + If TRUE, everything including the row and column names are + quoted so that you can copy it to Excel easily. + + missing: Whether to show missing data information (not implemented + yet, placeholder) + + explain: Whether to add explanation to the variable names, i.e., (%) + is added to the variable names when percentage is shown. + +printToggle: Whether to print the output. If FLASE, no output is + created, and a matrix is invisibly returned. + + test: Whether to show p-values. TRUE by default. If FALSE, only the + numerical summaries are shown. + + smd: Whether to show standardized mean differences. FALSE by + default. If there are more than one contrasts, the average of + all possible standardized mean differences is shown. For + individual contrasts, use ‘summary’. + +noSpaces: Whether to remove spaces added for alignment. Use this option + if you prefer to align numbers yourself in other software. + + format: The default is "fp" frequency (percentage). You can also + choose from "f" frequency only, "p" percentage only, and "pf" + percentage (frequency). + +showAllLevels: Whether to show all levels. FALSE by default, i.e., for + 2-level categorical variables, only the higher level is shown + to avoid redundant information. + +cramVars: A character vector to specify the two-level categorical + variables, for which both levels should be shown in one row. + + exact: A character vector to specify the variables for which the + p-values should be those of exact tests. By default all + p-values are from large sample approximation tests + (chisq.test). + +nonnormal: A character vector to specify the variables for which the + p-values should be those of nonparametric tests. By default + all p-values are from normal assumption-based tests + (oneway.test). + + minMax: Whether to use [min,max] instead of [p25,p75] for nonnormal + variables. The default is FALSE. + + ...: For compatibility with generic. Ignored. + +_V_a_l_u_e: + + A matrix object containing what you see is also invisibly + returned. This can be assinged a name and exported via + ‘write.csv’. + +_A_u_t_h_o_r(_s): + + Kazuki Yoshida, Justin Bohn + +_S_e_e _A_l_s_o: + + ‘CreateTableOne’, ‘CreateTableOne’, ‘summary.TableOne’ + +_E_x_a_m_p_l_e_s: + + ## See examples for CreateTableOne and svyCreateTableOne + + > -> ## If you want to center-align values in Word, use noSpaces option. -> print(tableOne, nonnormal = c("bili","chol","copper","alk.phos","trig"), -+ exact = c("status","stage"), cramVars = "sex", quote = TRUE, noSpaces = TRUE) - "Stratified by trt" - "" "1" - "n" "158" - "time (mean (sd))" "2015.62 (1094.12)" - "status (%)" "" - " 0" "83 (52.5)" - " 1" "10 (6.3)" - " 2" "65 (41.1)" - "age (mean (sd))" "51.42 (11.01)" - "sex = m/f (%)" "21/137 (13.3/86.7)" - "ascites = 1 (%)" "14 (8.9)" - "hepato = 1 (%)" "73 (46.2)" - "spiders = 1 (%)" "45 (28.5)" - "edema (%)" "" - " 0" "132 (83.5)" - " 0.5" "16 (10.1)" - " 1" "10 (6.3)" - "bili (median [IQR])" "1.40 [0.80, 3.20]" - "chol (median [IQR])" "315.50 [247.75, 417.00]" - "albumin (mean (sd))" "3.52 (0.44)" - "copper (median [IQR])" "73.00 [40.00, 121.00]" - "alk.phos (median [IQR])" "1214.50 [840.75, 2028.00]" - "ast (mean (sd))" "120.21 (54.52)" - "trig (median [IQR])" "106.00 [84.50, 146.00]" - "platelet (mean (sd))" "258.75 (100.32)" - "protime (mean (sd))" "10.65 (0.85)" - "stage (%)" "" - " 1" "12 (7.6)" - " 2" "35 (22.2)" - " 3" "56 (35.4)" - " 4" "55 (34.8)" - "Stratified by trt" - "" "2" "p" "test" - "n" "154" "" "" - "time (mean (sd))" "1996.86 (1155.93)" "0.883" "" - "status (%)" "" "0.884" "exact" - " 0" "85 (55.2)" "" "" - " 1" "9 (5.8)" "" "" - " 2" "60 (39.0)" "" "" - "age (mean (sd))" "48.58 (9.96)" "0.018" "" - "sex = m/f (%)" "15/139 (9.7/90.3)" "0.421" "" - "ascites = 1 (%)" "10 (6.5)" "0.567" "" - "hepato = 1 (%)" "87 (56.5)" "0.088" "" - "spiders = 1 (%)" "45 (29.2)" "0.985" "" - "edema (%)" "" "0.877" "" - " 0" "131 (85.1)" "" "" - " 0.5" "13 (8.4)" "" "" - " 1" "10 (6.5)" "" "" - "bili (median [IQR])" "1.30 [0.72, 3.60]" "0.842" "nonnorm" - "chol (median [IQR])" "303.50 [254.25, 377.00]" "0.544" "nonnorm" - "albumin (mean (sd))" "3.52 (0.40)" "0.874" "" - "copper (median [IQR])" "73.00 [43.00, 139.00]" "0.717" "nonnorm" - "alk.phos (median [IQR])" "1283.00 [922.50, 1949.75]" "0.812" "nonnorm" - "ast (mean (sd))" "124.97 (58.93)" "0.460" "" - "trig (median [IQR])" "113.00 [84.50, 155.00]" "0.370" "nonnorm" - "platelet (mean (sd))" "265.20 (90.73)" "0.555" "" - "protime (mean (sd))" "10.80 (1.14)" "0.197" "" - "stage (%)" "" "0.205" "exact" - " 1" "4 (2.6)" "" "" - " 2" "32 (20.8)" "" "" - " 3" "64 (41.6)" "" "" - " 4" "54 (35.1)" "" "" +> ## To examine categorical variables only +> tab1$CatTable + Stratified by RIAGENDR + 1 2 p test + n 134944553.9 141591892.0 + race (%) 0.042 + 1 21381884.2 (15.8) 20251367.4 ( 14.3) + 2 89315751.4 (66.2) 92486945.1 ( 65.3) + 3 15045455.5 (11.1) 17967228.3 ( 12.7) + 4 9201462.9 ( 6.8) 10886351.1 ( 7.7) + agecat (%) 0.001 + (0,19] 29299546.1 (21.7) 28150760.5 ( 19.9) + (19,39] 40497613.1 (30.0) 40640361.5 ( 28.7) + (39,59] 41053579.4 (30.4) 42817044.0 ( 30.2) + (59,Inf] 24093815.3 (17.9) 29983725.9 ( 21.2) + RIAGENDR = 2 (%) 0.0 ( 0.0) 141591892.0 (100.0) <0.001 +> +> ## To examine continuous variables only +> tab1$ContTable + Stratified by RIAGENDR + 1 2 p test + n 134944553.92 141591892.00 + HI_CHOL (mean (sd)) 0.10 (0.30) 0.12 (0.33) 0.009 +> +> ## If SMDs are needed as numericals, use ExtractSmd() +> ExtractSmd(tab1) + HI_CHOL race agecat RIAGENDR +0.07093003 0.06878329 0.08873652 NaN +> +> +> +> base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") +> base::cat("svyCreateTableOne", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") +> cleanEx() + +detaching ‘package:survey’, ‘package:grid’ + +> nameEx("tableone-package") +> ### * tableone-package +> +> flush(stderr()); flush(stdout()) +> +> base::assign(".ptime", proc.time(), pos = "CheckExEnv") +> ### Name: tableone-package +> ### Title: Create "Table 1" to describe baseline characteristics +> ### Aliases: tableone tableone-package +> +> ### ** Examples +> +> ## See examples for CreateTableOne and svyCreateTableOne > > > @@ -4590,7 +3273,7 @@ protime 0.19714026 0.58802048 > ### > options(digits = 7L) > base::cat("Time elapsed: ", proc.time() - base::get("ptime", pos = 'CheckExEnv'),"\n") -Time elapsed: 6.968 0.167 7.23 0 0 +Time elapsed: 4.533 0.144 4.746 0.005 0.007 > grDevices::dev.off() null device 1 diff --git a/test-all.txt b/test-all.txt new file mode 100644 index 0000000..c7a8a55 --- /dev/null +++ b/test-all.txt @@ -0,0 +1,2650 @@ +Unit tests for the CreateTableOne function : ................. Stratified by trt + 1 2 p test + n 158 154 + time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883 + status (%) 0.894 + 0 83 (52.5) 85 (55.2) + 1 10 ( 6.3) 9 ( 5.8) + 2 65 (41.1) 60 (39.0) + age (mean (sd)) 51.42 (11.01) 48.58 (9.96) 0.018 + sex = f (%) 137 (86.7) 139 (90.3) 0.421 + ascites = 1 (%) 14 ( 8.9) 10 ( 6.5) 0.567 + hepato = 1 (%) 73 (46.2) 87 (56.5) 0.088 + spiders = 1 (%) 45 (28.5) 45 (29.2) 0.985 + edema (%) 0.877 + 0 132 (83.5) 131 (85.1) + 0.5 16 (10.1) 13 ( 8.4) + 1 10 ( 6.3) 10 ( 6.5) + bili (mean (sd)) 2.87 (3.63) 3.65 (5.28) 0.131 + chol (mean (sd)) 365.01 (209.54) 373.88 (252.48) 0.748 + albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) 0.874 + copper (mean (sd)) 97.64 (90.59) 97.65 (80.49) 0.999 + alk.phos (mean (sd)) 2021.30 (2183.44) 1943.01 (2101.69) 0.747 + ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) 0.460 + trig (mean (sd)) 124.14 (71.54) 125.25 (58.52) 0.886 + platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) 0.555 + protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) 0.197 + stage (%) 0.201 + 1 12 ( 7.6) 4 ( 2.6) + 2 35 (22.2) 32 (20.8) + 3 56 (35.4) 64 (41.6) + 4 55 (34.8) 54 (35.1) +. + Overall + n 418 + time (mean (sd)) 1917.78 (1104.67) + status (%) + 0 232 (55.5) + 1 25 ( 6.0) + 2 161 (38.5) + age (mean (sd)) 50.74 (10.45) + sex = f (%) 374 (89.5) + ascites = 1 (%) 24 ( 7.7) + hepato = 1 (%) 160 (51.3) + spiders = 1 (%) 90 (28.8) + edema (%) + 0 354 (84.7) + 0.5 44 (10.5) + 1 20 ( 4.8) + bili (mean (sd)) 3.22 (4.41) + chol (mean (sd)) 369.51 (231.94) + albumin (mean (sd)) 3.50 (0.42) + copper (mean (sd)) 97.65 (85.61) + alk.phos (mean (sd)) 1982.66 (2140.39) + ast (mean (sd)) 122.56 (56.70) + trig (mean (sd)) 124.70 (65.15) + platelet (mean (sd)) 257.02 (98.33) + protime (mean (sd)) 10.73 (1.02) + stage (%) + 1 21 ( 5.1) + 2 92 (22.3) + 3 155 (37.6) + 4 144 (35.0) +. + Overall + n 418 + time (mean (sd)) 1917.78 (1104.67) + status (%) + 0 232 (55.5) + 1 25 ( 6.0) + 2 161 (38.5) + age (mean (sd)) 50.74 (10.45) + sex = f (%) 374 (89.5) + ascites (%) + 0 288 (68.9) + 1 24 ( 5.7) + NA 106 (25.4) + hepato (%) + 0 152 (36.4) + 1 160 (38.3) + NA 106 (25.4) + spiders (%) + 0 222 (53.1) + 1 90 (21.5) + NA 106 (25.4) + edema (%) + 0 354 (84.7) + 0.5 44 (10.5) + 1 20 ( 4.8) + bili (mean (sd)) 3.22 (4.41) + chol (mean (sd)) 369.51 (231.94) + albumin (mean (sd)) 3.50 (0.42) + copper (mean (sd)) 97.65 (85.61) + alk.phos (mean (sd)) 1982.66 (2140.39) + ast (mean (sd)) 122.56 (56.70) + trig (mean (sd)) 124.70 (65.15) + platelet (mean (sd)) 257.02 (98.33) + protime (mean (sd)) 10.73 (1.02) + stage (%) + 1 21 ( 5.0) + 2 92 (22.0) + 3 155 (37.1) + 4 144 (34.4) + NA 6 ( 1.4) +. Stratified by trt:sex + 1:m 2:m 1:f 2:f p test + n 21 15 137 139 + time (mean (sd)) 1793.48 (1244.70) 2156.33 (1428.56) 2049.67 (1070.20) 1979.65 (1127.52) 0.730 + status (%) 0.033 + 0 4 (19.0) 7 (46.7) 79 ( 57.7) 78 ( 56.1) + 1 3 (14.3) 0 ( 0.0) 7 ( 5.1) 9 ( 6.5) + 2 14 (66.7) 8 (53.3) 51 ( 37.2) 52 ( 37.4) + age (mean (sd)) 55.57 (12.61) 57.09 (10.07) 50.78 (10.65) 47.66 (9.54) <0.001 + sex = f (%) 0 ( 0.0) 0 ( 0.0) 137 (100.0) 139 (100.0) <0.001 + ascites = 1 (%) 1 ( 4.8) 2 (13.3) 13 ( 9.5) 8 ( 5.8) 0.516 + hepato = 1 (%) 12 (57.1) 9 (60.0) 61 ( 44.5) 78 ( 56.1) 0.208 + spiders = 1 (%) 3 (14.3) 1 ( 6.7) 42 ( 30.7) 44 ( 31.7) 0.089 + edema (%) 0.906 + 0 17 (81.0) 12 (80.0) 115 ( 83.9) 119 ( 85.6) + 0.5 3 (14.3) 1 ( 6.7) 13 ( 9.5) 12 ( 8.6) + 1 1 ( 4.8) 2 (13.3) 9 ( 6.6) 8 ( 5.8) + bili (mean (sd)) 2.98 (2.11) 2.72 (2.46) 2.86 (3.81) 3.75 (5.50) 0.394 + chol (mean (sd)) 403.43 (204.95) 301.00 (111.32) 358.24 (210.46) 381.73 (262.26) 0.512 + albumin (mean (sd)) 3.63 (0.43) 3.50 (0.49) 3.50 (0.44) 3.53 (0.39) 0.585 + copper (mean (sd)) 174.90 (105.33) 125.40 (89.18) 85.71 (82.27) 94.64 (79.25) <0.001 + alk.phos (mean (sd)) 2486.16 (2385.32) 1734.45 (2478.07) 1950.04 (2151.35) 1965.52 (2066.15) 0.706 + ast (mean (sd)) 128.71 (48.74) 112.58 (44.38) 118.91 (55.40) 126.30 (60.27) 0.598 + trig (mean (sd)) 145.81 (56.62) 114.86 (39.59) 120.28 (73.41) 126.38 (60.22) 0.370 + platelet (mean (sd)) 232.65 (97.38) 240.13 (73.93) 262.59 (100.53) 267.95 (92.20) 0.362 + protime (mean (sd)) 10.84 (0.94) 11.23 (0.97) 10.62 (0.84) 10.75 (1.15) 0.137 + stage (%) 0.646 + 1 2 ( 9.5) 1 ( 6.7) 10 ( 7.3) 3 ( 2.2) + 2 4 (19.0) 2 (13.3) 31 ( 22.6) 30 ( 21.6) + 3 7 (33.3) 5 (33.3) 49 ( 35.8) 59 ( 42.4) + 4 8 (38.1) 7 (46.7) 47 ( 34.3) 47 ( 33.8) +. Stratified by trt + 1 2 p test + n 158 154 + time (mean (sd)) 2015.6203 (1094.1233) 1996.8636 (1155.9289) 0.88305 + status (%) 0.89351 + 0 83 (52.532) 85 (55.195) + 1 10 ( 6.329) 9 ( 5.844) + 2 65 (41.139) 60 (38.961) + age (mean (sd)) 51.4191 (11.0072) 48.5825 (9.9578) 0.01767 + sex = f (%) 137 (86.709) 139 (90.260) 0.42123 + ascites = 1 (%) 14 ( 8.861) 10 ( 6.494) 0.56729 + hepato = 1 (%) 73 (46.203) 87 (56.494) 0.08821 + spiders = 1 (%) 45 (28.481) 45 (29.221) 0.98466 + edema (%) 0.87682 + 0 132 (83.544) 131 (85.065) + 0.5 16 (10.127) 13 ( 8.442) + 1 10 ( 6.329) 10 ( 6.494) + bili (mean (sd)) 2.8734 (3.6289) 3.6487 (5.2819) 0.13094 + chol (mean (sd)) 365.0143 (209.5439) 373.8819 (252.4846) 0.74799 + albumin (mean (sd)) 3.5163 (0.4433) 3.5238 (0.3958) 0.87388 + copper (mean (sd)) 97.6433 (90.5901) 97.6536 (80.4865) 0.99916 + alk.phos (mean (sd)) 2021.2975 (2183.4358) 1943.0104 (2101.6873) 0.74726 + ast (mean (sd)) 120.2087 (54.5214) 124.9650 (58.9313) 0.45970 + trig (mean (sd)) 124.1367 (71.5391) 125.2517 (58.5211) 0.88604 + platelet (mean (sd)) 258.7500 (100.3247) 265.2039 (90.7294) 0.55451 + protime (mean (sd)) 10.6532 (0.8514) 10.8000 (1.1382) 0.19714 + stage (%) 0.20130 + 1 12 ( 7.595) 4 ( 2.597) + 2 35 (22.152) 32 (20.779) + 3 56 (35.443) 64 (41.558) + 4 55 (34.810) 54 (35.065) +. Stratified by trt + 1 2 + n 158 154 + time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) + status (%) + 0 83 (52.5) 85 (55.2) + 1 10 ( 6.3) 9 ( 5.8) + 2 65 (41.1) 60 (39.0) + age (mean (sd)) 51.42 (11.01) 48.58 (9.96) + sex = f (%) 137 (86.7) 139 (90.3) + ascites = 1 (%) 14 ( 8.9) 10 ( 6.5) + hepato = 1 (%) 73 (46.2) 87 (56.5) + spiders = 1 (%) 45 (28.5) 45 (29.2) + edema (%) + 0 132 (83.5) 131 (85.1) + 0.5 16 (10.1) 13 ( 8.4) + 1 10 ( 6.3) 10 ( 6.5) + bili (mean (sd)) 2.87 (3.63) 3.65 (5.28) + chol (mean (sd)) 365.01 (209.54) 373.88 (252.48) + albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) + copper (mean (sd)) 97.64 (90.59) 97.65 (80.49) + alk.phos (mean (sd)) 2021.30 (2183.44) 1943.01 (2101.69) + ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) + trig (mean (sd)) 124.14 (71.54) 125.25 (58.52) + platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) + protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) + stage (%) + 1 12 ( 7.6) 4 ( 2.6) + 2 35 (22.2) 32 (20.8) + 3 56 (35.4) 64 (41.6) + 4 55 (34.8) 54 (35.1) +. Stratified by trt + 1 2 p test + n 158 154 + time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883 + status (%) 0.884 exact + 0 83 (52.5) 85 (55.2) + 1 10 ( 6.3) 9 ( 5.8) + 2 65 (41.1) 60 (39.0) + age (mean (sd)) 51.42 (11.01) 48.58 (9.96) 0.018 + sex = f (%) 137 (86.7) 139 (90.3) 0.421 + ascites = 1 (%) 14 ( 8.9) 10 ( 6.5) 0.567 + hepato = 1 (%) 73 (46.2) 87 (56.5) 0.088 + spiders = 1 (%) 45 (28.5) 45 (29.2) 0.985 + edema (%) 0.877 + 0 132 (83.5) 131 (85.1) + 0.5 16 (10.1) 13 ( 8.4) + 1 10 ( 6.3) 10 ( 6.5) + bili (median [IQR]) 1.40 [0.80, 3.20] 1.30 [0.72, 3.60] 0.842 nonnorm + chol (median [IQR]) 315.50 [247.75, 417.00] 303.50 [254.25, 377.00] 0.544 nonnorm + albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) 0.874 + copper (median [IQR]) 73.00 [40.00, 121.00] 73.00 [43.00, 139.00] 0.717 nonnorm + alk.phos (median [IQR]) 1214.50 [840.75, 2028.00] 1283.00 [922.50, 1949.75] 0.812 nonnorm + ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) 0.460 + trig (median [IQR]) 106.00 [84.50, 146.00] 113.00 [84.50, 155.00] 0.370 nonnorm + platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) 0.555 + protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) 0.197 + stage (%) 0.205 exact + 1 12 ( 7.6) 4 ( 2.6) + 2 35 (22.2) 32 (20.8) + 3 56 (35.4) 64 (41.6) + 4 55 (34.8) 54 (35.1) +. Stratified by trt + 1 2 p test + n 158 154 + time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883 + status (%) 0.894 + 0 83 (52.5) 85 (55.2) + 1 10 ( 6.3) 9 ( 5.8) + 2 65 (41.1) 60 (39.0) + age (mean (sd)) 51.42 (11.01) 48.58 (9.96) 0.018 + sex = f (%) 137 (86.7) 139 (90.3) 0.421 + ascites = 1 (%) 14 ( 8.9) 10 ( 6.5) 0.567 + hepato = 1 (%) 73 (46.2) 87 (56.5) 0.088 + spiders = 1 (%) 45 (28.5) 45 (29.2) 0.985 + edema (%) 0.877 + 0 132 (83.5) 131 (85.1) + 0.5 16 (10.1) 13 ( 8.4) + 1 10 ( 6.3) 10 ( 6.5) + bili (median [range]) 1.40 [0.30, 20.00] 1.30 [0.30, 28.00] 0.842 nonnorm + chol (median [range]) 315.50 [127.00, 1712.00] 303.50 [120.00, 1775.00] 0.544 nonnorm + albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) 0.874 + copper (median [range]) 73.00 [9.00, 588.00] 73.00 [4.00, 558.00] 0.717 nonnorm + alk.phos (median [range]) 1214.50 [369.00, 11552.00] 1283.00 [289.00, 13862.40] 0.812 nonnorm + ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) 0.460 + trig (median [range]) 106.00 [33.00, 598.00] 113.00 [44.00, 432.00] 0.370 nonnorm + platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) 0.555 + protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) 0.197 + stage (%) 0.201 + 1 12 ( 7.6) 4 ( 2.6) + 2 35 (22.2) 32 (20.8) + 3 56 (35.4) 64 (41.6) + 4 55 (34.8) 54 (35.1) +. Stratified by trt + 1 2 p test + n 158 154 + time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883 + status (%) 0.884 exact + 0 83 (52.5) 85 (55.2) + 1 10 (6.3) 9 (5.8) + 2 65 (41.1) 60 (39.0) + age (mean (sd)) 51.42 (11.01) 48.58 (9.96) 0.018 + sex = f (%) 137 (86.7) 139 (90.3) 0.421 + ascites = 1 (%) 14 (8.9) 10 (6.5) 0.567 + hepato = 1 (%) 73 (46.2) 87 (56.5) 0.088 + spiders = 1 (%) 45 (28.5) 45 (29.2) 0.985 + edema (%) 0.877 + 0 132 (83.5) 131 (85.1) + 0.5 16 (10.1) 13 (8.4) + 1 10 (6.3) 10 (6.5) + bili (median [IQR]) 1.40 [0.80, 3.20] 1.30 [0.72, 3.60] 0.842 nonnorm + chol (median [IQR]) 315.50 [247.75, 417.00] 303.50 [254.25, 377.00] 0.544 nonnorm + albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) 0.874 + copper (median [IQR]) 73.00 [40.00, 121.00] 73.00 [43.00, 139.00] 0.717 nonnorm + alk.phos (median [IQR]) 1214.50 [840.75, 2028.00] 1283.00 [922.50, 1949.75] 0.812 nonnorm + ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) 0.460 + trig (median [IQR]) 106.00 [84.50, 146.00] 113.00 [84.50, 155.00] 0.370 nonnorm + platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) 0.555 + protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) 0.197 + stage (%) 0.205 exact + 1 12 (7.6) 4 (2.6) + 2 35 (22.2) 32 (20.8) + 3 56 (35.4) 64 (41.6) + 4 55 (34.8) 54 (35.1) +. Stratified by trt + level 1 2 p test + n 158 154 + time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883 + status (%) 0 83 (52.5) 85 (55.2) 0.884 exact + 1 10 ( 6.3) 9 ( 5.8) + 2 65 (41.1) 60 (39.0) + age (mean (sd)) 51.42 (11.01) 48.58 (9.96) 0.018 + sex (%) m 21 (13.3) 15 ( 9.7) 0.421 + f 137 (86.7) 139 (90.3) + ascites (%) 0 144 (91.1) 144 (93.5) 0.567 + 1 14 ( 8.9) 10 ( 6.5) + hepato (%) 0 85 (53.8) 67 (43.5) 0.088 + 1 73 (46.2) 87 (56.5) + spiders (%) 0 113 (71.5) 109 (70.8) 0.985 + 1 45 (28.5) 45 (29.2) + edema (%) 0 132 (83.5) 131 (85.1) 0.877 + 0.5 16 (10.1) 13 ( 8.4) + 1 10 ( 6.3) 10 ( 6.5) + bili (median [IQR]) 1.40 [0.80, 3.20] 1.30 [0.72, 3.60] 0.842 nonnorm + chol (median [IQR]) 315.50 [247.75, 417.00] 303.50 [254.25, 377.00] 0.544 nonnorm + albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) 0.874 + copper (median [IQR]) 73.00 [40.00, 121.00] 73.00 [43.00, 139.00] 0.717 nonnorm + alk.phos (median [IQR]) 1214.50 [840.75, 2028.00] 1283.00 [922.50, 1949.75] 0.812 nonnorm + ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) 0.460 + trig (median [IQR]) 106.00 [84.50, 146.00] 113.00 [84.50, 155.00] 0.370 nonnorm + platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) 0.555 + protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) 0.197 + stage (%) 1 12 ( 7.6) 4 ( 2.6) 0.205 exact + 2 35 (22.2) 32 (20.8) + 3 56 (35.4) 64 (41.6) + 4 55 (34.8) 54 (35.1) +. "Stratified by trt" + "" "1" "2" "p" "test" + "n" "158" "154" "" "" + "time (mean (sd))" "2015.62 (1094.12)" "1996.86 (1155.93)" "0.883" "" + "status (%)" "" "" "0.884" "exact" + " 0" "83 (52.5)" "85 (55.2)" "" "" + " 1" "10 (6.3)" "9 (5.8)" "" "" + " 2" "65 (41.1)" "60 (39.0)" "" "" + "age (mean (sd))" "51.42 (11.01)" "48.58 (9.96)" "0.018" "" + "sex = f (%)" "137 (86.7)" "139 (90.3)" "0.421" "" + "ascites = 1 (%)" "14 (8.9)" "10 (6.5)" "0.567" "" + "hepato = 1 (%)" "73 (46.2)" "87 (56.5)" "0.088" "" + "spiders = 1 (%)" "45 (28.5)" "45 (29.2)" "0.985" "" + "edema (%)" "" "" "0.877" "" + " 0" "132 (83.5)" "131 (85.1)" "" "" + " 0.5" "16 (10.1)" "13 (8.4)" "" "" + " 1" "10 (6.3)" "10 (6.5)" "" "" + "bili (median [IQR])" "1.40 [0.80, 3.20]" "1.30 [0.72, 3.60]" "0.842" "nonnorm" + "chol (median [IQR])" "315.50 [247.75, 417.00]" "303.50 [254.25, 377.00]" "0.544" "nonnorm" + "albumin (mean (sd))" "3.52 (0.44)" "3.52 (0.40)" "0.874" "" + "copper (median [IQR])" "73.00 [40.00, 121.00]" "73.00 [43.00, 139.00]" "0.717" "nonnorm" + "alk.phos (median [IQR])" "1214.50 [840.75, 2028.00]" "1283.00 [922.50, 1949.75]" "0.812" "nonnorm" + "ast (mean (sd))" "120.21 (54.52)" "124.97 (58.93)" "0.460" "" + "trig (median [IQR])" "106.00 [84.50, 146.00]" "113.00 [84.50, 155.00]" "0.370" "nonnorm" + "platelet (mean (sd))" "258.75 (100.32)" "265.20 (90.73)" "0.555" "" + "protime (mean (sd))" "10.65 (0.85)" "10.80 (1.14)" "0.197" "" + "stage (%)" "" "" "0.205" "exact" + " 1" "12 (7.6)" "4 (2.6)" "" "" + " 2" "35 (22.2)" "32 (20.8)" "" "" + " 3" "56 (35.4)" "64 (41.6)" "" "" + " 4" "55 (34.8)" "54 (35.1)" "" "" +. Stratified by trt:sex + 1:m 2:m 1:f 2:f p test + n 21 15 137 139 + time (mean (sd)) 1793.48 (1244.70) 2156.33 (1428.56) 2049.67 (1070.20) 1979.65 (1127.52) 0.730 + age (mean (sd)) 55.57 (12.61) 57.09 (10.07) 50.78 (10.65) 47.66 (9.54) <0.001 + protime (mean (sd)) 10.84 (0.94) 11.23 (0.97) 10.62 (0.84) 10.75 (1.15) 0.137 +. Stratified by trt:sex + 1:m 2:m 1:f 2:f p test + n 21 15 137 139 + status (%) 0.033 + 0 4 (19.0) 7 ( 46.7) 79 ( 57.7) 78 ( 56.1) + 1 3 (14.3) 0 ( 0.0) 7 ( 5.1) 9 ( 6.5) + 2 14 (66.7) 8 ( 53.3) 51 ( 37.2) 52 ( 37.4) + trt = 2 (%) 0 ( 0.0) 15 (100.0) 0 ( 0.0) 139 (100.0) <0.001 + sex = f (%) 0 ( 0.0) 0 ( 0.0) 137 (100.0) 139 (100.0) <0.001 +. Stratified by trt + 1 2 p test + n 158 154 + status (%) 0.894 + 0 83 (52.5) 85 (55.2) + 1 10 ( 6.3) 9 ( 5.8) + 2 65 (41.1) 60 (39.0) + sex = f (%) 137 (86.7) 139 (90.3) 0.421 + ascites = 1 (%) 14 ( 8.9) 10 ( 6.5) 0.567 + hepato = 1 (%) 73 (46.2) 87 (56.5) 0.088 + spiders = 1 (%) 45 (28.5) 45 (29.2) 0.985 + edema (%) 0.877 + 0 132 (83.5) 131 (85.1) + 0.5 16 (10.1) 13 ( 8.4) + 1 10 ( 6.3) 10 ( 6.5) + stage (%) 0.201 + 1 12 ( 7.6) 4 ( 2.6) + 2 35 (22.2) 32 (20.8) + 3 56 (35.4) 64 (41.6) + 4 55 (34.8) 54 (35.1) +. + Overall + n 418 + status (%) + 0 232 (55.5) + 1 25 ( 6.0) + 2 161 (38.5) + sex = f (%) 374 (89.5) + ascites = 1 (%) 24 ( 7.7) + hepato = 1 (%) 160 (51.3) + spiders = 1 (%) 90 (28.8) + edema (%) + 0 354 (84.7) + 0.5 44 (10.5) + 1 20 ( 4.8) + stage (%) + 1 21 ( 5.1) + 2 92 (22.3) + 3 155 (37.6) + 4 144 (35.0) +. + Overall + n 418 + status (%) + 0 232 (55.5) + 1 25 ( 6.0) + 2 161 (38.5) + sex = f (%) 374 (89.5) + ascites (%) + 0 288 (68.9) + 1 24 ( 5.7) + NA 106 (25.4) + hepato (%) + 0 152 (36.4) + 1 160 (38.3) + NA 106 (25.4) + spiders (%) + 0 222 (53.1) + 1 90 (21.5) + NA 106 (25.4) + edema (%) + 0 354 (84.7) + 0.5 44 (10.5) + 1 20 ( 4.8) + stage (%) + 1 21 ( 5.0) + 2 92 (22.0) + 3 155 (37.1) + 4 144 (34.4) + NA 6 ( 1.4) +. Stratified by trt:sex + 1:m 2:m 1:f 2:f p test + n 21 15 137 139 + status (%) 0.033 + 0 4 (19.0) 7 (46.7) 79 ( 57.7) 78 ( 56.1) + 1 3 (14.3) 0 ( 0.0) 7 ( 5.1) 9 ( 6.5) + 2 14 (66.7) 8 (53.3) 51 ( 37.2) 52 ( 37.4) + sex = f (%) 0 ( 0.0) 0 ( 0.0) 137 (100.0) 139 (100.0) <0.001 + ascites = 1 (%) 1 ( 4.8) 2 (13.3) 13 ( 9.5) 8 ( 5.8) 0.516 + hepato = 1 (%) 12 (57.1) 9 (60.0) 61 ( 44.5) 78 ( 56.1) 0.208 + spiders = 1 (%) 3 (14.3) 1 ( 6.7) 42 ( 30.7) 44 ( 31.7) 0.089 + edema (%) 0.906 + 0 17 (81.0) 12 (80.0) 115 ( 83.9) 119 ( 85.6) + 0.5 3 (14.3) 1 ( 6.7) 13 ( 9.5) 12 ( 8.6) + 1 1 ( 4.8) 2 (13.3) 9 ( 6.6) 8 ( 5.8) + stage (%) 0.646 + 1 2 ( 9.5) 1 ( 6.7) 10 ( 7.3) 3 ( 2.2) + 2 4 (19.0) 2 (13.3) 31 ( 22.6) 30 ( 21.6) + 3 7 (33.3) 5 (33.3) 49 ( 35.8) 59 ( 42.4) + 4 8 (38.1) 7 (46.7) 47 ( 34.3) 47 ( 33.8) +. Stratified by trt:sex + 1:m 2:m 1:f 2:f p test + n 21 15 137 139 + status (%) 0.03270 + 0 4 (19.048) 7 (46.667) 79 ( 57.664) 78 ( 56.115) + 1 3 (14.286) 0 ( 0.000) 7 ( 5.109) 9 ( 6.475) + 2 14 (66.667) 8 (53.333) 51 ( 37.226) 52 ( 37.410) + sex = f (%) 0 ( 0.000) 0 ( 0.000) 137 (100.000) 139 (100.000) <0.00001 + ascites = 1 (%) 1 ( 4.762) 2 (13.333) 13 ( 9.489) 8 ( 5.755) 0.51569 + hepato = 1 (%) 12 (57.143) 9 (60.000) 61 ( 44.526) 78 ( 56.115) 0.20806 + spiders = 1 (%) 3 (14.286) 1 ( 6.667) 42 ( 30.657) 44 ( 31.655) 0.08899 + edema (%) 0.90584 + 0 17 (80.952) 12 (80.000) 115 ( 83.942) 119 ( 85.612) + 0.5 3 (14.286) 1 ( 6.667) 13 ( 9.489) 12 ( 8.633) + 1 1 ( 4.762) 2 (13.333) 9 ( 6.569) 8 ( 5.755) + stage (%) 0.64630 + 1 2 ( 9.524) 1 ( 6.667) 10 ( 7.299) 3 ( 2.158) + 2 4 (19.048) 2 (13.333) 31 ( 22.628) 30 ( 21.583) + 3 7 (33.333) 5 (33.333) 49 ( 35.766) 59 ( 42.446) + 4 8 (38.095) 7 (46.667) 47 ( 34.307) 47 ( 33.813) +. Stratified by trt:sex + 1:m 2:m 1:f 2:f + n 21 15 137 139 + status (%) + 0 4 (19.0) 7 (46.7) 79 ( 57.7) 78 ( 56.1) + 1 3 (14.3) 0 ( 0.0) 7 ( 5.1) 9 ( 6.5) + 2 14 (66.7) 8 (53.3) 51 ( 37.2) 52 ( 37.4) + sex = f (%) 0 ( 0.0) 0 ( 0.0) 137 (100.0) 139 (100.0) + ascites = 1 (%) 1 ( 4.8) 2 (13.3) 13 ( 9.5) 8 ( 5.8) + hepato = 1 (%) 12 (57.1) 9 (60.0) 61 ( 44.5) 78 ( 56.1) + spiders = 1 (%) 3 (14.3) 1 ( 6.7) 42 ( 30.7) 44 ( 31.7) + edema (%) + 0 17 (81.0) 12 (80.0) 115 ( 83.9) 119 ( 85.6) + 0.5 3 (14.3) 1 ( 6.7) 13 ( 9.5) 12 ( 8.6) + 1 1 ( 4.8) 2 (13.3) 9 ( 6.6) 8 ( 5.8) + stage (%) + 1 2 ( 9.5) 1 ( 6.7) 10 ( 7.3) 3 ( 2.2) + 2 4 (19.0) 2 (13.3) 31 ( 22.6) 30 ( 21.6) + 3 7 (33.3) 5 (33.3) 49 ( 35.8) 59 ( 42.4) + 4 8 (38.1) 7 (46.7) 47 ( 34.3) 47 ( 33.8) +. Stratified by trt + 1 2 p test + n 158 154 + status (%) 0.894 + 0 83 (52.5) 85 (55.2) + 1 10 (6.3) 9 (5.8) + 2 65 (41.1) 60 (39.0) + sex = f (%) 137 (86.7) 139 (90.3) 0.421 + ascites = 1 (%) 14 (8.9) 10 (6.5) 0.567 + hepato = 1 (%) 73 (46.2) 87 (56.5) 0.088 + spiders = 1 (%) 45 (28.5) 45 (29.2) 0.985 + edema (%) 0.877 + 0 132 (83.5) 131 (85.1) + 0.5 16 (10.1) 13 (8.4) + 1 10 (6.3) 10 (6.5) + stage (%) 0.201 + 1 12 (7.6) 4 (2.6) + 2 35 (22.2) 32 (20.8) + 3 56 (35.4) 64 (41.6) + 4 55 (34.8) 54 (35.1) +. Stratified by trt + level 1 2 p test + n 158 154 + status (%) 0 83 (52.5) 85 (55.2) 0.894 + 1 10 ( 6.3) 9 ( 5.8) + 2 65 (41.1) 60 (39.0) + sex (%) m 21 (13.3) 15 ( 9.7) 0.421 + f 137 (86.7) 139 (90.3) + ascites (%) 0 144 (91.1) 144 (93.5) 0.567 + 1 14 ( 8.9) 10 ( 6.5) + hepato (%) 0 85 (53.8) 67 (43.5) 0.088 + 1 73 (46.2) 87 (56.5) + spiders (%) 0 113 (71.5) 109 (70.8) 0.985 + 1 45 (28.5) 45 (29.2) + edema (%) 0 132 (83.5) 131 (85.1) 0.877 + 0.5 16 (10.1) 13 ( 8.4) + 1 10 ( 6.3) 10 ( 6.5) + stage (%) 1 12 ( 7.6) 4 ( 2.6) 0.201 + 2 35 (22.2) 32 (20.8) + 3 56 (35.4) 64 (41.6) + 4 55 (34.8) 54 (35.1) +. Stratified by trt + 1 2 p test + n 158 154 + status 0.894 + 0 83 (52.5) 85 (55.2) + 1 10 ( 6.3) 9 ( 5.8) + 2 65 (41.1) 60 (39.0) + sex = f 137 (86.7) 139 (90.3) 0.421 + ascites = 1 14 ( 8.9) 10 ( 6.5) 0.567 + hepato = 1 73 (46.2) 87 (56.5) 0.088 + spiders = 1 45 (28.5) 45 (29.2) 0.985 + edema 0.877 + 0 132 (83.5) 131 (85.1) + 0.5 16 (10.1) 13 ( 8.4) + 1 10 ( 6.3) 10 ( 6.5) + stage 0.201 + 1 12 ( 7.6) 4 ( 2.6) + 2 35 (22.2) 32 (20.8) + 3 56 (35.4) 64 (41.6) + 4 55 (34.8) 54 (35.1) +. Stratified by trt + 1 2 p test + n 158 154 + status 0.894 + 0 83 85 + 1 10 9 + 2 65 60 + sex = f 137 139 0.421 + ascites = 1 14 10 0.567 + hepato = 1 73 87 0.088 + spiders = 1 45 45 0.985 + edema 0.877 + 0 132 131 + 0.5 16 13 + 1 10 10 + stage 0.201 + 1 12 4 + 2 35 32 + 3 56 64 + 4 55 54 +. Stratified by trt + 1 2 p test + n 158 154 + status (%) 0.894 + 0 52.5 55.2 + 1 6.3 5.8 + 2 41.1 39.0 + sex = f (%) 86.7 90.3 0.421 + ascites = 1 (%) 8.9 6.5 0.567 + hepato = 1 (%) 46.2 56.5 0.088 + spiders = 1 (%) 28.5 29.2 0.985 + edema (%) 0.877 + 0 83.5 85.1 + 0.5 10.1 8.4 + 1 6.3 6.5 + stage (%) 0.201 + 1 7.6 2.6 + 2 22.2 20.8 + 3 35.4 41.6 + 4 34.8 35.1 +. Stratified by trt + 1 2 p test + n 158 154 + status % (freq) 0.894 + 0 52.5 ( 83) 55.2 ( 85) + 1 6.3 ( 10) 5.8 ( 9) + 2 41.1 ( 65) 39.0 ( 60) + sex = f % (freq) 86.7 (137) 90.3 (139) 0.421 + ascites = 1 % (freq) 8.9 ( 14) 6.5 ( 10) 0.567 + hepato = 1 % (freq) 46.2 ( 73) 56.5 ( 87) 0.088 + spiders = 1 % (freq) 28.5 ( 45) 29.2 ( 45) 0.985 + edema % (freq) 0.877 + 0 83.5 (132) 85.1 (131) + 0.5 10.1 ( 16) 8.4 ( 13) + 1 6.3 ( 10) 6.5 ( 10) + stage % (freq) 0.201 + 1 7.6 ( 12) 2.6 ( 4) + 2 22.2 ( 35) 20.8 ( 32) + 3 35.4 ( 56) 41.6 ( 64) + 4 34.8 ( 55) 35.1 ( 54) +. Stratified by trt + 1 2 p test + n 158 154 + status (%) 0.894 + 0 83 (52.5) 85 (55.2) + 1 10 ( 6.3) 9 ( 5.8) + 2 65 (41.1) 60 (39.0) + sex = m/f (%) 21/137 (13.3/86.7) 15/139 (9.7/90.3) 0.421 + ascites = 1 (%) 14 ( 8.9) 10 ( 6.5) 0.567 + hepato = 1 (%) 73 (46.2) 87 (56.5) 0.088 + spiders = 1 (%) 45 (28.5) 45 (29.2) 0.985 + edema (%) 0.877 + 0 132 (83.5) 131 (85.1) + 0.5 16 (10.1) 13 ( 8.4) + 1 10 ( 6.3) 10 ( 6.5) + stage (%) 0.201 + 1 12 ( 7.6) 4 ( 2.6) + 2 35 (22.2) 32 (20.8) + 3 56 (35.4) 64 (41.6) + 4 55 (34.8) 54 (35.1) +. "Stratified by trt" + "" "level" "1" "2" "p" "test" + "n" "" "158" "154" "" "" + "status (%)" "0" "83 (52.5)" "85 (55.2)" "0.894" "" + "" "1" "10 (6.3)" "9 (5.8)" "" "" + "" "2" "65 (41.1)" "60 (39.0)" "" "" + "sex (%)" "m" "21 (13.3)" "15 (9.7)" "0.421" "" + "" "f" "137 (86.7)" "139 (90.3)" "" "" + "ascites (%)" "0" "144 (91.1)" "144 (93.5)" "0.567" "" + "" "1" "14 (8.9)" "10 (6.5)" "" "" + "hepato (%)" "0" "85 (53.8)" "67 (43.5)" "0.088" "" + "" "1" "73 (46.2)" "87 (56.5)" "" "" + "spiders (%)" "0" "113 (71.5)" "109 (70.8)" "0.985" "" + "" "1" "45 (28.5)" "45 (29.2)" "" "" + "edema (%)" "0" "132 (83.5)" "131 (85.1)" "0.877" "" + "" "0.5" "16 (10.1)" "13 (8.4)" "" "" + "" "1" "10 (6.3)" "10 (6.5)" "" "" + "stage (%)" "1" "12 (7.6)" "4 (2.6)" "0.201" "" + "" "2" "35 (22.2)" "32 (20.8)" "" "" + "" "3" "56 (35.4)" "64 (41.6)" "" "" + "" "4" "55 (34.8)" "54 (35.1)" "" "" +. Stratified by trt + 1 2 p test + n 158 154 + status (%) 0.894 + 0 83 (52.5) 85 (55.2) + 1 10 ( 6.3) 9 ( 5.8) + 2 65 (41.1) 60 (39.0) + sex = f (%) 137 (86.7) 139 (90.3) 0.421 + ascites = 1 (%) 14 ( 8.9) 10 ( 6.5) 0.567 + hepato = 1 (%) 73 (46.2) 87 (56.5) 0.088 + spiders = 1 (%) 45 (28.5) 45 (29.2) 0.985 + edema (%) 0.877 + 0 132 (83.5) 131 (85.1) + 0.5 16 (10.1) 13 ( 8.4) + 1 10 ( 6.3) 10 ( 6.5) + stage (%) 0.201 + 1 12 ( 7.6) 4 ( 2.6) + 2 35 (22.2) 32 (20.8) + 3 56 (35.4) 64 (41.6) + 4 55 (34.8) 54 (35.1) + + + Cell Contents +|-------------------------| +| N | +| Chi-square contribution | +| N / Row Total | +| N / Col Total | +| N / Table Total | +|-------------------------| + + +Total Observations in Table: 312 + + + | trt + status | 1 | 2 | Row Total | +-------------|-----------|-----------|-----------| + 0 | 83 | 85 | 168 | + | 0.051 | 0.052 | | + | 0.494 | 0.506 | 0.538 | + | 0.525 | 0.552 | | + | 0.266 | 0.272 | | +-------------|-----------|-----------|-----------| + 1 | 10 | 9 | 19 | + | 0.015 | 0.015 | | + | 0.526 | 0.474 | 0.061 | + | 0.063 | 0.058 | | + | 0.032 | 0.029 | | +-------------|-----------|-----------|-----------| + 2 | 65 | 60 | 125 | + | 0.046 | 0.047 | | + | 0.520 | 0.480 | 0.401 | + | 0.411 | 0.390 | | + | 0.208 | 0.192 | | +-------------|-----------|-----------|-----------| +Column Total | 158 | 154 | 312 | + | 0.506 | 0.494 | | +-------------|-----------|-----------|-----------| + + + + + Cell Contents +|-------------------------| +| N | +| Chi-square contribution | +| N / Row Total | +| N / Col Total | +| N / Table Total | +|-------------------------| + + +Total Observations in Table: 312 + + + | trt + sex | 1 | 2 | Row Total | +-------------|-----------|-----------|-----------| + m | 21 | 15 | 36 | + | 0.421 | 0.432 | | + | 0.583 | 0.417 | 0.115 | + | 0.133 | 0.097 | | + | 0.067 | 0.048 | | +-------------|-----------|-----------|-----------| + f | 137 | 139 | 276 | + | 0.055 | 0.056 | | + | 0.496 | 0.504 | 0.885 | + | 0.867 | 0.903 | | + | 0.439 | 0.446 | | +-------------|-----------|-----------|-----------| +Column Total | 158 | 154 | 312 | + | 0.506 | 0.494 | | +-------------|-----------|-----------|-----------| + + + + + Cell Contents +|-------------------------| +| N | +| Chi-square contribution | +| N / Row Total | +| N / Col Total | +| N / Table Total | +|-------------------------| + + +Total Observations in Table: 312 + + + | trt + ascites | 1 | 2 | Row Total | +-------------|-----------|-----------|-----------| + 0 | 144 | 144 | 288 | + | 0.023 | 0.024 | | + | 0.500 | 0.500 | 0.923 | + | 0.911 | 0.935 | | + | 0.462 | 0.462 | | +-------------|-----------|-----------|-----------| + 1 | 14 | 10 | 24 | + | 0.280 | 0.288 | | + | 0.583 | 0.417 | 0.077 | + | 0.089 | 0.065 | | + | 0.045 | 0.032 | | +-------------|-----------|-----------|-----------| +Column Total | 158 | 154 | 312 | + | 0.506 | 0.494 | | +-------------|-----------|-----------|-----------| + + + + + Cell Contents +|-------------------------| +| N | +| Chi-square contribution | +| N / Row Total | +| N / Col Total | +| N / Table Total | +|-------------------------| + + +Total Observations in Table: 312 + + + | trt + hepato | 1 | 2 | Row Total | +-------------|-----------|-----------|-----------| + 0 | 85 | 67 | 152 | + | 0.837 | 0.859 | | + | 0.559 | 0.441 | 0.487 | + | 0.538 | 0.435 | | + | 0.272 | 0.215 | | +-------------|-----------|-----------|-----------| + 1 | 73 | 87 | 160 | + | 0.795 | 0.816 | | + | 0.456 | 0.544 | 0.513 | + | 0.462 | 0.565 | | + | 0.234 | 0.279 | | +-------------|-----------|-----------|-----------| +Column Total | 158 | 154 | 312 | + | 0.506 | 0.494 | | +-------------|-----------|-----------|-----------| + + + + + Cell Contents +|-------------------------| +| N | +| Chi-square contribution | +| N / Row Total | +| N / Col Total | +| N / Table Total | +|-------------------------| + + +Total Observations in Table: 312 + + + | trt + spiders | 1 | 2 | Row Total | +-------------|-----------|-----------|-----------| + 0 | 113 | 109 | 222 | + | 0.003 | 0.003 | | + | 0.509 | 0.491 | 0.712 | + | 0.715 | 0.708 | | + | 0.362 | 0.349 | | +-------------|-----------|-----------|-----------| + 1 | 45 | 45 | 90 | + | 0.007 | 0.007 | | + | 0.500 | 0.500 | 0.288 | + | 0.285 | 0.292 | | + | 0.144 | 0.144 | | +-------------|-----------|-----------|-----------| +Column Total | 158 | 154 | 312 | + | 0.506 | 0.494 | | +-------------|-----------|-----------|-----------| + + + + + Cell Contents +|-------------------------| +| N | +| Chi-square contribution | +| N / Row Total | +| N / Col Total | +| N / Table Total | +|-------------------------| + + +Total Observations in Table: 312 + + + | trt + edema | 1 | 2 | Row Total | +-------------|-----------|-----------|-----------| + 0 | 132 | 131 | 263 | + | 0.011 | 0.011 | | + | 0.502 | 0.498 | 0.843 | + | 0.835 | 0.851 | | + | 0.423 | 0.420 | | +-------------|-----------|-----------|-----------| + 0.5 | 16 | 13 | 29 | + | 0.118 | 0.121 | | + | 0.552 | 0.448 | 0.093 | + | 0.101 | 0.084 | | + | 0.051 | 0.042 | | +-------------|-----------|-----------|-----------| + 1 | 10 | 10 | 20 | + | 0.002 | 0.002 | | + | 0.500 | 0.500 | 0.064 | + | 0.063 | 0.065 | | + | 0.032 | 0.032 | | +-------------|-----------|-----------|-----------| +Column Total | 158 | 154 | 312 | + | 0.506 | 0.494 | | +-------------|-----------|-----------|-----------| + + + + + Cell Contents +|-------------------------| +| N | +| Chi-square contribution | +| N / Row Total | +| N / Col Total | +| N / Table Total | +|-------------------------| + + +Total Observations in Table: 312 + + + | trt + stage | 1 | 2 | Row Total | +-------------|-----------|-----------|-----------| + 1 | 12 | 4 | 16 | + | 1.875 | 1.923 | | + | 0.750 | 0.250 | 0.051 | + | 0.076 | 0.026 | | + | 0.038 | 0.013 | | +-------------|-----------|-----------|-----------| + 2 | 35 | 32 | 67 | + | 0.034 | 0.035 | | + | 0.522 | 0.478 | 0.215 | + | 0.222 | 0.208 | | + | 0.112 | 0.103 | | +-------------|-----------|-----------|-----------| + 3 | 56 | 64 | 120 | + | 0.374 | 0.384 | | + | 0.467 | 0.533 | 0.385 | + | 0.354 | 0.416 | | + | 0.179 | 0.205 | | +-------------|-----------|-----------|-----------| + 4 | 55 | 54 | 109 | + | 0.001 | 0.001 | | + | 0.505 | 0.495 | 0.349 | + | 0.348 | 0.351 | | + | 0.176 | 0.173 | | +-------------|-----------|-----------|-----------| +Column Total | 158 | 154 | 312 | + | 0.506 | 0.494 | | +-------------|-----------|-----------|-----------| + + +. Stratified by trt + 1 2 p test + n 158 154 + time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883 + age (mean (sd)) 51.42 (11.01) 48.58 (9.96) 0.018 + bili (mean (sd)) 2.87 (3.63) 3.65 (5.28) 0.131 + chol (mean (sd)) 365.01 (209.54) 373.88 (252.48) 0.748 + albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) 0.874 + copper (mean (sd)) 97.64 (90.59) 97.65 (80.49) 0.999 + alk.phos (mean (sd)) 2021.30 (2183.44) 1943.01 (2101.69) 0.747 + ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) 0.460 + trig (mean (sd)) 124.14 (71.54) 125.25 (58.52) 0.886 + platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) 0.555 + protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) 0.197 +. + Overall + n 418 + time (mean (sd)) 1917.78 (1104.67) + age (mean (sd)) 50.74 (10.45) + bili (mean (sd)) 3.22 (4.41) + chol (mean (sd)) 369.51 (231.94) + albumin (mean (sd)) 3.50 (0.42) + copper (mean (sd)) 97.65 (85.61) + alk.phos (mean (sd)) 1982.66 (2140.39) + ast (mean (sd)) 122.56 (56.70) + trig (mean (sd)) 124.70 (65.15) + platelet (mean (sd)) 257.02 (98.33) + protime (mean (sd)) 10.73 (1.02) +. Stratified by trt:sex + 1:m 2:m 1:f 2:f p test + n 21 15 137 139 + time (mean (sd)) 1793.48 (1244.70) 2156.33 (1428.56) 2049.67 (1070.20) 1979.65 (1127.52) 0.730 + age (mean (sd)) 55.57 (12.61) 57.09 (10.07) 50.78 (10.65) 47.66 (9.54) <0.001 + bili (mean (sd)) 2.98 (2.11) 2.72 (2.46) 2.86 (3.81) 3.75 (5.50) 0.394 + chol (mean (sd)) 403.43 (204.95) 301.00 (111.32) 358.24 (210.46) 381.73 (262.26) 0.512 + albumin (mean (sd)) 3.63 (0.43) 3.50 (0.49) 3.50 (0.44) 3.53 (0.39) 0.585 + copper (mean (sd)) 174.90 (105.33) 125.40 (89.18) 85.71 (82.27) 94.64 (79.25) <0.001 + alk.phos (mean (sd)) 2486.16 (2385.32) 1734.45 (2478.07) 1950.04 (2151.35) 1965.52 (2066.15) 0.706 + ast (mean (sd)) 128.71 (48.74) 112.58 (44.38) 118.91 (55.40) 126.30 (60.27) 0.598 + trig (mean (sd)) 145.81 (56.62) 114.86 (39.59) 120.28 (73.41) 126.38 (60.22) 0.370 + platelet (mean (sd)) 232.65 (97.38) 240.13 (73.93) 262.59 (100.53) 267.95 (92.20) 0.362 + protime (mean (sd)) 10.84 (0.94) 11.23 (0.97) 10.62 (0.84) 10.75 (1.15) 0.137 +. Stratified by trt + 1 2 p test + n 158 154 + time (mean (sd)) 2015.620 (1094.123) 1996.864 (1155.929) 0.88305 + age (mean (sd)) 51.419 (11.007) 48.583 (9.958) 0.01767 + bili (mean (sd)) 2.873 (3.629) 3.649 (5.282) 0.13094 + chol (mean (sd)) 365.014 (209.544) 373.882 (252.485) 0.74799 + albumin (mean (sd)) 3.516 (0.443) 3.524 (0.396) 0.87388 + copper (mean (sd)) 97.643 (90.590) 97.654 (80.486) 0.99916 + alk.phos (mean (sd)) 2021.297 (2183.436) 1943.010 (2101.687) 0.74726 + ast (mean (sd)) 120.209 (54.521) 124.965 (58.931) 0.45970 + trig (mean (sd)) 124.137 (71.539) 125.252 (58.521) 0.88604 + platelet (mean (sd)) 258.750 (100.325) 265.204 (90.729) 0.55451 + protime (mean (sd)) 10.653 (0.851) 10.800 (1.138) 0.19714 +. Stratified by trt + 1 2 + n 158 154 + time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) + age (mean (sd)) 51.42 (11.01) 48.58 (9.96) + bili (mean (sd)) 2.87 (3.63) 3.65 (5.28) + chol (mean (sd)) 365.01 (209.54) 373.88 (252.48) + albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) + copper (mean (sd)) 97.64 (90.59) 97.65 (80.49) + alk.phos (mean (sd)) 2021.30 (2183.44) 1943.01 (2101.69) + ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) + trig (mean (sd)) 124.14 (71.54) 125.25 (58.52) + platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) + protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) +. Stratified by trt + 1 2 p test + n 158 154 + time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883 + age (mean (sd)) 51.42 (11.01) 48.58 (9.96) 0.018 + bili (median [IQR]) 1.40 [0.80, 3.20] 1.30 [0.72, 3.60] 0.842 nonnorm + chol (median [IQR]) 315.50 [247.75, 417.00] 303.50 [254.25, 377.00] 0.544 nonnorm + albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) 0.874 + copper (median [IQR]) 73.00 [40.00, 121.00] 73.00 [43.00, 139.00] 0.717 nonnorm + alk.phos (median [IQR]) 1214.50 [840.75, 2028.00] 1283.00 [922.50, 1949.75] 0.812 nonnorm + ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) 0.460 + trig (median [IQR]) 106.00 [84.50, 146.00] 113.00 [84.50, 155.00] 0.370 nonnorm + platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) 0.555 + protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) 0.197 +. Stratified by trt + 1 2 p test + n 158 154 + time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883 + age (mean (sd)) 51.42 (11.01) 48.58 (9.96) 0.018 + bili (median [range]) 1.40 [0.30, 20.00] 1.30 [0.30, 28.00] 0.842 nonnorm + chol (median [range]) 315.50 [127.00, 1712.00] 303.50 [120.00, 1775.00] 0.544 nonnorm + albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) 0.874 + copper (median [range]) 73.00 [9.00, 588.00] 73.00 [4.00, 558.00] 0.717 nonnorm + alk.phos (median [range]) 1214.50 [369.00, 11552.00] 1283.00 [289.00, 13862.40] 0.812 nonnorm + ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) 0.460 + trig (median [range]) 106.00 [33.00, 598.00] 113.00 [44.00, 432.00] 0.370 nonnorm + platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) 0.555 + protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) 0.197 +. Stratified by trt + 1 2 p test + n 158 154 + time (mean (sd)) 2015.62 (1094.12) 1996.86 (1155.93) 0.883 + age (mean (sd)) 51.42 (11.01) 48.58 (9.96) 0.018 + bili (mean (sd)) 2.87 (3.63) 3.65 (5.28) 0.131 + chol (mean (sd)) 365.01 (209.54) 373.88 (252.48) 0.748 + albumin (mean (sd)) 3.52 (0.44) 3.52 (0.40) 0.874 + copper (mean (sd)) 97.64 (90.59) 97.65 (80.49) 0.999 + alk.phos (mean (sd)) 2021.30 (2183.44) 1943.01 (2101.69) 0.747 + ast (mean (sd)) 120.21 (54.52) 124.97 (58.93) 0.460 + trig (mean (sd)) 124.14 (71.54) 125.25 (58.52) 0.886 + platelet (mean (sd)) 258.75 (100.32) 265.20 (90.73) 0.555 + protime (mean (sd)) 10.65 (0.85) 10.80 (1.14) 0.197 +. Stratified by trt + 1 2 p test + n 158 154 + time 2015.62 (1094.12) 1996.86 (1155.93) 0.883 + age 51.42 (11.01) 48.58 (9.96) 0.018 + bili 2.87 (3.63) 3.65 (5.28) 0.131 + chol 365.01 (209.54) 373.88 (252.48) 0.748 + albumin 3.52 (0.44) 3.52 (0.40) 0.874 + copper 97.64 (90.59) 97.65 (80.49) 0.999 + alk.phos 2021.30 (2183.44) 1943.01 (2101.69) 0.747 + ast 120.21 (54.52) 124.97 (58.93) 0.460 + trig 124.14 (71.54) 125.25 (58.52) 0.886 + platelet 258.75 (100.32) 265.20 (90.73) 0.555 + protime 10.65 (0.85) 10.80 (1.14) 0.197 +. "Stratified by trt" + "" "1" "2" "p" "test" + "n" "158" "154" "" "" + "time (mean (sd))" "2015.62 (1094.12)" "1996.86 (1155.93)" "0.883" "" + "age (mean (sd))" "51.42 (11.01)" "48.58 (9.96)" "0.018" "" + "bili (mean (sd))" "2.87 (3.63)" "3.65 (5.28)" "0.131" "" + "chol (mean (sd))" "365.01 (209.54)" "373.88 (252.48)" "0.748" "" + "albumin (mean (sd))" "3.52 (0.44)" "3.52 (0.40)" "0.874" "" + "copper (mean (sd))" "97.64 (90.59)" "97.65 (80.49)" "0.999" "" + "alk.phos (mean (sd))" "2021.30 (2183.44)" "1943.01 (2101.69)" "0.747" "" + "ast (mean (sd))" "120.21 (54.52)" "124.97 (58.93)" "0.460" "" + "trig (mean (sd))" "124.14 (71.54)" "125.25 (58.52)" "0.886" "" + "platelet (mean (sd))" "258.75 (100.32)" "265.20 (90.73)" "0.555" "" + "protime (mean (sd))" "10.65 (0.85)" "10.80 (1.14)" "0.197" "" +. + ### Summary of continuous variables ### + +strata: Overall + n miss p.miss mean sd median p25 p75 min max skew kurt +time 418 0 0.0 1918 1104.7 1730 1092.8 2614 41.0 4795 0.47 -0.5 +age 418 0 0.0 51 10.4 51 42.8 58 26.3 78 0.09 -0.6 +bili 418 0 0.0 3 4.4 1 0.8 3 0.3 28 2.72 8.1 +chol 418 134 32.1 370 231.9 310 249.5 400 120.0 1775 3.41 14.3 +albumin 418 0 0.0 3 0.4 4 3.2 4 2.0 5 -0.47 0.6 +copper 418 108 25.8 98 85.6 73 41.2 123 4.0 588 2.30 7.6 +alk.phos 418 106 25.4 1983 2140.4 1259 871.5 1980 289.0 13862 2.99 9.7 +ast 418 106 25.4 123 56.7 115 80.6 152 26.4 457 1.45 4.3 +trig 418 136 32.5 125 65.1 108 84.2 151 33.0 598 2.52 11.8 +platelet 418 11 2.6 257 98.3 251 188.5 318 62.0 721 0.63 0.9 +protime 418 2 0.5 11 1.0 11 10.0 11 9.0 18 2.22 10.0 + +======================================================================================= + + ### Summary of categorical variables ### + +strata: Overall + var n miss p.miss level freq percent cum.percent + status 418 0 0.0 0 232 55.5 55.5 + 1 25 6.0 61.5 + 2 161 38.5 100.0 + + sex 418 0 0.0 m 44 10.5 10.5 + f 374 89.5 100.0 + + ascites 418 106 25.4 0 288 92.3 92.3 + 1 24 7.7 100.0 + + hepato 418 106 25.4 0 152 48.7 48.7 + 1 160 51.3 100.0 + + spiders 418 106 25.4 0 222 71.2 71.2 + 1 90 28.8 100.0 + + edema 418 0 0.0 0 354 84.7 84.7 + 0.5 44 10.5 95.2 + 1 20 4.8 100.0 + + stage 418 6 1.4 1 21 5.1 5.1 + 2 92 22.3 27.4 + 3 155 37.6 65.0 + 4 144 35.0 100.0 + +. + ### Summary of continuous variables ### + +strata: Overall + n miss p.miss mean sd median p25 p75 min max skew kurt +time 418 0 0.0 1918 1104.7 1730 1092.8 2614 41.0 4795 0.47 -0.5 +age 418 0 0.0 51 10.4 51 42.8 58 26.3 78 0.09 -0.6 +bili 418 0 0.0 3 4.4 1 0.8 3 0.3 28 2.72 8.1 +chol 418 134 32.1 370 231.9 310 249.5 400 120.0 1775 3.41 14.3 +albumin 418 0 0.0 3 0.4 4 3.2 4 2.0 5 -0.47 0.6 +copper 418 108 25.8 98 85.6 73 41.2 123 4.0 588 2.30 7.6 +alk.phos 418 106 25.4 1983 2140.4 1259 871.5 1980 289.0 13862 2.99 9.7 +ast 418 106 25.4 123 56.7 115 80.6 152 26.4 457 1.45 4.3 +trig 418 136 32.5 125 65.1 108 84.2 151 33.0 598 2.52 11.8 +platelet 418 11 2.6 257 98.3 251 188.5 318 62.0 721 0.63 0.9 +protime 418 2 0.5 11 1.0 11 10.0 11 9.0 18 2.22 10.0 + +======================================================================================= + + ### Summary of categorical variables ### + +strata: Overall + var n miss p.miss level freq percent cum.percent + status 418 0 0.0 0 232 55.5 55.5 + 1 25 6.0 61.5 + 2 161 38.5 100.0 + + sex 418 0 0.0 m 44 10.5 10.5 + f 374 89.5 100.0 + + ascites 418 0 0.0 0 288 68.9 68.9 + 1 24 5.7 74.6 + 106 25.4 100.0 + + hepato 418 0 0.0 0 152 36.4 36.4 + 1 160 38.3 74.6 + 106 25.4 100.0 + + spiders 418 0 0.0 0 222 53.1 53.1 + 1 90 21.5 74.6 + 106 25.4 100.0 + + edema 418 0 0.0 0 354 84.7 84.7 + 0.5 44 10.5 95.2 + 1 20 4.8 100.0 + + stage 418 0 0.0 1 21 5.0 5.0 + 2 92 22.0 27.0 + 3 155 37.1 64.1 + 4 144 34.4 98.6 + 6 1.4 100.0 + +. + ### Summary of continuous variables ### + +trt: 1 + n miss p.miss mean sd median p25 p75 min max skew kurt +time 158 0 0.0 2016 1094.1 1895 1231.0 2632 41.0 4556 0.41 -0.4 +age 158 0 0.0 51 11.0 52 43.0 59 26.3 78 0.06 -0.5 +bili 158 0 0.0 3 3.6 1 0.8 3 0.3 20 2.67 7.6 +chol 158 18 11.4 365 209.5 316 247.8 417 127.0 1712 3.83 20.2 +albumin 158 0 0.0 4 0.4 4 3.2 4 2.1 5 -0.40 0.3 +copper 158 1 0.6 98 90.6 73 40.0 121 9.0 588 2.50 8.2 +alk.phos 158 0 0.0 2021 2183.4 1214 840.8 2028 369.0 11552 2.71 7.4 +ast 158 0 0.0 120 54.5 112 76.7 152 26.4 338 1.09 1.6 +trig 158 19 12.0 124 71.5 106 84.5 146 33.0 598 2.95 14.3 +platelet 158 2 1.3 259 100.3 255 189.5 322 62.0 563 0.50 0.2 +protime 158 0 0.0 11 0.9 11 10.0 11 9.0 14 1.10 1.6 +--------------------------------------------------------------------------------------- +trt: 2 + n miss p.miss mean sd median p25 p75 min max skew kurt +time 154 0 0.0 1997 1155.9 1811 1153.0 2771 51.0 4523 0.4 -0.7 +age 154 0 0.0 49 10.0 48 41.4 56 30.6 75 0.2 -0.5 +bili 154 0 0.0 4 5.3 1 0.7 4 0.3 28 2.7 7.3 +chol 154 10 6.5 374 252.5 304 254.2 377 120.0 1775 3.1 11.1 +albumin 154 0 0.0 4 0.4 4 3.3 4 2.0 4 -0.8 2.0 +copper 154 1 0.6 98 80.5 73 43.0 139 4.0 558 2.0 6.6 +alk.phos 154 0 0.0 1943 2101.7 1283 922.5 1950 289.0 13862 3.3 12.8 +ast 154 0 0.0 125 58.9 117 83.8 152 28.4 457 1.7 6.3 +trig 154 11 7.1 125 58.5 113 84.5 155 44.0 432 1.7 5.5 +platelet 154 2 1.3 265 90.7 260 206.8 322 71.0 487 0.2 -0.3 +protime 154 0 0.0 11 1.1 11 10.0 11 9.2 17 1.9 6.4 + +p-values + pNormal pNonNormal +time 0.88304691 0.82661809 +age 0.01767247 0.01962155 +bili 0.13093942 0.84168460 +chol 0.74799072 0.54433899 +albumin 0.87388074 0.95045176 +copper 0.99915849 0.71745444 +alk.phos 0.74726165 0.81198200 +ast 0.45969842 0.45892358 +trig 0.88604213 0.36980434 +platelet 0.55451136 0.45482564 +protime 0.19714026 0.58802048 + +Standardize mean differences + 1 vs 2 +time 0.0166658751 +age 0.2702619258 +bili 0.1710905651 +chol 0.0382210537 +albumin 0.0180021838 +copper 0.0001200022 +alk.phos 0.0365323630 +ast 0.0837836058 +trig 0.0170615337 +platelet 0.0674763888 +protime 0.1460939117 + +======================================================================================= + + ### Summary of categorical variables ### + +trt: 1 + var n miss p.miss level freq percent cum.percent + status 158 0 0.0 0 83 52.5 52.5 + 1 10 6.3 58.9 + 2 65 41.1 100.0 + + sex 158 0 0.0 m 21 13.3 13.3 + f 137 86.7 100.0 + + ascites 158 0 0.0 0 144 91.1 91.1 + 1 14 8.9 100.0 + + hepato 158 0 0.0 0 85 53.8 53.8 + 1 73 46.2 100.0 + + spiders 158 0 0.0 0 113 71.5 71.5 + 1 45 28.5 100.0 + + edema 158 0 0.0 0 132 83.5 83.5 + 0.5 16 10.1 93.7 + 1 10 6.3 100.0 + + stage 158 0 0.0 1 12 7.6 7.6 + 2 35 22.2 29.7 + 3 56 35.4 65.2 + 4 55 34.8 100.0 + +--------------------------------------------------------------------------------------- +trt: 2 + var n miss p.miss level freq percent cum.percent + status 154 0 0.0 0 85 55.2 55.2 + 1 9 5.8 61.0 + 2 60 39.0 100.0 + + sex 154 0 0.0 m 15 9.7 9.7 + f 139 90.3 100.0 + + ascites 154 0 0.0 0 144 93.5 93.5 + 1 10 6.5 100.0 + + hepato 154 0 0.0 0 67 43.5 43.5 + 1 87 56.5 100.0 + + spiders 154 0 0.0 0 109 70.8 70.8 + 1 45 29.2 100.0 + + edema 154 0 0.0 0 131 85.1 85.1 + 0.5 13 8.4 93.5 + 1 10 6.5 100.0 + + stage 154 0 0.0 1 4 2.6 2.6 + 2 32 20.8 23.4 + 3 64 41.6 64.9 + 4 54 35.1 100.0 + + +p-values + pApprox pExact +status 0.89350975 0.88422188 +sex 0.42122610 0.37743235 +ascites 0.56728647 0.52558267 +hepato 0.08820884 0.07137522 +spiders 0.98466036 0.90113734 +edema 0.87681949 0.89370131 +stage 0.20129629 0.20455558 + +Standardize mean differences + 1 vs 2 +status 0.05375763 +sex 0.11141161 +ascites 0.08900618 +hepato 0.20699413 +spiders 0.01632844 +edema 0.05811659 +stage 0.24600834 +. + ### Summary of continuous variables ### + +trt: 1 +sex: m + n miss p.miss mean sd median p25 p75 min max skew kurt +time 21 0 0 1793 1244.7 1302 999 2386 140.0 4459 1.04 0.1 +age 21 0 0 56 12.6 56 46 66 33.5 78 -0.20 -0.7 +bili 21 0 0 3 2.1 2 1 4 0.6 7 1.02 -0.1 +chol 21 0 0 403 204.9 376 251 456 168.0 1000 1.33 2.2 +albumin 21 0 0 4 0.4 4 3 4 2.6 4 -0.97 0.3 +copper 21 0 0 175 105.3 158 100 225 25.0 444 1.08 1.0 +alk.phos 21 0 0 2486 2385.3 1664 983 2496 516.0 10165 2.05 4.5 +ast 21 0 0 129 48.7 127 86 158 56.8 222 0.31 -0.9 +trig 21 0 0 146 56.6 142 107 194 55.0 242 0.16 -1.0 +platelet 21 1 5 233 97.4 228 148 314 70.0 394 -0.05 -1.2 +protime 21 0 0 11 0.9 11 10 11 9.7 14 2.11 6.8 +--------------------------------------------------------------------------------------- +trt: 2 +sex: m + n miss p.miss mean sd median p25 p75 min max skew kurt +time 15 0 0 2156 1428.6 1656 1054 3420 191.0 4427 0.3 -1.4 +age 15 0 0 57 10.1 53 49 66 43.9 75 0.5 -1.3 +bili 15 0 0 3 2.5 2 1 3 0.6 9 1.5 1.4 +chol 15 1 7 301 111.3 259 236 342 151.0 546 0.9 0.4 +albumin 15 0 0 4 0.5 4 3 4 2.3 4 -1.0 1.9 +copper 15 0 0 125 89.2 84 52 198 13.0 281 0.5 -1.1 +alk.phos 15 0 0 1734 2478.1 1070 737 1212 577.0 10397 3.5 12.7 +ast 15 0 0 113 44.4 110 74 129 46.5 188 0.3 -0.8 +trig 15 1 7 115 39.6 108 90 137 49.0 188 0.4 -0.4 +platelet 15 0 0 240 73.9 214 190 294 119.0 360 0.2 -1.0 +protime 15 0 0 11 1.0 11 11 12 10.0 13 0.7 0.2 +--------------------------------------------------------------------------------------- +trt: 1 +sex: f + n miss p.miss mean sd median p25 p75 min max skew kurt +time 137 0 0.0 2050 1070.2 1945 1293.0 2644 41.0 4556 0.32 -0.4 +age 137 0 0.0 51 10.7 51 42.5 57 26.3 77 0.05 -0.5 +bili 137 0 0.0 3 3.8 1 0.8 3 0.3 20 2.65 7.1 +chol 137 18 13.1 358 210.5 309 247.5 400 127.0 1712 4.28 23.8 +albumin 137 0 0.0 3 0.4 4 3.2 4 2.1 5 -0.33 0.4 +copper 137 1 0.7 86 82.3 67 36.8 105 9.0 588 3.18 13.9 +alk.phos 137 0 0.0 1950 2151.4 1212 823.0 1877 369.0 11552 2.88 8.4 +ast 137 0 0.0 119 55.4 108 74.4 150 26.4 338 1.20 1.9 +trig 137 19 13.9 120 73.4 101 84.0 134 33.0 598 3.28 16.0 +platelet 137 1 0.7 263 100.5 258 194.2 322 62.0 563 0.57 0.3 +protime 137 0 0.0 11 0.8 11 10.0 11 9.0 13 0.92 0.6 +--------------------------------------------------------------------------------------- +trt: 2 +sex: f + n miss p.miss mean sd median p25 p75 min max skew kurt +time 139 0 0.0 1980 1127.5 1832 1157.0 2717 51.0 4523 0.3 -0.6 +age 139 0 0.0 48 9.5 47 41.3 55 30.6 71 0.2 -0.7 +bili 139 0 0.0 4 5.5 1 0.7 4 0.3 28 2.6 6.5 +chol 139 9 6.5 382 262.3 306 258.0 381 120.0 1775 3.0 10.1 +albumin 139 0 0.0 4 0.4 4 3.3 4 2.0 4 -0.8 2.1 +copper 139 1 0.7 95 79.3 70 42.5 127 4.0 558 2.2 8.3 +alk.phos 139 0 0.0 1966 2066.2 1345 941.0 1992 289.0 13862 3.4 13.6 +ast 139 0 0.0 126 60.3 118 84.5 153 28.4 457 1.8 6.2 +trig 139 10 7.2 126 60.2 113 84.0 157 44.0 432 1.7 5.3 +platelet 139 2 1.4 268 92.2 265 213.0 324 71.0 487 0.2 -0.4 +protime 139 0 0.0 11 1.1 11 9.9 11 9.2 17 2.0 7.2 + +p-values + pNormal pNonNormal +time 0.73004959763 0.55664397971 +age 0.00012863810 0.00065174365 +bili 0.39374304731 0.20495248146 +chol 0.51222227887 0.45787732033 +albumin 0.58539258269 0.40412063286 +copper 0.00006237989 0.00004712924 +alk.phos 0.70638120444 0.18014798221 +ast 0.59828976332 0.50866156390 +trig 0.37010725909 0.05827725951 +platelet 0.36247402287 0.44512771593 +protime 0.13654633001 0.08114382938 + +Standardize mean differences + average 1 vs 2 1 vs 3 1 vs 4 2 vs 3 2 vs 4 3 vs 4 +time 0.1556365 0.27083134 0.22071813 0.15677410 0.08450703 0.13729269 0.06369595 +age 0.5214974 0.13353519 0.40993004 0.70700091 0.60864596 0.96133382 0.30853876 +bili 0.1352025 0.11373946 0.04023105 0.18444097 0.04265701 0.24158721 0.18855913 +chol 0.2950579 0.62108339 0.21756422 0.09219212 0.33996564 0.40072828 0.09881382 +albumin 0.1637683 0.28262444 0.30878937 0.26228013 0.01074465 0.05159074 0.06658036 +copper 0.5416574 0.50727571 0.94378898 0.86116098 0.46259003 0.36464167 0.11048709 +alk.phos 0.1633259 0.30907333 0.23603622 0.23332162 0.09290687 0.10128012 0.00733728 +ast 0.1818681 0.34614452 0.18797132 0.04399580 0.12602823 0.25929154 0.12777729 +trig 0.2940632 0.63360898 0.38944815 0.33242327 0.09194315 0.22610097 0.09085446 +platelet 0.2340433 0.08655951 0.30251429 0.37226635 0.25448008 0.33286083 0.05557853 +protime 0.3283721 0.40162254 0.24596092 0.08476002 0.66430044 0.44433997 0.12924857 + +======================================================================================= + + ### Summary of categorical variables ### + +trt: 1 +sex: m + var n miss p.miss level freq percent cum.percent + status 21 0 0.0 0 4 19.0 19.0 + 1 3 14.3 33.3 + 2 14 66.7 100.0 + + sex 21 0 0.0 m 21 100.0 100.0 + f 0 0.0 100.0 + + ascites 21 0 0.0 0 20 95.2 95.2 + 1 1 4.8 100.0 + + hepato 21 0 0.0 0 9 42.9 42.9 + 1 12 57.1 100.0 + + spiders 21 0 0.0 0 18 85.7 85.7 + 1 3 14.3 100.0 + + edema 21 0 0.0 0 17 81.0 81.0 + 0.5 3 14.3 95.2 + 1 1 4.8 100.0 + + stage 21 0 0.0 1 2 9.5 9.5 + 2 4 19.0 28.6 + 3 7 33.3 61.9 + 4 8 38.1 100.0 + +--------------------------------------------------------------------------------------- +trt: 2 +sex: m + var n miss p.miss level freq percent cum.percent + status 15 0 0.0 0 7 46.7 46.7 + 1 0 0.0 46.7 + 2 8 53.3 100.0 + + sex 15 0 0.0 m 15 100.0 100.0 + f 0 0.0 100.0 + + ascites 15 0 0.0 0 13 86.7 86.7 + 1 2 13.3 100.0 + + hepato 15 0 0.0 0 6 40.0 40.0 + 1 9 60.0 100.0 + + spiders 15 0 0.0 0 14 93.3 93.3 + 1 1 6.7 100.0 + + edema 15 0 0.0 0 12 80.0 80.0 + 0.5 1 6.7 86.7 + 1 2 13.3 100.0 + + stage 15 0 0.0 1 1 6.7 6.7 + 2 2 13.3 20.0 + 3 5 33.3 53.3 + 4 7 46.7 100.0 + +--------------------------------------------------------------------------------------- +trt: 1 +sex: f + var n miss p.miss level freq percent cum.percent + status 137 0 0.0 0 79 57.7 57.7 + 1 7 5.1 62.8 + 2 51 37.2 100.0 + + sex 137 0 0.0 m 0 0.0 0.0 + f 137 100.0 100.0 + + ascites 137 0 0.0 0 124 90.5 90.5 + 1 13 9.5 100.0 + + hepato 137 0 0.0 0 76 55.5 55.5 + 1 61 44.5 100.0 + + spiders 137 0 0.0 0 95 69.3 69.3 + 1 42 30.7 100.0 + + edema 137 0 0.0 0 115 83.9 83.9 + 0.5 13 9.5 93.4 + 1 9 6.6 100.0 + + stage 137 0 0.0 1 10 7.3 7.3 + 2 31 22.6 29.9 + 3 49 35.8 65.7 + 4 47 34.3 100.0 + +--------------------------------------------------------------------------------------- +trt: 2 +sex: f + var n miss p.miss level freq percent cum.percent + status 139 0 0.0 0 78 56.1 56.1 + 1 9 6.5 62.6 + 2 52 37.4 100.0 + + sex 139 0 0.0 m 0 0.0 0.0 + f 139 100.0 100.0 + + ascites 139 0 0.0 0 131 94.2 94.2 + 1 8 5.8 100.0 + + hepato 139 0 0.0 0 61 43.9 43.9 + 1 78 56.1 100.0 + + spiders 139 0 0.0 0 95 68.3 68.3 + 1 44 31.7 100.0 + + edema 139 0 0.0 0 119 85.6 85.6 + 0.5 12 8.6 94.2 + 1 8 5.8 100.0 + + stage 139 0 0.0 1 3 2.2 2.2 + 2 30 21.6 23.7 + 3 59 42.4 66.2 + 4 47 33.8 100.0 + + +p-values + pApprox pExact +status 3.269881e-02 2.420814e-02 +sex 2.514569e-67 4.934035e-48 +ascites 5.156942e-01 4.354075e-01 +hepato 2.080568e-01 2.048012e-01 +spiders 8.898575e-02 8.569292e-02 +edema 9.058420e-01 8.483322e-01 +stage 6.463005e-01 NA + +Standardize mean differences + average 1 vs 2 1 vs 3 1 vs 4 2 vs 3 2 vs 4 3 vs 4 +status 0.5770647 0.79644734 0.8748771 0.83097631 0.4375075 0.46214324 0.06043662 +sex NaN NaN NaN NaN NaN NaN NaN +ascites 0.1755851 0.30219043 0.1845375 0.04452096 0.1211310 0.26007640 0.14105454 +hepato 0.1598214 0.05802589 0.2544151 0.02073917 0.3135916 0.07878788 0.23336861 +spiders 0.4018377 0.25073566 0.3999925 0.42200838 0.6471968 0.66954801 0.02154469 +edema 0.2122338 0.37625250 0.1624118 0.18063879 0.2420277 0.26502087 0.04705137 +stage 0.2773411 0.21945509 0.1333844 0.35632272 0.3000785 0.39205566 0.26275030 +. + ### Summary of continuous variables ### + +trt: 1 +sex: m + n miss p.miss mean sd median p25 p75 min max skew kurt +time 21 0 0 1793 1244.7 1302 999 2386 140 4459 1.0 0.1 +age 21 0 0 56 12.6 56 46 66 33 78 -0.2 -0.7 +protime 21 0 0 11 0.9 11 10 11 10 14 2.1 6.8 +--------------------------------------------------------------------------------------- +trt: 2 +sex: m + n miss p.miss mean sd median p25 p75 min max skew kurt +time 15 0 0 2156 1429 1656 1054 3420 191 4427 0.3 -1.4 +age 15 0 0 57 10 53 49 66 44 75 0.5 -1.3 +protime 15 0 0 11 1 11 11 12 10 13 0.7 0.2 +--------------------------------------------------------------------------------------- +trt: 1 +sex: f + n miss p.miss mean sd median p25 p75 min max skew kurt +time 137 0 0 2050 1070.2 1945 1293 2644 41 4556 0.32 -0.4 +age 137 0 0 51 10.7 51 43 57 26 77 0.05 -0.5 +protime 137 0 0 11 0.8 11 10 11 9 13 0.92 0.6 +--------------------------------------------------------------------------------------- +trt: 2 +sex: f + n miss p.miss mean sd median p25 p75 min max skew kurt +time 139 0 0 1980 1128 1832 1157 2717 51 4523 0.3 -0.6 +age 139 0 0 48 10 47 41 55 31 71 0.2 -0.7 +protime 139 0 0 11 1 11 10 11 9 17 2.0 7.2 + +p-values + pNormal pNonNormal +time 0.7300495976 0.5566439797 +age 0.0001286381 0.0006517437 +protime 0.1365463300 0.0811438294 + +Standardize mean differences + average 1 vs 2 1 vs 3 1 vs 4 2 vs 3 2 vs 4 3 vs 4 +time 0.1556365 0.2708313 0.2207181 0.15677410 0.08450703 0.1372927 0.06369595 +age 0.5214974 0.1335352 0.4099300 0.70700091 0.60864596 0.9613338 0.30853876 +protime 0.3283721 0.4016225 0.2459609 0.08476002 0.66430044 0.4443400 0.12924857 +. + ### Summary of categorical variables ### + +trt: 1 +sex: m + var n miss p.miss level freq percent cum.percent + status 21 0 0.0 0 4 19.0 19.0 + 1 3 14.3 33.3 + 2 14 66.7 100.0 + + trt 21 0 0.0 1 21 100.0 100.0 + 2 0 0.0 100.0 + + sex 21 0 0.0 m 21 100.0 100.0 + f 0 0.0 100.0 + +--------------------------------------------------------------------------------------- +trt: 2 +sex: m + var n miss p.miss level freq percent cum.percent + status 15 0 0.0 0 7 46.7 46.7 + 1 0 0.0 46.7 + 2 8 53.3 100.0 + + trt 15 0 0.0 1 0 0.0 0.0 + 2 15 100.0 100.0 + + sex 15 0 0.0 m 15 100.0 100.0 + f 0 0.0 100.0 + +--------------------------------------------------------------------------------------- +trt: 1 +sex: f + var n miss p.miss level freq percent cum.percent + status 137 0 0.0 0 79 57.7 57.7 + 1 7 5.1 62.8 + 2 51 37.2 100.0 + + trt 137 0 0.0 1 137 100.0 100.0 + 2 0 0.0 100.0 + + sex 137 0 0.0 m 0 0.0 0.0 + f 137 100.0 100.0 + +--------------------------------------------------------------------------------------- +trt: 2 +sex: f + var n miss p.miss level freq percent cum.percent + status 139 0 0.0 0 78 56.1 56.1 + 1 9 6.5 62.6 + 2 52 37.4 100.0 + + trt 139 0 0.0 1 0 0.0 0.0 + 2 139 100.0 100.0 + + sex 139 0 0.0 m 0 0.0 0.0 + f 139 100.0 100.0 + + +p-values + pApprox pExact +status 3.269881e-02 2.420814e-02 +trt 2.514569e-67 2.724125e-93 +sex 2.514569e-67 4.934035e-48 + +Standardize mean differences + average 1 vs 2 1 vs 3 1 vs 4 2 vs 3 2 vs 4 3 vs 4 +status 0.5770647 0.7964473 0.8748771 0.8309763 0.4375075 0.4621432 0.06043662 +trt NaN NaN NaN NaN NaN NaN NaN +sex NaN NaN NaN NaN NaN NaN NaN +. +Unit tests for the ExtractSmd function : ..... average 1 vs 2 1 vs 3 1 vs 4 2 vs 3 2 vs 4 3 vs 4 +time 0.1556365 0.27083134 0.22071813 0.15677410 0.08450703 0.13729269 0.06369595 +status 0.5770647 0.79644734 0.87487707 0.83097631 0.43750751 0.46214324 0.06043662 +age 0.5214974 0.13353519 0.40993004 0.70700091 0.60864596 0.96133382 0.30853876 +sex NaN NaN NaN NaN NaN NaN NaN +ascites 0.1755851 0.30219043 0.18453749 0.04452096 0.12113103 0.26007640 0.14105454 +hepato 0.1598214 0.05802589 0.25441512 0.02073917 0.31359163 0.07878788 0.23336861 +spiders 0.4018377 0.25073566 0.39999254 0.42200838 0.64719678 0.66954801 0.02154469 +edema 0.2122338 0.37625250 0.16241184 0.18063879 0.24202772 0.26502087 0.04705137 +bili 0.1352025 0.11373946 0.04023105 0.18444097 0.04265701 0.24158721 0.18855913 +chol 0.2950579 0.62108339 0.21756422 0.09219212 0.33996564 0.40072828 0.09881382 +albumin 0.1637683 0.28262444 0.30878937 0.26228013 0.01074465 0.05159074 0.06658036 +copper 0.5416574 0.50727571 0.94378898 0.86116098 0.46259003 0.36464167 0.11048709 +alk.phos 0.1633259 0.30907333 0.23603622 0.23332162 0.09290687 0.10128012 0.00733728 +ast 0.1818681 0.34614452 0.18797132 0.04399580 0.12602823 0.25929154 0.12777729 +trig 0.2940632 0.63360898 0.38944815 0.33242327 0.09194315 0.22610097 0.09085446 +platelet 0.2340433 0.08655951 0.30251429 0.37226635 0.25448008 0.33286083 0.05557853 +protime 0.3283721 0.40162254 0.24596092 0.08476002 0.66430044 0.44433997 0.12924857 +stage 0.2773411 0.21945509 0.13338440 0.35632272 0.30007852 0.39205566 0.26275030 +.... average 1 vs 2 1 vs 3 1 vs 4 2 vs 3 2 vs 4 3 vs 4 +HI_CHOL 0.07290304 0.06404806 0.07986822 0.006024398 0.14361148 0.07003988 0.073826228 +race NaN NaN NaN NaN NaN NaN NaN +agecat 0.26102029 0.47958417 0.21054511 0.163093544 0.26927031 0.33972601 0.103902581 +RIAGENDR 0.06902841 0.04460885 0.11590734 0.111246698 0.07120651 0.06655532 0.004645715 +.... +Tests for functions for standardized mean differences : ..................................................................................................................................................... + ### Summary of continuous variables ### + +HI_CHOL: 0 +RIAGENDR: 1 + n miss p.miss mean sd median p25 p75 min max skew kurt +race 3526 0 0 2 0.9 2 1 2 1 4 0.6 -0.3 +--------------------------------------------------------------------------------------- +HI_CHOL: 1 +RIAGENDR: 1 + n miss p.miss mean sd median p25 p75 min max skew kurt +race 363 0 0 2 0.9 2 1 2 1 4 0.8 -0.1 +--------------------------------------------------------------------------------------- +HI_CHOL: 0 +RIAGENDR: 2 + n miss p.miss mean sd median p25 p75 min max skew kurt +race 3533 0 0 2 0.9 2 1 2 1 4 0.6 -0.4 +--------------------------------------------------------------------------------------- +HI_CHOL: 1 +RIAGENDR: 2 + n miss p.miss mean sd median p25 p75 min max skew kurt +race 424 0 0 2 0.8 2 1 2 1 4 0.7 0.6 + +p-values + pNormal pNonNormal +race 0.5071597 0.5761282 + +Standardize mean differences + average 1 vs 2 1 vs 3 1 vs 4 2 vs 3 2 vs 4 3 vs 4 +race 0.04247045 0.03492105 0.0006074061 0.07524267 0.03396878 0.03638678 0.073696 + +======================================================================================= + + ### Summary of categorical variables ### + +HI_CHOL: 0 +RIAGENDR: 1 + var n miss p.miss level freq percent cum.percent + agecat 3526 0 0.0 (0,19] 1119 31.7 31.7 + (19,39] 797 22.6 54.3 + (39,59] 775 22.0 76.3 + (59,Inf] 835 23.7 100.0 + +--------------------------------------------------------------------------------------- +HI_CHOL: 1 +RIAGENDR: 1 + var n miss p.miss level freq percent cum.percent + agecat 363 0 0.0 (0,19] 10 2.8 2.8 + (19,39] 88 24.2 27.0 + (39,59] 173 47.7 74.7 + (59,Inf] 92 25.3 100.0 + +--------------------------------------------------------------------------------------- +HI_CHOL: 0 +RIAGENDR: 2 + var n miss p.miss level freq percent cum.percent + agecat 3533 0 0.0 (0,19] 1015 28.7 28.7 + (19,39] 950 26.9 55.6 + (39,59] 796 22.5 78.1 + (59,Inf] 772 21.9 100.0 + +--------------------------------------------------------------------------------------- +HI_CHOL: 1 +RIAGENDR: 2 + var n miss p.miss level freq percent cum.percent + agecat 424 0 0.0 (0,19] 6 1.4 1.4 + (19,39] 70 16.5 17.9 + (39,59] 167 39.4 57.3 + (59,Inf] 181 42.7 100.0 + + +p-values + pApprox pExact +agecat 9.387606e-89 NA + +Standardize mean differences + average 1 vs 2 1 vs 3 1 vs 4 2 vs 3 2 vs 4 3 vs 4 +agecat 0.702331 0.9040822 0.109801 0.985333 0.8536402 0.3838583 0.9772713 +.. Stratified by HI_CHOL:RIAGENDR + 0:1 1:1 0:2 1:2 p test + n 3526 363 3533 424 + race (mean (sd)) 1.98 (0.85) 1.95 (0.90) 1.98 (0.87) 1.92 (0.76) 0.507 +... Stratified by HI_CHOL:RIAGENDR + 0:1 1:1 0:2 1:2 p test SMD + n 3526 363 3533 424 + race (mean (sd)) 1.98 (0.85) 1.95 (0.90) 1.98 (0.87) 1.92 (0.76) 0.507 0.042 + agecat (%) <0.001 0.702 + (0,19] 1119 (31.7) 10 ( 2.8) 1015 (28.7) 6 ( 1.4) + (19,39] 797 (22.6) 88 (24.2) 950 (26.9) 70 (16.5) + (39,59] 775 (22.0) 173 (47.7) 796 (22.5) 167 (39.4) + (59,Inf] 835 (23.7) 92 (25.3) 772 (21.9) 181 (42.7) +. +Unit tests for the survey-related modules : ................................................ +Unit tests for the modules : ........ +Unit tests for regression summary function : . exp(coef) [confint] p +trt 1.03 [0.72, 1.47] 0.891 +age 1.03 [1.01, 1.05] 0.005 +albumin 0.26 [0.17, 0.41] <0.001 +ascites 2.93 [1.68, 5.11] <0.001 +. exp(coef) [confint] p +trt 1.03 [0.72, 1.47] 0.891 +age 1.03 [1.01, 1.05] 0.005 +albumin 0.26 [0.17, 0.41] <0.001 +ascites 2.93 [1.68, 5.11] <0.001 +.. exp(coef) [confint] p +(Intercept) 4.81558 [0.26099, 93.77267] 0.293 +trt 1.05879 [0.63994, 1.75622] 0.824 +age 1.03850 [1.01306, 1.06547] 0.003 +albumin 0.31078 [0.15316, 0.60752] 0.001 +ascites 19.05020 [3.69390, 349.54310] 0.005 +. exp(coef) [confint] p +(Intercept) 4.81558 [0.25650, 90.40912] 0.293 +trt 1.05879 [0.63975, 1.75230] 0.824 +age 1.03850 [1.01271, 1.06494] 0.003 +albumin 0.31078 [0.15640, 0.61757] 0.001 +ascites 19.05020 [2.44625, 148.35373] 0.005 +.. coef [confint] p +(Intercept) -1194.06334 [-2499.63435, 111.50767] 0.073 +trt -50.39655 [-275.96185, 175.16874] 0.661 +age -2.97371 [-14.01246, 8.06504] 0.596 +albumin 987.37770 [695.98678, 1278.76862] <0.001 +ascites -664.69235 [-1126.85877, -202.52592] 0.005 +. coef [confint] p +(Intercept) -1194.06334 [-2494.48743, 106.36074] 0.073 +trt -50.39655 [-275.07261, 174.27950] 0.661 +age -2.97371 [-13.96894, 8.02152] 0.596 +albumin 987.37770 [697.13553, 1277.61988] <0.001 +ascites -664.69235 [-1125.03679, -204.34791] 0.005 +. +Unit tests for svy* user functions : .......... Stratified by E + 1 2 3 p test + n 150.01 150.00 150.00 + E (mean (sd)) 1.00 (0.00) 2.00 (0.00) 3.00 (0.00) <0.001 + C (mean (sd)) 2.13 (0.89) 2.13 (0.89) 2.13 (0.89) 1.000 + Y = 1 (%) 59.0 (39.4) 59.0 (39.3) 59.0 (39.3) 1.000 + C1 = 1 (%) 50.0 (33.3) 50.0 (33.3) 50.0 (33.3) 1.000 + C2 (mean (sd)) 0.47 (0.50) 0.47 (0.50) 0.47 (0.50) 1.000 +. + Overall + n 450.01 + E (mean (sd)) 2.00 (0.82) + C (mean (sd)) 2.13 (0.88) + Y = 1 (%) 177.0 (39.4) + C1 = 1 (%) 150.0 (33.3) + C2 (mean (sd)) 0.47 (0.50) +. + Overall + n 450.01 + E (mean (sd)) 2.00 (0.82) + C (mean (sd)) 2.13 (0.88) + Y (%) + 0 272.7 (60.6) + 1 177.0 (39.3) + NA 0.3 ( 0.1) + C1 = 1 (%) 150.0 (33.3) + C2 (mean (sd)) 0.47 (0.50) +. Stratified by E:C1 + 1:0 2:0 3:0 1:1 2:1 3:1 p + n 100.01 100.00 100.00 50.00 50.00 50.00 + E (mean (sd)) 1.00 (0.00) 2.00 (0.00) 3.00 (0.00) 1.00 (0.00) 2.00 (0.00) 3.00 (0.00) <0.001 + C (mean (sd)) 1.60 (0.49) 1.60 (0.49) 1.60 (0.49) 3.20 (0.40) 3.20 (0.40) 3.20 (0.40) <0.001 + Y = 1 (%) 22.0 (22.1) 22.0 (22.0) 22.0 (22.0) 37.0 ( 74.0) 37.0 ( 74.0) 37.0 ( 74.0) <0.001 + C1 = 1 (%) 0.0 ( 0.0) 0.0 ( 0.0) 0.0 ( 0.0) 50.0 (100.0) 50.0 (100.0) 50.0 (100.0) <0.001 + C2 (mean (sd)) 0.60 (0.49) 0.60 (0.49) 0.60 (0.49) 0.20 (0.40) 0.20 (0.40) 0.20 (0.40) <0.001 + Stratified by E:C1 + test + n + E (mean (sd)) + C (mean (sd)) + Y = 1 (%) + C1 = 1 (%) + C2 (mean (sd)) +. Stratified by E + 1 2 3 p test + n 150.0120 150.0030 150.0000 + E (mean (sd)) 1.0000 (0.0000) 2.0000 (0.0000) 3.0000 (0.0000) <0.00001 + C (mean (sd)) 2.1332 (0.8854) 2.1333 (0.8861) 2.1333 (0.8861) 1.00000 + Y = 1 (%) 59.001 (39.406) 59.001 (39.333) 59.000 (39.333) 0.99982 + C1 = 1 (%) 50.000 (33.331) 50.000 (33.333) 50.000 (33.333) 1.00000 + C2 (mean (sd)) 0.4666 (0.4994) 0.4667 (0.4999) 0.4667 (0.4998) 1.00000 +. Stratified by E + 1 2 3 + n 150.01 150.00 150.00 + E (mean (sd)) 1.00 (0.00) 2.00 (0.00) 3.00 (0.00) + C (mean (sd)) 2.13 (0.89) 2.13 (0.89) 2.13 (0.89) + Y = 1 (%) 59.0 (39.4) 59.0 (39.3) 59.0 (39.3) + C1 = 1 (%) 50.0 (33.3) 50.0 (33.3) 50.0 (33.3) + C2 (mean (sd)) 0.47 (0.50) 0.47 (0.50) 0.47 (0.50) +. Stratified by E + 1 2 3 p test + n 150.01 150.00 150.00 + E (mean (sd)) 1.00 (0.00) 2.00 (0.00) 3.00 (0.00) <0.001 + C (median [IQR]) 2.00 [1.00, 3.00] 2.00 [1.00, 3.00] 2.00 [1.00, 3.00] 1.000 nonnorm + Y = 1 (%) 59.0 (39.4) 59.0 (39.3) 59.0 (39.3) NA exact + C1 = 1 (%) 50.0 (33.3) 50.0 (33.3) 50.0 (33.3) 1.000 + C2 (mean (sd)) 0.47 (0.50) 0.47 (0.50) 0.47 (0.50) 1.000 +. Stratified by E + 1 2 3 p test + n 150.01 150.00 150.00 + E (mean (sd)) 1.00 (0.00) 2.00 (0.00) 3.00 (0.00) <0.001 + C (median [range]) 2.00 [1.00, 4.00] 2.00 [1.00, 4.00] 2.00 [1.00, 4.00] 1.000 nonnorm + Y = 1 (%) 59.0 (39.4) 59.0 (39.3) 59.0 (39.3) 1.000 + C1 = 1 (%) 50.0 (33.3) 50.0 (33.3) 50.0 (33.3) 1.000 + C2 (mean (sd)) 0.47 (0.50) 0.47 (0.50) 0.47 (0.50) 1.000 +. Stratified by E + 1 2 3 p test + n 150.01 150.00 150.00 + E (mean (sd)) 1.00 (0.00) 2.00 (0.00) 3.00 (0.00) <0.001 + C (mean (sd)) 2.13 (0.89) 2.13 (0.89) 2.13 (0.89) 1.000 + Y = 1 (%) 59.001 (39.406) 59.001 (39.333) 59.000 (39.333) 1.000 + C1 = 1 (%) 50.000 (33.331) 50.000 (33.333) 50.000 (33.333) 1.000 + C2 (mean (sd)) 0.47 (0.50) 0.47 (0.50) 0.47 (0.50) 1.000 +. Stratified by E + level 1 2 3 p test + n 150.01 150.00 150.00 + E (mean (sd)) 1.00 (0.00) 2.00 (0.00) 3.00 (0.00) <0.001 + C (median [IQR]) 2.00 [1.00, 3.00] 2.00 [1.00, 3.00] 2.00 [1.00, 3.00] 1.000 nonnorm + Y (%) 0 90.7 (60.6) 91.0 (60.7) 91.0 (60.7) NA exact + 1 59.0 (39.4) 59.0 (39.3) 59.0 (39.3) + C1 (%) 0 100.0 (66.7) 100.0 (66.7) 100.0 (66.7) 1.000 + 1 50.0 (33.3) 50.0 (33.3) 50.0 (33.3) + C2 (mean (sd)) 0.47 (0.50) 0.47 (0.50) 0.47 (0.50) 1.000 +. "Stratified by E" + "" "1" "2" "3" "p" "test" + "n" "150.01" "150.00" "150.00" "" "" + "E (mean (sd))" "1.00 (0.00)" "2.00 (0.00)" "3.00 (0.00)" "<0.001" "" + "C (median [IQR])" "2.00 [1.00, 3.00]" "2.00 [1.00, 3.00]" "2.00 [1.00, 3.00]" "1.000" "nonnorm" + "Y = 1 (%)" "59.001 (39.406)" "59.001 (39.333)" "59.000 (39.333)" "NA" "exact" + "C1 = 1 (%)" "50.000 (33.331)" "50.000 (33.333)" "50.000 (33.333)" "1.000" "" + "C2 (mean (sd))" "0.47 (0.50)" "0.47 (0.50)" "0.47 (0.50)" "1.000" "" +. Stratified by E:C1 + 1:0 2:0 3:0 1:1 2:1 3:1 p test + n 100.01 100.00 100.00 50.00 50.00 50.00 + E (mean (sd)) 1.00 (0.00) 2.00 (0.00) 3.00 (0.00) 1.00 (0.00) 2.00 (0.00) 3.00 (0.00) <0.001 + C (mean (sd)) 1.60 (0.49) 1.60 (0.49) 1.60 (0.49) 3.20 (0.40) 3.20 (0.40) 3.20 (0.40) <0.001 +. Stratified by E:C1 + 1:0 2:0 3:0 1:1 2:1 3:1 p test + n 100.0 100.0 100.0 50.0 50.0 50.0 + Y = 1 (%) 22.0 (22.1) 22.0 (22.0) 22.0 (22.0) 37.0 ( 74.0) 37.0 ( 74.0) 37.0 ( 74.0) <0.001 + C1 = 1 (%) 0.0 ( 0.0) 0.0 ( 0.0) 0.0 ( 0.0) 50.0 (100.0) 50.0 (100.0) 50.0 (100.0) <0.001 +. Stratified by E + 1 2 3 p test + n 150.0 150.0 150.0 + Y = 1 (%) 59.0 (39.4) 59.0 (39.3) 59.0 (39.3) 1.000 + C1 = 1 (%) 50.0 (33.3) 50.0 (33.3) 50.0 (33.3) 1.000 +. Stratified by + Overall + n 450.0 + Y = 1 (%) 177.0 (39.4) + C1 = 1 (%) 150.0 (33.3) +. Stratified by + Overall + n 450.0 + Y (%) + 0 272.7 (60.6) + 1 177.0 (39.3) + NA 0.3 ( 0.1) + C1 = 1 (%) 150.0 (33.3) +. Stratified by E:C1 + 1:0 2:0 3:0 1:1 2:1 3:1 p test + n 100.0 100.0 100.0 50.0 50.0 50.0 + Y = 1 (%) 22.0 (22.1) 22.0 (22.0) 22.0 (22.0) 37.0 ( 74.0) 37.0 ( 74.0) 37.0 ( 74.0) <0.001 + C1 = 1 (%) 0.0 ( 0.0) 0.0 ( 0.0) 0.0 ( 0.0) 50.0 (100.0) 50.0 (100.0) 50.0 (100.0) <0.001 +. Stratified by E:C1 + 1:0 2:0 3:0 1:1 2:1 + n 100.012 100.003 100.000 50.000 50.000 + Y = 1 (%) 22.001 (22.062) 22.001 (22.000) 22.000 (22.000) 37.000 ( 74.000) 37.000 ( 74.000) + C1 = 1 (%) 0.000 ( 0.000) 0.000 ( 0.000) 0.000 ( 0.000) 50.000 (100.000) 50.000 (100.000) + Stratified by E:C1 + 3:1 p test + n 50.000 + Y = 1 (%) 37.000 ( 74.000) <0.00001 + C1 = 1 (%) 50.000 (100.000) <0.00001 +. Stratified by E:C1 + 1:0 2:0 3:0 1:1 2:1 3:1 + n 100.0 100.0 100.0 50.0 50.0 50.0 + Y = 1 (%) 22.0 (22.1) 22.0 (22.0) 22.0 (22.0) 37.0 ( 74.0) 37.0 ( 74.0) 37.0 ( 74.0) + C1 = 1 (%) 0.0 ( 0.0) 0.0 ( 0.0) 0.0 ( 0.0) 50.0 (100.0) 50.0 (100.0) 50.0 (100.0) +. Stratified by E + 1 2 3 p test + n 150.012 150.003 150.000 + Y = 1 (%) 59.001 (39.406) 59.001 (39.333) 59.000 (39.333) 1.000 + C1 = 1 (%) 50.000 (33.331) 50.000 (33.333) 50.000 (33.333) 1.000 +. Stratified by E + level 1 2 3 p test + n 150.0 150.0 150.0 + Y (%) 0 90.7 (60.6) 91.0 (60.7) 91.0 (60.7) 1.000 + 1 59.0 (39.4) 59.0 (39.3) 59.0 (39.3) + C1 (%) 0 100.0 (66.7) 100.0 (66.7) 100.0 (66.7) 1.000 + 1 50.0 (33.3) 50.0 (33.3) 50.0 (33.3) +. Stratified by E + 1 2 3 p test + n 150.0 150.0 150.0 + Y = 1 59.0 (39.4) 59.0 (39.3) 59.0 (39.3) 1.000 + C1 = 1 50.0 (33.3) 50.0 (33.3) 50.0 (33.3) 1.000 +. Stratified by E + 1 2 3 p test + n 150.0 150.0 150.0 + Y = 1 59.0 59.0 59.0 1.000 + C1 = 1 50.0 50.0 50.0 1.000 +. Stratified by E + 1 2 3 p test + n 150.0 150.0 150.0 + Y = 1 (%) 39.4 39.3 39.3 1.000 + C1 = 1 (%) 33.3 33.3 33.3 1.000 +. Stratified by E + 1 2 3 p test + n 150.0 150.0 150.0 + Y = 1 % (freq) 39.4 (59.0) 39.3 (59.0) 39.3 (59.0) 1.000 + C1 = 1 % (freq) 33.3 (50.0) 33.3 (50.0) 33.3 (50.0) 1.000 +. Stratified by E + 1 2 3 p test + n 150.0 150.0 150.0 + Y = 0/1 (%) 90.7/59.0 (60.6/39.4) 91.0/59.0 (60.7/39.3) 91.0/59.0 (60.7/39.3) 1.000 + C1 = 1 (%) 50.0 (33.3) 50.0 (33.3) 50.0 (33.3) 1.000 +. "Stratified by E" + "" "level" "1" "2" "3" "p" "test" + "n" "" "150.0" "150.0" "150.0" "" "" + "Y (%)" "0" "90.7 (60.6)" "91.0 (60.7)" "91.0 (60.7)" "1.000" "" + "" "1" "59.0 (39.4)" "59.0 (39.3)" "59.0 (39.3)" "" "" + "C1 (%)" "0" "100.0 (66.7)" "100.0 (66.7)" "100.0 (66.7)" "1.000" "" + "" "1" "50.0 (33.3)" "50.0 (33.3)" "50.0 (33.3)" "" "" +. Stratified by E:C1 + 1:0 2:0 3:0 1:1 2:1 3:1 p test + n 100.0 100.0 100.0 50.0 50.0 50.0 + Y = 1 (%) 22.0 (22.1) 22.0 (22.0) 22.0 (22.0) 37.0 ( 74.0) 37.0 ( 74.0) 37.0 ( 74.0) <0.001 + C1 = 1 (%) 0.0 ( 0.0) 0.0 ( 0.0) 0.0 ( 0.0) 50.0 (100.0) 50.0 (100.0) 50.0 (100.0) <0.001 + + + Cell Contents +|-------------------------| +| N | +| Chi-square contribution | +| N / Row Total | +| N / Col Total | +| N / Table Total | +|-------------------------| + + +Total Observations in Table: 449.7292 + + + | E:C1 + Y | 1:0 | 2:0 | 3:0 | 1:1 | 2:1 | 3:1 | Row Total | +-------------|-----------|-----------|-----------|-----------|-----------|-----------|-----------| + 0 | 77 | 78 | 78 | 13 | 13 | 13 | 272 | + | 4.919 | 4.968 | 4.968 | 9.895 | 9.895 | 9.895 | | + | 0.285 | 0.286 | 0.286 | 0.048 | 0.048 | 0.048 | 0.606 | + | 0.779 | 0.780 | 0.780 | 0.260 | 0.260 | 0.260 | | + | 0.173 | 0.173 | 0.173 | 0.029 | 0.029 | 0.029 | | +-------------|-----------|-----------|-----------|-----------|-----------|-----------|-----------| + 1 | 22 | 22 | 22 | 37 | 37 | 37 | 177 | + | 7.580 | 7.655 | 7.655 | 15.246 | 15.246 | 15.246 | | + | 0.124 | 0.124 | 0.124 | 0.209 | 0.209 | 0.209 | 0.394 | + | 0.221 | 0.220 | 0.220 | 0.740 | 0.740 | 0.740 | | + | 0.049 | 0.049 | 0.049 | 0.082 | 0.082 | 0.082 | | +-------------|-----------|-----------|-----------|-----------|-----------|-----------|-----------| +Column Total | 99 | 100 | 100 | 50 | 50 | 50 | 449 | + | 0.222 | 0.222 | 0.222 | 0.111 | 0.111 | 0.111 | | +-------------|-----------|-----------|-----------|-----------|-----------|-----------|-----------| + + + + + Cell Contents +|-------------------------| +| N | +| Chi-square contribution | +| N / Row Total | +| N / Col Total | +| N / Table Total | +|-------------------------| + + +Total Observations in Table: 450.015 + + + | E:C1 + C1 | 1:0 | 2:0 | 3:0 | 1:1 | 2:1 | 3:1 | Row Total | +-------------|-----------|-----------|-----------|-----------|-----------|-----------|-----------| + 0 | 100 | 100 | 100 | 0 | 0 | 0 | 300 | + | 16.667 | 16.666 | 16.665 | 33.334 | 33.334 | 33.334 | | + | 0.333 | 0.333 | 0.333 | 0.000 | 0.000 | 0.000 | 0.667 | + | 1.000 | 1.000 | 1.000 | 0.000 | 0.000 | 0.000 | | + | 0.222 | 0.222 | 0.222 | 0.000 | 0.000 | 0.000 | | +-------------|-----------|-----------|-----------|-----------|-----------|-----------|-----------| + 1 | 0 | 0 | 0 | 50 | 50 | 50 | 150 | + | 33.336 | 33.333 | 33.332 | 66.671 | 66.671 | 66.671 | | + | 0.000 | 0.000 | 0.000 | 0.333 | 0.333 | 0.333 | 0.333 | + | 0.000 | 0.000 | 0.000 | 1.000 | 1.000 | 1.000 | | + | 0.000 | 0.000 | 0.000 | 0.111 | 0.111 | 0.111 | | +-------------|-----------|-----------|-----------|-----------|-----------|-----------|-----------| +Column Total | 100 | 100 | 100 | 50 | 50 | 50 | 450 | + | 0.222 | 0.222 | 0.222 | 0.111 | 0.111 | 0.111 | | +-------------|-----------|-----------|-----------|-----------|-----------|-----------|-----------| + + +. Stratified by E + 1 2 3 p test + n 150.01 150.00 150.00 + E (mean (sd)) 1.00 (0.00) 2.00 (0.00) 3.00 (0.00) <0.001 + C (mean (sd)) 2.13 (0.89) 2.13 (0.89) 2.13 (0.89) 1.000 + C2 (mean (sd)) 0.47 (0.50) 0.47 (0.50) 0.47 (0.50) 1.000 +. Stratified by + Overall + n 450.01 + E (mean (sd)) 2.00 (0.82) + C (mean (sd)) 2.13 (0.88) + C2 (mean (sd)) 0.47 (0.50) +. Stratified by E:C1 + 1:0 2:0 3:0 1:1 2:1 3:1 p test + n 100.01 100.00 100.00 50.00 50.00 50.00 + E (mean (sd)) 1.00 (0.00) 2.00 (0.00) 3.00 (0.00) 1.00 (0.00) 2.00 (0.00) 3.00 (0.00) <0.001 + C (mean (sd)) 1.60 (0.49) 1.60 (0.49) 1.60 (0.49) 3.20 (0.40) 3.20 (0.40) 3.20 (0.40) <0.001 + C2 (mean (sd)) 0.60 (0.49) 0.60 (0.49) 0.60 (0.49) 0.20 (0.40) 0.20 (0.40) 0.20 (0.40) <0.001 +. Stratified by E + 1 2 3 p test + n 150.012 150.003 150.000 + E (mean (sd)) 1.000 (0.000) 2.000 (0.000) 3.000 (0.000) <0.00001 + C (mean (sd)) 2.133 (0.885) 2.133 (0.886) 2.133 (0.886) 1.00000 + C2 (mean (sd)) 0.467 (0.499) 0.467 (0.500) 0.467 (0.500) 1.00000 +. Stratified by E + 1 2 3 + n 150.01 150.00 150.00 + E (mean (sd)) 1.00 (0.00) 2.00 (0.00) 3.00 (0.00) + C (mean (sd)) 2.13 (0.89) 2.13 (0.89) 2.13 (0.89) + C2 (mean (sd)) 0.47 (0.50) 0.47 (0.50) 0.47 (0.50) +. Stratified by E + 1 2 3 p test + n 150.01 150.00 150.00 + E (mean (sd)) 1.00 (0.00) 2.00 (0.00) 3.00 (0.00) <0.001 + C (median [IQR]) 2.00 [1.00, 3.00] 2.00 [1.00, 3.00] 2.00 [1.00, 3.00] 1.000 nonnorm + C2 (mean (sd)) 0.47 (0.50) 0.47 (0.50) 0.47 (0.50) 1.000 +. Stratified by E + 1 2 3 p test + n 150.01 150.00 150.00 + E (mean (sd)) 1.00 (0.00) 2.00 (0.00) 3.00 (0.00) <0.001 + C (median [range]) 2.00 [1.00, 4.00] 2.00 [1.00, 4.00] 2.00 [1.00, 4.00] 1.000 nonnorm + C2 (mean (sd)) 0.47 (0.50) 0.47 (0.50) 0.47 (0.50) 1.000 +. Stratified by E + 1 2 3 p test + n 150.01 150.00 150.00 + E (mean (sd)) 1.00 (0.00) 2.00 (0.00) 3.00 (0.00) <0.001 + C (mean (sd)) 2.13 (0.89) 2.13 (0.89) 2.13 (0.89) 1.000 + C2 (mean (sd)) 0.47 (0.50) 0.47 (0.50) 0.47 (0.50) 1.000 +. Stratified by E + 1 2 3 p test + n 150.01 150.00 150.00 + E 1.00 (0.00) 2.00 (0.00) 3.00 (0.00) <0.001 + C 2.13 (0.89) 2.13 (0.89) 2.13 (0.89) 1.000 + C2 0.47 (0.50) 0.47 (0.50) 0.47 (0.50) 1.000 +. "Stratified by E" + "" "1" "2" "3" "p" "test" + "n" "150.01" "150.00" "150.00" "" "" + "E (mean (sd))" "1.00 (0.00)" "2.00 (0.00)" "3.00 (0.00)" "<0.001" "" + "C (mean (sd))" "2.13 (0.89)" "2.13 (0.89)" "2.13 (0.89)" "1.000" "" + "C2 (mean (sd))" "0.47 (0.50)" "0.47 (0.50)" "0.47 (0.50)" "1.000" "" +........ Stratified by RIAGENDR + 1 2 p test + n 134944553.92 141591892.00 + HI_CHOL (mean (sd)) 0.10 (0.30) 0.12 (0.33) 0.009 +... Stratified by RIAGENDR + 1 2 p test + n 134944553.92 141591892.00 + HI_CHOL (mean (sd)) 0.10 (0.30) 0.12 (0.33) 0.009 +. Stratified by RIAGENDR + 1 2 p test + n 134944553.92 141591892.00 + HI_CHOL (median [IQR]) 0.00 [0.00, 0.00] 0.00 [0.00, 0.00] 0.009 nonnorm +. Stratified by RIAGENDR + 1 2 p test + n 134944553.92 141591892.00 + HI_CHOL (median [range]) 0.00 [0.00, 1.00] 0.00 [0.00, 1.00] 0.009 nonnorm +. Stratified by RIAGENDR + 1 2 p test + n 134944553.92 141591892.00 + HI_CHOL (mean (sd)) 0.10 (0.30) 0.12 (0.33) 0.0092211 +. Stratified by RIAGENDR + 1 2 p test + n 134944553.92 141591892.00 + HI_CHOL (median [IQR]) 0.00 [0.00, 0.00] 0.00 [0.00, 0.00] 0.0092211 nonnorm +. Stratified by RIAGENDR + 1 2 p test + n 134944553.923 141591891.998 + race (%) 0.042 + 1 21381884.189 (15.845) 20251367.389 ( 14.303) + 2 89315751.415 (66.187) 92486945.142 ( 65.319) + 3 15045455.461 (11.149) 17967228.319 ( 12.689) + 4 9201462.858 ( 6.819) 10886351.148 ( 7.689) + agecat (%) 0.001 + (0,19] 29299546.109 (21.712) 28150760.544 ( 19.882) + (19,39] 40497613.070 (30.011) 40640361.534 ( 28.702) + (39,59] 41053579.409 (30.423) 42817044.014 ( 30.240) + (59,Inf] 24093815.335 (17.855) 29983725.904 ( 21.176) + RIAGENDR = 2 (%) 0.000 ( 0.000) 141591891.998 (100.000) <0.001 +. Stratified by RIAGENDR + 1 2 p test + n 134944553.923 141591891.998 + race (%) 0.042 + 1 21381884.189 (15.845) 20251367.389 ( 14.303) + 2 89315751.415 (66.187) 92486945.142 ( 65.319) + 3 15045455.461 (11.149) 17967228.319 ( 12.689) + 4 9201462.858 ( 6.819) 10886351.148 ( 7.689) + agecat (%) 0.001 + (0,19] 29299546.109 (21.712) 28150760.544 ( 19.882) + (19,39] 40497613.070 (30.011) 40640361.534 ( 28.702) + (39,59] 41053579.409 (30.423) 42817044.014 ( 30.240) + (59,Inf] 24093815.335 (17.855) 29983725.904 ( 21.176) + RIAGENDR = 2 (%) 0.000 ( 0.000) 141591891.998 (100.000) <0.001 +. Stratified by RIAGENDR + 1 2 p test + n 134944553.9 141591892.0 + race (%) 0.0416288 + 1 21381884.2 (15.8) 20251367.4 ( 14.3) + 2 89315751.4 (66.2) 92486945.1 ( 65.3) + 3 15045455.5 (11.1) 17967228.3 ( 12.7) + 4 9201462.9 ( 6.8) 10886351.1 ( 7.7) + agecat (%) 0.0011757 + (0,19] 29299546.1 (21.7) 28150760.5 ( 19.9) + (19,39] 40497613.1 (30.0) 40640361.5 ( 28.7) + (39,59] 41053579.4 (30.4) 42817044.0 ( 30.2) + (59,Inf] 24093815.3 (17.9) 29983725.9 ( 21.2) + RIAGENDR = 2 (%) 0.0 ( 0.0) 141591892.0 (100.0) <0.0000001 +. Stratified by RIAGENDR + 1 2 p test + n 134944553.92 141591892.00 + HI_CHOL (mean (sd)) 0.10 (0.30) 0.12 (0.33) 0.0092211 + race (%) 0.0416288 + 1 21381884.2 (15.8) 20251367.4 ( 14.3) + 2 89315751.4 (66.2) 92486945.1 ( 65.3) + 3 15045455.5 (11.1) 17967228.3 ( 12.7) + 4 9201462.9 ( 6.8) 10886351.1 ( 7.7) + agecat (%) 0.0011757 + (0,19] 29299546.1 (21.7) 28150760.5 ( 19.9) + (19,39] 40497613.1 (30.0) 40640361.5 ( 28.7) + (39,59] 41053579.4 (30.4) 42817044.0 ( 30.2) + (59,Inf] 24093815.3 (17.9) 29983725.9 ( 21.2) + RIAGENDR = 2 (%) 0.0 ( 0.0) 141591892.0 (100.0) <0.0000001 +. Stratified by RIAGENDR + 1 2 p test + n 134944553.92 141591892.00 + HI_CHOL (mean (sd)) 0.10 (0.30) 0.12 (0.33) 0.0092211 + race (%) 0.0416288 + 1 21381884.2 (15.8) 20251367.4 ( 14.3) + 2 89315751.4 (66.2) 92486945.1 ( 65.3) + 3 15045455.5 (11.1) 17967228.3 ( 12.7) + 4 9201462.9 ( 6.8) 10886351.1 ( 7.7) + agecat (%) 0.0011757 + (0,19] 29299546.1 (21.7) 28150760.5 ( 19.9) + (19,39] 40497613.1 (30.0) 40640361.5 ( 28.7) + (39,59] 41053579.4 (30.4) 42817044.0 ( 30.2) + (59,Inf] 24093815.3 (17.9) 29983725.9 ( 21.2) + RIAGENDR = 2 (%) 0.0 ( 0.0) 141591892.0 (100.0) <0.0000001 +. Stratified by RIAGENDR + 1 2 p test + n 134944553.92 141591892.00 + HI_CHOL (median [IQR]) 0.00 [0.00, 0.00] 0.00 [0.00, 0.00] 0.0092211 nonnorm + race (%) 0.0416288 + 1 21381884.2 (15.8) 20251367.4 ( 14.3) + 2 89315751.4 (66.2) 92486945.1 ( 65.3) + 3 15045455.5 (11.1) 17967228.3 ( 12.7) + 4 9201462.9 ( 6.8) 10886351.1 ( 7.7) + agecat (%) 0.0011757 + (0,19] 29299546.1 (21.7) 28150760.5 ( 19.9) + (19,39] 40497613.1 (30.0) 40640361.5 ( 28.7) + (39,59] 41053579.4 (30.4) 42817044.0 ( 30.2) + (59,Inf] 24093815.3 (17.9) 29983725.9 ( 21.2) + RIAGENDR = 2 (%) 0.0 ( 0.0) 141591892.0 (100.0) <0.0000001 +. Stratified by RIAGENDR + 1 2 p test + n 134944553.92 141591892.00 + HI_CHOL (mean (sd)) 0.10 (0.30) 0.12 (0.33) 0.009 + race (%) 0.042 + 1 21381884.189 (15.845) 20251367.389 ( 14.303) + 2 89315751.415 (66.187) 92486945.142 ( 65.319) + 3 15045455.461 (11.149) 17967228.319 ( 12.689) + 4 9201462.858 ( 6.819) 10886351.148 ( 7.689) + agecat (%) 0.001 + (0,19] 29299546.109 (21.712) 28150760.544 ( 19.882) + (19,39] 40497613.070 (30.011) 40640361.534 ( 28.702) + (39,59] 41053579.409 (30.423) 42817044.014 ( 30.240) + (59,Inf] 24093815.335 (17.855) 29983725.904 ( 21.176) + RIAGENDR = 2 (%) 0.000 ( 0.000) 141591891.998 (100.000) <0.001 +.. Stratified by RIAGENDR + 1 2 p test + n 134944553.92 141591892.00 + HI_CHOL (median [IQR]) 0.00 [0.00, 0.00] 0.00 [0.00, 0.00] 0.0092211 nonnorm + race (%) 0.0416288 + 1 21381884.2 (15.8) 20251367.4 ( 14.3) + 2 89315751.4 (66.2) 92486945.1 ( 65.3) + 3 15045455.5 (11.1) 17967228.3 ( 12.7) + 4 9201462.9 ( 6.8) 10886351.1 ( 7.7) + agecat (%) 0.0011757 + (0,19] 29299546.1 (21.7) 28150760.5 ( 19.9) + (19,39] 40497613.1 (30.0) 40640361.5 ( 28.7) + (39,59] 41053579.4 (30.4) 42817044.0 ( 30.2) + (59,Inf] 24093815.3 (17.9) 29983725.9 ( 21.2) + RIAGENDR = 2 (%) 0.0 ( 0.0) 141591892.0 (100.0) <0.0000001 +. + ### Summary of continuous variables ### + +: Overall + n miss p.miss mean sd median p25 p75 min max +E 450 0 0 2.0 0.8 2 1 3 1 3 +C 450 0 0 2.1 0.9 2 1 3 1 4 +C2 450 0 0 0.5 0.5 0 0 1 0 1 + +======================================================================================= + + ### Summary of categorical variables ### + +: Overall + var n miss p.miss level freq percent cum.percent + Y 450.0 0.3 0.1 0 272.7 60.6 60.6 + 1 177.0 39.4 100.0 + + C1 450.0 0.0 0.0 0 300.0 66.7 66.7 + 1 150.0 33.3 100.0 + +. + ### Summary of continuous variables ### + +: Overall + n miss p.miss mean sd median p25 p75 min max +E 450 0 0 2.0 0.8 2 1 3 1 3 +C 450 0 0 2.1 0.9 2 1 3 1 4 +C2 450 0 0 0.5 0.5 0 0 1 0 1 + +======================================================================================= + + ### Summary of categorical variables ### + +: Overall + var n miss p.miss level freq percent cum.percent + Y 450.0 0.0 0.0 0 272.7 60.6 60.6 + 1 177.0 39.3 99.9 + 0.3 0.1 100.0 + + C1 450.0 0.0 0.0 0 300.0 66.7 66.7 + 1 150.0 33.3 100.0 + +. + ### Summary of continuous variables ### + +E: 1 + n miss p.miss mean sd median p25 p75 min max +E 150 0 0 1.0 0.0 1 1 1 1 1 +C 150 0 0 2.1 0.9 2 1 3 1 4 +C2 150 0 0 0.5 0.5 0 0 1 0 1 +--------------------------------------------------------------------------------------- +E: 2 + n miss p.miss mean sd median p25 p75 min max +E 150 0 0 2.0 0.0 2 2 2 2 2 +C 150 0 0 2.1 0.9 2 1 3 1 4 +C2 150 0 0 0.5 0.5 0 0 1 0 1 +--------------------------------------------------------------------------------------- +E: 3 + n miss p.miss mean sd median p25 p75 min max +E 150 0 0 3.0 0.0 3 3 3 3 3 +C 150 0 0 2.1 0.9 2 1 3 1 4 +C2 150 0 0 0.5 0.5 0 0 1 0 1 + +p-values + pNormal pNonNormal +E 0.0000000 0.0000000 +C 0.9999992 0.9999991 +C2 0.9999993 0.9999993 + +Standardize mean differences + average 1 vs 2 1 vs 3 2 vs 3 +E Inf Inf Inf Inf +C 0.00006823456 0.00009934218 0.00010235224 0.000003009264 +C2 0.00006404022 0.00009606505 0.00007471632 0.000021339301 + +======================================================================================= + + ### Summary of categorical variables ### + +E: 1 + var n miss p.miss level freq percent cum.percent + Y 150.0 0.3 0.2 0 90.7 60.6 60.6 + 1 59.0 39.4 100.0 + + C1 150.0 0.0 0.0 0 100.0 66.7 66.7 + 1 50.0 33.3 100.0 + +--------------------------------------------------------------------------------------- +E: 2 + var n miss p.miss level freq percent cum.percent + Y 150.0 0.0 0.0 0 91.0 60.7 60.7 + 1 59.0 39.3 100.0 + + C1 150.0 0.0 0.0 0 100.0 66.7 66.7 + 1 50.0 33.3 100.0 + +--------------------------------------------------------------------------------------- +E: 3 + var n miss p.miss level freq percent cum.percent + Y 150.0 0.0 0.0 0 91.0 60.7 60.7 + 1 59.0 39.3 100.0 + + C1 150.0 0.0 0.0 0 100.0 66.7 66.7 + 1 50.0 33.3 100.0 + + +p-values + pApprox pExact +Y 0.9998243 NA +C1 0.9999998 NA + +Standardize mean differences + average 1 vs 2 1 vs 3 2 vs 3 +Y 0.00099495892 0.00149243839 0.00148861716 0.000003821227 +C1 0.00003770972 0.00004242269 0.00005656458 0.000014141888 +. + ### Summary of continuous variables ### + +E:C1: 1:0 + n miss p.miss mean sd median p25 p75 min max +E 100 0 0 1.0 0.0 1 1 1 1 1 +C 100 0 0 1.6 0.5 2 1 2 1 2 +C2 100 0 0 0.6 0.5 1 0 1 0 1 +--------------------------------------------------------------------------------------- +E:C1: 2:0 + n miss p.miss mean sd median p25 p75 min max +E 100 0 0 2.0 2e-16 2 2 2 2 2 +C 100 0 0 1.6 5e-01 2 1 2 1 2 +C2 100 0 0 0.6 5e-01 1 0 1 0 1 +--------------------------------------------------------------------------------------- +E:C1: 3:0 + n miss p.miss mean sd median p25 p75 min max +E 100 0 0 3.0 0.0 3 3 3 3 3 +C 100 0 0 1.6 0.5 2 1 2 1 2 +C2 100 0 0 0.6 0.5 1 0 1 0 1 +--------------------------------------------------------------------------------------- +E:C1: 1:1 + n miss p.miss mean sd median p25 p75 min max +E 50 0 0 1.0 0.0 1 1 1 1 1 +C 50 0 0 3.2 0.4 3 3 3 3 4 +C2 50 0 0 0.2 0.4 0 0 0 0 1 +--------------------------------------------------------------------------------------- +E:C1: 2:1 + n miss p.miss mean sd median p25 p75 min max +E 50 0 0 2.0 0.0 2 2 2 2 2 +C 50 0 0 3.2 0.4 3 3 3 3 4 +C2 50 0 0 0.2 0.4 0 0 0 0 1 +--------------------------------------------------------------------------------------- +E:C1: 3:1 + n miss p.miss mean sd median p25 p75 min max +E 50 0 0 3.0 0.0 3 3 3 3 3 +C 50 0 0 3.2 0.4 3 3 3 3 4 +C2 50 0 0 0.2 0.4 0 0 0 0 1 + +p-values + pNormal pNonNormal +E 0.000000e+00 0.00000e+00 +C 2.518377e-307 0.00000e+00 +C2 1.950910e-37 1.95091e-37 + +Standardize mean differences + average 1 vs 2 1 vs 3 1 vs 4 1 vs 5 1 vs 6 2 vs 3 2 vs 4 2 vs 5 +E NaN 6.350292e+15 Inf NaN Inf Inf 6.350292e+15 6.350292e+15 1.4100480 +C 2.1373647 1.710913e-04 0.000146495 3.5608223 3.5673100 3.5708236 2.439672e-05 3.556921e+00 3.5633882 +C2 0.5343248 1.710913e-04 0.000146495 0.8900053 0.8916269 0.8925051 2.439672e-05 8.892636e-01 0.8908805 + 2 vs 6 3 vs 4 3 vs 5 3 vs 6 4 vs 5 4 vs 6 5 vs 6 +E 6.350292e+15 Inf Inf NaN Inf Inf Inf +C 3.566891e+00 3.5525326 3.5589756 3.5624649 0 0 0 +C2 8.917561e-01 0.8881331 0.8897439 0.8906162 0 0 0 + +======================================================================================= + + ### Summary of categorical variables ### + +E:C1: 1:0 + var n miss p.miss level freq percent cum.percent + Y 100.0 0.3 0.3 0 77.7 77.9 77.9 + 1 22.0 22.1 100.0 + + C1 100.0 0.0 0.0 0 100.0 100.0 100.0 + 1 0.0 0.0 100.0 + +--------------------------------------------------------------------------------------- +E:C1: 2:0 + var n miss p.miss level freq percent cum.percent + Y 100.0 0.0 0.0 0 78.0 78.0 78.0 + 1 22.0 22.0 100.0 + + C1 100.0 0.0 0.0 0 100.0 100.0 100.0 + 1 0.0 0.0 100.0 + +--------------------------------------------------------------------------------------- +E:C1: 3:0 + var n miss p.miss level freq percent cum.percent + Y 100.0 0.0 0.0 0 78.0 78.0 78.0 + 1 22.0 22.0 100.0 + + C1 100.0 0.0 0.0 0 100.0 100.0 100.0 + 1 0.0 0.0 100.0 + +--------------------------------------------------------------------------------------- +E:C1: 1:1 + var n miss p.miss level freq percent cum.percent + Y 50.0 0.0 0.0 0 13.0 26.0 26.0 + 1 37.0 74.0 100.0 + + C1 50.0 0.0 0.0 0 0.0 0.0 0.0 + 1 50.0 100.0 100.0 + +--------------------------------------------------------------------------------------- +E:C1: 2:1 + var n miss p.miss level freq percent cum.percent + Y 50.0 0.0 0.0 0 13.0 26.0 26.0 + 1 37.0 74.0 100.0 + + C1 50.0 0.0 0.0 0 0.0 0.0 0.0 + 1 50.0 100.0 100.0 + +--------------------------------------------------------------------------------------- +E:C1: 3:1 + var n miss p.miss level freq percent cum.percent + Y 50.0 0.0 0.0 0 13.0 26.0 26.0 + 1 37.0 74.0 100.0 + + C1 50.0 0.0 0.0 0 0.0 0.0 0.0 + 1 50.0 100.0 100.0 + + +p-values + pApprox pExact +Y 4.540885e-31 NA +C1 4.640660e-128 NA + +Standardize mean differences + average 1 vs 2 1 vs 3 1 vs 4 1 vs 5 1 vs 6 2 vs 3 2 vs 4 2 vs 5 2 vs 6 3 vs 4 +Y 0.7311319 0.001480611 0.001486405 1.216879 1.216879 1.216879 0.000005793469 1.218891 1.218891 1.218891 1.218899 +C1 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN + 3 vs 5 3 vs 6 4 vs 5 4 vs 6 5 vs 6 +Y 1.218899 1.218899 0 0 0 +C1 NaN NaN NaN NaN NaN +. + ### Summary of continuous variables ### + +E:C1: 1:0 + n miss p.miss mean sd median p25 p75 min max +E 100 0 0 1 0.0 1 1 1 1 1 +C 100 0 0 2 0.5 2 1 2 1 2 +--------------------------------------------------------------------------------------- +E:C1: 2:0 + n miss p.miss mean sd median p25 p75 min max +E 100 0 0 2 2e-16 2 2 2 2 2 +C 100 0 0 2 5e-01 2 1 2 1 2 +--------------------------------------------------------------------------------------- +E:C1: 3:0 + n miss p.miss mean sd median p25 p75 min max +E 100 0 0 3 0.0 3 3 3 3 3 +C 100 0 0 2 0.5 2 1 2 1 2 +--------------------------------------------------------------------------------------- +E:C1: 1:1 + n miss p.miss mean sd median p25 p75 min max +E 50 0 0 1 0.0 1 1 1 1 1 +C 50 0 0 3 0.4 3 3 3 3 4 +--------------------------------------------------------------------------------------- +E:C1: 2:1 + n miss p.miss mean sd median p25 p75 min max +E 50 0 0 2 0.0 2 2 2 2 2 +C 50 0 0 3 0.4 3 3 3 3 4 +--------------------------------------------------------------------------------------- +E:C1: 3:1 + n miss p.miss mean sd median p25 p75 min max +E 50 0 0 3 0.0 3 3 3 3 3 +C 50 0 0 3 0.4 3 3 3 3 4 + +p-values + pNormal pNonNormal +E 0.000000e+00 0 +C 2.518377e-307 0 + +Standardize mean differences + average 1 vs 2 1 vs 3 1 vs 4 1 vs 5 1 vs 6 2 vs 3 2 vs 4 2 vs 5 2 vs 6 +E NaN 6.350292e+15 Inf NaN Inf Inf 6.350292e+15 6.350292e+15 1.410048 6.350292e+15 +C 2.137365 1.710913e-04 0.000146495 3.560822 3.56731 3.570824 2.439672e-05 3.556921e+00 3.563388 3.566891e+00 + 3 vs 4 3 vs 5 3 vs 6 4 vs 5 4 vs 6 5 vs 6 +E Inf Inf NaN Inf Inf Inf +C 3.552533 3.558976 3.562465 0 0 0 +. + ### Summary of categorical variables ### + +E:C1: 1:0 + var n miss p.miss level freq percent cum.percent + Y 100.0 0.3 0.3 0 77.7 77.9 77.9 + 1 22.0 22.1 100.0 + + C1 100.0 0.0 0.0 0 100.0 100.0 100.0 + 1 0.0 0.0 100.0 + +--------------------------------------------------------------------------------------- +E:C1: 2:0 + var n miss p.miss level freq percent cum.percent + Y 100.0 0.0 0.0 0 78.0 78.0 78.0 + 1 22.0 22.0 100.0 + + C1 100.0 0.0 0.0 0 100.0 100.0 100.0 + 1 0.0 0.0 100.0 + +--------------------------------------------------------------------------------------- +E:C1: 3:0 + var n miss p.miss level freq percent cum.percent + Y 100.0 0.0 0.0 0 78.0 78.0 78.0 + 1 22.0 22.0 100.0 + + C1 100.0 0.0 0.0 0 100.0 100.0 100.0 + 1 0.0 0.0 100.0 + +--------------------------------------------------------------------------------------- +E:C1: 1:1 + var n miss p.miss level freq percent cum.percent + Y 50.0 0.0 0.0 0 13.0 26.0 26.0 + 1 37.0 74.0 100.0 + + C1 50.0 0.0 0.0 0 0.0 0.0 0.0 + 1 50.0 100.0 100.0 + +--------------------------------------------------------------------------------------- +E:C1: 2:1 + var n miss p.miss level freq percent cum.percent + Y 50.0 0.0 0.0 0 13.0 26.0 26.0 + 1 37.0 74.0 100.0 + + C1 50.0 0.0 0.0 0 0.0 0.0 0.0 + 1 50.0 100.0 100.0 + +--------------------------------------------------------------------------------------- +E:C1: 3:1 + var n miss p.miss level freq percent cum.percent + Y 50.0 0.0 0.0 0 13.0 26.0 26.0 + 1 37.0 74.0 100.0 + + C1 50.0 0.0 0.0 0 0.0 0.0 0.0 + 1 50.0 100.0 100.0 + + +p-values + pApprox pExact +Y 4.540885e-31 NA +C1 4.640660e-128 NA + +Standardize mean differences + average 1 vs 2 1 vs 3 1 vs 4 1 vs 5 1 vs 6 2 vs 3 2 vs 4 2 vs 5 2 vs 6 3 vs 4 +Y 0.7311319 0.001480611 0.001486405 1.216879 1.216879 1.216879 0.000005793469 1.218891 1.218891 1.218891 1.218899 +C1 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN + 3 vs 5 3 vs 6 4 vs 5 4 vs 6 5 vs 6 +Y 1.218899 1.218899 0 0 0 +C1 NaN NaN NaN NaN NaN +. + +DONE diff --git a/tests/testthat/ref-CatTable_2StrataVars b/tests/testthat/ref-CatTable_2StrataVars new file mode 100644 index 0000000..970b44a Binary files /dev/null and b/tests/testthat/ref-CatTable_2StrataVars differ diff --git a/tests/testthat/ref-CatTable_IncludeNA b/tests/testthat/ref-CatTable_IncludeNA new file mode 100644 index 0000000..97d0d24 Binary files /dev/null and b/tests/testthat/ref-CatTable_IncludeNA differ diff --git a/tests/testthat/ref-CatTable_cramVars b/tests/testthat/ref-CatTable_cramVars new file mode 100644 index 0000000..fecaa6c Binary files /dev/null and b/tests/testthat/ref-CatTable_cramVars differ diff --git a/tests/testthat/ref-CatTable_defaultPrint b/tests/testthat/ref-CatTable_defaultPrint new file mode 100644 index 0000000..82b3c9b Binary files /dev/null and b/tests/testthat/ref-CatTable_defaultPrint differ diff --git a/tests/testthat/ref-CatTable_digits b/tests/testthat/ref-CatTable_digits new file mode 100644 index 0000000..ef2330f Binary files /dev/null and b/tests/testthat/ref-CatTable_digits differ diff --git a/tests/testthat/ref-CatTable_explain b/tests/testthat/ref-CatTable_explain new file mode 100644 index 0000000..db023b2 Binary files /dev/null and b/tests/testthat/ref-CatTable_explain differ diff --git a/tests/testthat/ref-CatTable_format_f b/tests/testthat/ref-CatTable_format_f new file mode 100644 index 0000000..8b2ea40 Binary files /dev/null and b/tests/testthat/ref-CatTable_format_f differ diff --git a/tests/testthat/ref-CatTable_format_p b/tests/testthat/ref-CatTable_format_p new file mode 100644 index 0000000..c936fad Binary files /dev/null and b/tests/testthat/ref-CatTable_format_p differ diff --git a/tests/testthat/ref-CatTable_format_pf b/tests/testthat/ref-CatTable_format_pf new file mode 100644 index 0000000..745591b Binary files /dev/null and b/tests/testthat/ref-CatTable_format_pf differ diff --git a/tests/testthat/ref-CatTable_noSpaces b/tests/testthat/ref-CatTable_noSpaces new file mode 100644 index 0000000..0af4ff8 Binary files /dev/null and b/tests/testthat/ref-CatTable_noSpaces differ diff --git a/tests/testthat/ref-CatTable_noSpaces_showAllLevels_quote b/tests/testthat/ref-CatTable_noSpaces_showAllLevels_quote new file mode 100644 index 0000000..aaca728 Binary files /dev/null and b/tests/testthat/ref-CatTable_noSpaces_showAllLevels_quote differ diff --git a/tests/testthat/ref-CatTable_noTests b/tests/testthat/ref-CatTable_noTests new file mode 100644 index 0000000..793a086 Binary files /dev/null and b/tests/testthat/ref-CatTable_noTests differ diff --git a/tests/testthat/ref-CatTable_overallPrint b/tests/testthat/ref-CatTable_overallPrint new file mode 100644 index 0000000..399efb1 Binary files /dev/null and b/tests/testthat/ref-CatTable_overallPrint differ diff --git a/tests/testthat/ref-CatTable_showAllLevels b/tests/testthat/ref-CatTable_showAllLevels new file mode 100644 index 0000000..0585da0 Binary files /dev/null and b/tests/testthat/ref-CatTable_showAllLevels differ diff --git a/tests/testthat/ref-ContTable_2StrataVars b/tests/testthat/ref-ContTable_2StrataVars new file mode 100644 index 0000000..20e2b38 Binary files /dev/null and b/tests/testthat/ref-ContTable_2StrataVars differ diff --git a/tests/testthat/ref-ContTable_defaultPrint b/tests/testthat/ref-ContTable_defaultPrint new file mode 100644 index 0000000..9f884d7 Binary files /dev/null and b/tests/testthat/ref-ContTable_defaultPrint differ diff --git a/tests/testthat/ref-ContTable_digits b/tests/testthat/ref-ContTable_digits new file mode 100644 index 0000000..1edbb5f Binary files /dev/null and b/tests/testthat/ref-ContTable_digits differ diff --git a/tests/testthat/ref-ContTable_explain b/tests/testthat/ref-ContTable_explain new file mode 100644 index 0000000..34ffc9e Binary files /dev/null and b/tests/testthat/ref-ContTable_explain differ diff --git a/tests/testthat/ref-ContTable_noSpaces b/tests/testthat/ref-ContTable_noSpaces new file mode 100644 index 0000000..fbaa391 Binary files /dev/null and b/tests/testthat/ref-ContTable_noSpaces differ diff --git a/tests/testthat/ref-ContTable_noSpaces_showAllLevels_quote b/tests/testthat/ref-ContTable_noSpaces_showAllLevels_quote new file mode 100644 index 0000000..d2786b6 Binary files /dev/null and b/tests/testthat/ref-ContTable_noSpaces_showAllLevels_quote differ diff --git a/tests/testthat/ref-ContTable_noTests b/tests/testthat/ref-ContTable_noTests new file mode 100644 index 0000000..447c758 Binary files /dev/null and b/tests/testthat/ref-ContTable_noTests differ diff --git a/tests/testthat/ref-ContTable_nonnormal_exact b/tests/testthat/ref-ContTable_nonnormal_exact new file mode 100644 index 0000000..645563f Binary files /dev/null and b/tests/testthat/ref-ContTable_nonnormal_exact differ diff --git a/tests/testthat/ref-ContTable_nonnormal_minMax b/tests/testthat/ref-ContTable_nonnormal_minMax new file mode 100644 index 0000000..d1b6268 Binary files /dev/null and b/tests/testthat/ref-ContTable_nonnormal_minMax differ diff --git a/tests/testthat/ref-ContTable_overallPrint b/tests/testthat/ref-ContTable_overallPrint new file mode 100644 index 0000000..7dccf71 Binary files /dev/null and b/tests/testthat/ref-ContTable_overallPrint differ diff --git a/tests/testthat/ref-ExtractSmd_TableOne b/tests/testthat/ref-ExtractSmd_TableOne new file mode 100644 index 0000000..7bb94b3 Binary files /dev/null and b/tests/testthat/ref-ExtractSmd_TableOne differ diff --git a/tests/testthat/ref-ExtractSmd_svyTableOne b/tests/testthat/ref-ExtractSmd_svyTableOne new file mode 100644 index 0000000..bb08df4 Binary files /dev/null and b/tests/testthat/ref-ExtractSmd_svyTableOne differ diff --git a/tests/testthat/ref-TableOne_2StrataVars b/tests/testthat/ref-TableOne_2StrataVars new file mode 100644 index 0000000..64c460a Binary files /dev/null and b/tests/testthat/ref-TableOne_2StrataVars differ diff --git a/tests/testthat/ref-TableOne_CatOnly b/tests/testthat/ref-TableOne_CatOnly new file mode 100644 index 0000000..af9d731 Binary files /dev/null and b/tests/testthat/ref-TableOne_CatOnly differ diff --git a/tests/testthat/ref-TableOne_ContOnly b/tests/testthat/ref-TableOne_ContOnly new file mode 100644 index 0000000..3dba0ae Binary files /dev/null and b/tests/testthat/ref-TableOne_ContOnly differ diff --git a/tests/testthat/ref-TableOne_IncludeNA b/tests/testthat/ref-TableOne_IncludeNA new file mode 100644 index 0000000..2591775 Binary files /dev/null and b/tests/testthat/ref-TableOne_IncludeNA differ diff --git a/tests/testthat/ref-TableOne_defaultPrint b/tests/testthat/ref-TableOne_defaultPrint new file mode 100644 index 0000000..1596fa9 Binary files /dev/null and b/tests/testthat/ref-TableOne_defaultPrint differ diff --git a/tests/testthat/ref-TableOne_digits b/tests/testthat/ref-TableOne_digits new file mode 100644 index 0000000..c4c8425 Binary files /dev/null and b/tests/testthat/ref-TableOne_digits differ diff --git a/tests/testthat/ref-TableOne_noSpaces b/tests/testthat/ref-TableOne_noSpaces new file mode 100644 index 0000000..2114b09 Binary files /dev/null and b/tests/testthat/ref-TableOne_noSpaces differ diff --git a/tests/testthat/ref-TableOne_noSpaces_showAllLevels_quote b/tests/testthat/ref-TableOne_noSpaces_showAllLevels_quote new file mode 100644 index 0000000..f72e782 Binary files /dev/null and b/tests/testthat/ref-TableOne_noSpaces_showAllLevels_quote differ diff --git a/tests/testthat/ref-TableOne_noTests b/tests/testthat/ref-TableOne_noTests new file mode 100644 index 0000000..2b9b6d7 Binary files /dev/null and b/tests/testthat/ref-TableOne_noTests differ diff --git a/tests/testthat/ref-TableOne_nonnormal_exact b/tests/testthat/ref-TableOne_nonnormal_exact new file mode 100644 index 0000000..a21ac06 Binary files /dev/null and b/tests/testthat/ref-TableOne_nonnormal_exact differ diff --git a/tests/testthat/ref-TableOne_nonnormal_minMax b/tests/testthat/ref-TableOne_nonnormal_minMax new file mode 100644 index 0000000..da1e6a8 Binary files /dev/null and b/tests/testthat/ref-TableOne_nonnormal_minMax differ diff --git a/tests/testthat/ref-TableOne_overallPrint b/tests/testthat/ref-TableOne_overallPrint new file mode 100644 index 0000000..5de66cd Binary files /dev/null and b/tests/testthat/ref-TableOne_overallPrint differ diff --git a/tests/testthat/ref-TableOne_showAllLevels b/tests/testthat/ref-TableOne_showAllLevels new file mode 100644 index 0000000..3b5ae81 Binary files /dev/null and b/tests/testthat/ref-TableOne_showAllLevels differ diff --git a/tests/testthat/ref-svyCatTable_2StrataVars b/tests/testthat/ref-svyCatTable_2StrataVars new file mode 100644 index 0000000..571e134 Binary files /dev/null and b/tests/testthat/ref-svyCatTable_2StrataVars differ diff --git a/tests/testthat/ref-svyCatTable_IncludeNA b/tests/testthat/ref-svyCatTable_IncludeNA new file mode 100644 index 0000000..c7a9703 Binary files /dev/null and b/tests/testthat/ref-svyCatTable_IncludeNA differ diff --git a/tests/testthat/ref-svyCatTable_cramVars b/tests/testthat/ref-svyCatTable_cramVars new file mode 100644 index 0000000..5a3d3e8 Binary files /dev/null and b/tests/testthat/ref-svyCatTable_cramVars differ diff --git a/tests/testthat/ref-svyCatTable_defaultPrint b/tests/testthat/ref-svyCatTable_defaultPrint new file mode 100644 index 0000000..874eef4 Binary files /dev/null and b/tests/testthat/ref-svyCatTable_defaultPrint differ diff --git a/tests/testthat/ref-svyCatTable_digits b/tests/testthat/ref-svyCatTable_digits new file mode 100644 index 0000000..6d2fdba Binary files /dev/null and b/tests/testthat/ref-svyCatTable_digits differ diff --git a/tests/testthat/ref-svyCatTable_explain b/tests/testthat/ref-svyCatTable_explain new file mode 100644 index 0000000..f852f0b Binary files /dev/null and b/tests/testthat/ref-svyCatTable_explain differ diff --git a/tests/testthat/ref-svyCatTable_format_f b/tests/testthat/ref-svyCatTable_format_f new file mode 100644 index 0000000..2c493be Binary files /dev/null and b/tests/testthat/ref-svyCatTable_format_f differ diff --git a/tests/testthat/ref-svyCatTable_format_p b/tests/testthat/ref-svyCatTable_format_p new file mode 100644 index 0000000..23d62be Binary files /dev/null and b/tests/testthat/ref-svyCatTable_format_p differ diff --git a/tests/testthat/ref-svyCatTable_format_pf b/tests/testthat/ref-svyCatTable_format_pf new file mode 100644 index 0000000..4818ad8 Binary files /dev/null and b/tests/testthat/ref-svyCatTable_format_pf differ diff --git a/tests/testthat/ref-svyCatTable_noSpaces b/tests/testthat/ref-svyCatTable_noSpaces new file mode 100644 index 0000000..a3f737c Binary files /dev/null and b/tests/testthat/ref-svyCatTable_noSpaces differ diff --git a/tests/testthat/ref-svyCatTable_noSpaces_showAllLevels_quote b/tests/testthat/ref-svyCatTable_noSpaces_showAllLevels_quote new file mode 100644 index 0000000..27bd092 Binary files /dev/null and b/tests/testthat/ref-svyCatTable_noSpaces_showAllLevels_quote differ diff --git a/tests/testthat/ref-svyCatTable_noTests b/tests/testthat/ref-svyCatTable_noTests new file mode 100644 index 0000000..e356586 Binary files /dev/null and b/tests/testthat/ref-svyCatTable_noTests differ diff --git a/tests/testthat/ref-svyCatTable_overallPrint b/tests/testthat/ref-svyCatTable_overallPrint new file mode 100644 index 0000000..2a3399e Binary files /dev/null and b/tests/testthat/ref-svyCatTable_overallPrint differ diff --git a/tests/testthat/ref-svyCatTable_showAllLevels b/tests/testthat/ref-svyCatTable_showAllLevels new file mode 100644 index 0000000..2f8ae27 Binary files /dev/null and b/tests/testthat/ref-svyCatTable_showAllLevels differ diff --git a/tests/testthat/ref-svyContTable_2StrataVars b/tests/testthat/ref-svyContTable_2StrataVars new file mode 100644 index 0000000..ea80fa3 Binary files /dev/null and b/tests/testthat/ref-svyContTable_2StrataVars differ diff --git a/tests/testthat/ref-svyContTable_defaultPrint b/tests/testthat/ref-svyContTable_defaultPrint new file mode 100644 index 0000000..4f0cf92 Binary files /dev/null and b/tests/testthat/ref-svyContTable_defaultPrint differ diff --git a/tests/testthat/ref-svyContTable_digits b/tests/testthat/ref-svyContTable_digits new file mode 100644 index 0000000..b9d3f5b Binary files /dev/null and b/tests/testthat/ref-svyContTable_digits differ diff --git a/tests/testthat/ref-svyContTable_explain b/tests/testthat/ref-svyContTable_explain new file mode 100644 index 0000000..59a43a5 Binary files /dev/null and b/tests/testthat/ref-svyContTable_explain differ diff --git a/tests/testthat/ref-svyContTable_noSpaces b/tests/testthat/ref-svyContTable_noSpaces new file mode 100644 index 0000000..f30fd55 Binary files /dev/null and b/tests/testthat/ref-svyContTable_noSpaces differ diff --git a/tests/testthat/ref-svyContTable_noSpaces_showAllLevels_quote b/tests/testthat/ref-svyContTable_noSpaces_showAllLevels_quote new file mode 100644 index 0000000..debfa92 Binary files /dev/null and b/tests/testthat/ref-svyContTable_noSpaces_showAllLevels_quote differ diff --git a/tests/testthat/ref-svyContTable_noTests b/tests/testthat/ref-svyContTable_noTests new file mode 100644 index 0000000..e38e84b Binary files /dev/null and b/tests/testthat/ref-svyContTable_noTests differ diff --git a/tests/testthat/ref-svyContTable_nonnormal b/tests/testthat/ref-svyContTable_nonnormal new file mode 100644 index 0000000..d49797d Binary files /dev/null and b/tests/testthat/ref-svyContTable_nonnormal differ diff --git a/tests/testthat/ref-svyContTable_nonnormal_minMax b/tests/testthat/ref-svyContTable_nonnormal_minMax new file mode 100644 index 0000000..5fc73eb Binary files /dev/null and b/tests/testthat/ref-svyContTable_nonnormal_minMax differ diff --git a/tests/testthat/ref-svyContTable_overallPrint b/tests/testthat/ref-svyContTable_overallPrint new file mode 100644 index 0000000..824b09b Binary files /dev/null and b/tests/testthat/ref-svyContTable_overallPrint differ diff --git a/tests/testthat/ref-svyTableOne_2StrataVars b/tests/testthat/ref-svyTableOne_2StrataVars new file mode 100644 index 0000000..cf0c972 Binary files /dev/null and b/tests/testthat/ref-svyTableOne_2StrataVars differ diff --git a/tests/testthat/ref-svyTableOne_CatOnly b/tests/testthat/ref-svyTableOne_CatOnly new file mode 100644 index 0000000..d304ac5 Binary files /dev/null and b/tests/testthat/ref-svyTableOne_CatOnly differ diff --git a/tests/testthat/ref-svyTableOne_ContOnly b/tests/testthat/ref-svyTableOne_ContOnly new file mode 100644 index 0000000..7470c11 Binary files /dev/null and b/tests/testthat/ref-svyTableOne_ContOnly differ diff --git a/tests/testthat/ref-svyTableOne_IncludeNA b/tests/testthat/ref-svyTableOne_IncludeNA new file mode 100644 index 0000000..0de5d66 Binary files /dev/null and b/tests/testthat/ref-svyTableOne_IncludeNA differ diff --git a/tests/testthat/ref-svyTableOne_defaultPrint b/tests/testthat/ref-svyTableOne_defaultPrint new file mode 100644 index 0000000..53bacb4 Binary files /dev/null and b/tests/testthat/ref-svyTableOne_defaultPrint differ diff --git a/tests/testthat/ref-svyTableOne_digits b/tests/testthat/ref-svyTableOne_digits new file mode 100644 index 0000000..03b768f Binary files /dev/null and b/tests/testthat/ref-svyTableOne_digits differ diff --git a/tests/testthat/ref-svyTableOne_noSpaces b/tests/testthat/ref-svyTableOne_noSpaces new file mode 100644 index 0000000..fa6cc1b Binary files /dev/null and b/tests/testthat/ref-svyTableOne_noSpaces differ diff --git a/tests/testthat/ref-svyTableOne_noSpaces_showAllLevels_quote b/tests/testthat/ref-svyTableOne_noSpaces_showAllLevels_quote new file mode 100644 index 0000000..6cb5a8a Binary files /dev/null and b/tests/testthat/ref-svyTableOne_noSpaces_showAllLevels_quote differ diff --git a/tests/testthat/ref-svyTableOne_noTests b/tests/testthat/ref-svyTableOne_noTests new file mode 100644 index 0000000..eee34be Binary files /dev/null and b/tests/testthat/ref-svyTableOne_noTests differ diff --git a/tests/testthat/ref-svyTableOne_nonnormal_exact b/tests/testthat/ref-svyTableOne_nonnormal_exact new file mode 100644 index 0000000..1f9e864 Binary files /dev/null and b/tests/testthat/ref-svyTableOne_nonnormal_exact differ diff --git a/tests/testthat/ref-svyTableOne_nonnormal_minMax b/tests/testthat/ref-svyTableOne_nonnormal_minMax new file mode 100644 index 0000000..f522c25 Binary files /dev/null and b/tests/testthat/ref-svyTableOne_nonnormal_minMax differ diff --git a/tests/testthat/ref-svyTableOne_overallPrint b/tests/testthat/ref-svyTableOne_overallPrint new file mode 100644 index 0000000..e34235c Binary files /dev/null and b/tests/testthat/ref-svyTableOne_overallPrint differ diff --git a/tests/testthat/ref-svyTableOne_showAllLevels b/tests/testthat/ref-svyTableOne_showAllLevels new file mode 100644 index 0000000..0247ec6 Binary files /dev/null and b/tests/testthat/ref-svyTableOne_showAllLevels differ diff --git a/tests/testthat/test-CreateTableOne.R b/tests/testthat/test-CreateTableOne.R index b09fd2f..88e85f2 100644 --- a/tests/testthat/test-CreateTableOne.R +++ b/tests/testthat/test-CreateTableOne.R @@ -11,14 +11,61 @@ ### Prepare environment ################################################################################ library(testthat) -library(tableone) ### Context (1 for each file) context("Unit tests for the CreateTableOne function") -### Tests for -## Tests for ModuleTestSafe, a wrapper for test functions such as oneway.test and chisq.test +### Load data + +library(survival) +data(pbc) + +## Make categorical variables factors +varsToFactor <- c("status","trt","ascites","hepato","spiders","edema","stage") +pbc[varsToFactor] <- lapply(pbc[varsToFactor], factor) + +## Create a variable list +vars <- c("time","status","age","sex","ascites","hepato", + "spiders","edema","bili","chol","albumin", + "copper","alk.phos","ast","trig","platelet", + "protime","stage") +varsContOnly <- c("time","age","protime") +varsCatOnly <- c("status","trt","sex") + +### Tests for data checkers + +test_that("abnormal data are correctly detected", { + + ## Expectations + ## Use regular expressions + + ## Error in ModuleStopIfNoVarsLeft(vars) : No valid variables. + ## In addition: Warning message: + ## In ModuleReturnVarsExist(vars, data) : + ## The data frame does not have: non-existent Dropped + expect_error(expect_warning(CreateTableOne(vars = "non-existent", data = pbc), + "The data frame does not have: non-existent Dropped"), + "No valid variables.") + + ## Error in ModuleStopIfNotDataFrame(data) : + ## The data argument needs to be a data frame (no quote). + expect_error(CreateTableOne(data = "not a data frame"), + "The data argument needs to be a data frame") + + ## Error in ModuleReturnStrata(strata, data) : + ## None of the stratifying variables are present in the data frame. + ## In addition: Warning message: + ## In ModuleReturnVarsExist(strata, data) : + ## The data frame does not have: non-existent Dropped + expect_error(expect_warning(CreateTableOne(vars = vars, strata = c("non-existent"), data = pbc), + "The data frame does not have: non-existent Dropped"), + "None of the stratifying variables are present in the data frame") + +}) + + +### Tests for ModuleTestSafe, a wrapper for test functions such as oneway.test and chisq.test ## Create a dataset for a table dat <- read.table(header = TRUE, text = " @@ -51,11 +98,16 @@ test_that("P-values are returned for appropriate 2x2 xtabs, even with an empty f xtabs2 <- xtabs(~ rowVar + colVar, dat2) ## chisq.test - expect_that(attributes(tab1)$pValues["rowVar","pApprox"], equals(chisq.test(xtabs1)$p.value)) - expect_that(attributes(tab2)$pValues["rowVar","pApprox"], equals(chisq.test(xtabs2)$p.value)) + expect_that(attributes(tab1$CatTable)$pValues["rowVar","pApprox"], + equals(chisq.test(xtabs1)$p.value)) + expect_warning(expect_that(attributes(tab2$CatTable)$pValues["rowVar","pApprox"], + equals(chisq.test(xtabs2)$p.value)), + "Chi-squared approximation may be incorrect") ## fisher.test - expect_that(attributes(tab1)$pValues["rowVar","pExact"], equals(fisher.test(xtabs1)$p.value)) - expect_that(attributes(tab2)$pValues["rowVar","pExact"], equals(fisher.test(xtabs2)$p.value)) + expect_that(attributes(tab1$CatTable)$pValues["rowVar","pExact"], + equals(fisher.test(xtabs1)$p.value)) + expect_that(attributes(tab2$CatTable)$pValues["rowVar","pExact"], + equals(fisher.test(xtabs2)$p.value)) }) @@ -70,11 +122,203 @@ test_that("P-values should be NA for 1xM xtabs", { xtabs4 <- xtabs(~ rowVar + colVar, dat4) ## chisq.test - expect_that(attributes(tab3)$pValues["rowVar","pApprox"], equals(NA)) - expect_that(attributes(tab4)$pValues["rowVar","pApprox"], equals(NA)) + expect_that(attributes(tab3$CatTable)$pValues["rowVar","pApprox"], + equals(NA)) + expect_that(attributes(tab4$CatTable)$pValues["rowVar","pApprox"], + equals(NA)) ## fisher.test - expect_that(attributes(tab3)$pValues["rowVar","pExact"], equals(NA)) - expect_that(attributes(tab4)$pValues["rowVar","pExact"], equals(NA)) + expect_that(attributes(tab3$CatTable)$pValues["rowVar","pExact"], + equals(NA)) + expect_that(attributes(tab4$CatTable)$pValues["rowVar","pExact"], + equals(NA)) }) + +### Regression tests +################################################################################ + +## Create a table to test +pbcOverall <- CreateTableOne(vars = vars, data = pbc) +pbcInclNa <- CreateTableOne(vars = vars, data = pbc, includeNA = TRUE) +pbcByTrt <- CreateTableOne(vars = vars, strata = c("trt"), data = pbc) +pbcByTrtSex <- CreateTableOne(vars = vars, strata = c("trt","sex"), data = pbc) +pbcContOnlyByTrtSex <- CreateTableOne(vars = varsContOnly, strata = c("trt","sex"), data = pbc) +pbcCatOnlyByTrtSex <- CreateTableOne(vars = varsCatOnly, strata = c("trt","sex"), data = pbc) + + +## Specify variables for special handling +nonnormalVars <- c("bili","chol","copper","alk.phos","trig") +exactVars <- c("status","stage") + + +test_that("TableOne objects are always returned", { + + expect_equal(class(pbcOverall), "TableOne") + expect_equal(class(pbcInclNa), "TableOne") + expect_equal(class(pbcByTrt), "TableOne") + expect_equal(class(pbcByTrtSex), "TableOne") + expect_equal(class(pbcContOnlyByTrtSex), "TableOne") + expect_equal(class(pbcCatOnlyByTrtSex), "TableOne") + +}) + + +test_that("printing of a TableOne object does not regress", { + + ## Expectations + expect_equal_to_reference(print(pbcByTrt, printToggle = TRUE), + "ref-TableOne_defaultPrint") + + expect_equal_to_reference(print(pbcOverall, printToggle = TRUE), + "ref-TableOne_overallPrint") + + expect_equal_to_reference(print(pbcInclNa, printToggle = TRUE), + "ref-TableOne_IncludeNA") + + expect_equal_to_reference(print(pbcByTrtSex, printToggle = TRUE), + "ref-TableOne_2StrataVars") + + expect_equal_to_reference(print(pbcByTrt, catDigits = 3, contDigits = 4, pDigits = 5, printToggle = TRUE), + "ref-TableOne_digits") + + expect_equal_to_reference(print(pbcByTrt, test = FALSE, printToggle = TRUE), + "ref-TableOne_noTests") + + expect_equal_to_reference(print(pbcByTrt, nonnormal = nonnormalVars, exact = exactVars, printToggle = TRUE), + "ref-TableOne_nonnormal_exact") + + expect_equal_to_reference(print(pbcByTrt, nonnormal = nonnormalVars, minMax = TRUE, printToggle = TRUE), + "ref-TableOne_nonnormal_minMax") + + expect_equal_to_reference(print(pbcByTrt, nonnormal = nonnormalVars, exact = exactVars, noSpaces = TRUE, printToggle = TRUE), + "ref-TableOne_noSpaces") + + expect_equal_to_reference(print(pbcByTrt, nonnormal = nonnormalVars, exact = exactVars, showAllLevels = TRUE, printToggle = TRUE), + "ref-TableOne_showAllLevels") + + expect_equal_to_reference(print(pbcByTrt, nonnormal = nonnormalVars, exact = exactVars, noSpaces = TRUE, showAllLevels = FALSE, quote = TRUE, printToggle = TRUE), + "ref-TableOne_noSpaces_showAllLevels_quote") + + expect_equal_to_reference(print(pbcContOnlyByTrtSex), + "ref-TableOne_ContOnly") + + expect_equal_to_reference(print(pbcCatOnlyByTrtSex), + "ref-TableOne_CatOnly") + +}) + + +test_that("printing of a TableOne$CatTable object do not regress", { + + ## Expectations + expect_equal_to_reference(print(pbcByTrt$CatTable, printToggle = TRUE), + "ref-CatTable_defaultPrint") + + expect_equal_to_reference(print(pbcOverall$CatTable, printToggle = TRUE), + "ref-CatTable_overallPrint") + + expect_equal_to_reference(print(pbcInclNa$CatTable, printToggle = TRUE), + "ref-CatTable_IncludeNA") + + expect_equal_to_reference(print(pbcByTrtSex$CatTable, printToggle = TRUE), + "ref-CatTable_2StrataVars") + + expect_equal_to_reference(print(pbcByTrtSex$CatTable, digits = 3, pDigits = 5, printToggle = TRUE), + "ref-CatTable_digits") + + expect_equal_to_reference(print(pbcByTrtSex$CatTable, test = FALSE, printToggle = TRUE), + "ref-CatTable_noTests") + + expect_equal_to_reference(print(pbcByTrt$CatTable, noSpaces = TRUE, printToggle = TRUE), + "ref-CatTable_noSpaces") + + expect_equal_to_reference(print(pbcByTrt$CatTable, showAllLevels = TRUE, printToggle = TRUE), + "ref-CatTable_showAllLevels") + + expect_equal_to_reference(print(pbcByTrt$CatTable, explain = FALSE, printToggle = TRUE), + "ref-CatTable_explain") + + expect_equal_to_reference(print(pbcByTrt$CatTable, format = "f", printToggle = TRUE), + "ref-CatTable_format_f") + + expect_equal_to_reference(print(pbcByTrt$CatTable, format = "p", printToggle = TRUE), + "ref-CatTable_format_p") + + expect_equal_to_reference(print(pbcByTrt$CatTable, format = "pf", printToggle = TRUE), + "ref-CatTable_format_pf") + + expect_equal_to_reference(print(pbcByTrt$CatTable, cramVars = "sex", printToggle = TRUE), + "ref-CatTable_cramVars") + + expect_equal_to_reference(print(pbcByTrt$CatTable, noSpaces = TRUE, showAllLevels = TRUE, quote = TRUE, printToggle = TRUE), + "ref-CatTable_noSpaces_showAllLevels_quote") + + ## gmodels::CrossTable + print(pbcByTrt$CatTable, CrossTable = TRUE) + expect_output(print(pbcByTrt$CatTable, CrossTable = TRUE), +"|-------------------------| +| N | +| Chi-square contribution | +| N / Row Total | +| N / Col Total | +| N / Table Total | +|-------------------------|") + +}) + + +test_that("printing of a TableOne$ContTable object do not regress", { + + ## Expectations + expect_equal_to_reference(print(pbcByTrt$ContTable, printToggle = TRUE), + "ref-ContTable_defaultPrint") + + expect_equal_to_reference(print(pbcOverall$ContTable, printToggle = TRUE), + "ref-ContTable_overallPrint") + + expect_equal_to_reference(print(pbcByTrtSex$ContTable, printToggle = TRUE), + "ref-ContTable_2StrataVars") + + expect_equal_to_reference(print(pbcByTrt$ContTable, digits = 3, pDigits = 5, printToggle = TRUE), + "ref-ContTable_digits") + + expect_equal_to_reference(print(pbcByTrt$ContTable, test = FALSE, printToggle = TRUE), + "ref-ContTable_noTests") + + expect_equal_to_reference(print(pbcByTrt$ContTable, nonnormal = nonnormalVars, exact = exactVars, printToggle = TRUE), + "ref-ContTable_nonnormal_exact") + + expect_equal_to_reference(print(pbcByTrt$ContTable, nonnormal = nonnormalVars, minMax = TRUE, printToggle = TRUE), + "ref-ContTable_nonnormal_minMax") + + expect_equal_to_reference(print(pbcByTrt$ContTable, noSpaces = TRUE, printToggle = TRUE), + "ref-ContTable_noSpaces") + + expect_equal_to_reference(print(pbcByTrt$ContTable, explain = FALSE, printToggle = TRUE), + "ref-ContTable_explain") + + expect_equal_to_reference(print(pbcByTrt$ContTable, noSpaces = TRUE, showAllLevels = TRUE, quote = TRUE, printToggle = TRUE), + "ref-ContTable_noSpaces_showAllLevels_quote") +}) + + +test_that("summary method works without errors", { + + ## Expectations + summary(pbcOverall) + expect_output(summary(pbcOverall), "strata: Overall") + summary(pbcInclNa) + expect_output(summary(pbcInclNa), "") + summary(pbcByTrt) + expect_output(summary(pbcByTrt), "Standardize mean differences") + summary(pbcByTrtSex) + expect_output(summary(pbcByTrtSex), + "Standardize mean differences") + summary(pbcContOnlyByTrtSex) + expect_output(summary(pbcContOnlyByTrtSex), + "### Summary of continuous variables ###") + summary(pbcCatOnlyByTrtSex) + expect_output(summary(pbcCatOnlyByTrtSex), "3 vs 4") + +}) diff --git a/tests/testthat/test-ExtractSmd.R b/tests/testthat/test-ExtractSmd.R new file mode 100755 index 0000000..c9f389b --- /dev/null +++ b/tests/testthat/test-ExtractSmd.R @@ -0,0 +1,130 @@ +#!/usr/bin/Rscript + +################################################################################ +### Test ExtractSmd function +## +## Created on: 2015-08-06 +## Author: Kazuki Yoshida +################################################################################ + + +### Structure +## expectations within tests within context + +### Prepare environment +################################################################################ + +### Context (1 for each file) +context("Unit tests for the ExtractSmd function") + + +### Unweighted table + +library(survival) +data(pbc) +## Make categorical variables factors +varsToFactor <- c("status","trt","ascites","hepato","spiders","edema","stage") +pbc[varsToFactor] <- lapply(pbc[varsToFactor], factor) +## Create a variable list +vars <- c("time","status","age","sex","ascites","hepato", + "spiders","edema","bili","chol","albumin", + "copper","alk.phos","ast","trig","platelet", + "protime","stage") +varsContOnly <- c("time","age","protime") +varsCatOnly <- c("status","trt","sex") +pbcByTrtSex <- CreateTableOne(vars = vars, strata = c("trt","sex"), data = pbc) + +## Difficult objects +## SMD turned off +pbcByTrtSexOff <- CreateTableOne(vars = vars, strata = c("trt","sex"), data = pbc, smd = FALSE) +## No strata +pbcByOne <- CreateTableOne(vars = vars, data = pbc) +## Cont only +pbcContByTrtSex <- CreateTableOne(vars = varsContOnly, strata = c("trt","sex"), data = pbc) +## Cat only +pbcCatByTrtSex <- CreateTableOne(vars = varsCatOnly, strata = c("trt","sex"), data = pbc) + + +### Abnormal object test +test_that("Anomalous/difficult objects are handled correctly", { + + expect_warning(ExtractSmd(1), + "Unsupported object of class") + + ## Unstratified object necessarily returns NULL + expect_equal(ExtractSmd(pbcByOne), + NULL) + + ## Object not containing SMD necessarily returns NULL + expect_equal(ExtractSmd(pbcByTrtSexOff), + NULL) + + ## Continuous only + expect_equal(ExtractSmd(pbcContByTrtSex), + attr(pbcContByTrtSex$ContTable, "smd")) + + ## Categorical only + expect_equal(ExtractSmd(pbcCatByTrtSex), + attr(pbcCatByTrtSex$CatTable, "smd")) + +}) + + +test_that("ExtractSmd work on unweighted data", { + + ## Expectations + ## Correct variable order + print(ExtractSmd(pbcByTrtSex)) + expect_equal(rownames(ExtractSmd(pbcByTrtSex)), + pbcByTrtSex$MetaData$vars) + + ## Work on sub tables + expect_equal(ExtractSmd(pbcByTrtSex$ContTable), + attr(pbcByTrtSex$ContTable, "smd")) + expect_equal(ExtractSmd(pbcByTrtSex$CatTable), + attr(pbcByTrtSex$CatTable, "smd")) + + ## Regression test + expect_equal_to_reference(ExtractSmd(pbcByTrtSex), + "ref-ExtractSmd_TableOne") + +}) + + + +### Weighted table + +library(survey) + +## Create a weighted survey design object +data(nhanes) +nhanesSvy <- svydesign(ids = ~ SDMVPSU, strata = ~ SDMVSTRA, weights = ~ WTMEC2YR, + nest = TRUE, data = nhanes) + +## Create a table object +## factorVars are converted to factors; no need for variables already factors +## strata will stratify summaries; leave it unspecified for overall summaries +nhanesByRace <- svyCreateTableOne(vars = c("HI_CHOL","race","agecat","RIAGENDR"), + strata = "race", data = nhanesSvy, + factorVars = c("race","RIAGENDR")) + + +test_that("ExtractSmd work on weighted data", { + + ## Expectations + ## Correct variable order + print(ExtractSmd(nhanesByRace)) + expect_equal(rownames(ExtractSmd(nhanesByRace)), + nhanesByRace$MetaData$vars) + + ## Work on sub tables + expect_equal(ExtractSmd(nhanesByRace$ContTable), + attr(nhanesByRace$ContTable, "smd")) + expect_equal(ExtractSmd(nhanesByRace$CatTable), + attr(nhanesByRace$CatTable, "smd")) + + ## Regression test + expect_equal_to_reference(ExtractSmd(nhanesByRace), + "ref-ExtractSmd_svyTableOne") + +}) diff --git a/tests/testthat/test-ShowRegTable.R b/tests/testthat/test-ShowRegTable.R new file mode 100644 index 0000000..433f753 --- /dev/null +++ b/tests/testthat/test-ShowRegTable.R @@ -0,0 +1,89 @@ +################################################################################ +### Unit tests for ShowRegTable +## Reference: http://adv-r.had.co.nz/Testing.html +## Created on: 2015-08-01 +## Author: Kazuki Yoshida +################################################################################ + +### Structure +## expectations within tests within context + +### Prepare environment +################################################################################ +library(testthat) +library(survey) +library(survival) + + +### Context (1 for each file) +################################################################################ +context("Unit tests for regression summary function") + +## Load Mayo Clinic Primary Biliary Cirrhosis Data +data(pbc) + +## Cox +coxph1 <- coxph(formula = Surv(time, status == 2) ~ trt + age + albumin + ascites, + data = pbc) +## Logistic +glm1 <- glm(formula = (status == 2) ~ trt + age + albumin + ascites, + family = binomial(link = "logit"), + data = pbc) +## Linear +lm1 <- lm(formula = time ~ trt + age + albumin + ascites, + data = pbc) + +test_that("coxph works", { + + ## For coxph normal approximation is uses + expect_true(all(confint(coxph1) == confint.default(coxph1))) + + ## confint + ShowRegTable(coxph1) + expect_output(ShowRegTable(coxph1), + "0.72, 1.47") + + ## contint.default + ShowRegTable(coxph1, ciFun = confint.default) + expect_output(ShowRegTable(coxph1, ciFun = confint.default), + "0.72, 1.47") + +}) + + +test_that("glm works", { + + ## For GLM profile likelihood method and naive approximation differ + expect_true(!all(confint(glm1) == confint.default(glm1))) + + ## confint + ShowRegTable(glm1, digits = 5) + expect_output(ShowRegTable(glm1, digits = 5), + "0.63994, 1.75622") + + ## contint.default + ShowRegTable(glm1, ciFun = confint.default, digits = 5) + expect_output(ShowRegTable(glm1, ciFun = confint.default, digits = 5), + "0.63975, 1.75230") + +}) + + +test_that("lm works", { + + ## For lm t-distribution based method and naive approximation differ + expect_true(!all(confint(lm1) == confint.default(lm1))) + + ## confint + ShowRegTable(lm1, digits = 5, exp = FALSE) + expect_output(ShowRegTable(lm1, digits = 5, exp = FALSE), + "-275.96185, 175.16874") + + ## contint.default + ShowRegTable(lm1, ciFun = confint.default, digits = 5, exp = FALSE) + expect_output(ShowRegTable(lm1, ciFun = confint.default, digits = 5, exp = FALSE), + "-275.07261, 174.27950") + +}) + + diff --git a/tests/testthat/test-modules-smd.R b/tests/testthat/test-modules-smd.R new file mode 100755 index 0000000..f1d46ce --- /dev/null +++ b/tests/testthat/test-modules-smd.R @@ -0,0 +1,1079 @@ +################################################################################ +### Unit tests for the standardized mean difference modules +## Reference: http://adv-r.had.co.nz/Testing.html +## Created on: +## Author: Kazuki Yoshida +################################################################################ + +### Structure +## expectations within tests within context + + +### +### References +################################################################################ +## A unified approach to measuring the effect size between two groups using SAS +## http://support.sas.com/resources/papers/proceedings12/335-2012.pdf + + +### +### Prepare environment +################################################################################ +library(testthat) +library(survey) +library(Matrix) +library(dummies) + + +### +### Actual tests +################################################################################ +context("Tests for functions for standardized mean differences") + + +### Provide data +data(nhanes) +nhanesSvy <- svydesign(ids = ~ SDMVPSU, strata = ~ SDMVSTRA, weights = ~ WTMEC2YR, + nest = TRUE, data = nhanes) +## ‘SDMVPSU’ Primary sampling units +## ‘SDMVSTRA’ Sampling strata +## ‘WTMEC2YR’ Sampling weights +## ‘HI_CHOL’ Numeric vector: 1 for total cholesterol over 240mg/dl, 0 +## under 240mg/dl +## ‘race’ 1=Hispanic, 2=non-Hispanic white, 3=non-Hispanic black, +## 4=other +## ‘agecat’ Age group ‘(0,19]’ ‘(19,39]’ ‘(39,59]’ ‘(59,Inf]’ +## ‘RIAGENDR’ Gender: 1=male, 2=female + +## Use HI_CHOL for continuous, RIAGENDR for binary, race and agecat as categorical + + + +### Old functions explicitly for 3 groups (Keep; used for testing) +## Standardize differences +StdDiffOld <- function(variable, group) { + ## For each group + means <- tapply(variable, group, mean) + vars <- tapply(variable, group, var) + + ## Calculate for all three possible pairs + g1g2 <- abs(means[1] - means[2]) / sqrt((vars[1] + vars[2]) / 2) + g2g3 <- abs(means[2] - means[3]) / sqrt((vars[2] + vars[3]) / 2) + g3g1 <- abs(means[3] - means[1]) / sqrt((vars[3] + vars[1]) / 2) + + ## 2vs1, 3vs1, then 3vs2 to be consistent with regression + out <- c(g1g2, g3g1, g2g3) + names(out) <- c("3vs2","3vs1","2vs1") + out +} + +svyStdDiffOld <- function(varName, groupName, design) { + + varFormula <- as.formula(paste("~", varName)) + groupFormula <- as.formula(paste("~", groupName)) + + means <- svyby(formula = varFormula, by = groupFormula, FUN = svymean, design = design)[,2] + vars <- svyby(formula = varFormula, by = groupFormula, FUN = svyvar, design = design)[,2] + + ## Calculate for all three possible pairs + g1g2 <- abs(means[1] - means[2]) / sqrt((vars[1] + vars[2]) / 2) + g2g3 <- abs(means[2] - means[3]) / sqrt((vars[2] + vars[3]) / 2) + g3g1 <- abs(means[3] - means[1]) / sqrt((vars[3] + vars[1]) / 2) + + ## 2vs1, 3vs1, then 3vs2 to be consistent with regression + out <- c(g1g2, g3g1, g2g3) + names(out) <- c("3vs2","3vs1","2vs1") + out +} + + +test_that("continuous standardized difference is correct (nhanes unweighted)", { + + ## Two group with only one contrast 1 vs 2 + means1 <- tapply(nhanes$HI_CHOL, nhanes$RIAGENDR, mean, na.rm = TRUE) + vars1 <- tapply(nhanes$HI_CHOL, nhanes$RIAGENDR, var, na.rm = TRUE) + meanDiffs1 <- (means1[1] - means1[2]) / sqrt((vars1[1] + vars1[2]) / 2) + names(meanDiffs1) <- NULL + + expect_equal(StdDiff(nhanes$HI_CHOL, nhanes$RIAGENDR), abs(meanDiffs1)) + + + ## Four groups with 6 contrasts + means2 <- tapply(nhanes$HI_CHOL, nhanes$race, mean, na.rm = TRUE) + vars2 <- tapply(nhanes$HI_CHOL, nhanes$race, var, na.rm = TRUE) + meanDiffs2 <- + c((means2[1] - means2[2]) / sqrt((vars2[1] + vars2[2]) / 2), + (means2[1] - means2[3]) / sqrt((vars2[1] + vars2[3]) / 2), + (means2[1] - means2[4]) / sqrt((vars2[1] + vars2[4]) / 2), + (means2[2] - means2[3]) / sqrt((vars2[2] + vars2[3]) / 2), + (means2[2] - means2[4]) / sqrt((vars2[2] + vars2[4]) / 2), + (means2[3] - means2[4]) / sqrt((vars2[3] + vars2[4]) / 2)) + names(meanDiffs2) <- NULL + + ## Individual numbers + expect_equal(StdDiff(nhanes$HI_CHOL, nhanes$race), abs(meanDiffs2)) + ## Average across + expect_equal(mean(StdDiff(nhanes$HI_CHOL, nhanes$race)), + mean(abs(meanDiffs2))) + +}) + + +test_that("continuous standardized difference is correct (nhanes weighted)", { + + ## Two group + means1 <- svyby( ~ HI_CHOL, by = ~ RIAGENDR, design = nhanesSvy, FUN = svymean, na.rm = TRUE)[,2] + vars1 <- svyby( ~ HI_CHOL, by = ~ RIAGENDR, design = nhanesSvy, FUN = svyvar, na.rm = TRUE)[,2] + meanDiffs1 <- (means1[1] - means1[2]) / sqrt((vars1[1] + vars1[2]) / 2) + + expect_equal(svyStdDiff("HI_CHOL", "RIAGENDR", design = nhanesSvy), + abs(meanDiffs1)) + + + ## Four groups + means2 <- svyby( ~ HI_CHOL, by = ~ race, design = nhanesSvy, FUN = svymean, na.rm = TRUE)[,2] + vars2 <- svyby( ~ HI_CHOL, by = ~ race, design = nhanesSvy, FUN = svyvar, na.rm = TRUE)[,2] + meanDiffs2 <- + c((means2[1] - means2[2]) / sqrt((vars2[1] + vars2[2]) / 2), + (means2[1] - means2[3]) / sqrt((vars2[1] + vars2[3]) / 2), + (means2[1] - means2[4]) / sqrt((vars2[1] + vars2[4]) / 2), + (means2[2] - means2[3]) / sqrt((vars2[2] + vars2[3]) / 2), + (means2[2] - means2[4]) / sqrt((vars2[2] + vars2[4]) / 2), + (means2[3] - means2[4]) / sqrt((vars2[3] + vars2[4]) / 2)) + names(meanDiffs2) <- NULL + + ## Individual numbers + expect_equal(svyStdDiff("HI_CHOL", "race", design = nhanesSvy), + abs(meanDiffs2)) + ## Average across + expect_equal(mean(svyStdDiff("HI_CHOL", "race", design = nhanesSvy)), + mean(abs(meanDiffs2))) + +}) + + +test_that("binary standardized difference is correct (nhanes unweighted)", { + + ## Two group with only one contrast 1 vs 2 + means1 <- tapply(nhanes$HI_CHOL, nhanes$RIAGENDR, mean, na.rm = TRUE) + vars1 <- means1 * (1 - means1) + meanDiffs1 <- (means1[1] - means1[2]) / sqrt((vars1[1] + vars1[2]) / 2) + names(meanDiffs1) <- NULL + + expect_equal(StdDiff(nhanes$HI_CHOL, nhanes$RIAGENDR, binary = TRUE), + abs(meanDiffs1)) + + + ## Four groups with 6 contrasts + means2 <- tapply(nhanes$HI_CHOL, nhanes$race, mean, na.rm = TRUE) + vars2 <- means2 * (1 - means2) + meanDiffs2 <- + c((means2[1] - means2[2]) / sqrt((vars2[1] + vars2[2]) / 2), + (means2[1] - means2[3]) / sqrt((vars2[1] + vars2[3]) / 2), + (means2[1] - means2[4]) / sqrt((vars2[1] + vars2[4]) / 2), + (means2[2] - means2[3]) / sqrt((vars2[2] + vars2[3]) / 2), + (means2[2] - means2[4]) / sqrt((vars2[2] + vars2[4]) / 2), + (means2[3] - means2[4]) / sqrt((vars2[3] + vars2[4]) / 2)) + names(meanDiffs2) <- NULL + + ## Individual numbers + expect_equal(StdDiff(nhanes$HI_CHOL, nhanes$race, binary = TRUE), + abs(meanDiffs2)) + ## Average across + expect_equal(mean(StdDiff(nhanes$HI_CHOL, nhanes$race, binary = TRUE)), + mean(abs(meanDiffs2))) + +}) + + +test_that("binary standardized difference is correct (nhanes weighted)", { + + ## Two group + means1 <- svyby( ~ HI_CHOL, by = ~ RIAGENDR, design = nhanesSvy, FUN = svymean, na.rm = TRUE)[,2] + vars1 <- means1 * (1 - means1) + meanDiffs1 <- (means1[1] - means1[2]) / sqrt((vars1[1] + vars1[2]) / 2) + + expect_equal(svyStdDiff("HI_CHOL", "RIAGENDR", design = nhanesSvy, binary = TRUE), + abs(meanDiffs1)) + + + ## Four groups + means2 <- svyby( ~ HI_CHOL, by = ~ race, design = nhanesSvy, FUN = svymean, na.rm = TRUE)[,2] + vars2 <- means2 * (1 - means2) + meanDiffs2 <- + c((means2[1] - means2[2]) / sqrt((vars2[1] + vars2[2]) / 2), + (means2[1] - means2[3]) / sqrt((vars2[1] + vars2[3]) / 2), + (means2[1] - means2[4]) / sqrt((vars2[1] + vars2[4]) / 2), + (means2[2] - means2[3]) / sqrt((vars2[2] + vars2[3]) / 2), + (means2[2] - means2[4]) / sqrt((vars2[2] + vars2[4]) / 2), + (means2[3] - means2[4]) / sqrt((vars2[3] + vars2[4]) / 2)) + names(meanDiffs2) <- NULL + + ## Individual numbers + expect_equal(svyStdDiff("HI_CHOL", "race", design = nhanesSvy, binary = TRUE), + abs(meanDiffs2)) + ## Average across + expect_equal(mean(svyStdDiff("HI_CHOL", "race", design = nhanesSvy, binary = TRUE)), + mean(abs(meanDiffs2))) + +}) + + +test_that("multinomal helpers are correct", { + + means <- c(0.310336708264657, 0.224393689663292, 0.234283023310572, 0.230986578761479) + covs <- MultinomialVar(means) + + ## Expectations + ## Diagonal p(1-p) + expect_equal(diag(covs), means * (1 - means)) + ## Off diagonal -1 * p_i * p_j + expect_equal(covs[1,2], -1 * means[1] * means[2]) + expect_equal(covs[1,3], -1 * means[1] * means[3]) + expect_equal(covs[1,4], -1 * means[1] * means[4]) + expect_equal(covs[2,3], -1 * means[2] * means[3]) + expect_equal(covs[2,4], -1 * means[2] * means[4]) + expect_equal(covs[3,4], -1 * means[3] * means[4]) + +}) + + +test_that("categorical standardized difference is correct (nhanes unweighted)", { + + ## Four groups with 6 contrasts + means2 <- tapply(nhanes$HI_CHOL, nhanes$race, mean, na.rm = TRUE) + vars2 <- means2 * (1 - means2) + meanDiffs2 <- + c((means2[1] - means2[2]) / sqrt((vars2[1] + vars2[2]) / 2), + (means2[1] - means2[3]) / sqrt((vars2[1] + vars2[3]) / 2), + (means2[1] - means2[4]) / sqrt((vars2[1] + vars2[4]) / 2), + (means2[2] - means2[3]) / sqrt((vars2[2] + vars2[3]) / 2), + (means2[2] - means2[4]) / sqrt((vars2[2] + vars2[4]) / 2), + (means2[3] - means2[4]) / sqrt((vars2[3] + vars2[4]) / 2)) + names(meanDiffs2) <- NULL + + ## Individual numbers + expect_equal(StdDiffMulti(nhanes$HI_CHOL, nhanes$race), + abs(meanDiffs2)) + ## Average across + expect_equal(mean(StdDiffMulti(nhanes$HI_CHOL, nhanes$race)), + mean(abs(meanDiffs2))) + + + ## Two group with only one contrast 1 vs 2 + strataByLevels1 <- xtabs( ~ RIAGENDR + agecat, data = nhanes) + ## drop first column after calculating proportion + propTable1 <- prop.table(strataByLevels1, margin = 1)[, -1, drop = FALSE] + meanDiffs1 <- propTable1[1,] - propTable1[2,] + covMean1 <- (MultinomialVar(propTable1[1,]) + MultinomialVar(propTable1[2,])) / 2 + + ## R is not strict about transposition + expect_equal(meanDiffs1 %*% MASS::ginv(covMean1) %*% meanDiffs1, + t(meanDiffs1) %*% MASS::ginv(covMean1) %*% t(t(meanDiffs1))) + + ## Test actual functions + expect_equal(StdDiffMulti(nhanes$agecat, nhanes$RIAGENDR), + sqrt(drop(t(meanDiffs1) %*% MASS::ginv(covMean1) %*% t(t(meanDiffs1))))) + + + ## Four groups with 6 contrasts + strataByLevels2 <- xtabs( ~ race + agecat, data = nhanes) + propTable2 <- prop.table(strataByLevels2, margin = 1)[, -1, drop = FALSE] + meanDiffs2 <- list(propTable2[1,] - propTable2[2,], + propTable2[1,] - propTable2[3,], + propTable2[1,] - propTable2[4,], + propTable2[2,] - propTable2[3,], + propTable2[2,] - propTable2[4,], + propTable2[3,] - propTable2[4,]) + + covMean2 <- + list((MultinomialVar(propTable2[1,]) + MultinomialVar(propTable2[2,])) / 2, + (MultinomialVar(propTable2[1,]) + MultinomialVar(propTable2[3,])) / 2, + (MultinomialVar(propTable2[1,]) + MultinomialVar(propTable2[4,])) / 2, + (MultinomialVar(propTable2[2,]) + MultinomialVar(propTable2[3,])) / 2, + (MultinomialVar(propTable2[2,]) + MultinomialVar(propTable2[4,])) / 2, + (MultinomialVar(propTable2[3,]) + MultinomialVar(propTable2[4,])) / 2) + + ## These should match in length. + expect_equal(length(meanDiffs2), length(covMean2)) + + smds <- unlist(lapply(seq_along(meanDiffs2), function(i) { + + sqrt(drop(t(meanDiffs2[[i]]) %*% MASS::ginv(covMean2[[i]]) %*% t(t(meanDiffs2[[i]])))) + })) + + + ## Individual numbers + expect_equal(StdDiffMulti(nhanes$agecat, nhanes$race), + smds) + ## Average across + expect_equal(mean(StdDiffMulti(nhanes$agecat, nhanes$race)), + mean(smds)) + +}) + + +test_that("categorical standardized difference is correct (nhanes weighted)", { + + ## Binary four groups + means2 <- svyby( ~ HI_CHOL, by = ~ race, design = nhanesSvy, FUN = svymean, na.rm = TRUE)[,2] + vars2 <- means2 * (1 - means2) + meanDiffs2 <- + c((means2[1] - means2[2]) / sqrt((vars2[1] + vars2[2]) / 2), + (means2[1] - means2[3]) / sqrt((vars2[1] + vars2[3]) / 2), + (means2[1] - means2[4]) / sqrt((vars2[1] + vars2[4]) / 2), + (means2[2] - means2[3]) / sqrt((vars2[2] + vars2[3]) / 2), + (means2[2] - means2[4]) / sqrt((vars2[2] + vars2[4]) / 2), + (means2[3] - means2[4]) / sqrt((vars2[3] + vars2[4]) / 2)) + names(meanDiffs2) <- NULL + + ## Individual numbers + expect_equal(svyStdDiffMulti("HI_CHOL", "race", design = nhanesSvy), + abs(meanDiffs2)) + ## Average across + expect_equal(mean(svyStdDiffMulti("HI_CHOL", "race", design = nhanesSvy)), + mean(abs(meanDiffs2))) + + + ## Two group with only one contrast 1 vs 2 + strataByLevels1 <- svytable( ~ RIAGENDR + agecat, design = nhanesSvy) + propTable1 <- prop.table(strataByLevels1, margin = 1)[, -1, drop = FALSE] + meanDiffs1 <- propTable1[1,] - propTable1[2,] + covMean1 <- (MultinomialVar(propTable1[1,]) + MultinomialVar(propTable1[2,])) / 2 + + ## Test actual functions + expect_equal(svyStdDiffMulti("agecat", "RIAGENDR", design = nhanesSvy), + sqrt(drop(t(meanDiffs1) %*% MASS::ginv(covMean1) %*% t(t(meanDiffs1))))) + + + ## Four groups with 6 contrasts + strataByLevels2 <- svytable( ~ race + agecat, design = nhanesSvy) + propTable2 <- prop.table(strataByLevels2, margin = 1)[, -1, drop = FALSE] + meanDiffs2 <- list(propTable2[1,] - propTable2[2,], + propTable2[1,] - propTable2[3,], + propTable2[1,] - propTable2[4,], + propTable2[2,] - propTable2[3,], + propTable2[2,] - propTable2[4,], + propTable2[3,] - propTable2[4,]) + + covMean2 <- + list((MultinomialVar(propTable2[1,]) + MultinomialVar(propTable2[2,])) / 2, + (MultinomialVar(propTable2[1,]) + MultinomialVar(propTable2[3,])) / 2, + (MultinomialVar(propTable2[1,]) + MultinomialVar(propTable2[4,])) / 2, + (MultinomialVar(propTable2[2,]) + MultinomialVar(propTable2[3,])) / 2, + (MultinomialVar(propTable2[2,]) + MultinomialVar(propTable2[4,])) / 2, + (MultinomialVar(propTable2[3,]) + MultinomialVar(propTable2[4,])) / 2) + + ## These should match in length. + expect_equal(length(meanDiffs2), length(covMean2)) + + smds <- unlist(lapply(seq_along(meanDiffs2), function(i) { + + sqrt(drop(t(meanDiffs2[[i]]) %*% MASS::ginv(covMean2[[i]]) %*% t(t(meanDiffs2[[i]])))) + })) + + ## Individual numbers + expect_equal(svyStdDiffMulti("agecat", "race", design = nhanesSvy), + smds) + ## Average across + expect_equal(mean(svyStdDiffMulti("agecat", "race", design = nhanesSvy)), + mean(smds)) + +}) + + +### Test on anomalous data +################################################################################ + +test_that("decent results are returned for anomalous/difficult data", { + + data(nhanes) + nhanes$onlyOne <- 1 + nhanes$onlyNa <- as.numeric(NA) + nhanes$logi <- nhanes$RIAGENDR == 1 + nhanes$race2 <- nhanes$race + nhanesSvy <- svydesign(ids = ~ SDMVPSU, strata = ~ SDMVSTRA, + weights = ~ WTMEC2YR, nest = TRUE, + data = nhanes) + + ## Note: 2015-08-01 + ## Categorical SMD: When both of two group have at one or more zero + ## categories, both contribute singular matrices to the pooled + ## variance covariance matrix S in Yang and Dalton 2012. + ## + ## When both groups have a single category that is probability 1 and + ## other cells that are all zeros, both contribute zero matrices to + ## the pooled variance covariance matrix S. solve() dies on this + ## singular matrix. MASS::ginv() returns generalized inverse matrix, + ## which is the zero matrix of the same dimension, instead. + ## + ## This results in SMD of zero regardless of the mean vector difference + ## (T-C in Yang and Dalton 2012). + ## + ## This behavior looks odd, thus, categorical SMD functions were + ## modified to return NaN when S is a zero matrix. + + ## Logical + ## 0 due to [0]^- = 0 + table(nhanes$logi, nhanes$RIAGENDR) + expect_equal(StdDiffMulti(nhanes$logi, group = nhanes$RIAGENDR), NaN) + ## Matches with result from original variable + expect_equal(StdDiffMulti(nhanes$logi, group = nhanes$race), + StdDiffMulti(nhanes$RIAGENDR, group = nhanes$race)) + + ## Multiple group where T-C is not a zero vector. + ## All groups have single group that is probability 1, thus, + ## all contrasts have zero matrix S. + table(nhanes$race2, nhanes$race) + expect_equal(StdDiffMulti(nhanes$race2, group = nhanes$race), rep(NaN, 6)) + expect_equal(svyStdDiffMulti("race2", group = "race", design = nhanesSvy), rep(NaN, 6)) + + + ## Only one value + ## NaN due to division by zero variance + by(nhanes$onlyOne, nhanes$RIAGENDR, summary) + expect_equal(StdDiff(nhanes$onlyOne, group = nhanes$RIAGENDR), NaN) + ## 0 because [0]^- = 0, and [1]^T [0]^-1 [1] = 0 + table(nhanes$onlyOne, nhanes$RIAGENDR) + expect_equal(StdDiffMulti(nhanes$onlyOne, group = nhanes$RIAGENDR), NaN) + ## When weighted problematic + means1 <- svyby(~ onlyOne, by = ~ RIAGENDR, nhanesSvy, FUN = svymean)[,2] + vars1 <- svyby(~ onlyOne, by = ~ RIAGENDR, nhanesSvy, FUN = svyvar)[,2] + ## Very small difference is inflated by even smaller variance + expect_equal(svyStdDiff("onlyOne", "RIAGENDR", nhanesSvy), + (means1[1] - means1[2]) / sqrt(sum(vars1) / 2)) + ## NaN should be the case, but it's not, but it's consistent with survey + ## expect_equal(svyStdDiff("onlyOne", "RIAGENDR", nhanesSvy), NaN) + + ## 0 because [0]^- = 0, and [1]^T [0]^-1 [1] = 0 + ## No error even with a single level variable (constant) as redundant + ## level drop from table occurs only when 2+ levels are present. + ## If any group has more than 2 levels, then strata-by-level table + ## is correctly created, which is not the case here. Redefined NaN. + expect_equal(svyStdDiffMulti("onlyOne", "RIAGENDR", nhanesSvy), NaN) + + ## Four groups (six contrasts) + ## NaN due to division by zero variance + by(nhanes$onlyOne, nhanes$race, summary) + expect_equal(StdDiff(nhanes$onlyOne, group = nhanes$race), rep(NaN, 6)) + ## 0 because [0]^- = 0, and [1]^T [0]^-1 [1] = 0 + expect_equal(StdDiffMulti(nhanes$onlyOne, group = nhanes$race), rep(NaN, 6)) + ## When weighted problematic; not in this case?? + means2 <- svyby(~ onlyOne, by = ~ race, nhanesSvy, FUN = svymean)[,2] + vars2 <- svyby(~ onlyOne, by = ~ race, nhanesSvy, FUN = svyvar)[,2] + meanDiffs2 <- c((means2[1] - means2[2]) / sqrt((vars2[1] + vars2[2]) / 2), + (means2[1] - means2[3]) / sqrt((vars2[1] + vars2[3]) / 2), + (means2[1] - means2[4]) / sqrt((vars2[1] + vars2[4]) / 2), + (means2[2] - means2[3]) / sqrt((vars2[2] + vars2[3]) / 2), + (means2[2] - means2[4]) / sqrt((vars2[2] + vars2[4]) / 2), + (means2[3] - means2[4]) / sqrt((vars2[3] + vars2[4]) / 2)) + expect_equal(svyStdDiff("onlyOne", "race", nhanesSvy), meanDiffs2) + expect_equal(svyStdDiff("onlyOne", "race", nhanesSvy), rep(NaN, 6)) + ## 0 because [0]^- = 0, and [1]^T [0]^-1 [1] = 0 + expect_equal(svyStdDiffMulti("onlyOne", "race", nhanesSvy), rep(NaN, 6)) + + + ## onlyNa + ## NA as na.rm is turned off + expect_warning(expect_equal(StdDiff(nhanes$onlyNa, group = nhanes$RIAGENDR), + as.numeric(NA)), + "Variable has only NA's in at least one stratum. na.rm turned off.") + ## 0 only one level + expect_warning(expect_equal(StdDiffMulti(nhanes$onlyNa, group = nhanes$RIAGENDR), NaN), + "Variable has only NA's in all strata. Regarding NA as a level") + ## When weighted problematic + means1 <- svyby(~ onlyNa, by = ~ RIAGENDR, nhanesSvy, FUN = svymean)[,2] + vars1 <- svyby(~ onlyNa, by = ~ RIAGENDR, nhanesSvy, FUN = svyvar)[,2] + ## Very small difference is inflated by even smaller variance + expect_warning(svyStdDiff("onlyNa", "RIAGENDR", nhanesSvy), + "onlyNa has only NA's in at least one stratum. na.rm turned off.") + expect_warning(expect_equal(svyStdDiff("onlyNa", "RIAGENDR", nhanesSvy), + as.numeric(NA)), + "onlyNa has only NA's in at least one stratum. na.rm turned off.") + ## 0 because [0]^- = 0, and [1]^T [0]^-1 [1] = 0 + expect_warning(expect_equal(svyStdDiffMulti("onlyNa", "RIAGENDR", nhanesSvy), NaN), + "onlyNa has only NA's in all strata. Regarding NA as a level.") + + ## Four groups (six contrasts) + ## NaN due to division by zero variance + expect_warning(expect_equal(StdDiff(nhanes$onlyNa, group = nhanes$race), rep(NaN, 6)), + "Variable has only NA's in at least one stratum. na.rm turned off.") + ## 0 because [0]^- = 0, and [1]^T [0]^-1 [1] = 0 + expect_warning(expect_equal(StdDiffMulti(nhanes$onlyNa, group = nhanes$race), rep(NaN, 6)), + "Variable has only NA's in all strata. Regarding NA as a level.") + ## When weighted problematic; not in this case?? + means2 <- svyby(~ onlyNa, by = ~ race, nhanesSvy, FUN = svymean)[,2] + vars2 <- svyby(~ onlyNa, by = ~ race, nhanesSvy, FUN = svyvar)[,2] + meanDiffs2 <- c((means2[1] - means2[2]) / sqrt((vars2[1] + vars2[2]) / 2), + (means2[1] - means2[3]) / sqrt((vars2[1] + vars2[3]) / 2), + (means2[1] - means2[4]) / sqrt((vars2[1] + vars2[4]) / 2), + (means2[2] - means2[3]) / sqrt((vars2[2] + vars2[3]) / 2), + (means2[2] - means2[4]) / sqrt((vars2[2] + vars2[4]) / 2), + (means2[3] - means2[4]) / sqrt((vars2[3] + vars2[4]) / 2)) + expect_warning(expect_equal(svyStdDiff("onlyNa", "race", nhanesSvy), meanDiffs2), + "onlyNa has only NA's in at least one stratum. na.rm turned off.") + expect_warning(expect_equal(svyStdDiff("onlyNa", "race", nhanesSvy), rep(NaN, 6)), + "onlyNa has only NA's in at least one stratum. na.rm turned off.") + ## 0 because [0]^- = 0, and [1]^T [0]^-1 [1] = 0 + expect_warning(expect_equal(svyStdDiffMulti("onlyNa", "race", nhanesSvy), rep(NaN, 6)), + "onlyNa has only NA's in all strata. Regarding NA as a level.") + +}) + + + +### Define a function for pooled S matrix (used in two tests) +pooledSMat <- function(strataByLevels) { + lstMeans <- LstMeansFromFullTable(strataByLevels) + S1 <- MultinomialVar(lstMeans[[1]]) + S2 <- MultinomialVar(lstMeans[[2]]) + S <- (S1 + S2) / 2 + ## first group element, second group element, pooled + list(S1 = S1, S2 = S2, S = S) +} + + +test_that("zero-cell vectors are decently handled by categorical SMD (artificial data)", { + + set.seed(2015080200) + ## Simple artificial data + wt <- runif(n = 100) + dat <- data.frame(group = rep(c(1,2), each = 100), + var1 = factor(c(rep(1:4, c(0,10,20,70)), rep(1:4, c( 0,10,20,70))), level = 1:4), + var2 = factor(c(rep(1:4, c(0,10,20,70)), rep(1:4, c( 0,20,30,50))), level = 1:4), + var3 = factor(c(rep(1:4, c(0,10,20,70)), rep(1:4, c(20, 0,30,50))), level = 1:4), + var4 = factor(c(rep(1:4, c(0,10,20,70)), rep(1:4, c(10,20,30,40))), level = 1:4), + wt = rep(wt, 2)) + datSvy <- svydesign(ids = ~ 1, data = dat, weights = ~wt) + + + ## Expectations + ## First case + var1S <- pooledSMat(table(dat$group, dat$var1)) + expect_true(rankMatrix(var1S[[1]]) < 3) + expect_true(rankMatrix(var1S[[2]]) < 3) + expect_true(rankMatrix(var1S[[3]]) < 3) + ## Using ginv zero due to numerator (OK) + expect_equal(StdDiffMulti(dat$var1, dat$group), 0) + + ## Second case + var2S <- pooledSMat(table(dat$group, dat$var2)) + expect_true(rankMatrix(var2S[[1]]) < 3) + expect_true(rankMatrix(var2S[[2]]) < 3) + expect_true(rankMatrix(var2S[[3]]) < 3) + ## Using ginv() non-zero + expect_true(StdDiffMulti(dat$var2, dat$group) > 0) + + ## Third case + var3S <- pooledSMat(table(dat$group, dat$var3)) + expect_true(rankMatrix(var3S[[1]]) < 3) + expect_true(rankMatrix(var3S[[2]]) < 3) + ## pooled can be full rank if zero cell positions are different + expect_true(rankMatrix(var3S[[3]]) == 3) + ## Using true inverse from ginv, non-zero + expect_true(StdDiffMulti(dat$var3, dat$group) > 0) + + ## Third case + var4S <- pooledSMat(table(dat$group, dat$var4)) + expect_true(rankMatrix(var4S[[1]]) < 3) + ## All cells populated and full rank + expect_true(rankMatrix(var4S[[2]]) == 3) + ## pooled can be full rank if one group is full rank + expect_true(rankMatrix(var4S[[3]]) == 3) + expect_true(StdDiffMulti(dat$var4, dat$group) > 0) + + + ## Weighted + ## First case + var1S <- pooledSMat(svytable( ~ group + var1, datSvy)) + expect_true(rankMatrix(var1S[[1]]) < 3) + expect_true(rankMatrix(var1S[[2]]) < 3) + expect_true(rankMatrix(var1S[[3]]) < 3) + ## Using ginv zero due to numerator (OK) + expect_equal(svyStdDiffMulti("var1", "group", datSvy), 0) + + ## Second case + var2S <- pooledSMat(svytable( ~ group + var2, datSvy)) + expect_true(rankMatrix(var2S[[1]]) < 3) + expect_true(rankMatrix(var2S[[2]]) < 3) + expect_true(rankMatrix(var2S[[3]]) < 3) + ## Using ginv() non-zero + expect_true(svyStdDiffMulti("var2", "group", datSvy) > 0) + + ## Third case + var3S <- pooledSMat(svytable( ~ group + var3, datSvy)) + expect_true(rankMatrix(var3S[[1]]) < 3) + expect_true(rankMatrix(var3S[[2]]) < 3) + ## pooled can be full rank if zero cell positions are different + expect_true(rankMatrix(var3S[[3]]) == 3) + ## Using true inverse from ginv, non-zero + expect_true(svyStdDiffMulti("var3", "group", datSvy) > 0) + + ## Third case + var4S <- pooledSMat(svytable( ~ group + var4, datSvy)) + expect_true(rankMatrix(var4S[[1]]) < 3) + ## All cells populated and full rank + expect_true(rankMatrix(var4S[[2]]) == 3) + ## pooled can be full rank if one group is full rank + expect_true(rankMatrix(var4S[[3]]) == 3) + expect_true(svyStdDiffMulti("var4", "group", datSvy) > 0) + +}) + + +test_that("zero-cell vectors are decently handled by categorical SMD (real data)", { + + data(nhanes) + set.seed(2015080200) + + ## grouping is by a binary nhanes$RIAGENDR + + ## (0,0.1,0.2,0.7)^T for both groups: same distribution + nhanes$var1 <- NA + nhanes$var1[nhanes$RIAGENDR == 1] <- + as.vector(t(1:4) %*% rmultinom(n = nrow(subset(nhanes, RIAGENDR == 1)), size = 1, prob = c(0,0.1,0.2,0.7))) + nhanes$var1[nhanes$RIAGENDR == 2] <- + as.vector(t(1:4) %*% rmultinom(n = nrow(subset(nhanes, RIAGENDR == 2)), size = 1, prob = c(0,0.1,0.2,0.7))) + nhanes$var1 <- factor(nhanes$var1, levels = 1:4) + + ## (0,0.1,0.2,0.7)^T vs (0,0.2,0.3,0.5)^T: different distribution, but same zero cell + nhanes$var2 <- NA + nhanes$var2[nhanes$RIAGENDR == 1] <- + as.vector(t(1:4) %*% rmultinom(n = nrow(subset(nhanes, RIAGENDR == 1)), size = 1, prob = c(0,0.1,0.2,0.7))) + nhanes$var2[nhanes$RIAGENDR == 2] <- + as.vector(t(1:4) %*% rmultinom(n = nrow(subset(nhanes, RIAGENDR == 2)), size = 1, prob = c(0,0.2,0.3,0.5))) + nhanes$var2 <- factor(nhanes$var2, levels = 1:4) + + ## (0,0.1,0.2,0.7)^T vs (0.2,0,0.3,0.5)^T: different distribution and zero cell + nhanes$var3 <- NA + nhanes$var3[nhanes$RIAGENDR == 1] <- + as.vector(t(1:4) %*% rmultinom(n = nrow(subset(nhanes, RIAGENDR == 1)), size = 1, prob = c(0,0.1,0.2,0.7))) + nhanes$var3[nhanes$RIAGENDR == 2] <- + as.vector(t(1:4) %*% rmultinom(n = nrow(subset(nhanes, RIAGENDR == 2)), size = 1, prob = c(0.2,0,0.3,0.5))) + nhanes$var3 <- factor(nhanes$var3, levels = 1:4) + + ## (0,0.1,0.2,0.7)^T vs (0.1,0.2,0.3,0.5)^T: different distribution and one is ok + nhanes$var4 <- NA + nhanes$var4[nhanes$RIAGENDR == 1] <- + as.vector(t(1:4) %*% rmultinom(n = nrow(subset(nhanes, RIAGENDR == 1)), size = 1, prob = c(0,0.1,0.2,0.7))) + nhanes$var4[nhanes$RIAGENDR == 2] <- + as.vector(t(1:4) %*% rmultinom(n = nrow(subset(nhanes, RIAGENDR == 2)), size = 1, prob = c(0.1,0.2,0.3,0.4))) + nhanes$var4 <- factor(nhanes$var4, levels = 1:4) + + ## svydesign + nhanesSvy <- svydesign(ids = ~ SDMVPSU, strata = ~ SDMVSTRA, + weights = ~ WTMEC2YR, nest = TRUE, + data = nhanes) + + + ## Expectations + ## First case + var1S <- pooledSMat(table(nhanes$RIAGENDR, nhanes$var1)) + expect_true(rankMatrix(var1S[[1]]) < 3) + expect_true(rankMatrix(var1S[[2]]) < 3) + expect_true(rankMatrix(var1S[[3]]) < 3) + ## Using ginv, near zero due to near-zero numerator + expect_true(StdDiffMulti(nhanes$var1, nhanes$RIAGENDR) < 0.1) + + ## Second case + var2S <- pooledSMat(table(nhanes$RIAGENDR, nhanes$var2)) + expect_true(rankMatrix(var2S[[1]]) < 3) + expect_true(rankMatrix(var2S[[2]]) < 3) + expect_true(rankMatrix(var2S[[3]]) < 3) + ## Using ginv() non-zero + expect_true(StdDiffMulti(nhanes$var2, nhanes$RIAGENDR) > 0) + + ## Third case + var3S <- pooledSMat(table(nhanes$RIAGENDR, nhanes$var3)) + expect_true(rankMatrix(var3S[[1]]) < 3) + expect_true(rankMatrix(var3S[[2]]) < 3) + ## pooled can be full rank if zero cell positions are different + expect_true(rankMatrix(var3S[[3]]) == 3) + ## Using true inverse from ginv, non-zero + expect_true(StdDiffMulti(nhanes$var3, nhanes$RIAGENDR) > 0) + + ## Fourth case + var4S <- pooledSMat(table(nhanes$RIAGENDR, nhanes$var4)) + expect_true(rankMatrix(var4S[[1]]) < 3) + ## All cells populated and full rank + expect_true(rankMatrix(var4S[[2]]) == 3) + ## pooled can be full rank if one group is full rank + expect_true(rankMatrix(var4S[[3]]) == 3) + expect_true(StdDiffMulti(nhanes$var4, nhanes$RIAGENDR) > 0) + + + ## Weighted + ## First case + var1S <- pooledSMat(svytable( ~ RIAGENDR + var1, nhanesSvy)) + expect_true(rankMatrix(var1S[[1]]) < 3) + expect_true(rankMatrix(var1S[[2]]) < 3) + expect_true(rankMatrix(var1S[[3]]) < 3) + ## Using ginv, near zero due to small numerator vector + expect_true(svyStdDiffMulti("var1", "RIAGENDR", nhanesSvy) < 0.1) + + ## Second case + var2S <- pooledSMat(svytable( ~ RIAGENDR + var2, nhanesSvy)) + expect_true(rankMatrix(var2S[[1]]) < 3) + expect_true(rankMatrix(var2S[[2]]) < 3) + expect_true(rankMatrix(var2S[[3]]) < 3) + ## Using ginv() non-zero + expect_true(svyStdDiffMulti("var2", "RIAGENDR", nhanesSvy) > 0) + + ## Third case + var3S <- pooledSMat(svytable( ~ RIAGENDR + var3, nhanesSvy)) + expect_true(rankMatrix(var3S[[1]]) < 3) + expect_true(rankMatrix(var3S[[2]]) < 3) + ## pooled can be full rank if zero cell positions are different + expect_true(rankMatrix(var3S[[3]]) == 3) + ## Using true inverse from ginv, non-zero + expect_true(svyStdDiffMulti("var3", "RIAGENDR", nhanesSvy) > 0) + + ## Third case + var4S <- pooledSMat(svytable( ~ RIAGENDR + var4, nhanesSvy)) + expect_true(rankMatrix(var4S[[1]]) < 3) + ## All cells populated and full rank + expect_true(rankMatrix(var4S[[2]]) == 3) + ## pooled can be full rank if one RIAGENDR is full rank + expect_true(rankMatrix(var4S[[3]]) == 3) + expect_true(svyStdDiffMulti("var4", "RIAGENDR", nhanesSvy) > 0) + +}) + + +### Tests working on multiple variables +################################################################################ + +test_that("multiple variables can be looped", { + + + contVars <- c("WTMEC2YR", "HI_CHOL", "race", "RIAGENDR") + catVars <- c("HI_CHOL", "race", "agecat", "RIAGENDR") + + + ## Two groups + expect_equal(as.numeric(sapply(contVars, function(var) { + StdDiff(variable = nhanes[,var], group = nhanes[,"RIAGENDR"]) + })), + c(StdDiff(variable = nhanes[,contVars[1]], group = nhanes[,"RIAGENDR"]), + StdDiff(variable = nhanes[,contVars[2]], group = nhanes[,"RIAGENDR"]), + StdDiff(variable = nhanes[,contVars[3]], group = nhanes[,"RIAGENDR"]), + StdDiff(variable = nhanes[,contVars[4]], group = nhanes[,"RIAGENDR"]))) + + expect_equal(as.numeric(sapply(catVars, function(var) { + StdDiffMulti(variable = nhanes[,var], group = nhanes[,"RIAGENDR"]) + })), + c(StdDiffMulti(variable = nhanes[,catVars[1]], group = nhanes[,"RIAGENDR"]), + StdDiffMulti(variable = nhanes[,catVars[2]], group = nhanes[,"RIAGENDR"]), + StdDiffMulti(variable = nhanes[,catVars[3]], group = nhanes[,"RIAGENDR"]), + StdDiffMulti(variable = nhanes[,catVars[4]], group = nhanes[,"RIAGENDR"]))) + + ## Four groups + expect_equal(lapply(contVars, function(var) { + StdDiff(variable = nhanes[,var], group = nhanes[,"race"]) + }), + list(StdDiff(variable = nhanes[,contVars[1]], group = nhanes[,"race"]), + StdDiff(variable = nhanes[,contVars[2]], group = nhanes[,"race"]), + StdDiff(variable = nhanes[,contVars[3]], group = nhanes[,"race"]), + StdDiff(variable = nhanes[,contVars[4]], group = nhanes[,"race"]))) + + expect_equal(lapply(catVars, function(var) { + StdDiffMulti(variable = nhanes[,var], group = nhanes[,"race"]) + }), + list(StdDiffMulti(variable = nhanes[,catVars[1]], group = nhanes[,"race"]), + StdDiffMulti(variable = nhanes[,catVars[2]], group = nhanes[,"race"]), + StdDiffMulti(variable = nhanes[,catVars[3]], group = nhanes[,"race"]), + StdDiffMulti(variable = nhanes[,catVars[4]], group = nhanes[,"race"]))) + + + ## Two groups + expect_equal(as.numeric(sapply(contVars, function(var) { + svyStdDiff(var, "RIAGENDR", nhanesSvy) + })), + c(svyStdDiff(contVars[1], "RIAGENDR", nhanesSvy), + svyStdDiff(contVars[2], "RIAGENDR", nhanesSvy), + svyStdDiff(contVars[3], "RIAGENDR", nhanesSvy), + svyStdDiff(contVars[4], "RIAGENDR", nhanesSvy))) + + expect_equal(as.numeric(sapply(catVars, function(var) { + svyStdDiffMulti(var, "RIAGENDR", nhanesSvy) + })), + c(svyStdDiffMulti(catVars[1], "RIAGENDR", nhanesSvy), + svyStdDiffMulti(catVars[2], "RIAGENDR", nhanesSvy), + svyStdDiffMulti(catVars[3], "RIAGENDR", nhanesSvy), + svyStdDiffMulti(catVars[4], "RIAGENDR", nhanesSvy))) + + ## Four groups + expect_equal(lapply(contVars, function(var) { + svyStdDiff(var, "race", nhanesSvy) + }), + list(svyStdDiff(contVars[1], "race", nhanesSvy), + svyStdDiff(contVars[2], "race", nhanesSvy), + svyStdDiff(contVars[3], "race", nhanesSvy), + svyStdDiff(contVars[4], "race", nhanesSvy))) + + expect_equal(lapply(catVars, function(var) { + svyStdDiffMulti(var, "race", nhanesSvy) + }), + list(svyStdDiffMulti(catVars[1], "race", nhanesSvy), + svyStdDiffMulti(catVars[2], "race", nhanesSvy), + svyStdDiffMulti(catVars[3], "race", nhanesSvy), + svyStdDiffMulti(catVars[4], "race", nhanesSvy))) + +}) + + +### Older tests with simpler data +################################################################################ + +test_that("binary standardized difference is correct", { + + ## Prepare data + variable <- c(1,0,0,0,0, 1,1,0,0,0, 1,1,1,0,0) + group <- rep(1:3, each = 5) + + data1 <- data.frame(variable = variable, group = group, + weight = rep(0.5, 15)) + data1Svy <- svydesign(ids = ~ 1, data = data1, weights = ~ weight) + + ## Gold standard + props <- c(0.2, 0.4, 0.6) + vars <- c(0.2*0.8, 0.4*0.6, 0.6*0.4) + + g1g2 <- abs(props[1] - props[2]) / sqrt((vars[1] + vars[2]) / 2) + g2g3 <- abs(props[2] - props[3]) / sqrt((vars[2] + vars[3]) / 2) + g3g1 <- abs(props[3] - props[1]) / sqrt((vars[3] + vars[1]) / 2) + + + ## Expectations + ## Binary case + expect_equal(StdDiff(variable, group, binary = TRUE), + c(g1g2, g3g1, g2g3)) + ## Continuous case + expect_equal(StdDiff(variable, group, binary = FALSE), + as.vector(StdDiffOld(variable, group))) + + ## Weighted + ## Binary case + expect_equal(svyStdDiff("variable","group", design = data1Svy, binary = TRUE), + c(g1g2, g3g1, g2g3)) + ## Continuous case + expect_equal(svyStdDiff("variable","group", design = data1Svy, binary = FALSE), + as.vector(svyStdDiffOld("variable","group", design = data1Svy))) + + + ## Multiple variables + expect_equal(StdDiffs(data = data1, vars = c("variable","variable"), + groupVar = "group", binaryVars = "variable"), + list(c(g1g2, g3g1, g2g3), c(g1g2, g3g1, g2g3))) + + expect_equal(StdDiffs(data = data1, vars = c("variable","variable"), + groupVar = "group", binaryVars = NA), + list(as.vector(StdDiffOld(variable, group)), + as.vector(StdDiffOld(variable, group)))) + + expect_equal(svyStdDiffs(data = data1Svy, vars = c("variable","variable"), + groupVar = "group", binaryVars = "variable"), + list(c(g1g2, g3g1, g2g3), c(g1g2, g3g1, g2g3))) + + expect_equal(svyStdDiffs(data = data1Svy, vars = c("variable","variable"), + groupVar = "group", binaryVars = NA), + list(as.vector(svyStdDiffOld("variable","group", design = data1Svy)), + as.vector(svyStdDiffOld("variable","group", design = data1Svy)))) +}) + +test_that("Multinomial SMD is correct", { + + set.seed(102) + ## 4-category variable + probs <- c(0.1,0.3,0.5,0.2) + X <- rmultinom(n = 100, size = 1, prob = probs) + X <- as.vector(seq_along(probs) %*% X) + ## Three groups + group <- c(rep(1,20), rep(2,30), rep(3,50)) + + ## Drop first column to avoid dependent column + ## The result does not change if generalized inverse is used + dummyX <- dummies::dummy(X)[, -1, drop = FALSE] + + lstDummyX <- split(x = as.data.frame(dummyX), f = group, drop = FALSE) + + ## Means for each indicator + means <- lapply(lstDummyX, MultinomialMeans) + ## cov mat for each indicators + covs <- lapply(means, MultinomialVar) + + meanDiff1 <- means[[1]] - means[[2]] + meanDiff2 <- means[[1]] - means[[3]] + meanDiff3 <- means[[2]] - means[[3]] + + covMean1 <- (covs[[1]] + covs[[2]]) / 2 + covMean2 <- (covs[[1]] + covs[[3]]) / 2 + covMean3 <- (covs[[2]] + covs[[3]]) / 2 + + smd1 <- drop(sqrt(t(meanDiff1) %*% MASS::ginv(covMean1) %*% t(t(meanDiff1)))) + smd2 <- drop(sqrt(t(meanDiff2) %*% MASS::ginv(covMean2) %*% t(t(meanDiff2)))) + smd3 <- drop(sqrt(t(meanDiff3) %*% MASS::ginv(covMean3) %*% t(t(meanDiff3)))) + + ## Calculate using multi-category to SMD function + outSmd <- StdDiffMulti(variable = X, group = group) + + expect_equal(length(outSmd), 3) + expect_equal(outSmd, c(smd1,smd2,smd3)) + + + ## + ## Repeat for dummy variable matrix with all columns (k-1 independent) + dummyX <- dummies::dummy(X) + lstDummyX <- split(x = as.data.frame(dummyX), f = group, drop = FALSE) + ## Means for each indicator + means <- lapply(lstDummyX, MultinomialMeans) + ## cov mat for each indicators + covs <- lapply(means, MultinomialVar) + meanDiff1 <- means[[1]] - means[[2]] + meanDiff2 <- means[[1]] - means[[3]] + meanDiff3 <- means[[2]] - means[[3]] + covMean1 <- (covs[[1]] + covs[[2]]) / 2 + covMean2 <- (covs[[1]] + covs[[3]]) / 2 + covMean3 <- (covs[[2]] + covs[[3]]) / 2 + smd1 <- drop(sqrt(t(meanDiff1) %*% MASS::ginv(covMean1) %*% t(t(meanDiff1)))) + smd2 <- drop(sqrt(t(meanDiff2) %*% MASS::ginv(covMean2) %*% t(t(meanDiff2)))) + smd3 <- drop(sqrt(t(meanDiff3) %*% MASS::ginv(covMean3) %*% t(t(meanDiff3)))) + + expect_equal(outSmd, c(smd1,smd2,smd3)) + + + ## Check behavior with a binary variable + set.seed(102) + ## Binary variable + X <- rbinom(n = 100, size = 1, prob = 0.2) + group <- rep(c(0,1), c(20,80)) + + dummyX <- dummies::dummy(X)[, -1, drop = FALSE] + + lstDummyX <- split(x = as.data.frame(dummyX), f = group, drop = FALSE) + + ## Means for each indicator + means <- lapply(lstDummyX, MultinomialMeans) + ## cov mat for each indicators + covs <- lapply(means, MultinomialVar) + ## + meanDiff1 <- means[[1]] - means[[2]] + covMean1 <- (covs[[1]] + covs[[2]]) / 2 + + smd1 <- drop(sqrt(t(meanDiff1) %*% MASS::ginv(covMean1) %*% t(t(meanDiff1)))) + + ## Comparison to manual calculation + expect_equal(StdDiffMulti(variable = X, group = group), smd1) + ## Comparison to continuous version with binary support + expect_equal(StdDiffMulti(variable = X, group = group), + StdDiff(variable = X, group = group, binary = TRUE)) + + ## Check missing value handling (binary) + X[13] <- NA + ## within group values + means <- tapply(X, group, mean, na.rm = TRUE) + covs <- means * (1 - means) + ## Diff and cov mean + meanDiff1 <- means[[1]] - means[[2]] + covMean1 <- (covs[[1]] + covs[[2]]) / 2 + smd1 <- drop(sqrt(t(meanDiff1) %*% MASS::ginv(covMean1) %*% t(t(meanDiff1)))) + smd2 <- meanDiff1 / sqrt(covMean1) + + expect_equal(smd1, smd2) + + expect_equal(StdDiff(variable = X, group = group, binary = TRUE), smd1) + expect_equal(StdDiff(variable = X, group = group, binary = TRUE), smd2) + + expect_equal(StdDiffMulti(variable = X, group = group), + StdDiff(variable = X, group = group, binary = TRUE)) + + expect_equal(StdDiffMulti(variable = X, group = group), + StdDiff(variable = X, group = group, binary = TRUE)) + +}) + + +test_that("mutlinomial SMD for survey data is correct", { + + set.seed(102) + ## 4-category variable + probs <- c(0.1,0.3,0.5,0.2) + X <- rmultinom(n = 100, size = 1, prob = probs) + X <- as.vector(seq_along(probs) %*% X) + ## Three groups + group <- c(rep(1,20), rep(2,30), rep(3,50)) + + dat <- data.frame(X = X, group = group, weight = 1) + datSvy <- svydesign(ids = ~ 1, data = dat, weights = ~ weight) + + ## Check tables match with weight of 1 for everyone + tab1svytable <- svytable(~ group + X, design = datSvy) + tab1xtabs <- xtabs(~ group + X, data = dat) + expect_true(all(tab1svytable == tab1xtabs)) + + ## Check equality for equal weight case + expect_equal(svyStdDiffMulti("X", "group", datSvy), + StdDiffMulti(dat$X, group = dat$group)) + + ## Missing value is introduced + dat$X[13] <- NA + ## Recreate + datSvy <- svydesign(ids = ~ 1, data = dat, weights = ~ weight) + ## Tables again + tab1svytable <- svytable(~ group + X, design = datSvy) + tab1xtabs <- xtabs(~ group + X, data = dat) + + expect_true(all(tab1svytable == tab1xtabs)) + + + expect_equal(svyStdDiffMulti("X", "group", datSvy), + StdDiffMulti(dat$X, group = dat$group)) + +}) + + + +### +### User level functionality check +################################################################################ + +test_that("SMDs are correctly shown in print()", { + + vars <- c("race","agecat") + strata <- c("HI_CHOL","RIAGENDR") + + ## Create an interaction variable + nhanes$strataVar <- interaction(nhanes$HI_CHOL, nhanes$RIAGENDR, sep = ":") + ## Create an weighted data + nhanesSvy <- svydesign(ids = ~ SDMVPSU, strata = ~ SDMVSTRA, weights = ~ WTMEC2YR, + nest = TRUE, data = nhanes) + + + ## Unweighed version + ## Create a table + tab1 <- CreateTableOne(vars = vars, strata = strata, data = nhanes) + + ## Show summary including + summary(tab1) + expect_output(summary(tab1), "Standardize mean differences") + + ## First variable changes fastest, and its consistent with by()'s order + expect_equal(levels(nhanes$strataVar), + c("0:1", "1:1", "0:2", "1:2")) + expect_equal(levels(nhanes$strataVar), + colnames(print(tab1$ContTable))[1:4]) + + ## Check ordering is correct (continuous) + expect_equal(as.vector(attr(tab1$ContTable, "smd"))[-1], + StdDiff(nhanes$race, nhanes$strataVar)) + + ## Check ordering is correct (categorical) + expect_equal(as.vector(attr(tab1$CatTable, "smd"))[-1], + StdDiffMulti(nhanes$agecat, nhanes$strataVar)) + + out1 <- print(tab1, smd = TRUE) + expect_equal(as.vector(out1[,"SMD"][2:3]), + c(sprintf(" %.3f", attr(tab1$ContTable, "smd")[1,1]), + sprintf(" %.3f", attr(tab1$CatTable, "smd")[1,1]))) + +}) diff --git a/tests/testthat/test-modules-svy.R b/tests/testthat/test-modules-svy.R new file mode 100644 index 0000000..f7dcbe3 --- /dev/null +++ b/tests/testthat/test-modules-svy.R @@ -0,0 +1,268 @@ +################################################################################ +### Unit tests for the modules +## Reference: http://adv-r.had.co.nz/Testing.html +## Created on: 2015-07-24 +## Author: Kazuki Yoshida +################################################################################ + +### Structure +## expectations within tests within context + +### Prepare environment +################################################################################ +library(testthat) +library(survey) + + +### Context (1 for each file) +################################################################################ +context("Unit tests for the survey-related modules") + + + +### Provide data +################################################################################ + +## Three-group dataset with matching weight (Li & Greene 2013) +datMw <- structure(list(E = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, +2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), C = c(1L, +1L, 2L, 2L, 3L, 3L, 4L, 4L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 1L, +1L, 2L, 2L, 3L, 3L, 4L, 4L), Y = c(0, 1, 0, 1, 0, 1, 0, 1, 0, +1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1), C1 = c(0, 0, 0, +0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1 +), C2 = c(0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, +0, 1, 1, 0, 0, 1, 1), n = c(252L, 28L, 105L, 45L, 12L, 28L, 1L, +9L, 72L, 8L, 63L, 27L, 24L, 56L, 1L, 9L, 36L, 4L, 42L, 18L, 24L, +56L, 8L, 72L), Mw = c(0.1429, 0.1429, 0.4, 0.4, 1, 1, 1, 1, 0.5, +0.5, 0.6667, 0.6667, 0.5, 0.5, 1, 1, 1, 1, 1, 1, 0.5, 0.5, 0.125, +0.125)), class = "data.frame", .Names = c("E", "C", "Y", "C1", +"C2", "n", "Mw"), row.names = c(NA, -24L)) + +## Create individual level data +datMw <- datMw[rep(seq_along(datMw$n), datMw$n),] +## Introduce missingness +datMw[c(1,150), "Y"] <- NA +## Create a survey design object +datSvy <- svydesign(ids = ~ 1, data = datMw, weights = ~ Mw) + + +### Tests +################################################################################ +## A test should group together expectations for one functionality. + + +test_that("indicator functions work correctly", { + + ## one for counting + expect_equal(one(c(NA,NA,NA)), c(1,1,1)) + expect_equal(one(c(1,2,3)), c(1,1,1)) + + ## missing value indicator + expect_equal(miss(c(NA,NA,NA)), c(1,1,1)) + expect_equal(miss(c(1,2,3)), c(0,0,0)) + expect_equal(miss(c(1,NA,3)), c(0,1,0)) +}) + + +test_that("variable names are transformed to correct formula", { + + ## Expectations + expect_equal(FormulaString(c("a","b","c")), " ~ a + b + c") + expect_equal(FormulaString("a"), " ~ a") +}) + + +test_that("post-weighting dataset has 150 x 3 weighted subjects", { + + ## Expectations + expect_equal(as.integer(round(svyN(vars = "E", datSvy))), 450) + expect_equal(as.numeric(svyN(vars = "E", datSvy)), + as.numeric(svytotal( ~ one(E), datSvy))) + + expect_equal(as.integer(round(svyN(vars = "Y", datSvy))), 450) + expect_equal(as.numeric(svyN(vars = "Y", datSvy)), + as.numeric(svytotal( ~ one(Y), datSvy))) + + expect_equal(as.integer(round(svyN(vars = "E", subset(datSvy, E == 1)))), 150) + expect_equal(as.integer(round(svyN(vars = "E", subset(datSvy, E == 2)))), 150) + expect_equal(as.integer(round(svyN(vars = "E", subset(datSvy, E == 3)))), 150) + + expect_equal(as.numeric(svyN(vars = "E", subset(datSvy, E == 1))), + as.numeric(svytotal( ~ one(E), subset(datSvy, E == 1)))) + expect_equal(as.numeric(svyN(vars = "E", subset(datSvy, E == 2))), + as.numeric(svytotal( ~ one(E), subset(datSvy, E == 2)))) + expect_equal(as.numeric(svyN(vars = "E", subset(datSvy, E == 3))), + as.numeric(svytotal( ~ one(E), subset(datSvy, E == 3)))) + + expect_equal(as.integer(round(svyN(vars = "E", subset(datSvy, E == 1)))), 150) + expect_equal(as.integer(round(svyN(vars = "E", subset(datSvy, E == 2)))), 150) + expect_equal(as.integer(round(svyN(vars = "E", subset(datSvy, E == 3)))), 150) + + expect_equal(as.integer(round(svyN(vars = c("E","Y"), subset(datSvy, E == 3)))), c(150,150)) +}) + + +test_that("NA counting works", { + + ## Expectations + expect_true(svyMiss(vars = "E", datSvy) == 0) + ## Two observations with a weight of 0.1429 each + expect_true(svyMiss(vars = "Y", datSvy) == 0.2858) + ## Can work on multiple variables at a time + expect_true(all(svyMiss(vars = c("E","C1"), datSvy) == c(0, 0))) + expect_true(all(svyMiss(vars = c("E","C1","Y"), datSvy) == c(0, 0, 0.2858))) +}) + + +test_that("Means are correct", { + + ## Expectations + expect_equal(as.numeric(svyMean(vars = "E", datSvy)), + as.numeric(svymean( ~ E, datSvy))) + + ## NA's are always removed + expect_equal(as.numeric(svyMean(vars = "Y", datSvy)), + as.numeric(svymean( ~ Y, datSvy, na.rm = TRUE))) + + expect_equal(as.numeric(svyMean(vars = "Y", subset(datSvy, E == 1))), + as.numeric(svymean( ~ Y, subset(datSvy, E == 1), na.rm = TRUE))) +}) + + +test_that("SDs are correct", { + + ## Expectations + expect_equal(as.numeric(svySd(vars = "E", datSvy)), + as.numeric(sqrt(svyvar( ~ E, datSvy)))) + + ## NA's are always removed + expect_equal(as.numeric(svySd(vars = "Y", datSvy)), + as.numeric(sqrt(svyvar( ~ Y, datSvy, na.rm = TRUE)))) + + ## Take diagnoal of matrix for multiple variables + expect_equal(as.numeric(svySd(vars = c("C1","C2"), datSvy)), + as.numeric(sqrt(diag(svyvar( ~ C1 + C2, datSvy, na.rm = TRUE))))) +}) + + +test_that("Quantiles are correct", { + + ## Expectations + expect_equal(as.numeric(svyQuant(vars = "E", datSvy, q = 0.5)), + as.numeric(svyquantile( ~ E, datSvy, quantiles = 0.5))) + + expect_equal(as.numeric(svyQuant(vars = "E", datSvy, q = 1)), + as.numeric(svyquantile( ~ E, datSvy, quantiles = 1))) + + expect_equal(as.numeric(svyQuant(vars = "Y", datSvy, q = 0)), + as.numeric(svyquantile( ~ Y, datSvy, quantiles = 0, na.rm = TRUE))) + + ## Only first of q is used + expect_equal(as.numeric(svyQuant(vars = "E", datSvy, q = c(0.5, 0, 1))), + as.numeric(svyquantile( ~ E, datSvy, quantiles = 0.5))) +}) + + +test_that("Regression test for svyContSummary", { + + res1 <- structure(c(450.015, 450.015, 0, 0.2858, 0, 0.0635089941446396, + 2.00060881081326, 0.39357484459537, 0.816777838825025, 0.488786864487986, + 2, 0, 1, 0, 3, 1, 1, 0, 3, 1), .Dim = c(2L, 10L), .Dimnames = list( + c("E", "Y"), c("n", "miss", "p.miss", "mean", "sd", "median", + "p25", "p75", "min", "max"))) + expect_equal(svyContSummary(vars = c("E","Y"), datSvy), res1) +}) + + +test_that("table works", { + + ## Expectations + expect_true(all(svyTable("E", datSvy) == svytable( ~ E, datSvy))) + expect_true(all(svyTable("Y", datSvy) == svytable( ~ Y, datSvy))) +}) + + +test_that("Levels are obtained", { + + ## Expectations + expect_equal(svyLevel("E", datSvy), c("1","2","3")) + expect_equal(svyLevel("Y", datSvy), c("0","1")) +}) + + +test_that("Prop table works", { + + ## Expectations + expect_equal(as.numeric(round(svyPropTable("E", datSvy), 3)), + round(c(1/3,1/3,1/3), 3)) + expect_equal(as.numeric(svyPropTable("Y", datSvy)), + as.numeric(prop.table(svytable( ~ Y, datSvy)))) +}) + + +test_that("Regression test for one variable categorical summary", { + + ## Expectations + res1 <- structure(list(n = c(450.015, 450.015), miss = c(0.2858, 0.2858 +), p.miss = c(0.0635089941446396, 0.0635089941446396), level = structure(1:2, .Label = c("0", +"1"), class = "factor"), freq = c(272.7271, 177.0021), percent = c(60.642515540463, +39.357484459537), cum.percent = c(60.642515540463, 100)), .Names = c("n", +"miss", "p.miss", "level", "freq", "percent", "cum.percent"), row.names = c(NA, +-2L), class = "data.frame") + expect_equal(svyCatSummaryForOneVar("Y", datSvy), res1) + + res2 <- structure(list(n = c(450.015, 450.015, 450.015), miss = c(0, +0, 0), p.miss = c(0, 0, 0), level = structure(1:3, .Label = c("1", +"2", "3"), class = "factor"), freq = c(150.012, 150.003, 150), + percent = c(33.3348888370388, 33.3328889037032, 33.332222259258 + ), cum.percent = c(33.3348888370388, 66.667777740742, 100 + )), .Names = c("n", "miss", "p.miss", "level", "freq", "percent", +"cum.percent"), row.names = c(NA, -3L), class = "data.frame") + expect_equal(svyCatSummaryForOneVar("E", datSvy), res2) +}) + + +test_that("Regression test for multiple variable categorical summary", { + + ## Expectations + + res1 <- structure(list(E = structure(list(n = c(450.015, 450.015, 450.015 +), miss = c(0, 0, 0), p.miss = c(0, 0, 0), level = structure(1:3, .Label = c("1", +"2", "3"), class = "factor"), freq = c(150.012, 150.003, 150), + percent = c(33.3348888370388, 33.3328889037032, 33.332222259258 + ), cum.percent = c(33.3348888370388, 66.667777740742, 100 + )), .Names = c("n", "miss", "p.miss", "level", "freq", "percent", +"cum.percent"), row.names = c(NA, -3L), class = "data.frame"), + Y = structure(list(n = c(450.015, 450.015), miss = c(0.2858, + 0.2858), p.miss = c(0.0635089941446396, 0.0635089941446396 + ), level = structure(1:2, .Label = c("0", "1"), class = "factor"), + freq = c(272.7271, 177.0021), percent = c(60.642515540463, + 39.357484459537), cum.percent = c(60.642515540463, 100 + )), .Names = c("n", "miss", "p.miss", "level", "freq", + "percent", "cum.percent"), row.names = c(NA, -2L), class = "data.frame"), + C1 = structure(list(n = c(450.015, 450.015), miss = c(0, + 0), p.miss = c(0, 0), level = structure(1:2, .Label = c("0", + "1"), class = "factor"), freq = c(300.015, 150), percent = c(66.667777740742, + 33.332222259258), cum.percent = c(66.667777740742, 100)), .Names = c("n", + "miss", "p.miss", "level", "freq", "percent", "cum.percent" + ), row.names = c(NA, -2L), class = "data.frame")), .Names = c("E", + "Y", "C1")) + expect_equal(svyCatSummary(c("E","Y","C1"), datSvy), res1) +}) + + +### Statistical test functions + +test_that("Statistical test wrappers work", { + + ## Expectations + ## ANOVA equivalent + expect_true(regTermTest(svyglm(Y ~ E, datSvy), test.terms = "E")$p[1,1] == svyTestNormal("Y ~ E", design = datSvy, test.terms = "E", method = "Wald")) + + ## Kruskal-Wallis test equivalent (the whole htest object match) + expect_equal(svyranktest(Y ~ E, datSvy), svyTestNonNormal("Y ~ E", design = datSvy)) + + ## Chi-squared test equivalent (the whole thing had partial mismatch) + expect_equal(svychisq(~ C1 + E, datSvy)$p.value, svyTestChisq("~ C1 + E", design = datSvy)$p.value) + +}) diff --git a/tests/testthat/test-modules.R b/tests/testthat/test-modules.R index 4115ae1..ba1bfef 100644 --- a/tests/testthat/test-modules.R +++ b/tests/testthat/test-modules.R @@ -11,7 +11,6 @@ ### Prepare environment ################################################################################ library(testthat) -library(tableone) ### Context (1 for each file) context("Unit tests for the modules") diff --git a/tests/testthat/test-svyCreateTableOne.R b/tests/testthat/test-svyCreateTableOne.R new file mode 100644 index 0000000..cde85c0 --- /dev/null +++ b/tests/testthat/test-svyCreateTableOne.R @@ -0,0 +1,446 @@ +################################################################################ +### Unit tests for the svyCreateTableOne +## Reference: http://adv-r.had.co.nz/Testing.html +## Created on: 2015-07-24 +## Author: Kazuki Yoshida +################################################################################ + +### Structure +## expectations within tests within context + +### Prepare environment +################################################################################ +library(testthat) +library(survey) + + +### Context (1 for each file) +################################################################################ +context("Unit tests for svy* user functions") + + + +### Provide data +################################################################################ + +## Three-group dataset with matching weight (Li & Greene 2013) +datMw <- structure(list(E = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, +2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), C = c(1L, +1L, 2L, 2L, 3L, 3L, 4L, 4L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 1L, +1L, 2L, 2L, 3L, 3L, 4L, 4L), Y = c(0, 1, 0, 1, 0, 1, 0, 1, 0, +1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1), C1 = c(0, 0, 0, +0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1 +), C2 = c(0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, +0, 1, 1, 0, 0, 1, 1), n = c(252L, 28L, 105L, 45L, 12L, 28L, 1L, +9L, 72L, 8L, 63L, 27L, 24L, 56L, 1L, 9L, 36L, 4L, 42L, 18L, 24L, +56L, 8L, 72L), Mw = c(0.1429, 0.1429, 0.4, 0.4, 1, 1, 1, 1, 0.5, +0.5, 0.6667, 0.6667, 0.5, 0.5, 1, 1, 1, 1, 1, 1, 0.5, 0.5, 0.125, +0.125)), class = "data.frame", .Names = c("E", "C", "Y", "C1", +"C2", "n", "Mw"), row.names = c(NA, -24L)) + +## Create individual level data +datMw <- datMw[rep(seq_along(datMw$n), datMw$n),] +## Introduce missingness +datMw[c(1,150), "Y"] <- NA +## Create a survey design object +datSvy <- svydesign(ids = ~ 1, data = datMw, weights = ~ Mw) + +## Create a variable list +vars <- c("E", "C", "Y", "C1", "C2") +factorVars <- c("Y","C1") + + +### Tests +################################################################################ +## A test should group together expectations for one functionality. + + +### Error handling + +test_that("data assessment detects anomalies", { + + ## Error in ModuleStopIfNoVarsLeft(vars) (from modules.R#52) : No valid variables. + ## In addition: Warning message: + ## In ModuleReturnVarsExist(vars, data$variables) : + ## The data frame does not have: none Dropped + expect_warning(expect_error(svyCreateTableOne(vars = "non-existent", data = datSvy), + "No valid variables"), + "The data frame does not have: non-existent Dropped") + + + ## Error in StopIfNotSurveyDesign(data) : + ## The data argument needs to be a survey design object. + expect_error(svyCreateTableOne(vars = c("E"), data = "not a svy object"), + "The data argument needs to be a survey design object") + + ## Data frame is not allowed + expect_error(svyCreateTableOne(vars = c("E"), data = data.frame(E = c(1,2,3))), + "The data argument needs to be a survey design object") + + ## Error in ModuleReturnStrata(strata, data$variable) (from modules.R#84) : + ## None of the stratifying variables are present in the data frame. + ## In addition: Warning message: + ## In ModuleReturnVarsExist(strata, data) : + ## The data frame does not have: non-existent Dropped + expect_warning(expect_error(svyCreateTableOne(vars = vars, strata = c("non-existent"), data = datSvy), + "None of the stratifying variables are present in the data frame"), + "The data frame does not have: non-existent Dropped") + +}) + + +### User-level functionalities + +## Make categorical variables factors + +## Create a table to test +mwOverall <- svyCreateTableOne(vars = vars, data = datSvy, factorVars = factorVars) +mwInclNa <- svyCreateTableOne(vars = vars, data = datSvy, factorVars = factorVars, includeNA = TRUE) +mwByE <- svyCreateTableOne(vars = vars, strata = c("E"), data = datSvy, factorVars = factorVars) +mwByEC1 <- svyCreateTableOne(vars = vars, strata = c("E","C1"), data = datSvy, factorVars = factorVars) +mwContOnlyByEC1 <- svyCreateTableOne(vars = c("E","C"), strata = c("E","C1"), data = datSvy, factorVars = factorVars) +mwCatOnlyByEC1 <- svyCreateTableOne(vars = c("Y","C1"), strata = c("E","C1"), data = datSvy, factorVars = factorVars) + +## Specify variables for special handling +nonnormalVars <- c("C") +exactVars <- c("Y") + + +test_that("svyTableOne objects are always returned", { + + expect_equal(class(mwOverall), c("svyTableOne","TableOne")) + expect_equal(class(mwInclNa), c("svyTableOne","TableOne")) + expect_equal(class(mwByE), c("svyTableOne","TableOne")) + expect_equal(class(mwByEC1), c("svyTableOne","TableOne")) + expect_equal(class(mwContOnlyByEC1), c("svyTableOne","TableOne")) + expect_equal(class(mwCatOnlyByEC1), c("svyTableOne","TableOne")) + +}) + + +test_that("printing of a svyTableOne object does not regress", { + + ## Expectations + expect_equal_to_reference(print(mwByE, printToggle = TRUE), + "ref-svyTableOne_defaultPrint") + + expect_equal_to_reference(print(mwOverall, printToggle = TRUE), + "ref-svyTableOne_overallPrint") + + expect_equal_to_reference(print(mwInclNa, printToggle = TRUE), + "ref-svyTableOne_IncludeNA") + + expect_equal_to_reference(print(mwByEC1, printToggle = TRUE), + "ref-svyTableOne_2StrataVars") + + expect_equal_to_reference(print(mwByE, catDigits = 3, contDigits = 4, pDigits = 5, printToggle = TRUE), + "ref-svyTableOne_digits") + + expect_equal_to_reference(print(mwByE, test = FALSE, printToggle = TRUE), + "ref-svyTableOne_noTests") + + expect_equal_to_reference(print(mwByE, nonnormal = nonnormalVars, exact = exactVars, printToggle = TRUE), + "ref-svyTableOne_nonnormal_exact") + + expect_equal_to_reference(print(mwByE, nonnormal = nonnormalVars, minMax = TRUE, printToggle = TRUE), + "ref-svyTableOne_nonnormal_minMax") + + expect_equal_to_reference(print(mwByE, catDigits = 3, noSpaces = TRUE, printToggle = TRUE), + "ref-svyTableOne_noSpaces") + + expect_equal_to_reference(print(mwByE, nonnormal = nonnormalVars, exact = exactVars, showAllLevels = TRUE, printToggle = TRUE), + "ref-svyTableOne_showAllLevels") + + expect_equal_to_reference(print(mwByE, catDigits = 3, nonnormal = nonnormalVars, exact = exactVars, noSpaces = TRUE, showAllLevels = FALSE, quote = TRUE, printToggle = TRUE), + "ref-svyTableOne_noSpaces_showAllLevels_quote") + + expect_equal_to_reference(print(mwContOnlyByEC1), + "ref-svyTableOne_ContOnly") + + expect_equal_to_reference(print(mwCatOnlyByEC1), + "ref-svyTableOne_CatOnly") + +}) + + +test_that("printing of a svyTableOne$CatTable object do not regress", { + + ## Expectations + expect_equal_to_reference(print(mwByE$CatTable, printToggle = TRUE), + "ref-svyCatTable_defaultPrint") + + expect_equal_to_reference(print(mwOverall$CatTable, printToggle = TRUE), + "ref-svyCatTable_overallPrint") + + expect_equal_to_reference(print(mwInclNa$CatTable, printToggle = TRUE), + "ref-svyCatTable_IncludeNA") + + expect_equal_to_reference(print(mwByEC1$CatTable, printToggle = TRUE), + "ref-svyCatTable_2StrataVars") + + expect_equal_to_reference(print(mwByEC1$CatTable, digits = 3, pDigits = 5, printToggle = TRUE), + "ref-svyCatTable_digits") + + expect_equal_to_reference(print(mwByEC1$CatTable, test = FALSE, printToggle = TRUE), + "ref-svyCatTable_noTests") + + expect_equal_to_reference(print(mwByE$CatTable, digits = 3, noSpaces = TRUE, printToggle = TRUE), + "ref-svyCatTable_noSpaces") + + expect_equal_to_reference(print(mwByE$CatTable, showAllLevels = TRUE, printToggle = TRUE), + "ref-svyCatTable_showAllLevels") + + expect_equal_to_reference(print(mwByE$CatTable, explain = FALSE, printToggle = TRUE), + "ref-svyCatTable_explain") + + expect_equal_to_reference(print(mwByE$CatTable, format = "f", printToggle = TRUE), + "ref-svyCatTable_format_f") + + expect_equal_to_reference(print(mwByE$CatTable, format = "p", printToggle = TRUE), + "ref-svyCatTable_format_p") + + expect_equal_to_reference(print(mwByE$CatTable, format = "pf", printToggle = TRUE), + "ref-svyCatTable_format_pf") + + expect_equal_to_reference(print(mwByE$CatTable, cramVars = "Y", printToggle = TRUE), + "ref-svyCatTable_cramVars") + + expect_equal_to_reference(print(mwByE$CatTable, noSpaces = TRUE, showAllLevels = TRUE, quote = TRUE, printToggle = TRUE), + "ref-svyCatTable_noSpaces_showAllLevels_quote") + + ## gmodels::CrossTable + print(mwByEC1$CatTable, CrossTable = TRUE) + expect_output(print(mwByEC1$CatTable, CrossTable = TRUE), +"|-------------------------| +| N | +| Chi-square contribution | +| N / Row Total | +| N / Col Total | +| N / Table Total | +|-------------------------|") + +}) + + +test_that("printing of a svyTableOne$ContTable object do not regress", { + + ## Expectations + expect_equal_to_reference(print(mwByE$ContTable, printToggle = TRUE), + "ref-svyContTable_defaultPrint") + + expect_equal_to_reference(print(mwOverall$ContTable, printToggle = TRUE), + "ref-svyContTable_overallPrint") + + expect_equal_to_reference(print(mwByEC1$ContTable, printToggle = TRUE), + "ref-svyContTable_2StrataVars") + + expect_equal_to_reference(print(mwByE$ContTable, digits = 3, pDigits = 5, printToggle = TRUE), + "ref-svyContTable_digits") + + expect_equal_to_reference(print(mwByE$ContTable, test = FALSE, printToggle = TRUE), + "ref-svyContTable_noTests") + + expect_equal_to_reference(print(mwByE$ContTable, nonnormal = nonnormalVars, printToggle = TRUE), + "ref-svyContTable_nonnormal") + + expect_equal_to_reference(print(mwByE$ContTable, nonnormal = nonnormalVars, minMax = TRUE, printToggle = TRUE), + "ref-svyContTable_nonnormal_minMax") + + ## This does not make a difference here + expect_equal_to_reference(print(mwByE$ContTable, noSpaces = TRUE, printToggle = TRUE), + "ref-svyContTable_noSpaces") + + + expect_equal_to_reference(print(mwByE$ContTable, explain = FALSE, printToggle = TRUE), + "ref-svyContTable_explain") + + expect_equal_to_reference(print(mwByE$ContTable, noSpaces = TRUE, showAllLevels = TRUE, quote = TRUE, printToggle = TRUE), + "ref-svyContTable_noSpaces_showAllLevels_quote") +}) + + + +### p value calculations + +test_that("p values are correctly calculated", { + + ## svychisq + pValuesTestChisq <- c(svychisq( ~ Y + E, datSvy)$p.value, svychisq( ~ C1 + E, datSvy)$p.value) + ## Drop names X-squared X-squared + names(pValuesTestChisq) <- NULL + expect_equal(attr(mwByE$CatTable, "pValues")[, "pApprox"], pValuesTestChisq) + + ## no exact tests + expect_equal(attr(mwByE$CatTable, "pValues")[, "pExact"], c(NA, NA)) + + ## svyglm to do ANOVA equivalent (first is removed to avoid singularity error on i386) + pValuesTestNormal <- + c(svyTestNormal("C ~ factor(E)", datSvy, test.terms = "factor(E)", method = "Wald")$p.value, + svyTestNormal("C2 ~ factor(E)", datSvy, test.terms = "factor(E)", method = "Wald")$p.value) + expect_equal(attr(mwByE$ContTable, "pValues")[, "pNormal"][-1], pValuesTestNormal) + + ## svyglm to do ANOVA equivalent (only solvable on non-i386 systems) + if (R.Version()$arch != "i386") { + expect_equal(attr(mwByE$ContTable, "pValues")[, "pNormal"][1], + svyTestNormal("E ~ factor(E)", datSvy, test.terms = "factor(E)", method = "Wald")$p.value) } + + ## svyranktest + pValuesTestNonNormal <- + c(svyTestNonNormal("E ~ factor(E)", datSvy)$p.value, + svyTestNonNormal("C ~ factor(E)", datSvy)$p.value, + svyTestNonNormal("C2 ~ factor(E)", datSvy)$p.value) + expect_equal(attr(mwByE$ContTable, "pValues")[, "pNonNormal"], pValuesTestNonNormal) + +}) + + + +### Correctness of data object and final print out +################################################################################ + +## Create svydesign and TableOne object +data(nhanes) +nhanesSvy <- svydesign(ids = ~ SDMVPSU, strata = ~ SDMVSTRA, weights = ~ WTMEC2YR, nest = TRUE, data = nhanes) +tab1 <- svyCreateTableOne(vars = c("HI_CHOL","race","agecat","RIAGENDR"), + strata = "RIAGENDR", data = nhanesSvy, + factorVars = c("race","RIAGENDR")) + +### Gold standar data +## Sample size +outTotals <- svyby(formula = ~ tableone:::one(HI_CHOL), by = ~ RIAGENDR, design = nhanesSvy, + FUN = svytotal, na.rm = TRUE) +## Means +outMeans <- svyby(formula = ~ HI_CHOL, by = ~ RIAGENDR, design = nhanesSvy, FUN = svymean, na.rm = TRUE) +## Variances (for SD) +outVar <- svyby(formula = ~ HI_CHOL, by = ~ RIAGENDR, design = nhanesSvy, FUN = svyvar, na.rm = TRUE) +## Quantiles +outQt1 <- svyquantile( ~ HI_CHOL, subset(nhanesSvy, RIAGENDR == 1), quantiles = c(0.5,0.25,0.75,0,1), na.rm = TRUE) +outQt2 <- svyquantile( ~ HI_CHOL, subset(nhanesSvy, RIAGENDR == 2), quantiles = c(0.5,0.25,0.75,0,1), na.rm = TRUE) +## Tests +pTTest <- sprintf(" %.7f", as.vector(svyttest(HI_CHOL ~ RIAGENDR, nhanesSvy)$p.value)) +pRankTest <- sprintf(" %.7f", as.vector(svyranktest(HI_CHOL ~ RIAGENDR, nhanesSvy)$p.value)) + +test_that("continuous data object and final print outs are numerically correct", { + +### Continuous part +### Sample sizes +## Correctness of one() function for sample size against total weights +expect_equal(as.vector(by(nhanes$WTMEC2YR, nhanes$RIAGENDR, sum)), + outTotals[,2]) +## Numerical correctness +expect_equal(c(tab1$ContTable[[1]][,"n"], tab1$ContTable[[2]][,"n"]), + outTotals[,2]) +## print out correctness +expect_equal(as.vector(print(tab1$ContTable, printToggle = TRUE)[1,1:2]), + sprintf("%.2f", outTotals[,2])) + +### Means +## Numerical correctness +expect_equal(c(tab1$ContTable[[1]][,"mean"], tab1$ContTable[[2]][,"mean"]), + outMeans[,2]) + +### Standard deviations +## Numerically +expect_equal(c(tab1$ContTable[[1]][,"sd"], tab1$ContTable[[2]][,"sd"]), + sqrt(outVar[,2])) +## print out (mean and sd) +expect_equal(as.vector(print(tab1$ContTable, digits = 2, printToggle = TRUE)[2,1:2]), + sprintf("%.2f (%.2f)", outMeans[,2], sqrt(outVar[,2]))) + +### Quantiles +## median [p25, p75] +expect_equal(as.vector(print(tab1$ContTable, nonnormal = "HI_CHOL", printToggle = TRUE)[2,1:2]), + c(do.call(sprintf, c(list(fmt = "%.2f [%.2f, %.2f]"), outQt1[1:3])), + do.call(sprintf, c(list(fmt = "%.2f [%.2f, %.2f]"), outQt2[1:3])))) +## median [min,max] +expect_equal(as.vector(print(tab1$ContTable, nonnormal = "HI_CHOL", minMax = TRUE, printToggle = TRUE)[2,1:2]), + c(do.call(sprintf, c(list(fmt = "%.2f [%.2f, %.2f]"), outQt1[c(1,4,5)])), + do.call(sprintf, c(list(fmt = "%.2f [%.2f, %.2f]"), outQt2[c(1,4,5)])))) + +### p-values +## t test +expect_equal(print(tab1$ContTable, pDigits = 7, printToggle = TRUE)[2,3], + ## One space for < + pTTest) +## KW +expect_equal(print(tab1$ContTable, pDigits = 7, nonnormal = "HI_CHOL", printToggle = TRUE)[2,3], + ## One space for < + pRankTest) +}) + + +### Gold standard for categorical +outFreq <- svytable( ~ race + RIAGENDR, nhanesSvy) +outPerc <- prop.table(outFreq, margin = 2) +## Take first three rows only as space pading is tedious to reproduce +outFP <- cbind(sprintf("%.3f (%.3f) ", outFreq[,1], (outPerc[,1] * 100)), + sprintf(" %.3f ( %.3f) ", outFreq[,2], (outPerc[,2] * 100)))[1:3,] +outFP2 <- cbind(sprintf(" %.3f (%.3f) ", outFreq[,1], (outPerc[,1] * 100)), + sprintf(" %.3f ( %.3f) ", outFreq[,2], (outPerc[,2] * 100)))[1:3,] +## Test +pChisq <- sprintf(" %.7f", as.vector(svychisq( ~ race + RIAGENDR, nhanesSvy)$p.value)) + + + +test_that("categorical data object and final print outs are numerically correct", { + +### Categorical part + +### Sample sizes +expect_equal(as.vector(print(tab1$CatTable, digits = 3, printToggle = TRUE)[1,1:2]), + sprintf("%.3f", outTotals[,2])) +### Frequencies and percentages +matFP <- print(tab1$CatTable, digits = 3, printToggle = TRUE)[3:5,1:2] +dimnames(matFP) <- NULL +expect_equal(matFP, outFP) + +### p-values +expect_equal(print(tab1$CatTable, pDigits = 7, printToggle = TRUE)[2,3], + pChisq) +}) + + +test_that("mixed data object print outs are numerically correct", { + +### Mixed object +## Mean and SD +expect_equal(as.vector(gsub("^ *", "", print(tab1, pDigits = 7)[2,1:2])), + sprintf("%.2f (%.2f)", outMeans[,2], sqrt(outVar[,2]))) +## normal test +expect_equal(print(tab1, pDigits = 7)[2,3], pTTest) +## nonnormal test +expect_equal(print(tab1, pDigits = 7, nonnormal = "HI_CHOL")[2,3], pTTest) + +## Table +matFP <- print(tab1, catDigits = 3, printToggle = TRUE)[4:6,1:2] +dimnames(matFP) <- NULL +## column by column deleting spaces +expect_equal(matFP[,1], outFP2[,1]) +expect_equal(gsub(" ", "", matFP[,1]), gsub(" ", "", outFP[,1])) +## chisq test +expect_equal(print(tab1, pDigits = 7, nonnormal = "HI_CHOL")[3,3], + pChisq) + +}) + + +test_that("summary method works without errors", { + + ## Expectations + summary(mwOverall) + expect_output(summary(mwOverall), "### Summary of categorical variables ### ") + summary(mwInclNa) + expect_output(summary(mwInclNa), "") + summary(mwByE) + expect_output(summary(mwByE), "2 vs 3") + summary(mwByEC1) + expect_output(summary(mwByEC1), + "Standardize mean differences") + summary(mwContOnlyByEC1) + expect_output(summary(mwContOnlyByEC1), + "Standardize mean differences") + summary(mwCatOnlyByEC1) + expect_output(summary(mwCatOnlyByEC1), + "Standardize mean differences") + +}) diff --git a/vignettes/introduction.Rmd b/vignettes/introduction.Rmd index 65f3280..1471697 100644 --- a/vignettes/introduction.Rmd +++ b/vignettes/introduction.Rmd @@ -1,7 +1,7 @@ --- title: "Introduction to tableone" author: "Kazuki Yoshida" -date: "`r Sys.Date()`" +date: "2014-12-28" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Introduction to tableone} @@ -118,8 +118,10 @@ As you can see in the previous table, when there are two or more groups group co You may be worried about the nonnormal variables and small cell counts in the stage variable. In such a situation, you can use the nonnormal argument like before as well as the exact (test) argument in the print() method. Now kruskal.test() is used for the nonnormal continous variables and fisher.test() is used for categorical variables specified in the exact argument. kruskal.test() is equivalent to wilcox.test() in the two-group case. The column named test is to indicate which p-values were calculated using the non-default tests. +To also show standardized mean differences, use the smd option. + ```{r} -print(tab3, nonnormal = biomarkers, exact = "stage") +print(tab3, nonnormal = biomarkers, exact = "stage", smd = TRUE) ``` ## Exporting @@ -160,5 +162,5 @@ print(tab3$ContTable, nonnormal = biomarkers) -------------------- - Authored by Kazuki Yoshida -- CRAN page: http://cran.r-project.org/web/packages/tableone +- CRAN page: http://cran.r-project.org/package=tableone - github page: https://github.com/kaz-yos/tableone diff --git a/vignettes/smd.Rmd b/vignettes/smd.Rmd new file mode 100644 index 0000000..310788b --- /dev/null +++ b/vignettes/smd.Rmd @@ -0,0 +1,258 @@ +--- +title: "Using standardized mean differences" +author: "Kazuki Yoshida" +date: "2015-08-07" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{Introduction to tableone} + %\VignetteEngine{knitr::rmarkdown} + \usepackage[utf8]{inputenc} +--- + +```{r, message = FALSE, tidy = FALSE, echo = F} +## Create a header using devtools::use_vignette("my-vignette") +## knitr configuration: http://yihui.name/knitr/options#chunk_options +library(knitr) +showMessage <- FALSE +showWarning <- FALSE +set_alias(w = "fig.width", h = "fig.height", res = "results") +opts_chunk$set(comment = "", error= TRUE, warning = showWarning, message = showMessage, + tidy = FALSE, cache = F, echo = T, + fig.width = 10, fig.height = 10, dev.args = list(family = "sans")) + +## R configuration +options(width = 130, scipen = 5) +``` + +## Standardized mean difference + +The standardized (mean) difference is a measure of distance between two group means in terms of one or more variables. In practice it is often used as a balance measure of individual covariates before and after propensity score matching. As it is standardized, comparison across variables on different scales is possible. For definitions see http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3144483/#s11title . + + +Standardized mean differences can be easily calculated with tableone. All standardized mean differences in this package are absolute values, thus, there is no directionality. + + +## Load packages + +```{r} + +## tableone package itself +library(tableone) +## PS matching +library(Matching) +## Weighted analysis +library(survey) +## Reorganizing data +library(reshape2) +## plotting +library(ggplot2) + +``` + + +## Load data + +The right heart catheterization dataset is available at http://biostat.mc.vanderbilt.edu/wiki/Main/DataSets . This dataset was originally used in Connors *et al*. JAMA 1996;276:889-897, and has been made publicly available. + +```{r} + +## Right heart cath dataset +rhc <- read.csv("http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/rhc.csv") + +``` + + +## Unmatched table + +Out of the 50 covariates, 32 have standardized mean differences of greater than 0.1, which is often considered the sign of important covariate imbalance (http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3144483/#s11title ). + +```{r} + +## Covariates +vars <- c("age","sex","race","edu","income","ninsclas","cat1","das2d3pc","dnr1", + "ca","surv2md1","aps1","scoma1","wtkilo1","temp1","meanbp1","resp1", + "hrt1","pafi1","paco21","ph1","wblc1","hema1","sod1","pot1","crea1", + "bili1","alb1","resp","card","neuro","gastr","renal","meta","hema", + "seps","trauma","ortho","cardiohx","chfhx","dementhx","psychhx", + "chrpulhx","renalhx","liverhx","gibledhx","malighx","immunhx", + "transhx","amihx") + +## Construct a table +tabUnmatched <- CreateTableOne(vars = vars, strata = "swang1", data = rhc, test = FALSE) +## Show table with SMD +print(tabUnmatched, smd = TRUE) +## Count covariates with important imbalance +addmargins(table(ExtractSmd(tabUnmatched) > 0.1)) + +``` + + +## Propensity score estimation + +Usually a logistic regression model is used to estimate individual propensity scores. The model here is taken from "How To Use Propensity Score Analysis" (http://www.mc.vanderbilt.edu/crc/workshop_files/2008-04-11.pdf ). Predicted probabilities of being assigned to right heart catherterization, being assigned no right heart catherterization, being assigned to the true assignment, as well as the smaller of the probabilities of being assigned to right heart catherterization or no right heart catherterization are calculated for later use in propensity score matching and weighting. + +```{r} + +## Fit model +psModel <- glm(formula = swang1 ~ age + sex + race + edu + income + ninsclas + + cat1 + das2d3pc + dnr1 + ca + surv2md1 + aps1 + scoma1 + + wtkilo1 + temp1 + meanbp1 + resp1 + hrt1 + pafi1 + + paco21 + ph1 + wblc1 + hema1 + sod1 + pot1 + crea1 + + bili1 + alb1 + resp + card + neuro + gastr + renal + + meta + hema + seps + trauma + ortho + cardiohx + chfhx + + dementhx + psychhx + chrpulhx + renalhx + liverhx + gibledhx + + malighx + immunhx + transhx + amihx, + family = binomial(link = "logit"), + data = rhc) + +## Predicted probability of being assigned to RHC +rhc$pRhc <- predict(psModel, type = "response") +## Predicted probability of being assigned to no RHC +rhc$pNoRhc <- 1 - rhc$pRhc + +## Predicted probability of being assigned to the +## treatment actually assigned (either RHC or no RHC) +rhc$pAssign <- NA +rhc$pAssign[rhc$swang1 == "RHC"] <- rhc$pRhc[rhc$swang1 == "RHC"] +rhc$pAssign[rhc$swang1 == "No RHC"] <- rhc$pNoRhc[rhc$swang1 == "No RHC"] +## Smaller of pRhc vs pNoRhc for matching weight +rhc$pMin <- pmin(rhc$pRhc, rhc$pNoRhc) + +``` + + +## Propensity score matching + +The Matching package can be used for propensity score matching. The logit of propensity score is often used as the matching scale, and the matchign caliper is often 0.2 $\times$ SD(logit(PS)). See http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3144483/#s5title for suggestions. After matching, all the standardized mean differences are below 0.1. + +```{r} + +listMatch <- Match(Tr = (rhc$swang1 == "RHC"), # Need to be in 0,1 + ## logit of PS,i.e., log(PS/(1-PS)) as matching scale + X = log(rhc$pRhc / rhc$pNoRhc), + ## 1:1 matching + M = 1, + ## caliper = 0.2 * SD(logit(PS)) + caliper = 0.2, + replace = FALSE, + ties = TRUE, + version = "fast") +## Extract matched data +rhcMatched <- rhc[unlist(listMatch[c("index.treated","index.control")]), ] + +## Construct a table +tabMatched <- CreateTableOne(vars = vars, strata = "swang1", data = rhcMatched, test = FALSE) +## Show table with SMD +print(tabMatched, smd = TRUE) +## Count covariates with important imbalance +addmargins(table(ExtractSmd(tabMatched) > 0.1)) + +``` + + +## Propensity score matching weight + +The matching weight method is a weighting analogue to the 1:1 pairwise algorithmic matching (http://www.ncbi.nlm.nih.gov/pubmed/23902694 ). An earlier version of the paper is available free (http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.359.4724&rep=rep1&type=pdf ). The matching weight is defined as the smaller of the predicted probabilities of receiving or not receiving the treatment over the predicted probability of being assigned to the arm the patient is actually in. After weighting, all the standardized mean differences are below 0.1. The standardized mean differences in weighted data are explained in http://onlinelibrary.wiley.com/doi/10.1002/sim.6607/full . + +```{r} + +## Matching weight +rhc$mw <- rhc$pMin / rhc$pAssign +## Weighted data +rhcSvy <- svydesign(ids = ~ 1, data = rhc, weights = ~ mw) + +## Construct a table (This is a bit slow.) +tabWeighted <- svyCreateTableOne(vars = vars, strata = "swang1", data = rhcSvy, test = FALSE) +## Show table with SMD +print(tabWeighted, smd = TRUE) +## Count covariates with important imbalance +addmargins(table(ExtractSmd(tabWeighted) > 0.1)) + +``` + + +## Assessing balance before and after matching/weighting + +A plot showing covariate balance is often constructed to demonstrate the balancing effect of matching and/or weighting. Given the same propensity score model, the matching weight method often achieves better covariate balance than matching. + +```{r} + +## Construct a data frame containing variable name and SMD from all methods +dataPlot <- data.frame(variable = names(ExtractSmd(tabUnmatched)), + Unmatched = ExtractSmd(tabUnmatched), + Matched = ExtractSmd(tabMatched), + Weighted = ExtractSmd(tabWeighted)) + +## Create long-format data for ggplot2 +dataPlotMelt <- melt(data = dataPlot, + id.vars = c("variable"), + variable.name = "Method", + value.name = "SMD") + +## Order variable names by magnitude of SMD +varNames <- as.character(dataPlot$variable)[order(dataPlot$Unmatched)] + +## Order factor levels in the same order +dataPlotMelt$variable <- factor(dataPlotMelt$variable, + levels = varNames) + +## Plot using ggplot2 +ggplot(data = dataPlotMelt, mapping = aes(x = variable, y = SMD, + group = Method, color = Method)) + +layer(geom = "line") + +layer(geom = "point") + +layer(geom = "hline", yintercept = 0.1, color = "black", size = 0.1) + +coord_flip() + +theme_bw() + theme(legend.key = element_blank()) + +``` + +To construct a side-by-side table, data can be extracted as a matrix and combined using the print() method, which actually invisibly returns a matrix. + +```{r} + +## Column bind tables +resCombo <- cbind(print(tabUnmatched, printToggle = FALSE), + print(tabMatched, printToggle = FALSE), + print(tabWeighted, printToggle = FALSE)) + +## Add group name row, and rewrite column names +resCombo <- rbind(Group = rep(c("No RHC","RHC"), 3), resCombo) +colnames(resCombo) <- c("Unmatched","","Matched","","Weighted","") +print(resCombo, quote = FALSE) + +``` + + +## Outcome analysis + +The final analysis can be conducted using matched and weighted data. The results from the matching and matching weight are similar. ShowRegTable() function may come in handly. + + +```{r} + +## Unmatched model (unadjsuted) +glmUnmatched <- glm(formula = (death == "Yes") ~ swang1, + family = binomial(link = "logit"), + data = rhc) +## Matched model +glmMatched <- glm(formula = (death == "Yes") ~ swang1, + family = binomial(link = "logit"), + data = rhcMatched) +## Weighted model +glmWeighted <- svyglm(formula = (death == "Yes") ~ swang1, + family = binomial(link = "logit"), + design = rhcSvy) + +## Show results together +resTogether <- list(Unmatched = ShowRegTable(glmUnmatched, printToggle = FALSE), + Matched = ShowRegTable(glmMatched, printToggle = FALSE), + Weighted = ShowRegTable(glmWeighted, printToggle = FALSE)) +print(resTogether, quote = FALSE) + +``` + +-------------------- +- Authored by Kazuki Yoshida +- CRAN page: http://cran.r-project.org/package=tableone +- github page: https://github.com/kaz-yos/tableone