Learn R Programming

sjPlot (version 1.7)

sjt.lm: Show linear regression as HTML table

Description

Shows (multiple) fitted linear models (beta coefficients, std. beta values etc.) as HTML table, or saves them as file. The fitted models may hav different predictors, e.g. when comparing different stepwise fitted models.

Usage

sjt.lm(..., file = NULL, labelPredictors = NULL,
  labelDependentVariables = NULL, stringPredictors = "Predictors",
  stringDependentVariables = "Dependent Variables", stringModel = "Model",
  showHeaderStrings = FALSE, stringIntercept = "(Intercept)",
  stringObservations = "Observations", stringB = "B",
  stringSB = "std. Beta", stringCI = "CI", stringSE = "std. Error",
  stringP = "p", showEst = TRUE, showConfInt = TRUE,
  showStdBeta = FALSE, showStdError = FALSE, digits.est = 2,
  digits.p = 3, digits.ci = 2, digits.se = 2, digits.sb = 2,
  digits.summary = 3, pvaluesAsNumbers = TRUE, boldpvalues = TRUE,
  separateConfColumn = TRUE, newLineConf = TRUE, group.pred = TRUE,
  showAbbrHeadline = TRUE, showR2 = TRUE, showFStat = FALSE,
  showAIC = FALSE, remove.estimates = NULL, cellSpacing = 0.2,
  cellGroupIndent = 0.6, encoding = NULL, CSS = NULL, useViewer = TRUE,
  no.output = FALSE, remove.spaces = TRUE)

Arguments

Value

Invisibly returns a structure with
  • 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.

See Also

  • http://strengejacke.de/sjPlot/sjt.lm/{sjPlot manual: sjt.lm}
  • sjt.glm
  • sjp.lm

Examples

Run this code
# 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.
data(efc)

# attach variable labels to each variable of the data
# frame - useful for automatic label detection
efc <- set_var_labels(efc, get_var_labels(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 parameter
sjt.lm(fit1,
       fit2,
       labelDependentVariables = c("Barthel-Index",
                                   "Negative Impact"),
       labelPredictors = c("Carer's Age",
                           "Hours of Care",
                           "Carer's Sex",
                           "Educational Status"))

# show HTML-table, indicating p-values as asterisks
sjt.lm(fit1,
       fit2,
       labelDependentVariables = c("Barthel-Index",
                                   "Negative Impact"),
       labelPredictors = c("Carer's Age",
                           "Hours of Care",
                           "Carer's Sex",
                           "Educational Status"),
       showStdBeta = TRUE,
       pvaluesAsNumbers = FALSE)

# create and open HTML-table in RStudio Viewer Pane or web browser,
# integrate CI in estimate column
sjt.lm(fit1,
       fit2,
       labelDependentVariables = c("Barthel-Index",
                                   "Negative Impact"),
       labelPredictors = c("Carer's Age",
                           "Hours of Care",
                           "Carer's Sex",
                           "Educational Status"),
       separateConfColumn = FALSE)

# show HTML-table, indicating p-values as numbers
# and printing CI in a separate column
sjt.lm(fit1,
       fit2,
       labelDependentVariables = c("Barthel-Index",
                                   "Negative Impact"),
       labelPredictors = c("Carer's Age",
                           "Hours of Care",
                           "Carer's Sex",
                           "Educational Status"),
       showStdBeta = TRUE)

# show HTML-table, indicating p-values as stars
# and integrate CI in estimate column
sjt.lm(fit1,
       fit2,
       labelDependentVariables = c("Barthel-Index",
                                   "Negative Impact"),
       labelPredictors = c("Carer's Age",
                           "Hours of Care",
                           "Carer's Sex",
                           "Educational Status"),
       showStdBeta = TRUE,
       pvaluesAsNumbers = FALSE,
       separateConfColumn = 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,
                labelDependentVariables = c("Barthel-Index",
                                            "Negative Impact"),
                labelPredictors = c("Carer's Age",
                                    "Hours of Care",
                                    "Carer's Sex",
                                    "Educational Status"))
# create and save second HTML-table
part2 <- sjt.lm(fit3,
                fit4,
                labelDependentVariables = c("Service Usage",
                                            "Elder's Dependency"),
                labelPredictors = c("Carer's Age",
                                    "Hours of Care",
                                    "Carer's Sex",
                                    "Educational Status"))
# 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,
       labelDependentVariables = c("Barthel-Index", "Negative Impact"),
       labelPredictors = c("Carer's Age",
                           "Hours of Care",
                           "Carer's Sex",
                           "Educational Status"),
       CSS = list(css.table = "border: 2px solid;",
                  css.tdata = "border: 1px solid;",
                  css.depvarhead = "color:#003399;"))

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

# attach variable labels to each variable of the data
# frame - useful for automatic label detection
efc <- set_var_labels(efc, get_var_labels(efc))

# make education categorical
efc$c172code <- to_fac(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
# ----------------------------------------
data(efc)

# attach variable labels to each variable of the data
# frame - useful for automatic label detection
efc <- set_var_labels(efc, get_var_labels(efc))

# make education categorical
efc$c172code <- to_fac(efc$c172code)
# make education categorical
efc$e42dep <- to_fac(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_fac(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,
       showEst = FALSE,
       showStdBeta = TRUE,
       showAIC = TRUE,
       showFStat = 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,
       showStdBeta = TRUE,
       showAIC = TRUE,
       showFStat = TRUE,
       showStdError = 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;'))

sjt.lm(fit1, fit2, fit4, fit3,
       showEst = FALSE,
       showStdBeta = TRUE,
       pvaluesAsNumbers = FALSE,
       group.pred = FALSE,
       CSS = list(css.modelcolumn4 = 'border-left:1px solid black;',
                  css.modelcolumn5 = 'padding-right:50px;'))

Run the code above in your browser using DataLab