knitr::opts_chunk$set( collapse = TRUE, warning = FALSE, message = FALSE, fig.retina = 3, comment = "#>" )
The most common function used to view an overall summary of a model is the summary()
function:
library(logitr) model <- logitr( data = yogurt, outcome = "choice", obsID = "obsID", pars = c("price", "feat", "brand") ) summary(model)
The summary prints out a table of the model coefficients as well as other information about the model, such as the log-likelihood, the number of observations, etc.
You can extract the coefficient from the summary table as a data.frame
using coef(summary(model))
:
coefs <- coef(summary(model)) coefs
Another approach for extracting the model coefficients as a data frame is to use the tidy()
function from the {broom} package:
library(broom) coefs <- tidy(model) coefs
The tidy()
function returns a tibble
and provides a more standardized output and interfaces well with other packages. You can also append a confidence interval to the data frame:
coefs <- tidy(model, conf.int = TRUE, conf.level = 0.95) coefs
You can also extract other values of interest at the solution, such as:
The estimated coefficients
coef(model)
The estimated standard errors
se(model)
The log-likelihood
logLik(model)
The variance-covariance matrix
vcov(model)
You can also view a summary of statistics about the model using the glance()
function from the {broom} package:
glance(model)
Often times you will need to create summary tables that are formatted for publication. The {gtsummary} package offers a convenient solution that works well with logitr
models. For example, a formatted summary table can be obtained using the tbl_regression()
function:
library(gtsummary) model |> tbl_regression()
The tbl_regression()
function has many options for customizing the output table. For example, you can change the coefficient names with the label
argument:
model |> tbl_regression( label = list( feat = "Newspaper ad shown?", brand = "Yogurt's brand" ) )
The {gtsummary} package supports a wide variety of output types, including support for LaTeX. One you create the table with tbl_regression()
, you can print it a variety of ways. For example, once you've created a table x
,
x <- model |> tbl_regression()
you can print it to LaTeX with any of the following ways:
as_gt(x) |> gt::as_latex()
as_kable_extra(x, format = "latex")
as_hux_table(x) |> to_latex()
as_kable(x, format = "latex")
Multiple models can also be printed in the same table:
model1 <- model model2 <- logitr( data = yogurt, outcome = "choice", obsID = "obsID", pars = c("price*feat", "brand") ) # Make individual tables t1 <- tbl_regression(model1) t2 <- tbl_regression(model2) # Merge tables tbl_merge( tbls = list(t1, t2), tab_spanner = c("**Baseline**", "**Interaction**") )
Another option for obtaining a formatted table is to use the {texreg} package. This is particularly useful for obtaining tables formatted for use in LaTeX.
For example, you can print a summary to the screen using screenreg()
:
library(texreg) screenreg(model, stars = c(0.01, 0.05, 0.1))
Likewise, you can print the LaTeX code for a summary table using texreg()
library(texreg) texreg(model, stars = c(0.01, 0.05, 0.1))
Similar to {gtsummary}, multiple models can be printed using screenreg()
or texreg()
:
model1 <- model model2 <- logitr( data = yogurt, outcome = "choice", obsID = "obsID", pars = c("price*feat", "brand") ) screenreg( list( model1, model2 ), stars = c(0.01, 0.05, 0.1), custom.model.names = c("Baseline", "Interaction") )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.