Skip to content

Commit

Permalink
Merge branch 'release/0.9.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
kaz-yos committed Nov 5, 2017
2 parents fe1671e + 9b4b4a2 commit 65970fd
Show file tree
Hide file tree
Showing 21 changed files with 491 additions and 97 deletions.
8 changes: 5 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: tableone
Type: Package
Title: Create 'Table 1' to Describe Baseline Characteristics
Version: 0.8.1
Date: 2017-06-16
Version: 0.9.0
Date: 2017-11-03
Author: Kazuki Yoshida, Justin Bohn.
Maintainer: Kazuki Yoshida <[email protected]>
Description: Creates 'Table 1', i.e., description of baseline patient
Expand All @@ -20,7 +20,9 @@ Imports:
e1071,
zoo,
gmodels,
nlme
nlme,
lmerTest,
labelled
Suggests:
survival,
testthat,
Expand Down
30 changes: 30 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
tableone 0.9.0 (2017-11-03)
----------------------------------------------------------------

NEW FEATURES

* The "varLabels" option for the print.TableOne method was added.
When TRUE, instead of printing the variable names, their
corresponding variable labels are used. Variable labels must be
stored in the data frame to be used via labelled::var_label
function. This option is also available in ExtractSmd function.

* The "dropEqual" option for the print methods was implemented. If
TRUE, the level description for two-level variables such as " = 1"
and " = TRUE" are not shown. This can obscure what level is being
shown depending on the variable naming scheme, thus, should only
be used after the initial results were checked for correctness.

BUG FIXES

* Handling of lme4 models was improved in ShowRegTable.


tableone 0.8.2 (2017-08-31)
----------------------------------------------------------------

BUG FIXES

* Handle labelled class as categorical.


tableone 0.8.1 (2017-06-17)
----------------------------------------------------------------

Expand Down
10 changes: 8 additions & 2 deletions R/CreateTableOne.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ function(vars, # character vector of variab
## Abort if no variables exist at this point
ModuleStopIfNoVarsLeft(vars)

## Get variable labels (named list of label string or NULL)
## Need to occur before applying factor().
varLabels <- labelled::var_label(data[vars])

## Factor conversions if the factorVars argument exist
if (!missing(factorVars)) {
## Check if variables exist. Drop them if not.
Expand All @@ -149,7 +153,7 @@ function(vars, # character vector of variab

## Classify as varFactors if any one of these classes are contained
varFactors <-sapply(varClasses, function(VEC) {
any(VEC %in% c("factor", "ordered", "logical", "character"))
any(VEC %in% c("factor", "ordered", "logical", "character", "labelled"))
})
varFactors <- names(varFactors)[varFactors]

Expand Down Expand Up @@ -243,7 +247,9 @@ function(vars, # character vector of variab
varFactors = varFactors,
varNumerics = varNumerics,
## Missing data percentage for each variable (no strata).
percentMissing = percentMissing))
percentMissing = percentMissing,
## Variable labels
varLabels = varLabels))

