Learn R Programming

sjPlot (version 2.3.3)

sjt.lm: Summary of linear regression as HTML table

Description

Summarizes (multiple) fitted linear models (coefficients, std. beta values etc.) as HTML table, or saves them as file. The fitted models may have different predictors, e.g. when comparing different stepwise fitted models. This function also supports panel models fitted with the plm-function from the plm-package and generalized least squares models fitted with the gls-function from the nlme-package.

Usage

sjt.lm(..., pred.labels = NULL, depvar.labels = NULL,
  remove.estimates = NULL, group.pred = TRUE, p.numeric = TRUE,
  emph.p = FALSE, p.zero = FALSE, p.kr = TRUE, robust = FALSE,
  separate.ci.col = TRUE, newline.ci = TRUE, show.est = TRUE,
  show.std = NULL, show.ci = TRUE, show.se = FALSE, show.header = FALSE,
  show.col.header = TRUE, show.r2 = TRUE, show.icc = FALSE,
  show.re.var = FALSE, show.fstat = FALSE, show.aic = FALSE,
  show.aicc = FALSE, show.dev = FALSE, string.pred = "Predictors",
  string.dv = "Dependent Variables", string.interc = "(Intercept)",
  string.obs = "Observations", string.est = "B", string.std = "std. Beta",
  string.ci = "CI", string.se = "std. Error", string.p = "p",
  ci.hyphen = " – ", minus.sign = "-", digits.est = 2,
  digits.std = 2, digits.p = 3, digits.ci = 2, digits.se = 2,
  digits.summary = 3, cell.spacing = 0.2, cell.gpr.indent = 0.6,
  sep.column = TRUE, CSS = NULL, encoding = NULL, file = NULL,
  use.viewer = TRUE, no.output = FALSE, remove.spaces = TRUE)

Arguments

...

One or more fitted linear (mixed) models.

pred.labels

Character vector with labels of predictor variables. If not NULL, pred.labels will be used in the first table column with the predictors' names. If NULL, variable labels are set based on label attributes (see get_label). If pred.labels = "", column names (vector names) are used as predictor labels. See 'Examples'.

depvar.labels

Character vector with labels of dependent variables of all fitted models. See 'Examples'.

remove.estimates

Numeric vector with indices (order equals to row index of coef(fit)) or character vector with coefficient names that indicate which estimates should be removed from the table output. The first estimate is the intercept, followed by the model predictors. The intercept cannot be removed from the table output! remove.estimates = c(2:4) would remove the 2nd to the 4th estimate (1st to 3rd predictor after intercept) from the output. remove.estimates = "est_name" would remove the estimate est_name. Default is NULL, i.e. all estimates are printed.

group.pred

Logical, if TRUE (default), automatically groups table rows with factor levels of same factor, i.e. predictors of type factor will be grouped, if the factor has more than two levels. Grouping means that a separate headline row is inserted to the table just before the predictor values.

p.numeric

Logical, if TRUE, the p-values are printed as numbers. If FALSE (default), asterisks are used.

emph.p

Logical, if TRUE, significant p-values are shown bold faced.

p.zero

logical, if TRUE, p-values have a leading 0 before the period (e.g. 0.002), else p-values start with a period and without a zero (e.g. .002).

p.kr

logical, if TRUE, p-value estimation is based on conditional F-tests with Kenward-Roger approximation for the df. Caution: Computation may take very long time for large samples!

robust

Logical, if TRUE, robust standard errors and confidence intervals will be reported. Computation of robust standard errors is based on the robust-function in the sjstats-package.

separate.ci.col

Logical, if TRUE, the CI values are shown in a separate table column. Default is FALSE.

newline.ci

Logical, if TRUE and separate.ci.col = FALSE, inserts a line break between estimate and CI values. If FALSE, CI values are printed in the same line as estimate values.

show.est

Logical, if TRUE (default), the estimates are printed.

show.std

Indicates whether standardized beta-coefficients should also printed, and if yes, which type of standardization is done. See 'Details'.

show.ci

Logical, if TRUE (default), the confidence intervall is also printed to the table. Use FALSE to omit the CI in the table.

show.se

Logical, if TRUE, the standard errors are also printed. Default is FALSE.

show.header

Logical, if TRUE, the header strings string.pred and string.dv are shown. By default, they're hidden.

show.col.header

Logical, if TRUE (default), the table data columns have a headline with abbreviations for estimates, std. beta-values, confidence interval and p-values.

show.r2

Logical, if TRUE (default), the R2 and adjusted R2 values for each model are printed in the model summary. For linear mixed models, the R2 and Omega-squared values are printed (see r2 for details).

show.icc

Logical, if TRUE, the intra-class-correlation for each model is printed in the model summary. Only applies to mixed models.

show.re.var

Logical, if TRUE, the variance parameters for the random effects for each model are printed in the model summary. Only applies to mixed models. For details output, see 'Note' in icc.

show.fstat

Logical, if TRUE, the F-statistics for each model is printed in the model summary. Default is FALSE. This argument does not apply to sjt.lmer.

