The modelsum function

require(knitr)
require(broom)
require(MASS)
require(pROC)
require(rpart)

opts_chunk$set(comment = NA, echo=TRUE, prompt=TRUE, collapse=TRUE)

Introduction

Very often we are asked to summarize model results from multiple fits into a nice table. The endpoint might be of different types (e.g., survival, case/control, continuous) and there may be several independent variables that we want to examine univariately or adjusted for certain variables such as age and sex. Locally at Mayo, the SAS macros %modelsum, %glmuniv, and %logisuni were written to create such summary tables. With the increasing interest in R, we have developed the function modelsum to create similar tables within the R environment.

In developing the modelsum function, the goal was to bring the best features of these macros into an R function. However, the task was not simply to duplicate all the functionality, but rather to make use of R's strengths (modeling, method dispersion, flexibility in function definition and output format) and make a tool that fits the needs of R users. Additionally, the results needed to fit within the general reproducible research framework so the tables could be displayed within an R markdown report.

This report provides step-by-step directions for using the functions associated with modelsum. All functions presented here are available within the arsenal package. An assumption is made that users are somewhat familiar with R markdown documents. For those who are new to the topic, a good initial resource is available at rmarkdown.rstudio.com.

Simple Example

The first step when using the modelsum function is to load the arsenal package. All the examples in this report use a dataset called mockstudy made available by Paul Novotny which includes a variety of types of variables (character, numeric, factor, ordered factor, survival) to use as examples.

require(arsenal)
data(mockstudy) # load data
dim(mockstudy)  # look at how many subjects and variables are in the dataset 
# help(mockstudy) # learn more about the dataset and variables
str(mockstudy) # quick look at the data

To create a simple linear regression table (the default), use a formula statement to specify the variables that you want summarized. The example below predicts BMI with the variables sex and age.

tab1 <- modelsum(bmi ~ sex + age, data=mockstudy)

If you want to take a quick look at the table, you can use summary on your modelsum object and the table will print out as text in your R console window. If you use summary without any options you will see a number of $\ $ statements which translates to "space" in HTML.

Pretty text version of table

If you want a nicer version in your console window then adding the text=TRUE option.

summary(tab1, text=TRUE)

Pretty Rmarkdown version of table

In order for the report to look nice within an R markdown (knitr) report, you just need to specify results="asis" when creating the r chunk. This changes the layout slightly (compresses it) and bolds the variable names.

summary(tab1)

Data frame version of table

If you want a data.frame version, simply use as.data.frame.

as.data.frame(tab1)

Add an adjustor to the model

The argument adjust allows the user to indicate that all the variables should be adjusted for these terms. To adjust each model for age and sex (for instance), we use adjust = ~ age + sex:

tab2 <- modelsum(alk.phos ~ arm + ps + hgb, adjust= ~age + sex, data=mockstudy)
summary(tab2)

Models for each endpoint type

To make sure the correct model is run you need to specify "family". The options available right now are : gaussian, binomial, survival, and poisson. If there is enough interest, additional models can be added.

Gaussian

Fit and summarize linear regression model

Look at whether there is any evidence that AlkPhos values vary by study arm after adjusting for sex and age (assuming a linear age relationship).

fit <- lm(alk.phos ~ arm + age + sex, data=mockstudy)
summary(fit)
plot(fit)

The results suggest that the endpoint may need to be transformed. Calculating the Box-Cox transformation suggests a log transformation.

require(MASS)
boxcox(fit)
fit2 <- lm(log(alk.phos) ~ arm + age + sex, data=mockstudy)
summary(fit2)
plot(fit2)

Finally, look to see whether there there is a non-linear relationship with age.

require(splines)
fit3 <- lm(log(alk.phos) ~ arm + ns(age, df=2) + sex, data=mockstudy)

# test whether there is a difference between models 
stats::anova(fit2,fit3)

# look at functional form of age
termplot(fit3, term=2, se=T, rug=T)

In this instance it looks like there isn't enough evidence to say that the relationship is non-linear.

Extract data using the broom package

