Learn R Programming

⚠️There's a newer version (0.8.9) of this package.Take me there.

modelbased


:warning: estimate_link() now does not transform predictions on the response scale for GLMs. To keep the previous behaviour, use the new estimate_relation() instead. This follows a change in how predictions are made internally (which now relies on get_predicted(), so more details can be found there). This will allow modelbased to be more robust and polyvalent. Apologies for the breaks.


modelbased is a lightweight package helping with model-based estimations, used in the computation of marginal means, contrast analysis and model predictions.

Installation

Run the following to install the stable release of modelbased from CRAN:

install.packages("modelbased")

Or this one to install the latest development version:

install.packages("remotes")
remotes::install_github("easystats/modelbased")

Documentation

Click on the buttons above to access the package documentation and the easystats blog, and check-out these vignettes:

Features

The package is built around 5 main functions:

These functions are powered by the visualisation_matrix() function, a smart tool for guessing the appropriate reference grid.

The package currently only supports rstanarm models, but will be expanded to cover a large variety of frequentist and Bayesian models.

Examples

Create smart grids to represent complex interactions

Check-out this vignette to create this plot:

Estimate marginal means

Check-out this vignette to create this plot:

library(rstanarm)

model <- stan_glm(Sepal.Width ~ Species, data = iris)

estimate_means(model)
## Species    | Mean |       95% CI
## --------------------------------
## setosa     | 3.43 | [3.33, 3.52]
## versicolor | 2.77 | [2.67, 2.86]
## virginica  | 2.97 | [2.88, 3.08]

Contrast analysis

Check-out this vignette to create this plot:

estimate_contrasts(model)
## Level1     |     Level2 | Difference |         95% CI |     pd | % in ROPE | Std_Difference
## -------------------------------------------------------------------------------------------
## setosa     | versicolor |       0.66 | [ 0.52,  0.79] |   100% |        0% |           1.50
## setosa     |  virginica |       0.45 | [ 0.33,  0.59] |   100% |        0% |           1.04
## versicolor |  virginica |      -0.20 | [-0.33, -0.06] | 99.78% |     7.38% |          -0.47

Check the contrasts at different points of another linear predictor

model <- stan_glm(Sepal.Width ~ Species * Petal.Length, data = iris)

estimate_contrasts(model, modulate = "Petal.Length", length = 3)
## Level1     |     Level2 | Petal.Length | Difference |        95% CI |     pd | % in ROPE | Std_Difference
## ---------------------------------------------------------------------------------------------------------
## setosa     | versicolor |         1.00 |       1.68 | [ 1.01, 2.25] |   100% |        0% |           3.86
## setosa     |  virginica |         1.00 |       1.35 | [ 0.59, 2.02] | 99.92% |     0.15% |           3.09
## versicolor |  virginica |         1.00 |      -0.35 | [-1.27, 0.55] | 76.02% |    12.28% |          -0.79
## setosa     | versicolor |         3.95 |       1.61 | [ 0.69, 2.56] |   100% |     0.10% |           3.70
## setosa     |  virginica |         3.95 |       1.67 | [ 0.70, 2.67] |   100% |     0.05% |           3.83
## versicolor |  virginica |         3.95 |       0.05 | [-0.25, 0.32] | 63.18% |    48.08% |           0.11
## setosa     | versicolor |         6.90 |       1.55 | [-0.46, 3.57] | 92.30% |     3.17% |           3.55
## setosa     |  virginica |         6.90 |       1.98 | [-0.07, 3.94] | 97.45% |     1.30% |           4.54
## versicolor |  virginica |         6.90 |       0.44 | [-0.08, 0.99] | 95.40% |     7.95% |           1.01

Find a predictor’s slopes at each factor level

estimate_slopes(model)
## Species    | Median |        95% CI |     pd | % in ROPE | Std. Median
## ----------------------------------------------------------------------
## setosa     |   0.34 | [-0.03, 0.71] | 96.38% |    10.95% |        1.37
## versicolor |   0.36 | [ 0.18, 0.54] | 99.95% |     0.25% |        1.46
## virginica  |   0.23 | [ 0.07, 0.38] | 99.65% |     5.17% |        0.93

Generate predictions from your model to compare it with original data

Check-out this vignette to create this plot:

estimate_response(model)
Sepal.LengthSpeciesPredictedSECI_lowCI_high
5.1setosa1.480.270.982.03
4.9setosa1.440.270.911.98
4.7setosa1.420.270.881.97
4.6setosa1.410.270.901.94
5.0setosa1.460.270.972.04
5.4setosa1.510.271.002.01

Estimate the link between the response and a predictor

See this vignette to create this plot:

model <- stan_glm(Sepal.Width ~ poly(Petal.Length, 2), data = iris)

estimate_link(model)
Petal.LengthPredictedSECI_lowCI_high
1.003.620.063.503.75
1.983.180.043.103.26
2.972.900.052.812.99
3.952.780.052.692.87
4.932.830.042.762.91
5.923.050.062.953.16
6.903.440.123.203.67

Describe the smooth term by its linear parts

estimate_smooth(model)
## Part | Start |  End |   Size | Trend | Linearity
## ------------------------------------------------
## 1    |  1.00 | 3.62 | 50.00% | -0.19 |      0.96
## 2    |  3.62 | 6.90 | 50.00% |  0.08 |      0.81

Copy Link

Version

Install

install.packages('modelbased')

Monthly Downloads

19,013

Version

0.6.0

License

GPL-3

Maintainer

Dominique Makowski

Last Published

April 12th, 2021

Functions in modelbased (0.6.0)

estimate_slopes.lm

Estimate the slopes of a numeric predictor (over different factor levels)
estimate_contrasts.lm

Estimate contrasts
.uniroot.all

Copied from rootSolve package
visualisation_matrix

Create a reference grid
estimate_relation

Generates predictions from models
zero_crossings

Find zero crossings of a vector
estimate_smooth

Describe the smooth term (for GAMs) or non-linear predictors
estimate_slopes.stanreg

Estimate the slopes of a numeric predictor (over different factor levels)
find_inversions

Find points of inversion
smoothing

Smoothing a vector or a time series
estimate_means

Estimate average value of response variable at each factor levels
as.numeric_ifnumeric

Convert to Numeric if Possible
data_restoretype

Restore the type of columns according to a reference data.frame
estimate_contrasts

Estimate contrasts between factor levels
estimate_contrasts.stanreg

Estimate contrasts
estimate_slopes

Estimate the slopes of a numeric predictor (over different factor levels)