show.aic

Logical, if TRUE, the AIC value for each model is printed in the model summary. Default is FALSE.

show.aicc

Logical, if TRUE, the second-order AIC value for each model is printed in the model summary. Default is FALSE.

show.dev

Logical, if TRUE, the deviance for each model is printed in the model summary.

string.pred

Character vector,used as headline for the predictor column. Default is "Predictors".

string.dv

Character vector, used as headline for the dependent variable columns. Default is "Dependent Variables".

string.interc

Character vector, used as headline for the Intercept row. Default is "Intercept".

string.obs

character vector, used in the summary row for the count of observation (cases). Default is "Observations".

string.est

Character vector, used for the column heading of estimates.

string.std

Character vector, used for the column heading of standardized beta coefficients. Default is "std. Beta".

string.ci

Character vector, used for the column heading of confidence interval values. Default is "CI".

string.se

Character vector, used for the column heading of standard error values. Default is "std. Error".

string.p

Character vector, used for the column heading of p values. Default is "p".

ci.hyphen

Character vector, indicating the hyphen for confidence interval range. May be an HTML entity. See 'Examples'.

minus.sign

string, indicating the minus sign for negative numbers. May be an HTML entity. See 'Examples'.

digits.est

Amount of decimals for estimates

digits.std

Amount of decimals for standardized beta

digits.p

Amount of decimals for p-values

digits.ci

Amount of decimals for confidence intervals

digits.se

Amount of decimals for standard error

digits.summary

Amount of decimals for values in model summary

cell.spacing

Numeric, inner padding of table cells. By default, this value is 0.2 (unit is cm), which is suitable for viewing the table. Decrease this value (0.05 to 0.1) if you want to import the table into Office documents. This is a convenient argument for the CSS argument for changing cell spacing, which would be: CSS = list(css.thead = "padding:0.2cm;", css.tdata = "padding:0.2cm;").

cell.gpr.indent

Indent for table rows with grouped factor predictors. Only applies if group.pred = TRUE.

sep.column

Logical, if TRUE, an empty table column is added after each model column, to add margins between model columns. By default, this column will be added to the output; however, when copying tables to office applications, it might be helpful not to add this separator column when modifying the table layout.

CSS

A list with user-defined style-sheet-definitions, according to the official CSS syntax. For more details, see this package-vignette, or 'Details' in sjt.frq.

encoding

String, indicating the charset encoding used for variable and value labels. Default is NULL, so encoding will be auto-detected depending on your platform (e.g., "UTF-8" for Unix and "Windows-1252" for Windows OS). Change encoding if specific chars are not properly displayed (e.g. German umlauts).

file

Destination file, if the output should be saved as file. If NULL (default), the output will be saved as temporary file and openend either in the IDE's viewer pane or the default web browser.

use.viewer

Logical, if TRUE, the HTML table is shown in the IDE's viewer pane. If FALSE or no viewer available, the HTML table is opened in a web browser.

no.output

Logical, if TRUE, the html-output is neither opened in a browser nor shown in the viewer pane and not even saved to file. This option is useful when the html output should be used in knitr documents. The html output can be accessed via the return value.

remove.spaces

Logical, if TRUE, leading spaces are removed from all lines in the final string that contains the html-data. Use this, if you want to remove parantheses for html-tags. The html-source may look less pretty, but it may help when exporting html-tables to office tools.

Value

Invisibly returns

  • the web page style sheet (page.style),

  • the web page content (page.content),

  • the complete html-output (output.complete) and

  • the html-table with inline-css for use with knitr (knitr)

for further use.

Details

Concerning the show.std argument, show.std = "std" will print normal standardized estimates. For show.std = "std2", however, standardization of estimates follows Gelman's (2008) suggestion, rescaling the estimates by dividing them by two standard deviations instead of just one. Resulting coefficients are then directly comparable for untransformed binary predictors. This type of standardization uses the standardize-function from the arm-package. For backward compatibility reasons, show.std also may be a logical value; if TRUE, normal standardized estimates are printed (same effect as show.std = "std"). Use show.std = NULL (default) or show.std = FALSE, if standardized estimats should not be printed.

Furthermore, see 'Details' in sjt.frq.

See Also

sjPlot manual: sjt.lm

Examples

Run this code
# NOT RUN {
# Now fit the models. Note that both models share the same predictors
# and only differ in their dependent variable. See examples of stepwise
# models below at the end.
library(sjmisc)
data(efc)

# fit first model
fit1 <- lm(barthtot ~ c160age + c12hour + c161sex + c172code, data = efc)
# fit second model
fit2 <- lm(neg_c_7 ~ c160age + c12hour + c161sex + c172code, data = efc)

# create and open HTML-table in RStudio Viewer Pane or web browser
# note that we don't need to specify labels for the predictors,
# because these are automatically read
sjt.lm(fit1, fit2)

