prettify: Make Pretty Summary and Anova Tables

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/prettify.R

Description

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.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
## 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"), ...)

Arguments

object

object of class data.frame resulting (most likely) from a call to summary or directly the output from summary, anova or Anova (the latter from package car).

labels

specify labels here. For the format see labels.

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: FALSE).

confint

logical value indicating if confidence intervals sould be added or the confidence intervals themself.

Using confint = TRUE is experimental only and special care needs to be taken that the data set used for fitting is neither changed nor deleted. See ‘Details’ and ‘Examples’.

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 family = "binomial").

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 eps argument of format.pval. See there for details.

digits

number of significant digits. The default, NULL, uses getOption("digits") for formating p-values and leaves all other columns unchanged. If digits are specified, all columns use this number of significant digits (columnwise). See also argument digits in format.

scientific

specifies if numbers should be printed in scientific format. For details and possible values see format.

signif.stars

logical (default = TRUE). Should significance stars be added? Per default system options are used. See getOption("show.signif.stars").

simulate

should the asysmptotic or simulated confidence intervals be used? See confint.mer for details.

B

number of samples to take in mcmcsamp. See confint.mer for details.

method

Determines the method for computing confidence intervals; One of "profile" (default), "Wald", "boot". For details see confint.merMod in package lme4.

...

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.

Details

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.

Value

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.

Author(s)

Benjamin Hofner

See Also

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.

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
## 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)
}

papeR documentation built on March 23, 2021, 1:08 a.m.