The broom package makes it easy to extract information from the fit.

tmp <- tidy(fit3) # coefficients, p-values
class(tmp)
tmp

glance(fit3)

Create a summary table using modelsum

ms.logy <- modelsum(log(alk.phos) ~ arm + ps + hgb, data=mockstudy, adjust= ~age + sex, 
                    family=gaussian,  
                    gaussian.stats=c("estimate","CI.lower.estimate","CI.upper.estimate","p.value"))
summary(ms.logy)

Binomial

Fit and summarize logistic regression model

boxplot(age ~ mdquality.s, data=mockstudy, ylab=attr(mockstudy$age,'label'), xlab='mdquality.s')

fit <- glm(mdquality.s ~ age + sex, data=mockstudy, family=binomial)
summary(fit)

# create Odd's ratio w/ confidence intervals
tmp <- data.frame(summary(fit)$coef)
tmp

tmp$OR <- round(exp(tmp[,1]),2)
tmp$lower.CI <- round(exp(tmp[,1] - 1.96* tmp[,2]),2)
tmp$upper.CI <- round(exp(tmp[,1] + 1.96* tmp[,2]),2)
names(tmp)[4] <- 'P-value'

kable(tmp[,c('OR','lower.CI','upper.CI','P-value')])

# Assess the predictive ability of the model

# code using the pROC package
require(pROC)
pred <- predict(fit, type='response')
tmp <- pROC::roc(mockstudy$mdquality.s[!is.na(mockstudy$mdquality.s)]~ pred, plot=TRUE, percent=TRUE)
tmp$auc

Extract data using broom package

The broom package makes it easy to extract information from the fit.

tidy(fit, exp=T, conf.int=T) # coefficients, p-values, conf.intervals

glance(fit) # model summary statistics

Create a summary table using modelsum

summary(modelsum(mdquality.s ~ age + bmi, data=mockstudy, adjust=~sex, family=binomial))

fitall <- modelsum(mdquality.s ~ age, data=mockstudy, family=binomial,
                   binomial.stats=c("Nmiss2","OR","p.value"))
summary(fitall)

Survival

Fit and summarize a Cox regression model

require(survival)

# multivariable model with all 3 terms
fit  <- coxph(Surv(fu.time, fu.stat) ~ age + sex + arm, data=mockstudy)
summary(fit)

# check proportional hazards assumption
fit.z <- cox.zph(fit)
fit.z
plot(fit.z[1], resid=FALSE) # makes for a cleaner picture in this case
abline(h=coef(fit)[1], col='red')

# check functional form for age using pspline (penalized spline)
# results are returned for the linear and non-linear components
fit2 <- coxph(Surv(fu.time, fu.stat) ~ pspline(age) + sex + arm, data=mockstudy)
fit2

# plot smoothed age to visualize why significant
termplot(fit2, se=T, terms=1)
abline(h=0)

# The c-statistic comes out in the summary of the fit
summary(fit2)$concordance

# It can also be calculated using the survConcordance function
survConcordance(Surv(fu.time, fu.stat) ~ predict(fit2), data=mockstudy)

Extract data using broom package

The broom package makes it easy to extract information from the fit.

tidy(fit) # coefficients, p-values

glance(fit) # model summary statistics

Create a summary table using modelsum

##Note: You must use quotes when specifying family="survival" 
##      family=survival will not work
summary(modelsum(Surv(fu.time, fu.stat) ~ arm, 
                 adjust=~age + sex, data=mockstudy, family="survival"))

##Note: the pspline term is not working yet
#summary(modelsum(Surv(fu.time, fu.stat) ~ arm, 
#                adjust=~pspline(age) + sex, data=mockstudy, family='survival'))

Poisson

Poisson regression is useful when predicting an outcome variable representing counts. It can also be useful when looking at survival data. Cox models and Poisson models are very closely related and survival data can be summarized using Poisson regression. If you have overdispersion (see if the residual deviance is much larger than degrees of freedom), you may want to use quasipoisson() instead of poisson(). Some of these diagnostics need to be done outside of modelsum.

Example 1: fit and summarize a Poisson regression model

For the first example, use the solder dataset available in the rpart package. The endpoint skips has a definite Poisson look.

require(rpart) ##just to get access to solder dataset
data(solder)
hist(solder$skips)

fit <- glm(skips ~ Opening + Solder + Mask , data=solder, family=poisson)
stats::anova(fit, test='Chi')
summary(fit)

Overdispersion is when the Residual deviance is larger than the degrees of freedom. This can be tested, approximately using the following code. The goal is to have a p-value that is $>0.05$.

1-pchisq(fit$deviance, fit$df.residual)

One possible solution is to use the quasipoisson family instead of the poisson family. This adjusts for the overdispersion.

fit2 <- glm(skips ~ Opening + Solder + Mask, data=solder, family=quasipoisson)
summary(fit2)

Extract data using broom package

The broom package makes it easy to extract information from the fit.

tidy(fit) # coefficients, p-values

glance(fit) # model summary statistics

Create a summary table using modelsum

summary(modelsum(skips~Opening + Solder + Mask, data=solder, family="quasipoisson"))
summary(modelsum(skips~Opening + Solder + Mask, data=solder, family="poisson"))

Example 2: fit and summarize a Poisson regression model

This second example uses the survival endpoint available in the mockstudy dataset. There is a close relationship between survival and Poisson models, and often it is easier to fit the model using Poisson regression, especially if you want to present absolute risk.

# add .01 to the follow-up time (.01*1 day) in order to keep everyone in the analysis
fit <- glm(fu.stat ~ offset(log(fu.time+.01)) + age + sex + arm, data=mockstudy, family=poisson)
summary(fit)
1-pchisq(fit$deviance, fit$df.residual)

coef(coxph(Surv(fu.time,fu.stat) ~ age + sex + arm, data=mockstudy))
coef(fit)[-1]

# results from the Poisson model can then be described as risk ratios (similar to the hazard ratio)
exp(coef(fit)[-1])

# As before, we can model the dispersion which alters the standard error
fit2 <- glm(fu.stat ~ offset(log(fu.time+.01)) + age + sex + arm, 
            data=mockstudy, family=quasipoisson)
summary(fit2)

Extract data using broom package

The broom package makes it easy to extract information from the fit.

tidy(fit) ##coefficients, p-values

glance(fit) ##model summary statistics

Create a summary table using modelsum

Remember that the result from modelsum is different from the fit above. The modelsum summary shows the results for age + offset(log(fu.time+.01)) then sex + offset(log(fu.time+.01)) instead of age + sex + arm + offset(log(fu.time+.01)).

summary(modelsum(fu.stat ~ age, adjust=~offset(log(fu.time+.01))+ sex + arm, 
                 data=mockstudy, family=poisson))

Additional Examples

Here are multiple examples showing how to use some of the different options.

1. Change summary statistics globally

There are standard settings for each type of model regarding what information is summarized in the table. This behavior can be modified using the modelsum.control function. In fact, you can save your standard settings and use that for future tables.

mycontrols  <- modelsum.control(gaussian.stats=c("estimate","std.error","adj.r.squared","Nmiss"),
                                show.adjust=FALSE, show.intercept=FALSE)                            
tab2 <- modelsum(bmi ~ age, adjust=~sex, data=mockstudy, control=mycontrols)
summary(tab2)

You can also change these settings directly in the modelsum call.

tab3 <- modelsum(bmi ~  age, adjust=~sex, data=mockstudy,
                 gaussian.stats=c("estimate","std.error","adj.r.squared","Nmiss"), 
                 show.intercept=FALSE, show.adjust=FALSE)
summary(tab3)

2. Add labels to independent variables

In the above example, age is shown with a label (Age in Years), but sex is listed "as is". This is because the data was created in SAS and in the SAS dataset, age had a label but sex did not. The label is stored as an attribute within R.

## Look at one variable's label
attr(mockstudy$age,'label')

## See all the variables with a label
unlist(lapply(mockstudy,'attr','label'))