## Give a class
class(TableOneObject) <- "TableOne"
Expand Down
19 changes: 17 additions & 2 deletions R/ExtractSmd.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
##'
##'
##' @param x A stratified (svy)TableOne object containing standardized mean differences.
##' @param varLabels Whether to replace variable names with variable labels obtained from \code{labelled::var_label()} function.
##'
##' @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
Expand All @@ -13,7 +15,7 @@
##' ## See examples for CreateTableOne and svyCreateTableOne
##'
##' @export
ExtractSmd <- function(x) {
ExtractSmd <- function(x, varLabels = FALSE) {

if (class(x)[1] %in% c("TableOne","svyTableOne")) {

Expand All @@ -22,7 +24,20 @@ ExtractSmd <- function(x) {
attr(x$CatTable, "smd"))

## Order by table variable order
matSmd[x$MetaData$vars,]
matSmd <- matSmd[x$MetaData$vars,]

## Use variable labels if requested.
if (varLabels) {
for (i in seq_along(x$MetaData$vars)) {
if (!is.null(x$MetaData$varLabels[[i]])) {
## If the corresponding variable label is non-null replace
rownames(matSmd)[i] <- x$MetaData$varLabels[[i]]
}
}
}

## Return manipulated matrix
matSmd

} else if (class(x)[1] %in% c("ContTable","svyContTable","CatTable","svyCatTable")) {

Expand Down
18 changes: 14 additions & 4 deletions R/ShowRegTable.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ ShowRegTable <- function(model, exp = TRUE, digits = 2, pDigits = 3, printToggle
## nlme needs special handling
## Use column 2 because it is the point estimate
modelCoef <- nlme::intervals(model)[[1]][, 2]
} else if (any(class(model) %in% c("lmerMod","glmerMod"))) {
} else if (any(class(model) %in% c("lmerMod","glmerMod","merModLmerTest"))) {
## (g)lmer gives confint for other extra parameters
## lmerTest::lmer() gives a merModLmerTest object.
modelCoef <- coef(summary(model))[,1]
} else {
modelCoef <- coef(model)
Expand All @@ -58,17 +59,26 @@ ShowRegTable <- function(model, exp = TRUE, digits = 2, pDigits = 3, printToggle
## nlme needs special handling
## Drop column 2 because it is the point estimate
modelConfInt <- nlme::intervals(model)[[1]][, -2]
} else if (any(class(model) %in% c("lmerMod","glmerMod"))) {
## (g)lmer gives confint for other extra parameters
} else if (any(class(model) %in% c("lmerMod","glmerMod","merModLmerTest"))) {
## (g)lmer gives confint for other extra parameters.
## The bottom ones are for fixed effects.
modelConfInt <- tail(suppressMessages(ciFun(model)), length(modelCoef))
} else {
modelConfInt <- suppressMessages(ciFun(model))
}

## P-value extraction
## Extract p-values
if (any(class(model) %in% c("gls", "lme"))) {
## nlme needs special handling
modelSummaryMat <- summary(model)$tTable
} else if (any(class(model) %in% c("lmerMod"))) {
## lmerMod does not have p-values, add NA's
modelSummaryMat <- coef(summary(model))
modelSummaryMat <- cbind(modelSummaryMat,
rep(NA, nrow(modelSummaryMat)))
} else if (any(class(model) %in% c("merModLmerTest"))) {
## Need to specify explicitly to invoke the correct summary method.
modelSummaryMat <- coef(lmerTest::summary(model))
} else {
modelSummaryMat <- coef(summary(model))
}
Expand Down
17 changes: 11 additions & 6 deletions R/modules-print.R
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ ModuleContFormatStrata <- function(ContTable, nVars, listOfFunctions, digits) {
################################################################################

## Module to loop over variables within a stratum formatting categorical variables
ModuleCatFormatVariables <- function(lstVars, varsToFormat, fmt, level, cramVars, showAllLevels) {
ModuleCatFormatVariables <- function(lstVars, varsToFormat, fmt, level, cramVars, dropEqual, showAllLevels) {

## Loop over variables within a stratum
## Each list element is a data frame summarizing levels
Expand Down Expand Up @@ -378,9 +378,12 @@ ModuleCatFormatVariables <- function(lstVars, varsToFormat, fmt, level, cramVars
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)
## Otherwise, keep the second level only.
## Change variable name if dropEqual = FALSE.
if (!dropEqual) {
DF$var <- sprintf("%s = %s", DF$var, DF$level)
}
## Delete the first level.
DF <- DF[-1, , drop = FALSE]
}

Expand Down Expand Up @@ -412,7 +415,7 @@ ModuleCatFormatVariables <- function(lstVars, varsToFormat, fmt, level, cramVars


## Module to loop over strata formatting categorical variables
ModuleCatFormatStrata <- function(CatTable, digits, varsToFormat, cramVars, showAllLevels) {
ModuleCatFormatStrata <- function(CatTable, digits, varsToFormat, cramVars, dropEqual, showAllLevels) {

## Create format for percent used in the loop
fmt1 <- paste0("%.", digits, "f")
Expand All @@ -433,6 +436,7 @@ ModuleCatFormatStrata <- function(CatTable, digits, varsToFormat, cramVars, show
varsToFormat = varsToFormat,
fmt = fmt1,
cramVars = cramVars,
dropEqual = dropEqual,
showAllLevels = showAllLevels)


Expand Down Expand Up @@ -632,7 +636,7 @@ ModuleFormatTables <- function(x, catDigits, contDigits,
explain, pDigits,
## print.CatTable arguments passed
format, exact,
showAllLevels, cramVars,
showAllLevels, cramVars, dropEqual,
## print.ContTable arguments passed
nonnormal, minMax, insertLevel
) {
Expand Down Expand Up @@ -680,6 +684,7 @@ ModuleFormatTables <- function(x, catDigits, contDigits,
format = format, exact = exact,
showAllLevels = showAllLevels, # Returns one more column if TRUE
cramVars = cramVars,
dropEqual = dropEqual,

## print.ContTable arguments passed
nonnormal = nonnormal, minMax = minMax, insertLevel = showAllLevels
Expand Down
3 changes: 3 additions & 0 deletions R/print.CatTable.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
##' @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 dropEqual Whether to drop " = second level name" description indicating which level is shown for two-level categorical variables.
##' @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}.
Expand Down Expand Up @@ -95,6 +96,7 @@ function(x, # CatTable object
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
dropEqual = FALSE, # Do not show " = second level" for two-level variables

test = TRUE, # Whether to add p-values
exact = NULL, # Which variables should be tested with exact tests
Expand Down Expand Up @@ -163,6 +165,7 @@ function(x, # CatTable object
digits = digits,
varsToFormat = varsToFormat,
cramVars = cramVars,
dropEqual = dropEqual,
showAllLevels = showAllLevels)


Expand Down
29 changes: 28 additions & 1 deletion R/print.TableOne.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
##' @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 padColnames Whether to pad column names with spaces to center justify. The default is FALSE. It is not conducted if noSpaces = TRUE.
##' @param varLabels Whether to replace variable names with variable labels obtained from \code{labelled::var_label()} function.
##' @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 dropEqual Whether to drop " = second level name" description indicating which level is shown for two-level categorical variables.
##' @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 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.
Expand All @@ -43,11 +45,13 @@ function(x, # TableOne object
smd = FALSE, # Whether to add standardized mean differences
noSpaces = FALSE, # Whether to remove spaces for alignments
padColnames = FALSE, # Whether to pad column names for alignments
varLabels = FALSE, # Whether to show variable labels instead of names.

## 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
dropEqual = FALSE, # Do not show " = second level" for two-level variables
exact = NULL, # Which variables should be tested with exact tests

## Continuous options
Expand All @@ -67,6 +71,7 @@ function(x, # TableOne object
## Returns one more column if TRUE
showAllLevels = showAllLevels,
cramVars = cramVars,
dropEqual = dropEqual,

## print.ContTable arguments passed
nonnormal = nonnormal, minMax = minMax,
Expand All @@ -91,10 +96,32 @@ function(x, # TableOne object
spcFmtEltTables <- ModuleAddSpacesToTable(FmtElementTables, nSpacesToAdd, showAllLevels)


## Create a list of one variable tables excluding sample size row
## Create a list of one variable tables excluding sample size row.
## This is based on the variable order in the MetaData.
lstOneVarTables <- ModuleListOfOneVarTables(spcFmtEltTables,
MetaData = x$MetaData)

## Replace variable names with variable labels if requested.
## Loop over the variable replacing its name with its label.
if (varLabels) {
lstOneVarTables <-
lapply(seq_along(lstOneVarTables),
function(i) {
## Each element is a string matrix.
mat <- lstOneVarTables[[i]]
## Manipulate if a non-NULL label is available.
## Note MetaData$varLabels is a list.
if (!is.null(x$MetaData$varLabels[[i]])) {
## The first row name contains the variable name
## without preceding space. Replace by exact matching.
rownames(mat)[1] <- gsub(paste0("^", x$MetaData$vars[i]),
x$MetaData$varLabels[[i]],
rownames(mat)[1])
}
## Return the entire matrix.
mat
})
}

## Check if the first row is CatTable element
## if so, pick sample size row from space-padded CatTable element
Expand Down
3 changes: 3 additions & 0 deletions R/print.svyCatTable.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
##' @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 dropEqual Whether to drop " = second level name" description indicating which level is shown for two-level categorical variables.
##' @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}.
Expand Down Expand Up @@ -40,6 +41,7 @@ function(x, # CatTable object
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
dropEqual = FALSE, # Do not show " = second level" for two-level variables

test = TRUE, # Whether to add p-values
exact = NULL, # Which variables should be tested with exact tests
Expand Down Expand Up @@ -109,6 +111,7 @@ function(x, # CatTable object
digits = digits,
varsToFormat = varsToFormat,
cramVars = cramVars,
dropEqual = dropEqual,
showAllLevels = showAllLevels)


Expand Down
Loading

0 comments on commit 65970fd

Please sign in to comment.