Learn R Programming

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

modelbased

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.34, 3.52]
## versicolor | 2.77 | [2.68, 2.86]
## virginica  | 2.98 | [2.88, 3.07]

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.53,  0.79] |   100% |        0% |           1.51
## setosa     |  virginica |       0.45 | [ 0.31,  0.57] |   100% |        0% |           1.04
## versicolor |  virginica |      -0.21 | [-0.33, -0.07] | 99.83% |     6.53% |          -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.05, 2.29] |   100% |        0% |           3.85
## setosa     |  virginica |         1.00 |       1.34 | [ 0.63, 2.08] | 99.98% |     0.05% |           3.08
## versicolor |  virginica |         1.00 |      -0.33 | [-1.25, 0.62] | 76.02% |    13.23% |          -0.75
## setosa     | versicolor |         3.95 |       1.61 | [ 0.67, 2.56] | 99.95% |     0.12% |           3.70
## setosa     |  virginica |         3.95 |       1.66 | [ 0.64, 2.63] | 99.92% |     0.12% |           3.82
## versicolor |  virginica |         3.95 |       0.05 | [-0.24, 0.34] | 64.03% |    48.77% |           0.13
## setosa     | versicolor |         6.90 |       1.51 | [-0.45, 3.65] | 92.27% |     2.38% |           3.47
## setosa     |  virginica |         6.90 |       1.97 | [-0.05, 4.06] | 96.95% |     1.60% |           4.53
## versicolor |  virginica |         6.90 |       0.44 | [-0.10, 0.99] | 94.38% |     7.83% |           1.02

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] | 95.58% |    10.30% |        1.37
## versicolor |   0.36 | [ 0.17, 0.54] | 99.98% |     0.27% |        1.46
## virginica  |   0.23 | [ 0.08, 0.38] | 99.85% |     5.42% |        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.LengthSpeciesPredictedCI_lowCI_high
5.1setosa1.481.012.07
4.9setosa1.450.931.98
4.7setosa1.410.881.89
4.6setosa1.410.831.92
5.0setosa1.460.961.98
5.4setosa1.521.012.04

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.LengthPredictedCI_lowCI_high
1.003.623.493.76
1.983.183.103.26
2.972.892.802.99
3.952.782.682.86
4.932.832.762.90
5.923.052.943.17
6.903.443.203.68

Describe the smooth term by its linear parts

estimate_smooth(model)
## Part | Start |  End |   Size |     Trend | Linearity
## ----------------------------------------------------
## 1    |  1.00 | 4.11 | 53.00% | -8.07e-03 |      0.94
## 2    |  4.11 | 6.90 | 47.00% |  6.89e-03 |      0.93

Copy Link

Version

Install

install.packages('modelbased')

Monthly Downloads

21,707

Version

0.5.1

License

GPL-3

Maintainer

Dominique Makowski

Last Published

January 27th, 2021

Functions in modelbased (0.5.1)

estimate_contrasts

Estimate contrasts between factor levels
estimate_means

Estimate average value of response variable at each factor levels
estimate_contrasts.lm

Estimate contrasts
estimate_means.stanreg

Estimate marginal means
estimate_means.lm

Estimate marginal means
as.numeric_ifnumeric

Convert to Numeric if Possible
estimate_response

Generates predictions
estimate_contrasts.stanreg

Estimate contrasts
.uniroot.all

Copied from rootSolve package
estimate_response.glm

Generates predictions for Frequentist models
find_inversions

Find points of inversion
reshape_draws

Reshape estimations with Bayesian posterior draws to long format
estimate_smooth.stanreg

Describe the smooth term (for GAMs) or non-linear predictors
estimate_smooth

Describe the smooth term (for GAMs) or non-linear predictors
visualisation_matrix

Create a reference grid
smoothing

Smoothing a vector or a time series
estimate_slopes.stanreg

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

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

Find zero crossings of a vector
estimate_slopes

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

Generates predictions for Bayesian models