You write R packages and functions? This package will change your life!

What is it?

We are talking about the insight package. It is what allows other packages, like easystats (parameters, effectsize, performance, report, …) or ggstatsplot, sjstats or modelsummary to be as powerful as they are, supporting tons of different R models. So why make you life hard when you can be like them, and rely on insight?

It is made for developers (and users) that do some postprocessing of different models (e.g., extracting stuff like parameters, values, data, names, specifications, predictions, priors, etc.), whether it is to nicely display their results or to do further computation.

If you work with, and around, different R models, then this package is a must-have that will change your life.

What’s the problem

Because R has so many different packages, different models were implemented by different people in a different way. As a consequence, there are different ways of accessing the same stuff from each model.

For example, let’s say you want to find the names of the predictors (the independent variables) of a linear model. One way would be like this:

model_lm <- lm(mpg ~ drat * wt, data=mtcars)

names(model_lm$model)[-1]
## [1] "drat" "wt"

But what in the case of a lme4’s mixed model? Well the solution is a bit different, plus it’s not easy to drop the random factors…

model_lmer <- lme4::lmer(mpg ~ drat * wt + (1|cyl), data=mtcars)

names(model_lmer@frame)[-1]
## [1] "drat" "wt"   "cyl"

And what in the case of a GAMM4’s general additive model?

model_gam <- gamm4::gamm4(mpg ~ drat + wt + s(qsec), data=mtcars)

head(names(model_gam$gam$model)[-1], -3)
## [1] "drat" "wt"   "qsec"

Again different! Maybe you could do something like that, but then you have to account for all the edgecases and so on. And trust us, that’s a lot of work to have a robust and bug-free solution.

How ‘insight’ addresses it

insight allows you to extract stuff from all models in a consistent and robust way. For instance, for the fixed predictors of the examples above, here’s how you would do it with insight:

library(insight)

find_predictors(model_lm)
## $conditional
## [1] "drat" "wt"
find_predictors(model_lmer)
## $conditional
## [1] "drat" "wt"
find_predictors(model_gam)
## $conditional
## [1] "drat" "wt"   "qsec"

Boom! One function that works for all the models. And that’s not all, insight can help you extract data, parameters, intercepts, degrees of freedom, sigma, variance, predicted values, variable names, interaction terms, random factors, smooth terms, etc. etc. Basically, everything you might need. And if what you need is not there, just ask for it.

You can check all that insight can do here.

More reasons to use it

If you’re worried about adding a new dependency to your package, don’t be! Because insight is super light: it itself has no dependencies. So it’s a safe choice to add and rely on!

It includes other useful features that you don’t know you absolutely need, like value formatting, nice printing of text, table and data.frames exporting and much more! Check out them here.

Get Involved

easystats is a project in active development, looking for contributors and supporters. Thus, do not hesitate to contact us if you want to get involved :)

  • Check out our other blog posts here!

Stay tuned

To be updated about the upcoming features and cool R or data science stuff, you can follow the packages on GitHub (click on one of the easystats package) and then on the Watch button on the top right corner) as well as the easystats team on twitter and online: