Learn R Programming

insight

Gain insight into your models!

When fitting any statistical model, there are many useful pieces of information that are simultaneously calculated and stored beyond coefficient estimates and general model fit statistics. Although there exist some generic functions to obtain model information and data, many package-specific modelling functions do not provide such methods to allow users to access such valuable information.

insight is an R-package that fills this important gap by providing a suite of functions to support almost any model (see a list of the many models supported below in the List of Supported Packages and Models section). The goal of insight, then, is to provide tools to provide easy, intuitive, and consistent access to information contained in model objects. These tools aid applied research in virtually any field who fit, diagnose, and present statistical models by streamlining access to every aspect of many model objects via consistent syntax and output.

Installation

The insight package is available on CRAN, while its latest development version is available on R-universe (from rOpenSci) or GitHub.

TypeSourceCommand
ReleaseCRANinstall.packages("insight")
Developmentr-universeinstall.packages("insight", repos = "https://easystats.r-universe.dev")
DevelopmentGitHubremotes::install_github("easystats/insight")

Once you have downloaded the package, you can then load it using:

library("insight")

Tip

Instead of library(insight), use library(easystats). This will make all features of the easystats-ecosystem available.

To stay updated, use easystats::install_latest().

Documentation

Built with non-programmers in mind, insight offers a broad toolbox for making model and data information easily accessible. While insight offers many useful functions for working with and understanding model objects (discussed below), we suggest users start with model_info(), as this function provides a clean and consistent overview of model objects (e.g., functional form of the model, the model family, link function, number of observations, variables included in the specification, etc.). With a clear understanding of the model introduced, users are able to adapt other functions for more nuanced exploration of and interaction with virtually any model object.Please visit https://easystats.github.io/insight/ for documentation.

Definition of Model Components

The functions from insight address different components of a model. In an effort to avoid confusion about specific “targets” of each function, in this section we provide a short explanation of insight’s definitions of regression model components.

Data

The dataset used to fit the model.

Parameters

Values estimated or learned from data that capture the relationship between variables. In regression models, these are usually referred to as coefficients.

Response and Predictors

  • response: the outcome or response variable (dependent variable) of a regression model.
  • predictor: independent variables of (the fixed part of) a regression model. For mixed models, variables that are only in the random effects part (i.e. grouping factors) of the model are not returned as predictors by default. However, these can be included using additional arguments in the function call, treating predictors are “unique”. As such, if a variable appears as a fixed effect and a random slope, it is treated as one (the same) predictor.

Variables

Any unique variable names that appear in a regression model, e.g., response variable, predictors or random effects. A “variable” only relates to the unique occurence of a term, or the term name. For instance, the expression x + poly(x, 2) has only the variable x.

Terms

Terms themselves consist of variable and factor names separated by operators, or involve arithmetic expressions. For instance, the expression x + poly(x, 2) has one variable x, but two terms x and poly(x, 2).

Random Effects

  • random slopes: variables that are specified as random slopes in a mixed effects model.
  • random or grouping factors: variables that are specified as grouping variables in a mixed effects model.

Aren’t the predictors, terms and parameters the same thing?

In some cases, yes. But not in all cases. Find out more by clicking here to access the documentation.

Functions

The package revolves around two key prefixes: get_* and find_*. The get_* prefix extracts values (or data) associated with model-specific objects (e.g., parameters or variables), while the find_* prefix lists model-specific objects (e.g., priors or predictors). These are powerful families of functions allowing for great flexibility in use, whether at a high, descriptive level (find_*) or narrower level of statistical inspection and reporting (get_*).