## or
cbind(sapply(mockstudy,attr,'label'))

If you want to add labels to other variables, there are a couple of options. First, you could add labels to the variables in your dataset.

attr(mockstudy$age,'label')  <- 'Age, yrs'

tab1 <- modelsum(bmi ~  age, adjust=~sex, data=mockstudy)
summary(tab1)

You can also use the built-in data.frame method for labels<-:

labels(mockstudy)  <- c(age = 'Age, yrs')

tab1 <- modelsum(bmi ~  age, adjust=~sex, data=mockstudy)
summary(tab1)

Another option is to add labels after you have created the table

mylabels <- list(sexFemale = "Female", age ="Age, yrs")
summary(tab1, labelTranslations = mylabels)

Alternatively, you can check the variable labels and manipulate them with a function called labels, which works on the modelsum object.

labels(tab1)
labels(tab1) <- c(sexFemale="Female", age="Baseline Age (yrs)")
labels(tab1)
summary(tab1)

3. Don't show intercept values

summary(modelsum(age~mdquality.s+sex, data=mockstudy), show.intercept=FALSE)

4. Don't show results for adjustment variables

summary(modelsum(mdquality.s ~ age + bmi, data=mockstudy, adjust=~sex, family=binomial),
        show.adjust=FALSE)  

5. Summarize multiple variables without typing them out

Often one wants to summarize a number of variables. Instead of typing by hand each individual variable, an alternative approach is to create a formula using the paste command with the collapse="+" option.

# create a vector specifying the variable names
myvars <- names(mockstudy)

# select the 8th through the 12th
# paste them together, separated by the + sign
RHS <- paste(myvars[8:12], collapse="+")
RHS

# create a formula using the as.formula function
as.formula(paste('mdquality.s ~ ', RHS))

# use the formula in the modelsum function
summary(modelsum(as.formula(paste('mdquality.s ~', RHS)), family=binomial, data=mockstudy))

These steps can also be done using the formulize function.

## The formulize function does the paste and as.formula steps
tmp <- formulize('mdquality.s',myvars[8:10])
tmp

## More complex formulas could also be written using formulize
tmp2 <- formulize('mdquality.s',c('ps','hgb','sqrt(bmi)'))

## use the formula in the modelsum function
summary(modelsum(tmp, data=mockstudy, family=binomial))

6. Subset the dataset used in the analysis

Here are two ways to get the same result (limit the analysis to subjects age>50 and in the F: FOLFOX treatment group).

newdata <- subset(mockstudy, subset=age>50 & arm=='F: FOLFOX', select = c(age,sex, bmi:alk.phos))
dim(mockstudy)
table(mockstudy$arm)
dim(newdata)
names(newdata)
summary(modelsum(alk.phos ~ ., data=newdata))
summary(modelsum(log(alk.phos) ~ sex + ps + bmi, subset=age>50 & arm=="F: FOLFOX", data=mockstudy))
summary(modelsum(alk.phos ~ ps + bmi, adjust=~sex, subset = age>50 & bmi<24, data=mockstudy))
summary(modelsum(alk.phos ~ ps + bmi, adjust=~sex, subset=1:30, data=mockstudy))

7. Create combinations of variables on the fly

## create a variable combining the levels of mdquality.s and sex
with(mockstudy, table(interaction(mdquality.s,sex)))
summary(modelsum(age ~ interaction(mdquality.s,sex), data=mockstudy))

8. Transform variables on the fly

Certain transformations need to be surrounded by I() so that R knows to treat it as a variable transformation and not some special model feature. If the transformation includes any of the symbols / - + ^ * then surround the new variable by I().

summary(modelsum(arm=="F: FOLFOX" ~ I(age/10) + log(bmi) + mdquality.s,
                 data=mockstudy, family=binomial))

9. Change the ordering of the variables or delete a variable

mytab <- modelsum(bmi ~ sex + alk.phos + age, data=mockstudy)
mytab2 <- mytab[c('age','sex','alk.phos')]
summary(mytab2)
summary(mytab[c('age','sex')])
summary(mytab[c(3,1)])

