Learn R Programming

RprobitB

{RprobitB} is an R package for modeling and explaining choices among discrete alternatives.

The package name is a portmanteau, combining R (the programming language), probit (the model class) and B (for Bayesian, the estimation method).

The package is documented in several vignettes, see here.

Installation

You can install the released version of {RprobitB} from CRAN with:

install.packages("RprobitB")

Next, load it via:

library("RprobitB")
#> Thanks for using {RprobitB} version 1.2.0, happy choice modeling!
#> Documentation: https://loelschlaeger.de/RprobitB

Example

We analyze a data set of 2929 stated choices by 235 Dutch individuals deciding between two virtual train trip options based on the price, the travel time, the level of comfort, and the number of changes.

The following lines fit a probit model that explains the chosen trip alternatives (choice) by their price, time, number of changes, and level of comfort (the lower this value the higher the comfort). For normalization, the price coefficient is fixed to -1, which allows to interpret the other coefficients as monetary values:

form <- choice ~ price + time + change + comfort | 0
data <- prepare_data(form, train_choice, id = "deciderID", idc = "occasionID")
plot(data, by_choice = TRUE)
model <- fit_model(data, scale = "price := -1")

The summary method provides summary statistics about the Gibbs samples:

FUN <- c("mean" = mean, "mode" = mode_approx, "sd" = stats::sd, "R^" = R_hat)
summary(model, FUN = FUN)
#> Probit model
#> Formula: choice ~ price + time + change + comfort | 0 
#> R: 1000, B: 500, Q: 1
#> Level: Utility differences with respect to alternative 'B'.
#> Scale: Coefficient of effect 'price' (alpha_1) fixed to -1.
#> 
#> Gibbs sample statistics
#>           mean    mode      sd      R^
#>  alpha
#>                                       
#>      1   -1.00   -1.00    0.00    1.00
#>      2  -25.84  -25.86    2.18    1.00
#>      3   -4.96   -4.85    0.81    1.00
#>      4  -14.36  -14.64    0.89    1.00
#> 
#>  Sigma
#>                                       
#>    1,1  648.51  622.88   63.32    1.00

The estimated effects obtained from the Gibbs sample means can be visualized via:

coef(model) |> plot()

The results indicate that the deciders value one hour travel time by about 25€, an additional change by 5€, and a more comfortable class by 15€.

Now assume that a train company wants to anticipate the effect of a price increase on their market share. By our model, increasing the ticket price from 100€ to 110€ (ceteris paribus) draws 15% of the customers to the competitor who does not increase their prices:

new_prices <- data.frame(
  "price_A" = c(100, 110), "price_B" = c(100, 100)
)
predict(model, data = new_prices, overview = FALSE)
#>   deciderID occasionID    A    B prediction
#> 1         1          1 0.50 0.50          A
#> 2         2          1 0.35 0.65          B

However, offering a better comfort class (0 here is better than 1) compensates for the higher price and even results in a gain of 7% market share:

new_comfort <- data.frame(
  "price_A" = c(100, 110), "comfort_A" = c(1, 0),
  "price_B" = c(100, 100), "comfort_B" = c(1, 1)
)
predict(model, data = new_comfort, overview = FALSE)
#>   deciderID occasionID    A    B prediction
#> 1         1          1 0.50 0.50          A
#> 2         2          1 0.57 0.43          A

Copy Link

Version

Install

install.packages('RprobitB')

Monthly Downloads

354

Version

1.2.0

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Lennart Oelschläger

Last Published

August 25th, 2025

Functions in RprobitB (1.2.0)

choice_probabilities

Compute choice probabilities
classification

Preference-based classification of deciders
check_form

Check model formula
coef.RprobitB_fit

Extract model effects
d_to_gamma

Transform increments to thresholds
create_lagged_cov

Create lagged choice covariates
cov_mix

Extract estimated covariance matrix of mixing distribution
compute_p_si

Compute choice probabilities at posterior samples
compute_choice_probabilities

Compute probit choice probabilities
get_cov

Extract covariates of choice occasion
fit_model

Fit probit model to choice data
mode_approx

Gibbs sample mode
model_selection

Compare fitted models
ll_ordered

Compute ordered probit log-likelihood
gibbs_sampler

Gibbs sampler for probit models
mml

Approximate marginal model likelihood
missing_covariates

Handle missing covariates
plot_mixture_contour

Plot bivariate contour of mixing distributions
plot_class_seq

Visualizing the number of classes during Gibbs sampling
plot_acf

Autocorrelation plot of Gibbs samples
plot_class_allocation

Plot class allocation (for P_r = 2 only)
plot.RprobitB_fit

Visualize fitted probit model
prepare_data

Prepare choice data for estimation
parameter_labels

Create parameters labels
sample_allocation

Sample allocation
predict.RprobitB_fit

Predict choices
preference_flip

Check for flip in preferences after change in model scale.
pred_acc

Compute prediction accuracy
overview_effects

Print effect overview
npar

Extract number of model parameters
posterior_pars

Parameter sets from posterior samples
simulate_choices

Simulate choice data
train_choice

Stated Preferences for Train Traveling
train_test

Split choice data into train and test subset
update_Omega_c

Update covariance of a single class
update_Omega

Update class covariances
check_prior

Check prior parameters
update_U_ranked

Update ranked utility vector
sufficient_statistics

Compute sufficient statistics
filter_gibbs_samples

Filter Gibbs samples
draw_from_prior

Sample from prior distributions
transform.RprobitB_fit

Transform fitted probit model
point_estimates

Compute point estimates
plot_roc

Plot ROC curve
plot_trace

Visualizing the trace of Gibbs samples.
transform_gibbs_samples

Transformation of Gibbs samples
plot_mixture_marginal

Plot marginal mixing distributions
update_s

Update class weight vector
update_coefficient

Update coefficient vector
update_Sigma

Update error covariance matrix
update_classes_wb

Weight-based class updates
update_b

Update class means
update_U

Update utility vector
update_z

Update class allocation vector
transform_parameter

Transformation of parameter values
update_b_c

Update mean of a single class
update.RprobitB_fit

Update and re-fit probit model
update_d

Update utility threshold increments
update_classes_dp

Dirichlet process class updates
update_m

Update class sizes
RprobitB_gibbs_samples_statistics

Create object of class RprobitB_gibbs_samples_statistics
RprobitB_normalization

Utility normalization
RprobitB_data

Create object of class RprobitB_data
RprobitB_fit

Create object of class RprobitB_fit
R_hat

Compute Gelman-Rubin statistic
RprobitB_parameter

Define probit model parameter
as_cov_names

Re-label alternative specific covariates
WAIC

Compute WAIC value
RprobitB-package

RprobitB: Bayesian Probit Choice Modeling
RprobitB_latent_classes

Create object of class RprobitB_latent_classes