In total, the insight package includes 16 core functions: get_data(), get_priors(), get_variance(), get_parameters(), get_predictors(), get_random(), get_response(), find_algorithm(), find_formula(), find_variables(), find_terms(), find_parameters(), find_predictors(), find_random(), find_response(), and model_info(). In all cases, users must supply at a minimum, the name of the model fit object. In several functions, there are additional arguments that allow for more targeted returns of model information. For example, the find_terms() function’s effects argument allows for the extraction of “fixed effects” terms, “random effects” terms, or by default, “all” terms in the model object. We point users to the package documentation or the complementary package website, https://easystats.github.io/insight/, for a detailed list of the arguments associated with each function as well as the returned values from each function.

Examples of Use Cases in R

We now would like to provide examples of use cases of the insight package. These examples probably do not cover typical real-world problems, but serve as illustration of the core idea of this package: The unified interface to access model information. insight should help both users and package developers in order to reduce the hassle with the many exceptions from various modelling packages when accessing model information.

Making Predictions at Specific Values of a Term of Interest

Say, the goal is to make predictions for a certain term, holding remaining co-variates constant. This is achieved by calling predict() and feeding the newdata-argument with the values of the term of interest as well as the “constant” values for remaining co-variates. The functions get_data() and find_predictors() are used to get this information, which then can be used in the call to predict().

In this example, we fit a simple linear model, but it could be replaced by (m)any other models, so this approach is “universal” and applies to many different model objects.

library(insight)
m <- lm(
  Sepal.Length ~ Species + Petal.Width + Sepal.Width,
  data = iris
)

dat <- get_data(m)
pred <- find_predictors(m, flatten = TRUE)

l <- lapply(pred, function(x) {
  if (is.numeric(dat[[x]])) {
    mean(dat[[x]])
  } else {
    unique(dat[[x]])
  }
})

names(l) <- pred
l <- as.data.frame(l)

cbind(l, predictions = predict(m, newdata = l))
#>      Species Petal.Width Sepal.Width predictions
#> 1     setosa         1.2         3.1         5.1
#> 2 versicolor         1.2         3.1         6.1
#> 3  virginica         1.2         3.1         6.3

Printing Model Coefficients

The next example should emphasize the possibilities to generalize functions to many different model objects using insight. The aim is simply to print coefficients in a complete, human readable sentence.

The first approach uses the functions that are available for some, but obviously not for all models, to access the information about model coefficients.

print_params <- function(model) {
  paste0(
    "My parameters are ",
    toString(row.names(summary(model)$coefficients)),
    ", thank you for your attention!"
  )
}

m1 <- lm(Sepal.Length ~ Petal.Width, data = iris)
print_params(m1)
#> [1] "My parameters are (Intercept), Petal.Width, thank you for your attention!"

# obviously, something is missing in the output
m2 <- mgcv::gam(Sepal.Length ~ Petal.Width + s(Petal.Length), data = iris)
print_params(m2)
#> [1] "My parameters are , thank you for your attention!"

As we can see, the function fails for gam-models. As the access to models depends on the type of the model in the R ecosystem, we would need to create specific functions for all models types. With insight, users can write a function without having to worry about the model type.

print_params <- function(model) {
  paste0(
    "My parameters are ",
    toString(insight::find_parameters(model, flatten = TRUE)),
    ", thank you for your attention!"
  )
}

m1 <- lm(Sepal.Length ~ Petal.Width, data = iris)
print_params(m1)
#> [1] "My parameters are (Intercept), Petal.Width, thank you for your attention!"

m2 <- mgcv::gam(Sepal.Length ~ Petal.Width + s(Petal.Length), data = iris)
print_params(m2)
#> [1] "My parameters are (Intercept), Petal.Width, s(Petal.Length), thank you for your attention!"

Contributing and Support

In case you want to file an issue or contribute in another way to the package, please follow this guide. For questions about the functionality, you may either contact us via email or also file an issue.

List of Supported Models by Class

Currently, about 238 model classes are supported.

supported_models()
#>   [1] "aareg"                   "afex_aov"               
#>   [3] "AKP"                     "Anova.mlm"              
#>   [5] "anova.rms"               "aov"                    
#>   [7] "aovlist"                 "Arima"                  
#>   [9] "asym"                    "averaging"              
#>  [11] "bamlss"                  "bamlss.frame"           
#>  [13] "bayesQR"                 "bayesx"                 
#>  [15] "BBmm"                    "BBreg"                  
#>  [17] "bcplm"                   "betamfx"                
#>  [19] "betaor"                  "betareg"                
#>  [21] "BFBayesFactor"           "bfsl"                   
#>  [23] "BGGM"                    "bife"                   
#>  [25] "bifeAPEs"                "bigglm"                 
#>  [27] "biglm"                   "blavaan"                
#>  [29] "blrm"                    "bracl"                  
#>  [31] "brglm"                   "brmsfit"                
#>  [33] "brmultinom"              "btergm"                 
#>  [35] "censReg"                 "cgam"                   
#>  [37] "cgamm"                   "cglm"                   
#>  [39] "clm"                     "clm2"                   
#>  [41] "clmm"                    "clmm2"                  
#>  [43] "clogit"                  "coeftest"               
#>  [45] "complmrob"               "confusionMatrix"        
#>  [47] "coxme"                   "coxph"                  
#>  [49] "coxph.penal"             "coxph_weightit"         
#>  [51] "coxr"                    "cpglm"                  
#>  [53] "cpglmm"                  "crch"                   
#>  [55] "crq"                     "crqs"                   
#>  [57] "crr"                     "dep.effect"             
#>  [59] "DirichletRegModel"       "draws"                  
#>  [61] "drc"                     "eglm"                   
#>  [63] "elm"                     "emmGrid"                
#>  [65] "epi.2by2"                "ergm"                   
#>  [67] "fdm"                     "feglm"                  
#>  [69] "feis"                    "felm"                   
#>  [71] "fitdistr"                "fixest"                 
#>  [73] "flac"                    "flexsurvreg"            
#>  [75] "flic"                    "gam"                    
#>  [77] "Gam"                     "gamlss"                 
#>  [79] "gamm"                    "gamm4"                  
#>  [81] "garch"                   "gbm"                    
#>  [83] "gee"                     "geeglm"                 
#>  [85] "ggcomparisons"           "glht"                   
#>  [87] "glimML"                  "glm"                    
#>  [89] "Glm"                     "glm_weightit"           
#>  [91] "glmerMod"                "glmgee"                 
#>  [93] "glmm"                    "glmmadmb"               
#>  [95] "glmmPQL"                 "glmmTMB"                
#>  [97] "glmrob"                  "glmRob"                 
#>  [99] "glmx"                    "gls"                    
#> [101] "gmnl"                    "hglm"                   
#> [103] "HLfit"                   "htest"                  
#> [105] "hurdle"                  "iv_robust"              
#> [107] "ivFixed"                 "ivprobit"               
#> [109] "ivreg"                   "lavaan"                 
#> [111] "lm"                      "lm_robust"              
#> [113] "lme"                     "lmerMod"                
#> [115] "lmerModLmerTest"         "lmodel2"                
#> [117] "lmrob"                   "lmRob"                  
#> [119] "logistf"                 "logitmfx"               
#> [121] "logitor"                 "logitr"                 
#> [123] "LORgee"                  "lqm"                    
#> [125] "lqmm"                    "lrm"                    
#> [127] "manova"                  "MANOVA"                 
#> [129] "marginaleffects"         "marginaleffects.summary"
#> [131] "margins"                 "maxLik"                 
#> [133] "mblogit"                 "mclogit"                
#> [135] "mcmc"                    "mcmc.list"              
#> [137] "MCMCglmm"                "mcp1"                   
#> [139] "mcp12"                   "mcp2"                   
#> [141] "med1way"                 "mediate"                
#> [143] "merMod"                  "merModList"             
#> [145] "meta_bma"                "meta_fixed"             
#> [147] "meta_random"             "metaplus"               
#> [149] "mhurdle"                 "mipo"                   
#> [151] "mira"                    "mixed"                  
#> [153] "MixMod"                  "mixor"                  
#> [155] "mjoint"                  "mle"                    
#> [157] "mle2"                    "mlm"                    
#> [159] "mlogit"                  "mmclogit"               
#> [161] "mmlogit"                 "mmrm"                   
#> [163] "mmrm_fit"                "mmrm_tmb"               
#> [165] "model_fit"               "multinom"               
#> [167] "multinom_weightit"       "mvord"                  
#> [169] "negbinirr"               "negbinmfx"              
#> [171] "nestedLogit"             "ols"                    
#> [173] "onesampb"                "ordinal_weightit"       
#> [175] "orm"                     "pgmm"                   
#> [177] "phyloglm"                "phylolm"                
#> [179] "plm"                     "PMCMR"                  
#> [181] "poissonirr"              "poissonmfx"             
#> [183] "polr"                    "probitmfx"              
#> [185] "psm"                     "Rchoice"                
#> [187] "ridgelm"                 "riskRegression"         
#> [189] "rjags"                   "rlm"                    
#> [191] "rlmerMod"                "RM"                     
#> [193] "rma"                     "rma.uni"                
#> [195] "robmixglm"               "robtab"                 
#> [197] "rq"                      "rqs"                    
#> [199] "rqss"                    "rvar"                   
#> [201] "Sarlm"                   "scam"                   
#> [203] "selection"               "sem"                    
#> [205] "SemiParBIV"              "semLm"                  
#> [207] "semLme"                  "seqanova.svyglm"        
#> [209] "serp"                    "slm"                    
#> [211] "speedglm"                "speedlm"                
#> [213] "stanfit"                 "stanmvreg"              
#> [215] "stanreg"                 "summary.lm"             
#> [217] "survfit"                 "survreg"                
#> [219] "svy_vglm"                "svy2lme"                
#> [221] "svychisq"                "svyglm"                 
#> [223] "svyolr"                  "t1way"                  
#> [225] "tobit"                   "trimcibt"               
#> [227] "truncreg"                "vgam"                   
#> [229] "vglm"                    "wbgee"                  
#> [231] "wblm"                    "wbm"                    
#> [233] "wmcpAKP"                 "yuen"                   
#> [235] "yuend"                   "zcpglm"                 
#> [237] "zeroinfl"                "zerotrunc"
  • Didn’t find a model? File an issue and request additional model-support in insight!

Citation

If this package helped you, please consider citing as follows:

Lüdecke D, Waggoner P, Makowski D. insight: A Unified Interface to Access Information from Model Objects in R. Journal of Open Source Software 2019;4:1412. doi: 10.21105/joss.01412

Code of Conduct

Please note that the insight project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Copy Link

Version

Install

install.packages('insight')

Monthly Downloads

163,159

Version

1.0.0

License

GPL-3

Maintainer

Last Published

November 26th, 2024

Functions in insight (1.0.0)

find_random_slopes

Find names of random slopes
find_parameters

Find names of model parameters
find_parameters.glmmTMB

Find names of model parameters from mixed models
find_parameters.zeroinfl

Find names of model parameters from zero-inflated models
find_predictors

Find names of model predictors
find_random

Find names of random effects
find_statistic

Find statistic for model
find_smooth

Find smooth terms from a model object
fish

Sample data set
format_bf

Bayes Factor formatting
find_weights

Find names of model weights
find_variables

Find names of all variables
find_parameters.gamlss

Find names of model parameters from generalized additive models
find_parameters.emmGrid

Find model parameters from estimated marginal means objects
format_number

Convert number to words
format_message

Format messages and warnings
format_value

Numeric Values Formatting
format_capitalize

Capitalizes the first letter in a string
get_auxiliary

Get auxiliary parameters from models
format_ci

Confidence/Credible Interval (CI) Formatting
find_response

Find name of the response variable
format_table