10. Merge two modelsum objects together

It is possible to merge two modelsum objects so that they print out together, however you need to pay attention to the columns that are being displayed. It is sometimes easier to combine two models of the same family (such as two sets of linear models). Overlapping y-variables will have their x-variables concatenated, and (if all=TRUE) non-overlapping y-variables will have their tables printed separately.

## demographics
tab1 <- modelsum(bmi ~ sex + age, data=mockstudy)
## lab data
tab2 <- modelsum(mdquality.s ~ hgb + alk.phos, data=mockstudy, family=binomial)

tab12 <- merge(tab1, tab2, all = TRUE)
class(tab12)
summary(tab12)

11. Add a title to the table

When creating a pdf the tables are automatically numbered and the title appears below the table. In Word and HTML, the titles appear un-numbered and above the table.

t1 <- modelsum(bmi ~ sex + age, data=mockstudy)
summary(t1, title='Demographics')

12. Modify how missing values are treated

Depending on the report you are writing you have the following options:

## look at how many missing values there are for each variable
apply(is.na(mockstudy),2,sum)
## Show how many subjects have each variable (non-missing)
summary(modelsum(bmi ~ ast + age, data=mockstudy,
                control=modelsum.control(gaussian.stats=c("N","estimate"))))

## Always list the number of missing values
summary(modelsum(bmi ~ ast + age, data=mockstudy,
                control=modelsum.control(gaussian.stats=c("Nmiss2","estimate"))))

## Only show the missing values if there are some (default)
summary(modelsum(bmi ~ ast + age, data=mockstudy, 
                control=modelsum.control(gaussian.stats=c("Nmiss","estimate"))))

## Don't show N at all
summary(modelsum(bmi ~ ast + age, data=mockstudy, 
                control=modelsum.control(gaussian.stats=c("estimate"))))

13. Modify the number of digits used

Within modelsum.control function there are 3 options for controlling the number of significant digits shown.

summary(modelsum(bmi ~ sex + age + fu.time, data=mockstudy), digits=4, digits.test=2)

14. Use case-weights in the models

Occasionally it is of interest to fit models using case weights. The modelsum function allows you to pass on the weights to the models and it will do the appropriate fit.

mockstudy$agegp <- cut(mockstudy$age, breaks=c(18,50,60,70,90), right=FALSE)

## create weights based on agegp and sex distribution
tab1 <- with(mockstudy,table(agegp, sex))
tab1
tab2 <- with(mockstudy, table(agegp, sex, arm))
gpwts <- rep(tab1, length(unique(mockstudy$arm)))/tab2

## apply weights to subjects
index <- with(mockstudy, cbind(as.numeric(agegp), as.numeric(sex), as.numeric(as.factor(arm)))) 
mockstudy$wts <- gpwts[index]

## show weights by treatment arm group
tapply(mockstudy$wts,mockstudy$arm, summary)
mockstudy$newvarA <- as.numeric(mockstudy$arm=='A: IFL')
tab1 <- modelsum(newvarA ~ ast + bmi + hgb, data=mockstudy, subset=(arm !='G: IROX'), 
                 family=binomial)
summary(tab1, title='No Case Weights used')

suppressWarnings({
tab2 <- modelsum(newvarA ~ ast + bmi + hgb, data=mockstudy, subset=(arm !='G: IROX'), 
                 weights=wts, family=binomial)
summary(tab2, title='Case Weights used')
})

15. Use modelsum within an Sweave document

For those users who wish to create tables within an Sweave document, the following code seems to work.

\documentclass{article}

\usepackage{longtable}
\usepackage{pdfpages}

\begin{document}

\section{Read in Data}
<<echo=TRUE>>=
require(arsenal)
require(knitr)
require(rmarkdown)
data(mockstudy)

tab1 <- modelsum(bmi~sex+age, data=mockstudy)
@

\section{Convert Summary.modelsum to LaTeX}
<<echo=TRUE, results='hide', message=FALSE>>=
capture.output(summary(tab1), file="Test.md")

## Convert R Markdown Table to LaTeX
render("Test.md", pdf_document(keep_tex=TRUE))
@ 

\includepdf{Test.pdf}

\end{document}

16. Export modelsum results to a .CSV file

When looking at multiple variables it is sometimes useful to export the results to a csv file. The as.data.frame function creates a data frame object that can be exported or further manipulated within R.

summary(tab2, text=T)
tmp <- as.data.frame(summary(tab2, text = TRUE))
tmp
# write.csv(tmp, '/my/path/here/mymodel.csv')

17. Write modelsum object to a separate Word or HTML file

## write to an HTML document
write2html(tab2, "~/ibm/trash.html")

## write to a Word document
write2word(tab2, "~/ibm/trash.doc", title="My table in Word")

18. Use modelsum in R Shiny

The easiest way to output a modelsum() object in an R Shiny app is to use the tableOutput() UI in combination with the renderTable() server function and as.data.frame(summary(modelsum())):

# A standalone shiny app
library(shiny)
library(arsenal)
data(mockstudy)

shinyApp(
  ui = fluidPage(tableOutput("table")),
  server = function(input, output) {
    output$table <- renderTable({
      as.data.frame(summary(modelsum(age ~ sex, data = mockstudy), text = "html"))
    }, sanitize.text.function = function(x) x)
  }
)

This can be especially powerful if you feed the selections from a selectInput(multiple = TRUE) into formulize() to make the table dynamic!

23. Use modelsum in bookdown

Since the backbone of modelsum() is knitr::kable(), tables still render well in bookdown. However, print.summary.modelsum() doesn't use the caption= argument of kable(), so some tables may not have a properly numbered caption. To fix this, use the method described on the bookdown site to give the table a tag/ID.

summary(modelsum(age ~ sex, data = mockstudy), title="(\\#tab:mytableby) Caption here")

24. Model multiple endpoints

You can now use list() on the left-hand side of modelsum() to give multiple endpoints. Note that only one "family" can be specified this way (use merge() instead if you want multiple families).

summary(modelsum(list(age, hgb) ~ bmi + sex, adjust = ~ arm, data = mockstudy))

To avoid confusion about which table is which endpoint, you can set term.name=TRUE in summary(). This takes the labels for each endpoint and puts them in the top-left of the table.

summary(modelsum(list(age, hgb) ~ bmi + sex, adjust = ~ arm, data = mockstudy), term.name = TRUE)

25. Model data by a non-test group (strata)

You can also specify a grouping variable that doesn't get tested (but instead separates results): a strata variable.

summary(modelsum(list(age, hgb) ~ bmi + sex, strata = arm, data = mockstudy))

26. Add multiple sets of adjustors to the model

By putting multiple formulas into a list, you can use multiple sets of adjustors. Use ~ 1 or NULL for an "unadjusted" model. By using the adjustment.names=TRUE argument and giving names to your adjustor sets in the list, you can name the various analyses.

adj.list <- list(
  Unadjusted = ~ 1, # can also specify NULL here
  "Adjusted for Arm" = ~ arm
)
multi.adjust <- modelsum(list(age, bmi) ~ fu.time + ast, adjust = adj.list, data = mockstudy)
summary(multi.adjust, adjustment.names = TRUE)
summary(multi.adjust, adjustment.names = TRUE, show.intercept = FALSE, show.adjust = FALSE)

Available Function Options

Summary statistics

The available summary statistics, by varible type, are:

The full description of these parameters that can be shown for models include:

modelsum.control settings

A quick way to see what arguments are possible to utilize in a function is to use the args() command. Settings involving the number of digits can be set in modelsum.control or in summary.modelsum.

args(modelsum.control)

summary.modelsum settings

The summary.modelsum function has options that modify how the table appears (such as adding a title or modifying labels).

args(arsenal:::summary.modelsum)


Try the arsenal package in your browser

Any scripts or data that you put into this service are public.

arsenal documentation built on June 5, 2021, 1:06 a.m.