Title: | A Toolbox for Writing Pretty Papers and Reports |
---|---|
Description: | A toolbox for writing 'knitr', 'Sweave' or other 'LaTeX'- or 'markdown'-based reports and to prettify the output of various estimated models. |
Authors: | Benjamin Hofner, with contributions by many others (see inst/CONTRIBUTIONS) |
Maintainer: | Benjamin Hofner <[email protected]> |
License: | GPL-2 |
Version: | 1.0-5 |
Built: | 2024-11-19 04:25:57 UTC |
Source: | https://github.com/hofnerb/paper |
A toolbox for writing knitr, Sweave or other LaTeX- or markdown-based reports and to prettify the output of various estimated models.
Package: | papeR |
Type: | Package |
Version: | 1.0-5 |
Date: | 2021-03-19 |
License: | GPL-2 |
Version 1.0-0 is based on a completely refactored code base. Some
functions from previsous versions are deprecated. New functions to
create summary tables exist (see summarize
. The package now
also provides a vignette and was extensively tested using
testthat
.
For news and changes see news(package = "papeR")
.
Benjamin Hofner
Maintainer: Benjamin Hofner <[email protected]>
This is a wrapper to anova.lme
from package nlme
and is coded similar to Anova
from car as it
produces marginal tests by default.
## S3 method for class 'lme' Anova(mod, type = c("marginal", "sequential"), ...)
## S3 method for class 'lme' Anova(mod, type = c("marginal", "sequential"), ...)
mod |
linear mixed model fitted with package nlme. |
type |
type of anova, either marginal (default) or sequential. |
... |
further arguments to be passed to |
Anova
(package car)
## Example requires package nlme to be installed and loaded if (require("nlme")) { ## Load data set Orthodont data(Orthodont, package = "nlme") ## Fit a model for distance with random intercept for Subject mod <- lme(distance ~ age + Sex, data = Orthodont, random = ~ 1 | Subject) Anova(mod) }
## Example requires package nlme to be installed and loaded if (require("nlme")) { ## Load data set Orthodont data(Orthodont, package = "nlme") ## Fit a model for distance with random intercept for Subject mod <- lme(distance ~ age + Sex, data = Orthodont, random = ~ 1 | Subject) Anova(mod) }
Compute confidence intervals for mixed models from package lme4 (prior to version 1.0). This function is only needed for backward compatibility.
## S3 method for class 'mer' confint(object, parm, level = 0.95, simulate = c("ifneeded", TRUE, FALSE), B = 1000,...)
## S3 method for class 'mer' confint(object, parm, level = 0.95, simulate = c("ifneeded", TRUE, FALSE), B = 1000,...)
object |
Model of class |
parm |
Parameters to be included in the confidence interval. See
|
level |
the confidence level. |
simulate |
If “"ifneeded"” is specified (default), simulated confidence
intervals are returned if (and only if) no z-value exists in the
corresponding |
B |
number of samples to take in |
... |
Additional arguments. Currently not used. |
Matrix with confidence intervals.
Benjamin Hofner, partially based on code from package stats. See source code for documentation.
table.cont
and
table.fac
objects Helper function to extract printing options from table.cont
and
table.fac
objects as produced by latex.table.cont
and
latex.table.fac
.
get_option(object, name)
get_option(object, name)
object |
|
name |
name of the option, e.g. |
Option.
Benjamin Hofner
latex.table.cont
and latex.table.fac
Labels can be stored as an attribute "variable.label"
for each
variable in a data set using the assignment function. With the
extractor function one can assess these labels.
## S3 method for class 'data.frame' labels(object, which = NULL, abbreviate = FALSE, ...) ## assign labels labels(data, which = NULL) <- value ## check if data.frame is a special labeled data.frame ('ldf') is.ldf(object) ## convert object to labeled data.frame ('ldf') convert.labels(object) as.ldf(object, ...) ## special plotting function for labeled data.frames ('ldf') ## S3 method for class 'ldf' plot(x, variables = names(x), labels = TRUE, by = NULL, with = NULL, regression.line = TRUE, line.col = "red", ...)
## S3 method for class 'data.frame' labels(object, which = NULL, abbreviate = FALSE, ...) ## assign labels labels(data, which = NULL) <- value ## check if data.frame is a special labeled data.frame ('ldf') is.ldf(object) ## convert object to labeled data.frame ('ldf') convert.labels(object) as.ldf(object, ...) ## special plotting function for labeled data.frames ('ldf') ## S3 method for class 'ldf' plot(x, variables = names(x), labels = TRUE, by = NULL, with = NULL, regression.line = TRUE, line.col = "red", ...)
object |
a |
data |
a |
which |
either a number indicating the label to extract or a character
string with the variable name for which the label should be
extracted. One can also use a vector of numerics or character
strings to extract mutiple labels. If |
value |
a vector containing the labels (in the order of the variables). If which is given, only the corresponding subset is labeled. Note that all other labels contain the variable name as label afterwards. |
abbreviate |
logical (default: |
... |
further options passed to function In In |
x |
a labeled |
variables |
character vector or numeric vector defining (continuous) variables
that should be included in the table. Per default, all numeric and
factor variables of |
labels |
labels for the variables. If |
by |
a character or numeric value specifying a variable in the data set.
This variable can be either a grouping |
with |
a character or numeric value specifying a numeric variable
|
regression.line |
a logical argument specifying if a regression line should be added
to scatter plots (which are plotted if both |
line.col |
the color of the regression line. |
All labels are stored as attributes of the columns of the data frame, i.e., each variable has (up to) one attribute which contains the variable lable.
One can set or extract labels from data.frame
objects.
If no labels are specified labels(data)
returns the column
names of the data frame.
Using abbreviate = TRUE
, all labels are abbreviated to (at
least) 4 characters such that they are unique. Other minimal lengths
can specified by setting minlength
(see examples below).
Univariate plots can be easily obtained for all numeric and factor
variables in a data set data
by using plot(data)
.
Bivariate plots can be obtained by specifying by
. In case of a
factor variable, grouped boxplot
s or spineplot
s are
plotted depending on the class of the variable specified in
variables
. In case of a numeric variable, grouped
boxplot
s or scatter plots are plotted depending on the
class of the variable specified in variables
. Note that one
cannot specify by
and with
at the same time (as they are
internally identical). Note that missings are excluded plot wise (also
for bivariate plots).
labels(data)
returns a named vector of variable labels, where
the names match the variable names and the values represent the labels.
If a data set is generated by read.spss
in package
foreign, labels are stored in a single attribute of the data
set. Assigning new labels, e.g. via labels(data) <-
labels(data)
removes this attribute and stores all labels as
attributes of the variables. Alternatively one can use
data <- convert.labels(data)
.
Benjamin Hofner
read.spss
in package foreign
############################################################ ### Basic labels manipulations data <- data.frame(a = 1:10, b = 10:1, c = rep(1:2, 5)) labels(data) ## only the variable names is.ldf(data) ## not yet ## now set labels labels(data) <- c("my_a", "my_b", "my_c") ## one gets a named character vector of labels labels(data) ## data is now a ldf: is.ldf(data) ## Altervatively one could use as.ldf(data) or convert.labels(data); ## This would keep the default labels but set the class ## correctly. ## set labels for a and b only ## Note that which represents the variable names! labels(data, which = c("a", "b")) <- c("x", "y") labels(data) ## reset labels (to variable names): labels(data) <- NULL labels(data) ## set label for a only and use default for other labels: labels(data, which = "a") <- "x" labels(data) ## attach label for new variable: data2 <- data data2$z <- as.factor(rep(2:3, each = 5)) labels(data2) ## no real label for z, only variable name labels(data2, which = "z") <- "new_label" labels(data2) ############################################################ ### Abbreviate labels ## attach long labels to data labels(data) <- c("This is a long label", "This is another long label", "This also") labels(data) labels(data, abbreviate = TRUE, minlength = 10) ############################################################ ### Data manipulations ## reorder dataset: tmp <- data2[, c(1, 4, 3, 2)] labels(tmp) ## labels are kept and order is updated ## subsetting to single variables: labels(tmp[, 2]) ## not working as tmp[, 2] drops to vector ## note that the label still exists but cannot be extracted ## using labels.default() str(tmp[, 2]) labels(tmp[, 2, drop = FALSE]) ## prevent dropping ## one can also cbind labeled data.frame objects: labels(cbind(data, tmp[, 2])) ## or better: labels(cbind(data, tmp[, 2, drop = FALSE])) ## or rbind labeled.data.set objects: labels(rbind(data, tmp[, -2])) ############################################################ ### Plotting data sets ## plot the data auto"magically"; numerics as boxplot, factors as barplots par(mfrow = c(2,2)) plot(data2) ## a single plot plot(data2, variables = "a") ## grouped plot plot(data2, variables = "a", by = "z") ## make "c" a factor and plot "c" vs. "z" data2$c <- as.factor(data2$c) plot(data2, variables = "c", by = "z") ## the same plot(data2, variables = 3, by = 4) ## plot everithing against "b" ## (grouped boxplots, stacked barplots or scatterplots) plot(data2, with = "b")
############################################################ ### Basic labels manipulations data <- data.frame(a = 1:10, b = 10:1, c = rep(1:2, 5)) labels(data) ## only the variable names is.ldf(data) ## not yet ## now set labels labels(data) <- c("my_a", "my_b", "my_c") ## one gets a named character vector of labels labels(data) ## data is now a ldf: is.ldf(data) ## Altervatively one could use as.ldf(data) or convert.labels(data); ## This would keep the default labels but set the class ## correctly. ## set labels for a and b only ## Note that which represents the variable names! labels(data, which = c("a", "b")) <- c("x", "y") labels(data) ## reset labels (to variable names): labels(data) <- NULL labels(data) ## set label for a only and use default for other labels: labels(data, which = "a") <- "x" labels(data) ## attach label for new variable: data2 <- data data2$z <- as.factor(rep(2:3, each = 5)) labels(data2) ## no real label for z, only variable name labels(data2, which = "z") <- "new_label" labels(data2) ############################################################ ### Abbreviate labels ## attach long labels to data labels(data) <- c("This is a long label", "This is another long label", "This also") labels(data) labels(data, abbreviate = TRUE, minlength = 10) ############################################################ ### Data manipulations ## reorder dataset: tmp <- data2[, c(1, 4, 3, 2)] labels(tmp) ## labels are kept and order is updated ## subsetting to single variables: labels(tmp[, 2]) ## not working as tmp[, 2] drops to vector ## note that the label still exists but cannot be extracted ## using labels.default() str(tmp[, 2]) labels(tmp[, 2, drop = FALSE]) ## prevent dropping ## one can also cbind labeled data.frame objects: labels(cbind(data, tmp[, 2])) ## or better: labels(cbind(data, tmp[, 2, drop = FALSE])) ## or rbind labeled.data.set objects: labels(rbind(data, tmp[, -2])) ############################################################ ### Plotting data sets ## plot the data auto"magically"; numerics as boxplot, factors as barplots par(mfrow = c(2,2)) plot(data2) ## a single plot plot(data2, variables = "a") ## grouped plot plot(data2, variables = "a", by = "z") ## make "c" a factor and plot "c" vs. "z" data2$c <- as.factor(data2$c) plot(data2, variables = "c", by = "z") ## the same plot(data2, variables = 3, by = 4) ## plot everithing against "b" ## (grouped boxplots, stacked barplots or scatterplots) plot(data2, with = "b")
The function produces LaTeX tables with summary statistics for continous variables. It makes use of the booktabs package in LaTeX to obtain tables with a nice layout.
latex.table.cont(..., caption = NULL, label = NULL, table = c("tabular", "longtable"), align = NULL, floating = FALSE, center = TRUE)
latex.table.cont(..., caption = NULL, label = NULL, table = c("tabular", "longtable"), align = NULL, floating = FALSE, center = TRUE)
... |
arguments for |
caption |
(optional) character string. Caption of LaTeX table. Note that captions are suported for all tables (see also details below). |
label |
(optional) character string. Label of LaTeX table specified as
|
table |
character string. LaTeX table format, currently either
|
align |
character string. LaTeX alignment of table rows, per default
|
floating |
logical (default: |
center |
logical (default: |
This function is deprecated and only available for backward
comaptibility. Use summarize
for more flexibility.
The output requires \usepackage{booktabs}
in the LaTeX file.
Captions can be added to both, longtable
s and tabular
s.
In the latter case, captions are also suported if the table is no
floating object. In this case, the LaTeX package capt-of
is
required.
The output is printed with LaTeX style syntax highlighting to be used
e.g. in Sweave chunks with results=tex
.
Benjamin Hofner
latex.table.fac
and get_option
## Example requires package nlme to be installed and loaded if (require("nlme")) { ## Use dataset Orthodont data(Orthodont, package = "nlme") ## Get summary for continuous variables latex.table.cont(Orthodont) ## Change statistics to display latex.table.cont(Orthodont, quantiles = FALSE) latex.table.cont(Orthodont, count = FALSE, quantiles = FALSE) latex.table.cont(Orthodont, mean_sd = FALSE) ## Show column 'Missing' even if no missings are present latex.table.cont(Orthodont, show.NAs = TRUE) ## Change variables to display latex.table.cont(Orthodont, variables = "age") ## What happens in the display if we introduce some missing values: set.seed(1907) Orthodont$age[sample(nrow(Orthodont), 20)] <- NA latex.table.cont(Orthodont) }
## Example requires package nlme to be installed and loaded if (require("nlme")) { ## Use dataset Orthodont data(Orthodont, package = "nlme") ## Get summary for continuous variables latex.table.cont(Orthodont) ## Change statistics to display latex.table.cont(Orthodont, quantiles = FALSE) latex.table.cont(Orthodont, count = FALSE, quantiles = FALSE) latex.table.cont(Orthodont, mean_sd = FALSE) ## Show column 'Missing' even if no missings are present latex.table.cont(Orthodont, show.NAs = TRUE) ## Change variables to display latex.table.cont(Orthodont, variables = "age") ## What happens in the display if we introduce some missing values: set.seed(1907) Orthodont$age[sample(nrow(Orthodont), 20)] <- NA latex.table.cont(Orthodont) }
The function produces LaTeX tables with summary statistics for factor variables. It makes use of the booktabs package in LaTeX to obtain tables with a nice layout.
latex.table.fac(..., caption = NULL, label = NULL, table = c("tabular", "longtable"), align = NULL, floating = FALSE, center = TRUE)
latex.table.fac(..., caption = NULL, label = NULL, table = c("tabular", "longtable"), align = NULL, floating = FALSE, center = TRUE)
... |
arguments for |
caption |
(optional) character string. Caption of LaTeX table. Note that captions are suported for all tables (see also details below). |
label |
(optional) character string. Label of LaTeX table specified as
|
table |
character string. LaTeX table format, currently either
|
align |
character string. LaTeX alignment of table rows, per default
|
floating |
logical (default: |
center |
logical (default: |
This function is deprecated and only available for backward
comaptibility. Use summarize
for more flexibility.
The output requires \usepackage{booktabs}
in the LaTeX file.
Captions can be added to both, longtable
s and tabular
s.
In the latter case, captions are also suported if the table is no
floating object. In this case, the LaTeX package capt-of
is
required.
The output is printed with LaTeX style syntax highlighting to be used
e.g. in Sweave chunks with results=tex
.
Benjamin Hofner
latex.table.cont
and get_option
## Example requires package nlme to be installed and loaded if (require("nlme")) { ## Use dataset Orthodont data(Orthodont, package = "nlme") ## Get summary for continuous variables latex.table.fac(Orthodont) ## Reorder data for table: latex.table.fac(Orthodont, variables = c("Sex", "Subject")) ## What happens in the display if we introduce some missing values: set.seed(1907) Orthodont$Sex[sample(nrow(Orthodont), 20)] <- NA latex.table.fac(Orthodont) latex.table.fac(Orthodont, variables = "Sex") ## do not show statistics on missing values latex.table.fac(Orthodont, variables = "Sex", show.NAs = FALSE) }
## Example requires package nlme to be installed and loaded if (require("nlme")) { ## Use dataset Orthodont data(Orthodont, package = "nlme") ## Get summary for continuous variables latex.table.fac(Orthodont) ## Reorder data for table: latex.table.fac(Orthodont, variables = c("Sex", "Subject")) ## What happens in the display if we introduce some missing values: set.seed(1907) Orthodont$Sex[sample(nrow(Orthodont), 20)] <- NA latex.table.fac(Orthodont) latex.table.fac(Orthodont, variables = "Sex") ## do not show statistics on missing values latex.table.fac(Orthodont, variables = "Sex", show.NAs = FALSE) }
Improve summary tables by replacing variable names with labels and separating variable names and value labels of factor variables. Additionally, confidence intervalls are added to summaries per default and p-values are formated for pretty printing.
## generic function called by all prettify.summary.xxx functions ## S3 method for class 'data.frame' prettify(object, labels = NULL, sep = ": ", extra.column = FALSE, smallest.pval = 0.001, digits = NULL, scientific = FALSE, signif.stars = getOption("show.signif.stars"), ...) ## S3 method for class 'summary.lm' prettify(object, labels = NULL, sep = ": ", extra.column = FALSE, confint = TRUE, level = 0.95, smallest.pval = 0.001, digits = NULL, scientific = FALSE, signif.stars = getOption("show.signif.stars"), ...) ## S3 method for class 'summary.glm' prettify(object, labels = NULL, sep = ": ", extra.column = FALSE, confint = TRUE, level = 0.95, OR = TRUE, smallest.pval = 0.001, digits = NULL, scientific = FALSE, signif.stars = getOption("show.signif.stars"), ...) ## S3 method for class 'summary.coxph' prettify(object, labels = NULL, sep = ": ", extra.column = FALSE, confint = TRUE, level = 0.95, HR = TRUE, smallest.pval = 0.001, digits = NULL, scientific = FALSE, signif.stars = getOption("show.signif.stars"), env = parent.frame(), ...) ## S3 method for class 'summary.lme' prettify(object, labels = NULL, sep = ": ", extra.column = FALSE, confint = TRUE, level = 0.95, smallest.pval = 0.001, digits = NULL, scientific = FALSE, signif.stars = getOption("show.signif.stars"), ...) ## method for mixed models fitted with lme4 (vers. < 1.0) ## S3 method for class 'summary.mer' prettify(object, labels = NULL, sep = ": ", extra.column = FALSE, confint = TRUE, level = 0.95, smallest.pval = 0.001, digits = NULL, scientific = FALSE, signif.stars = getOption("show.signif.stars"), simulate = c("ifneeded", TRUE, FALSE), B = 1000, ...) ## method for mixed models fitted with lme4 (vers. >= 1.0) ## S3 method for class 'summary.merMod' prettify(object, labels = NULL, sep = ": ", extra.column = FALSE, confint = TRUE, level = 0.95, smallest.pval = 0.001, digits = NULL, scientific = FALSE, signif.stars = getOption("show.signif.stars"), method = c("profile", "Wald", "boot"), B = 1000, env = parent.frame(), ...) ## S3 method for class 'anova' prettify(object, labels, smallest.pval = 0.001, digits = NULL, scientific = FALSE, signif.stars = getOption("show.signif.stars"), ...) ## helper function for pretty p-values prettifyPValue(object, smallest.pval = 0.001, digits = NULL, scientific = FALSE, signif.stars = getOption("show.signif.stars"), ...)
## generic function called by all prettify.summary.xxx functions ## S3 method for class 'data.frame' prettify(object, labels = NULL, sep = ": ", extra.column = FALSE, smallest.pval = 0.001, digits = NULL, scientific = FALSE, signif.stars = getOption("show.signif.stars"), ...) ## S3 method for class 'summary.lm' prettify(object, labels = NULL, sep = ": ", extra.column = FALSE, confint = TRUE, level = 0.95, smallest.pval = 0.001, digits = NULL, scientific = FALSE, signif.stars = getOption("show.signif.stars"), ...) ## S3 method for class 'summary.glm' prettify(object, labels = NULL, sep = ": ", extra.column = FALSE, confint = TRUE, level = 0.95, OR = TRUE, smallest.pval = 0.001, digits = NULL, scientific = FALSE, signif.stars = getOption("show.signif.stars"), ...) ## S3 method for class 'summary.coxph' prettify(object, labels = NULL, sep = ": ", extra.column = FALSE, confint = TRUE, level = 0.95, HR = TRUE, smallest.pval = 0.001, digits = NULL, scientific = FALSE, signif.stars = getOption("show.signif.stars"), env = parent.frame(), ...) ## S3 method for class 'summary.lme' prettify(object, labels = NULL, sep = ": ", extra.column = FALSE, confint = TRUE, level = 0.95, smallest.pval = 0.001, digits = NULL, scientific = FALSE, signif.stars = getOption("show.signif.stars"), ...) ## method for mixed models fitted with lme4 (vers. < 1.0) ## S3 method for class 'summary.mer' prettify(object, labels = NULL, sep = ": ", extra.column = FALSE, confint = TRUE, level = 0.95, smallest.pval = 0.001, digits = NULL, scientific = FALSE, signif.stars = getOption("show.signif.stars"), simulate = c("ifneeded", TRUE, FALSE), B = 1000, ...) ## method for mixed models fitted with lme4 (vers. >= 1.0) ## S3 method for class 'summary.merMod' prettify(object, labels = NULL, sep = ": ", extra.column = FALSE, confint = TRUE, level = 0.95, smallest.pval = 0.001, digits = NULL, scientific = FALSE, signif.stars = getOption("show.signif.stars"), method = c("profile", "Wald", "boot"), B = 1000, env = parent.frame(), ...) ## S3 method for class 'anova' prettify(object, labels, smallest.pval = 0.001, digits = NULL, scientific = FALSE, signif.stars = getOption("show.signif.stars"), ...) ## helper function for pretty p-values prettifyPValue(object, smallest.pval = 0.001, digits = NULL, scientific = FALSE, signif.stars = getOption("show.signif.stars"), ...)
object |
object of class |
labels |
specify labels here. For the format see |
sep |
separator between variable label and value label of a factor
variable (default: |
extra.column |
logical: provide an extra column for the value labels of factors
(default: |
confint |
logical value indicating if confidence intervals sould be added or the confidence intervals themself. Using |
level |
confidence level; Per default 0.95% confidence intervals are returned |
OR |
logical. Should odds ratios be added? Only applicable if a logistic
regression model was fitted (i.e., with |
HR |
logical. Should hazard ratios be added? |
smallest.pval |
determines the smallest p-value to be printed
exactly. Smaller values are given as “< smallest.pval”.
This argument is passed to the |
digits |
number of significant digits. The default, |
scientific |
specifies if numbers should be printed in scientific
format. For details and possible values see |
signif.stars |
logical (default = TRUE). Should significance stars be added? Per
default system options are used. See |
simulate |
should the asysmptotic or simulated confidence intervals be used?
See |
B |
number of samples to take in |
method |
Determines the method for computing confidence intervals; One of
|
... |
further options. Currently not applicable. |
env |
specify environment in which the model was fitted. Needed to find the correct data for refitting the model in order to obtain confidence intervals. |
Specialized functions that prettify summary tables of various models
exist. For the data.frame
method, extra.column
and
sep
can only be used if labels
are specified as variable
names need to be known in order to split variable name and factor
level. For summary
objects, variable names can be extracted
from the objects.
To compute confidence intervalls, the model is refitted internally
extrating the call and environment from the model summary. All
functions then use confint
on the refitted model. For
mer
models special confint
functions
are defined in this package (for backward compatibility). For details see there.
Note that is it highly important
not to modify or delete the data in the fitting environment if
one wants to obtain correct confidence intervals. See examples for what
might happen. We try ourt best to find changes of the data and to warn
the user (but without any warranty).
Alternatively, one can directly specify the confidence intervals using
e.g. confint = confint(model)
, where model
is the fitted
model. This does not rely on refitting of the model and should always
work as expected. In this case, arguments level
,
simulate
and B
are ignored. Note that in this case it is
adviced to also specify the labels by hand!
prettifyPValue
is a helper function used within the prettify
functions but can also be used directly on a data.frame
object.
The function tries to (cleverly) “guess” the column of p-values
(based on the column names) and formats them nicely. Additionally,
significance stars are added if requested.
data.frame
with prettier variable labels.
For summary functions additionally confidence
intervalls (if requested), odds ratio (for logistic regression models,
if requested), p-values formated for pretty printing and significance
stars (if requested) are attached.
Benjamin Hofner
summary
, summary.lm
,
summary.glm
, summary.lme
,
summary.merMod
(or summary.mer-class
in lme4 < 1.0)
and summary.coxph
for summary functions.
anova
and Anova
for ANOVA functions.
confint
and ci
for confidence intervals.
Special functions are implemented for mixed models:
confint.mer
.
## Example requires package nlme to be installed and loaded if (require("nlme")) { ## Load data set Orthodont data(Orthodont, package = "nlme") ###################################################################### # Linear model ###################################################################### ## Fit a linear model linmod <- lm(distance ~ age + Sex, data = Orthodont) ## Extract pretty summary prettify(summary(linmod)) ## Extract anova (sequential tests) anova(linmod) ## now prettify it prettify(anova(linmod)) ###################################################################### # Random effects model (nlme) ###################################################################### ### (fit a more suitable model with random effects) ## With package nlme: require("nlme") ## Fit a model for distance with random intercept for Subject mod <- lme(distance ~ age + Sex, data = Orthodont, random = ~ 1 | Subject) summary(mod) ## Extract fixed effects table, add confidence interval and make it pretty prettify(summary(mod)) ## Extract fixed effects table only and make it pretty prettify(summary(mod), confint = FALSE) ###################################################################### # Random effects model (lme4) ###################################################################### set.seed(130913) ## With package lme4: if (require("lme4") && require("car")) { ## Fit a model for distance with random intercept for Subject mod4 <- lmer(distance ~ age + Sex + (1|Subject), data = Orthodont) summary(mod4) ## Extract fixed effects table and make it pretty prettify(summary(mod4)) ## Extract and prettify anova (sequential tests) prettify(anova(mod4)) ## Better: extract Anova (partial instead of sequential tests) library("car") Anova(mod4) ## now prettify it prettify(Anova(mod4)) } ###################################################################### # Cox model ###################################################################### ## survival models if (require("survival")) { ## Load data set ovarian (now part of cancer) data(cancer, package = "survival") ## fit a Cox model mod5 <- coxph(Surv(futime, fustat) ~ age, data=ovarian) summary(mod5) ## Make pretty summary prettify(summary(mod5)) ## Make pretty summary prettify(Anova(mod5)) } ###################################################################### # ATTENTION when confint = TRUE: Do not modify or delete data ###################################################################### ## Fit a linear model (same as above) linmod <- lm(distance ~ age + Sex, data = Orthodont) ## Extract pretty summary prettify(summary(linmod)) ## Change the data (age in month instead of years) Orthodont$age <- Orthodont$age * 12 prettify(summary(linmod)) ## confidence intervals for age have changed ## but coefficients stayed the same; a ## warning is issued ## Remove data in fitting environment rm(Orthodont) prettify(summary(linmod)) ## confidence intervals are missing as no ## data set was available to refit the model ###################################################################### # Use confint to specify confidence interval without refitting ###################################################################### ## make labels without using the data set labels <- c("distance", "age", "Subject", "Sex") names(labels) <- labels ## usually easier via: labels(Orthodont) prettify(summary(linmod), confint = confint(linmod), labels = labels) }
## Example requires package nlme to be installed and loaded if (require("nlme")) { ## Load data set Orthodont data(Orthodont, package = "nlme") ###################################################################### # Linear model ###################################################################### ## Fit a linear model linmod <- lm(distance ~ age + Sex, data = Orthodont) ## Extract pretty summary prettify(summary(linmod)) ## Extract anova (sequential tests) anova(linmod) ## now prettify it prettify(anova(linmod)) ###################################################################### # Random effects model (nlme) ###################################################################### ### (fit a more suitable model with random effects) ## With package nlme: require("nlme") ## Fit a model for distance with random intercept for Subject mod <- lme(distance ~ age + Sex, data = Orthodont, random = ~ 1 | Subject) summary(mod) ## Extract fixed effects table, add confidence interval and make it pretty prettify(summary(mod)) ## Extract fixed effects table only and make it pretty prettify(summary(mod), confint = FALSE) ###################################################################### # Random effects model (lme4) ###################################################################### set.seed(130913) ## With package lme4: if (require("lme4") && require("car")) { ## Fit a model for distance with random intercept for Subject mod4 <- lmer(distance ~ age + Sex + (1|Subject), data = Orthodont) summary(mod4) ## Extract fixed effects table and make it pretty prettify(summary(mod4)) ## Extract and prettify anova (sequential tests) prettify(anova(mod4)) ## Better: extract Anova (partial instead of sequential tests) library("car") Anova(mod4) ## now prettify it prettify(Anova(mod4)) } ###################################################################### # Cox model ###################################################################### ## survival models if (require("survival")) { ## Load data set ovarian (now part of cancer) data(cancer, package = "survival") ## fit a Cox model mod5 <- coxph(Surv(futime, fustat) ~ age, data=ovarian) summary(mod5) ## Make pretty summary prettify(summary(mod5)) ## Make pretty summary prettify(Anova(mod5)) } ###################################################################### # ATTENTION when confint = TRUE: Do not modify or delete data ###################################################################### ## Fit a linear model (same as above) linmod <- lm(distance ~ age + Sex, data = Orthodont) ## Extract pretty summary prettify(summary(linmod)) ## Change the data (age in month instead of years) Orthodont$age <- Orthodont$age * 12 prettify(summary(linmod)) ## confidence intervals for age have changed ## but coefficients stayed the same; a ## warning is issued ## Remove data in fitting environment rm(Orthodont) prettify(summary(linmod)) ## confidence intervals are missing as no ## data set was available to refit the model ###################################################################### # Use confint to specify confidence interval without refitting ###################################################################### ## make labels without using the data set labels <- c("distance", "age", "Subject", "Sex") names(labels) <- labels ## usually easier via: labels(Orthodont) prettify(summary(linmod), confint = confint(linmod), labels = labels) }
The function produces summary tables for factors and continuous
variables. The obtained tables can be used directly in R, with LaTeX
and HTML (by using the xtable
function) or Markdown
(e.g. by using the function kable
).
summarize(data, type = c("numeric", "factor"), variables = names(data), variable.labels = labels, labels = NULL, group = NULL, test = !is.null(group), colnames = NULL, digits = NULL, digits.pval = 3, smallest.pval = 0.001, sep = NULL, sanitize = TRUE, drop = TRUE, show.NAs = any(is.na(data[, variables])), ...)
summarize(data, type = c("numeric", "factor"), variables = names(data), variable.labels = labels, labels = NULL, group = NULL, test = !is.null(group), colnames = NULL, digits = NULL, digits.pval = 3, smallest.pval = 0.001, sep = NULL, sanitize = TRUE, drop = TRUE, show.NAs = any(is.na(data[, variables])), ...)
data |
data set to be used. |
type |
print summary table for either |
variables |
character vector defining variables that should be included in the
table. Per default, all numeric or factor variables of |
variable.labels , labels
|
labels for the variables. If |
group |
character specifying a grouping factor. Per default no grouping is applied. |
test |
logical or character string. If a |
colnames |
a vector of character strings of appropriate length.
The vector supplies alternative column names for the resulting
table. If |
digits |
number of digits to round to. For defaults see
|
digits.pval |
number of significant digits used for p-values. |
smallest.pval |
determines the smallest p-value to be printed
exactly. Smaller values are given as “< smallest.pval”.
This argument is passed to the |
sep |
logical. Determines whether separators (lines) should be added after
each variable. For defaults see |
sanitize |
logical (default: |
drop |
logical (default: |
show.NAs |
logical. Determines if NAs are displayed. Per default,
|
... |
additional arguments for |
A special data.frame
with additional class summary
containing the computed statistics is returned from function
summarize
. Addtional attributes required for the
xtable.summary
or print.xtable.summary
function are contained as attributes. These are extracted using the
function get_option
.
Benjamin Hofner
For details see
summarize_numeric
and summarize_factor
.
Conversion to LaTeX tables can be done using
xtable.summary
and print.xtable.summary
.
if (require("nlme")) { ## Use dataset Orthodont data(Orthodont, package = "nlme") ## Get summary for continuous variables (tab1 <- summarize(Orthodont, type = "numeric")) ## Change statistics to display summarize(Orthodont, quantiles = FALSE, type = "numeric") summarize(Orthodont, quantiles = FALSE, count = FALSE, type = "numeric") summarize(Orthodont, mean_sd = FALSE, type = "numeric") ## Get summary for categorical variables (tab2 <- summarize(Orthodont, type = "fac")) ## use fraction instead of percentage summarize(Orthodont, percent = FALSE, type = "fac") ## Using the tables with Markdown if (require("knitr")) { kable(tab1) kable(tab2) } ## Using the tables with LaTeX if (require("xtable")) { xtable(tab1) ## grouped table xtable(summarize(Orthodont, group = "Sex")) xtable(tab2) } }
if (require("nlme")) { ## Use dataset Orthodont data(Orthodont, package = "nlme") ## Get summary for continuous variables (tab1 <- summarize(Orthodont, type = "numeric")) ## Change statistics to display summarize(Orthodont, quantiles = FALSE, type = "numeric") summarize(Orthodont, quantiles = FALSE, count = FALSE, type = "numeric") summarize(Orthodont, mean_sd = FALSE, type = "numeric") ## Get summary for categorical variables (tab2 <- summarize(Orthodont, type = "fac")) ## use fraction instead of percentage summarize(Orthodont, percent = FALSE, type = "fac") ## Using the tables with Markdown if (require("knitr")) { kable(tab1) kable(tab2) } ## Using the tables with LaTeX if (require("xtable")) { xtable(tab1) ## grouped table xtable(summarize(Orthodont, group = "Sex")) xtable(tab2) } }
The function produces summary tables for factor variables.
The obtained tables can be used directly in R, with LaTeX
and HTML (by using the xtable
function) or Markdown
(e.g. by using the function kable
).
summarize_factor(data, variables = names(data), variable.labels = labels, labels = NULL, group = NULL, test = !is.null(group), colnames = NULL, digits = 3, digits.pval = 3, smallest.pval = 0.001, sep = TRUE, sanitize = TRUE, drop = TRUE, show.NAs = any(is.na(data[, variables])), ## additional specific arguments percent = TRUE, cumulative = FALSE, na.lab = "<Missing>", ...)
summarize_factor(data, variables = names(data), variable.labels = labels, labels = NULL, group = NULL, test = !is.null(group), colnames = NULL, digits = 3, digits.pval = 3, smallest.pval = 0.001, sep = TRUE, sanitize = TRUE, drop = TRUE, show.NAs = any(is.na(data[, variables])), ## additional specific arguments percent = TRUE, cumulative = FALSE, na.lab = "<Missing>", ...)
data |
data set to be used. |
variables |
variables that should be included in the table.
For details see |
variable.labels , labels
|
labels for the variables.
For details see |
group |
character specifying a grouping factor.
For details see |
test |
logical or charachter specifying test for group differences.
For details see |
colnames |
a vector of character strings of appropriate length.
For details see |
digits |
number of digits to round to (only used for fractions). Per default all values are rounded to three digits. |
digits.pval |
number of significant digits used for p-values. |
smallest.pval |
determines the smallest p-value to be printed exactly.
For details see |
sep |
logical (default: |
sanitize |
logical (default: |
drop |
logical (default: |
show.NAs |
logical. Determines if NAs are displayed as a separate category for
each factor variable with missings. If |
percent |
logical. Should the fractions be given as percent values? Otherwise, fractions are given as decimal numbers. |
cumulative |
logical. Should cumulative fractions be displayed? |
na.lab |
label for missing values (default: |
... |
additional arguments. Currently not used. |
A special data.frame
with additional class summary
containing the computed statistics is returned from function
summarize
. Addtional attributes required for the
xtable.summary
or print.xtable.summary
function are contained as attributes. These are extracted using the
function get_option
.
Benjamin Hofner
For details see link{summarize}
and link{summarize_factor}
.
Conversion to LaTeX tables can be done using
xtable.summary
and print.xtable.summary
.
## Example requires package nlme to be installed and loaded if (require("nlme")) { ## Use dataset Orthodont data(Orthodont, package = "nlme") ## Get summary for continuous variables summarize(Orthodont, type = "factor") ## Reorder data for table: summarize(Orthodont, variables = c("Sex", "Subject"), type = "factor") ## What happens in the display if we introduce some missing values: set.seed(1907) Orthodont$Sex[sample(nrow(Orthodont), 20)] <- NA summarize(Orthodont, type = "factor") summarize(Orthodont, variables = "Sex", type = "factor") ## do not show statistics on missing values summarize(Orthodont, variables = "Sex", show.NAs = FALSE, type = "factor") }
## Example requires package nlme to be installed and loaded if (require("nlme")) { ## Use dataset Orthodont data(Orthodont, package = "nlme") ## Get summary for continuous variables summarize(Orthodont, type = "factor") ## Reorder data for table: summarize(Orthodont, variables = c("Sex", "Subject"), type = "factor") ## What happens in the display if we introduce some missing values: set.seed(1907) Orthodont$Sex[sample(nrow(Orthodont), 20)] <- NA summarize(Orthodont, type = "factor") summarize(Orthodont, variables = "Sex", type = "factor") ## do not show statistics on missing values summarize(Orthodont, variables = "Sex", show.NAs = FALSE, type = "factor") }
The function produces summary tables for continuous
variables. The obtained tables can be used directly in R, with LaTeX
and HTML (by using the xtable
function) or Markdown
(e.g. by using the function kable
).
summarize_numeric(data, variables = names(data), variable.labels = labels, labels = NULL, group = NULL, test = !is.null(group), colnames = NULL, digits = 2, digits.pval = 3, smallest.pval = 0.001, sep = !is.null(group), sanitize = TRUE, drop = TRUE, show.NAs = any(is.na(data[, variables])), ## additional specific arguments count = TRUE, mean_sd = TRUE, quantiles = TRUE, incl_outliers = TRUE, ...)
summarize_numeric(data, variables = names(data), variable.labels = labels, labels = NULL, group = NULL, test = !is.null(group), colnames = NULL, digits = 2, digits.pval = 3, smallest.pval = 0.001, sep = !is.null(group), sanitize = TRUE, drop = TRUE, show.NAs = any(is.na(data[, variables])), ## additional specific arguments count = TRUE, mean_sd = TRUE, quantiles = TRUE, incl_outliers = TRUE, ...)
data |
data set to be used. |
variables |
variables that should be included in the table.
For details see |
variable.labels , labels
|
labels for the variables.
For details see |
group |
character specifying a grouping factor.
For details see |
test |
logical or charachter specifying test for group differences.
For details see |
colnames |
a vector of character strings of appropriate length.
For details see |
digits |
number of digits to round to. Per default all values are rounded to two digits. |
digits.pval |
number of significant digits used for p-values. |
smallest.pval |
determines the smallest p-value to be printed exactly.
For details see |
sep |
logical (default: |
sanitize |
logical (default: |
drop |
logical (default: |
show.NAs |
logical. Determines if the number of missings (NAs) is displayed as
a separate column. Per default, |
count |
(logical) indicator if number of complete cases ("n") should be
included in the table (default: |
mean_sd |
(logical) indicator if mean and standard deviation should be
included in the table (default: |
quantiles |
(logical) indicator if quantiles (including min and max) should be
included in the table (default: |
incl_outliers |
Per default we use |
... |
additional arguments. Currently not used. |
A special data.frame
with additional class summary
containing the computed statistics is returned from function
summarize
. Addtional attributes required for the
xtable.summary
or print.xtable.summary
function are contained as attributes. These are extracted using the
function get_option
.
Benjamin Hofner
For details see link{summarize}
and link{summarize_factor}
.
Conversion to LaTeX tables can be done using
xtable.summary
and print.xtable.summary
.
if (require("nlme")) { ## Use dataset Orthodont data(Orthodont, package = "nlme") ## Get summary for continuous variables summarize(Orthodont, type = "numeric") ## Change statistics to display summarize(Orthodont, quantiles = FALSE, type = "numeric") summarize(Orthodont, quantiles = FALSE, count = FALSE, type = "numeric") summarize(Orthodont, mean_sd = FALSE, type = "numeric") ## for more examples see ?summarize }
if (require("nlme")) { ## Use dataset Orthodont data(Orthodont, package = "nlme") ## Get summary for continuous variables summarize(Orthodont, type = "numeric") ## Change statistics to display summarize(Orthodont, quantiles = FALSE, type = "numeric") summarize(Orthodont, quantiles = FALSE, count = FALSE, type = "numeric") summarize(Orthodont, mean_sd = FALSE, type = "numeric") ## for more examples see ?summarize }
The function produces code that LaTeX is able to typeset.
## S3 method for class 'character' toLatex(object, ...) ## S3 method for class 'sessionInfo' toLatex(object, pkgs = NULL, locale = FALSE, base.pkgs = FALSE, other.pkgs = TRUE, namespace.pkgs = FALSE, citations = TRUE, citecommand = "\\citep", file = NULL, append = FALSE, ...)
## S3 method for class 'character' toLatex(object, ...) ## S3 method for class 'sessionInfo' toLatex(object, pkgs = NULL, locale = FALSE, base.pkgs = FALSE, other.pkgs = TRUE, namespace.pkgs = FALSE, citations = TRUE, citecommand = "\\citep", file = NULL, append = FALSE, ...)
object |
either an object of class character which should be cleaned for printing in
LaTeX environments or an object of class |
pkgs |
character vector (optional). Specify specific packages here to show
information on these (instead of all attached packages). See
|
locale |
logical (default = FALSE). Show information on locale. |
base.pkgs |
logical (default = FALSE). Show information on base packages. |
other.pkgs |
logical (default = TRUE). Show information on currently attached
packages. If |
namespace.pkgs |
logical (default = FALSE). Show information on packages whose namespaces are currently loaded but not attached. |
citations |
logical (default = TRUE). Should citations for all packages be added? BibTeX is used for storing the citations. |
citecommand |
Specify LaTeX-command for citation here. Curly brackets are added
internally. Note that |
file |
Specify path to BibTeX file where citations should be saved. If
|
append |
logical (default = FALSE). Should citations be added to an existing BibTeX file (if existing) or should old BibTeX files be overwritten? |
... |
additional arguments. Currently not used. |
A character string with special markup is returned: The output is
printed with LaTeX style syntax highlighting to be used
e.g. in Sweave chunks with results=tex
.
Benjamin Hofner, based on code from package xtable, bibtex and package utils. See source code for documentation.
toLatex
. For details on toLatex.sessionInfo
see
also sessionInfo
.
txt <- "Price: <= 500$ & additional goodies" toLatex(txt) ############################################################ ## session info for automatic inclusion in reports info <- toLatex(sessionInfo()) info ## extract first part (the Latex part) toLatex(info) ## extract second part (the BibTeX part) toBibtex(info) ############################################################ ## usual usage scenario ## Do not run the following code automatically as it needs ## write access to the current working directory. ## This code (without removing the file) could for example ## be included in a LaTeX chunk of your Sweave or knitr ## report. ## Not run: getwd() ## location where write access is needed toLatex(sessionInfo(), file = "packages.bib") file.remove("packages.bib") ## End(Not run)
txt <- "Price: <= 500$ & additional goodies" toLatex(txt) ############################################################ ## session info for automatic inclusion in reports info <- toLatex(sessionInfo()) info ## extract first part (the Latex part) toLatex(info) ## extract second part (the BibTeX part) toBibtex(info) ############################################################ ## usual usage scenario ## Do not run the following code automatically as it needs ## write access to the current working directory. ## This code (without removing the file) could for example ## be included in a LaTeX chunk of your Sweave or knitr ## report. ## Not run: getwd() ## location where write access is needed toLatex(sessionInfo(), file = "packages.bib") file.remove("packages.bib") ## End(Not run)
The function produces objects which can be printed to LaTeX and HTML code.
## S3 method for class 'summary' xtable(x, caption = NULL, label = NULL, align = NULL, digits = NULL, display = NULL, ...) ## S3 method for class 'xtable.summary' print(x, rules = NULL, header = NULL, caption.placement = getOption("xtable.caption.placement", "top"), hline.after = getOption("xtable.hline.after", NULL), include.rownames = FALSE, add.to.row = getOption("xtable.add.to.row", NULL), booktabs = getOption("xtable.booktabs", TRUE), sanitize.text.function = get_option(x, "sanitize"), math.style.negative = getOption("xtable.math.style.negative", TRUE), math.style.exponents = getOption("xtable.math.style.exponents", TRUE), tabular.environment = getOption("xtable.tabular.environment", "tabular"), floating = getOption("xtable.floating", FALSE), latex.environments = getOption("xtable.latex.environments", c("center")), ...)
## S3 method for class 'summary' xtable(x, caption = NULL, label = NULL, align = NULL, digits = NULL, display = NULL, ...) ## S3 method for class 'xtable.summary' print(x, rules = NULL, header = NULL, caption.placement = getOption("xtable.caption.placement", "top"), hline.after = getOption("xtable.hline.after", NULL), include.rownames = FALSE, add.to.row = getOption("xtable.add.to.row", NULL), booktabs = getOption("xtable.booktabs", TRUE), sanitize.text.function = get_option(x, "sanitize"), math.style.negative = getOption("xtable.math.style.negative", TRUE), math.style.exponents = getOption("xtable.math.style.exponents", TRUE), tabular.environment = getOption("xtable.tabular.environment", "tabular"), floating = getOption("xtable.floating", FALSE), latex.environments = getOption("xtable.latex.environments", c("center")), ...)
x |
object of class |
caption |
character vector specifying the table's caption; see
|
label |
character string specifying the LaTeX label or HTML anchor; see
|
align |
character string specifying the alignment of table columns; see
|
digits |
numeric vector specifying the number of digits to display in each
column; see |
display |
character string specifying the column types; see
|
rules |
character string specifying the rules to be used. Per default the
|
header |
character string specifying the table header to be used. Per default the
|
caption.placement |
can be either |
hline.after |
vector indicating the rows after which a horizontal line is printed.
Here, the default is to not draw hlines (i.e. |
add.to.row |
list of row numbers ( |
include.rownames |
logical. Always set to |
booktabs |
logical. If |
sanitize.text.function |
All non-numeric enteries (except row and column names) are sanitised
in an attempt to remove characters which have special meaning for
the output format. Per default the function |
math.style.negative |
logical. If |
math.style.exponents |
logical. If |
tabular.environment |
character string. Per default |
floating |
logical. Determine if the table is printed in a floating
environment. Note that the standard default of
|
latex.environments |
character string. Per default |
... |
additional arguments passed to |
We use the standard xtable
function but add a special class
that allows different defaults in the print.xtable
function.
In general, all options of print.xtable
can be used as well as
global options set via options()
. E.g.
options(xtable.booktabs = FALSE
will set the argument booktabs
per default to
FALSE
for all calls to print.xtable
.
After printing, a table with LaTeX markup is returned.
Benjamin Hofner
For details see xtable
and
print.xtable
.
if (require("nlme")) { ## Use dataset Orthodont data(Orthodont, package = "nlme") ## Get summary for continuous variables (tab1 <- summarize(Orthodont, type = "numeric")) ## Get summary for categorical variables (tab2 <- summarize(Orthodont, type = "fac")) ## Using the tables with LaTeX if (require("xtable")) { xtable(tab1) ## grouped table xtable(summarize(Orthodont, group = "Sex")) xtable(tab2) } }
if (require("nlme")) { ## Use dataset Orthodont data(Orthodont, package = "nlme") ## Get summary for continuous variables (tab1 <- summarize(Orthodont, type = "numeric")) ## Get summary for categorical variables (tab2 <- summarize(Orthodont, type = "fac")) ## Using the tables with LaTeX if (require("xtable")) { xtable(tab1) ## grouped table xtable(summarize(Orthodont, group = "Sex")) xtable(tab2) } }