# create and open HTML-table in RStudio Viewer Pane or web browser
# in the following examples, we set labels via argument
sjt.lm(fit1, fit2,
       depvar.labels = c("Barthel-Index", "Negative Impact"),
       pred.labels = c("Carer's Age", "Hours of Care",
                       "Carer's Sex", "Educational Status"))

# use vector names as labels
sjt.lm(fit1, fit2, pred.labels = "")

# show HTML-table, indicating p-values as asterisks
sjt.lm(fit1, fit2, show.std = TRUE, p.numeric = FALSE)

# create and open HTML-table in RStudio Viewer Pane or web browser,
# integrate CI in estimate column
sjt.lm(fit1, fit2, separate.ci.col = FALSE)

# show HTML-table, indicating p-values as numbers
# and printing CI in a separate column
sjt.lm(fit1, fit2, show.std = TRUE)

# show HTML-table, indicating p-values as stars
# and integrate CI in estimate column
sjt.lm(fit1, fit2, show.std = TRUE, ci.hyphen = " to ",
       minus.sign = "−", p.numeric = FALSE,
       separate.ci.col = FALSE)

# ----------------------------------
# connecting two html-tables
# ----------------------------------
# fit two more models
fit3 <- lm(tot_sc_e ~ c160age + c12hour + c161sex + c172code, data=efc)
fit4 <- lm(e42dep ~ c160age + c12hour + c161sex + c172code, data=efc)

# create and save first HTML-table
part1 <- sjt.lm(fit1, fit2)

# create and save second HTML-table
part2 <- sjt.lm(fit3, fit4)

# browse temporary file
htmlFile <- tempfile(fileext=".html")
write(sprintf("<html><head>%s</head><body>%s<p></p>%s</body></html>",
              part1$page.style, part1$page.content, part2$page.content),
      file = htmlFile)
viewer <- getOption("viewer")
if (!is.null(viewer)) viewer(htmlFile) else utils::browseURL(htmlFile)

# ----------------------------------
# User defined style sheet
# ----------------------------------
sjt.lm(fit1, fit2,
       CSS = list(css.table = "border: 2px solid;",
                  css.tdata = "border: 1px solid;",
                  css.depvarhead = "color:#003399;"))

# ----------------------------------
# automatic grouping of predictors
# ----------------------------------
library(sjmisc)
data(efc)

# make education categorical
efc$c172code <- to_factor(efc$c172code)

# fit first model again (with c172code as factor)
fit1 <- lm(barthtot ~ c160age + c12hour + c172code + c161sex, data=efc)
# fit second model again (with c172code as factor)
fit2 <- lm(neg_c_7 ~ c160age + c12hour + c172code + c161sex, data=efc)

# plot models, but group by predictors
sjt.lm(fit1, fit2, group.pred = TRUE)

# ----------------------------------------
# compare models with different predictors
# ----------------------------------------
library(sjmisc)
data(efc)

# make education categorical
efc$c172code <- to_factor(efc$c172code)
# make education categorical
efc$e42dep <- to_factor(efc$e42dep)

# fit first model
fit1 <- lm(neg_c_7 ~ c160age + c172code + c161sex, data = efc)
# fit second model
fit2 <- lm(neg_c_7 ~ c160age + c172code + c161sex + c12hour, data = efc)
# fit second model
fit3 <- lm(neg_c_7 ~ c160age + c172code + e42dep + tot_sc_e, data = efc)

sjt.lm(fit1, fit2, fit3)

# ----------------------------------------
# compare models with different predictors
# and grouping
# ----------------------------------------
# make cope-index categorical
efc$c82cop1 <- to_factor(efc$c82cop1)
# fit another model
fit4 <- lm(neg_c_7 ~ c160age + c172code + e42dep + tot_sc_e + c82cop1,
           data = efc)

sjt.lm(fit1, fit2, fit4, fit3)

# show standardized beta only
sjt.lm(fit1, fit2, fit4, fit3, show.est = FALSE, show.std = TRUE,
       show.aic = TRUE, show.fstat = TRUE)

# -----------------------------------------------------------
# color insanity. just to show that each column has an own
# CSS-tag, so - depending on the stats and values you show -
# you can define column spaces / margins, border etc. to
# visually separate your models in the table
# -----------------------------------------------------------
sjt.lm(fit1, fit2, fit4, fit3, show.std = TRUE, show.aic = TRUE,
       show.fstat = TRUE, show.se = TRUE,
       CSS = list(css.modelcolumn1 = 'color:blue;',
                  css.modelcolumn2 = 'color:red;',
                  css.modelcolumn3 = 'color:green;',
                  css.modelcolumn4 = 'color:#ffff00;',
                  css.modelcolumn5 = 'color:#777777;',
                  css.modelcolumn6 = 'color:#3399cc;',
                  css.modelcolumn7 = 'color:#cc9933;'))

sjt.lm(fit1, fit2, fit4, fit3, show.est = FALSE, show.std = TRUE,
       p.numeric = FALSE, group.pred = FALSE,
       CSS = list(css.modelcolumn4 = 'border-left:1px solid black;',
                  css.modelcolumn5 = 'padding-right:50px;'))
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab