项目作者: easystats

项目描述 :
:crystal_ball: Easy access to model information for various model objects
高级语言: R
项目地址: git://github.com/easystats/insight.git
创建时间: 2019-01-29T20:32:45Z
项目社区:https://github.com/easystats/insight

开源协议:GNU General Public License v3.0

下载


" class="reference-link">insight

DOI
downloads
total

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

CRAN_Status_Badge
insight status
badge
codecov

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

Type Source Command
Release CRAN install.packages("insight")
Development r-universe install.packages("insight", repos = "https://easystats.r-universe.dev")
Development GitHub remotes::install_github("easystats/insight")

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

  1. 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

Documentation
Blog
Features

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.

  1. library(insight)
  2. m <- lm(
  3. Sepal.Length ~ Species + Petal.Width + Sepal.Width,
  4. data = iris
  5. )
  6. dat <- get_data(m)
  7. pred <- find_predictors(m, flatten = TRUE)
  8. l <- lapply(pred, function(x) {
  9. if (is.numeric(dat[[x]])) {
  10. mean(dat[[x]])
  11. } else {
  12. unique(dat[[x]])
  13. }
  14. })
  15. names(l) <- pred
  16. l <- as.data.frame(l)
  17. cbind(l, predictions = predict(m, newdata = l))
  18. #> Species Petal.Width Sepal.Width predictions
  19. #> 1 setosa 1.2 3.1 5.1
  20. #> 2 versicolor 1.2 3.1 6.1
  21. #> 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.

  1. print_params <- function(model) {
  2. paste0(
  3. "My parameters are ",
  4. toString(row.names(summary(model)$coefficients)),
  5. ", thank you for your attention!"
  6. )
  7. }
  8. m1 <- lm(Sepal.Length ~ Petal.Width, data = iris)
  9. print_params(m1)
  10. #> [1] "My parameters are (Intercept), Petal.Width, thank you for your attention!"
  11. # obviously, something is missing in the output
  12. m2 <- mgcv::gam(Sepal.Length ~ Petal.Width + s(Petal.Length), data = iris)
  13. print_params(m2)
  14. #> [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.

  1. print_params <- function(model) {
  2. paste0(
  3. "My parameters are ",
  4. toString(insight::find_parameters(model, flatten = TRUE)),
  5. ", thank you for your attention!"
  6. )
  7. }
  8. m1 <- lm(Sepal.Length ~ Petal.Width, data = iris)
  9. print_params(m1)
  10. #> [1] "My parameters are (Intercept), Petal.Width, thank you for your attention!"
  11. m2 <- mgcv::gam(Sepal.Length ~ Petal.Width + s(Petal.Length), data = iris)
  12. print_params(m2)
  13. #> [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 243 model classes are supported.

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