knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(message = FALSE)
knitr::opts_chunk$set(warning = FALSE)
library(tablelight)

Standard OLS regression

ols <- lm(
  Sepal.Length ~ Sepal.Width,
  data = iris
)
light_table(
  object = ols, title = "My awesome OLS regression (iris data)",
  type = "html",
  dep.var.labels = "Sepal.Length",
  column.labels = NULL,
  stats.list = c("rsq","adj.rsq","n")
)
print_html(
  light_table(
    object = ols, title = "My awesome OLS regression (iris data)",
    type = "html",
    dep.var.labels = "Sepal.Length",
    column.labels = NULL,
    stats.list = c("rsq","adj.rsq","n")
  )
)

Several tables together

data("bioChemists", package = "pscl")
ols <- lm(
  art ~ phd,
  data = bioChemists
)
glm <- glm(
  art ~ phd + ment,
  data = bioChemists,
  family = poisson()
)
negbin <- MASS::glm.nb(
  art ~ phd + ment,
  data = bioChemists
)
light_table(
  object = list(ols, glm, strip(negbin)),
  title = "Compare models on IRIS data",
  type = "html",
  dep.var.labels = "Article",
  column.labels = c("OLS","Poisson","MASS::NegBin"),
  stats.list = c("alpha","n","lln","bic","link"),
  add.lines = "This table compares several models"
)
print_html(
  light_table(
    object = list(ols, glm, strip(negbin)),
    title = "Compare models using BioChemists data (pscl package)",
    type = "html",
    dep.var.labels = "Sepal.Length",
    column.labels = c("OLS","Poisson","MASS::NegBin"),
    stats.list = c("alpha","n","lln","bic","link"),
    add.lines = "This table compares several models"
  )
)

Zero-inflated models

zip <- pscl::zeroinfl(
  art ~ phd + ment,
  data = bioChemists,
  dist = "poisson",
  link = "probit"
)
zinb <- pscl::zeroinfl(
  art ~ phd + ment,
  data = bioChemists,
  dist = "negbin",
  link = "probit"
)
light_table(
  object = lapply(list(glm, zip, zip, zinb, zinb),strip),
  modeltype = c("outcome", "outcome", "selection",
                "outcome","selection"),
  title = "Compare models using BioChemists data (pscl package)",
  type = "html",
  dep.var.labels = c("Non-zero inflated","ZIP","ZINB"),
  dep.var.separate = c(1,2,2),
  column.labels = c("Poisson","Selection","Outcome",
                    "Selection","Outcome"),
  stats.list = c("alpha","n","lln","bic","link"),
  add.lines = "It is quite easy to make selection and outcome levels appear together"
)
print_html(
  light_table(
    object = lapply(list(glm, zip, zip, zinb, zinb),strip),
    modeltype = c("outcome", "outcome", "selection",
                  "outcome","selection"),
    title = "Compare models using BioChemists data (pscl package)",
    type = "html",
    dep.var.labels = c("Non-zero inflated","ZIP","ZINB"),
    dep.var.separate = c(1,2,2),
    column.labels = c("Poisson","Selection","Outcome",
                      "Selection","Outcome"),
    stats.list = c("alpha","n","lln","bic","link"),
    add.lines = "It is quite easy to make selection and outcome levels appear together"
  )
)

nnet models

n<-250
x1<-sample(c(0,1),n,replace=TRUE,prob=c(0.75,0.25))
x2<-vector("numeric",n)
x2[x1==0]<-sample(c(0,1),n-sum(x1==1),replace=TRUE,prob=c(2/3,1/3))
z<-rnorm(n,0.5)
# create latent outcome variable
latenty<-0.5+1.5*x1-0.5*x2+0.5*z+rnorm(n,sd=exp(0.5*x1-0.5*x2))
# observed y has four possible values: -1,0,1,2
# threshold values are: -0.5, 0.5, 1.5.
y<-vector("numeric",n)
y[latenty< -0.5]<--1
y[latenty>= -0.5 & latenty<0.5]<- 0
y[latenty>= 0.5 & latenty<1.5]<- 1
y[latenty>= 1.5]<- 2
dataset<-data.frame(y,x1,x2)


logit <- nnet::multinom(y ~ x1 + x2 + z, data=dataset)
light_table(
  logit, type = "html",
  dep.var.labels = "Outcome variable",
  column.labels = c("Low","Middle","High"),
  stats.list = c("n","lln"),
  covariate.labels = c("Variable 1","Another variable","A third variable"),
  omit = "(Intercept)",
  add.lines = "With multinomial logit, dependent variable modality"
)
print_html(
  light_table(
    logit, type = "html",
    title = "Different output for nnet::multinom regressions",
    dep.var.labels = "Outcome variable",
    column.labels = c("Low","Middle","High"),
    stats.list = c("n","lln"),
    omit = "(Intercept)",
    covariate.labels = c("Variable 1","Another variable","A third variable"),
    add.lines = "With multinomial logit, dependent variable modality"
  )
)


linogaliana/tablelight documentation built on Jan. 29, 2021, 10:30 a.m.