Parameter table formatting
format_rope

Percentage in ROPE formatting
get_data

Get the data that was used to fit the model
get_datagrid

Create a reference grid
format_string

String Values Formatting
get_call

Get the model's function call
find_terms

Find all model terms
format_pd

Probability of direction (pd) formatting
find_transformation

Find possible transformation of model variables
format_p

p-values formatting
get_deviance

Model Deviance
get_family

A robust alternative to stats::family
get_df

Extract degrees of freedom
get_intercept

Get the value at the intercept
get_loglikelihood

Log-Likelihood and Log-Likelihood correction
get_modelmatrix

Model Matrix
get_parameters.betareg

Get model parameters from models with special components
get_parameters.betamfx

Get model parameters from marginal effects models
get_parameters.BGGM

Get model parameters from Bayesian models
get_parameters

Get model parameters
get_priors

Get summary of priors used for a model
get_random

Get the data from random effects
get_parameters.zeroinfl

Get model parameters from zero-inflated and hurdle models
get_parameters.emmGrid

Get model parameters from estimated marginal means objects
get_parameters.htest

Get model parameters from htest-objects
get_parameters.gamm

Get model parameters from generalized additive models
get_parameters.glmmTMB

Get model parameters from mixed models
get_predicted

Model predictions (robust) and their confidence intervals
get_predictors

Get the data from model predictors
get_predicted_ci

Confidence intervals around predicted values
get_statistic

Get statistic associated with estimates
get_residuals

Extract model residuals
get_sigma

Get residual standard deviation from models
get_varcov

Get variance-covariance matrix from models
get_weights

Get the values from model weights
insight-package

insight: A Unified Interface to Access Information from Model Objects in R.
get_response

Get the values from the response variable
get_transformation

Return function of transformed response variables
has_intercept

Checks if model has an intercept
get_variance

Get variance components from random effects models
is_model_supported

Checks if a regression model object is supported by the insight package
is_nullmodel

Checks if model is a null-model (intercept-only)
is_model

Checks if an object is a regression model or statistical test object
is_gam_model

Checks if a model is a generalized additive model
is_converged

Convergence test for mixed effects models
link_function

Get link-function from model object
is_mixed_model

Checks if a model is a mixed effects model
link_inverse

Get link-inverse function from model object
is_nested_models

Checks whether a list of models are nested models
model_name

Name the model
is_multivariate

Checks if an object stems from a multivariate response model
n_parameters

Count number of parameters in a model
n_obs

Get number of observations from a model
null_model

Compute intercept-only model for regression models
object_has_names

Check names and rownames
n_grouplevels

Count number of random effect levels in a mixed model
validate_argument

Validate arguments against a given set of options
trim_ws

Small helper functions
model_info

Access information from model objects
text_remove_backticks

Remove backticks from a string
is_empty_object

Check if object is empty
print_parameters

Prepare summary statistics of model parameters for printing
standardize_column_order

Standardize column order
standardize_names

Standardize column names
print_color

Coloured console output
compact_list

Remove empty elements from lists
clean_names

Get clean names of model terms
compact_character

Remove empty strings from character
display

Generic export of data frames into formatted tables
clean_parameters

Get clean names of model parameters
download_model

Download circus models
check_if_installed

Checking if needed package is installed
color_if

Color-formatting for data columns based on condition
.colour_detect

Detect coloured cells
all_models_equal

Checks if all objects are models of same class
find_parameters.BGGM

Find names of model parameters from Bayesian models
find_parameters.betamfx

Find names of model parameters from marginal effects models
find_interactions

Find interaction terms from models
apply_table_theme

Data frame and Tables Pretty Formatting
ellipsis_info

Gather information about objects in ellipsis (dot dot dot)
find_offset

Find possible offset terms in a model
find_algorithm

Find sampling algorithm and optimizers
find_parameters.averaging

Find model parameters from models with special components
find_formula

Find model formula