| Title: | Integration of 'Blimp' Software into R |
|---|---|
| Description: | Provides an interface to 'Blimp' software for Bayesian latent variable modeling, missing data analysis, and multiple imputation. The package generates 'Blimp' syntax, executes 'Blimp' models, and imports results back into 'R' as structured objects with methods for visualization and analysis. Requires 'Blimp' software (freely available at <https://www.appliedmissingdata.com/blimp>) to be installed separately. |
| Authors: | Brian T. Keller [aut, cre, cph] |
| Maintainer: | Brian T. Keller <[email protected]> |
| License: | GPL-3 |
| Version: | 1.2.0 |
| Built: | 2026-06-02 18:43:13 UTC |
| Source: | https://github.com/blimp-stats/rblimp |
Extracts the algorithmic options section from Blimp output.
algorithm(object)algorithm(object)
object |
A |
A blimp_out object containing algorithmic options
S3 methods for the posterior::as_draws() family of generics, allowing
blimp_obj to be used anywhere a posterior::draws object is expected.
This is the gateway to the posterior and bayesplot ecosystems:
once converted, the usual summarise_draws(), mcmc_trace(),
mcmc_areas(), etc., all work.
The posterior package must be installed to use these methods. It is
listed in Suggests, so install it explicitly with
install.packages("posterior") if needed.
## S3 method for class 'blimp_obj' as_draws(x, warmup = c("exclude", "include", "only"), ...) ## S3 method for class 'blimp_obj' as_draws_array(x, warmup = c("exclude", "include", "only"), ...) ## S3 method for class 'blimp_obj' as_draws_df(x, warmup = c("exclude", "include", "only"), ...)## S3 method for class 'blimp_obj' as_draws(x, warmup = c("exclude", "include", "only"), ...) ## S3 method for class 'blimp_obj' as_draws_array(x, warmup = c("exclude", "include", "only"), ...) ## S3 method for class 'blimp_obj' as_draws_df(x, warmup = c("exclude", "include", "only"), ...)
x |
A |
warmup |
One of |
... |
Additional arguments (unused). |
All three methods accept a warmup argument with the same semantics as
as.array,blimp_obj-method. Because draws_array objects do not carry
a separate warmup/sampling distinction, the "n_warmup" attribute set by
as.array(x, warmup = "include") is dropped during conversion; users who
need the warmup marker should convert via as.array() and pass the
result to bayesplot::mcmc_trace() directly.
An object of class posterior::draws_array (for as_draws and
as_draws_array) or posterior::draws_df (for as_draws_df).
mydata <- rblimp_sim( c( 'f ~ normal(0, 1)', 'x1:x5 ~ normal(f, 1)', 'y ~ normal(10 + 0.3*f, 1 - .3^2)' ), n = 500, seed = 19723, variables = c('y', 'x1:x5') ) model <- rblimp( list(structure = 'y ~ f', measurement = 'f -> x1:x5'), mydata, chains = 4, seed = 3927, latent = ~ f ) draws <- posterior::as_draws_array(model) posterior::summarise_draws(draws) ## Not run: if (requireNamespace("bayesplot", quietly = TRUE)) { bayesplot::mcmc_trace(draws) bayesplot::mcmc_areas(draws) } ## End(Not run)mydata <- rblimp_sim( c( 'f ~ normal(0, 1)', 'x1:x5 ~ normal(f, 1)', 'y ~ normal(10 + 0.3*f, 1 - .3^2)' ), n = 500, seed = 19723, variables = c('y', 'x1:x5') ) model <- rblimp( list(structure = 'y ~ f', measurement = 'f -> x1:x5'), mydata, chains = 4, seed = 3927, latent = ~ f ) draws <- posterior::as_draws_array(model) posterior::summarise_draws(draws) ## Not run: if (requireNamespace("bayesplot", quietly = TRUE)) { bayesplot::mcmc_trace(draws) bayesplot::mcmc_areas(draws) } ## End(Not run)
Reshapes the MCMC draws in a blimp_obj into a 3-D numeric array with
dimensions [iteration, chain, parameter]. This is the format consumed
directly by bayesplot::mcmc_* functions and by posterior::as_draws_array().
## S4 method for signature 'blimp_obj' as.array(x, warmup = c("exclude", "include", "only"), ...)## S4 method for signature 'blimp_obj' as.array(x, warmup = c("exclude", "include", "only"), ...)
x |
A |
warmup |
One of |
... |
Additional arguments (unused). |
The warmup argument controls whether burn-in draws are included:
"exclude"(default) returns only post-burn sampling draws.
"include"returns warmup draws stacked on top of sampling
draws within each chain. The returned array carries an
"n_warmup" attribute giving the number of warmup iterations, so
it can be piped directly into
bayesplot::mcmc_trace(arr, n_warmup = attr(arr, "n_warmup")).
"only"returns only warmup draws.
A numeric 3-D array with dimensions [iteration, chain, parameter],
named dimnames, and (when warmup = "include") an "n_warmup"
attribute.
as_draws_array.blimp_obj() for the posterior::draws_array
equivalent.
# Generate Data with `rblimp_sim` mydata <- rblimp_sim( c( 'f ~ normal(0, 1)', 'x1:x5 ~ normal(f, 1)', 'y ~ normal(10 + 0.3*f, 1 - .3^2)' ), n = 500, seed = 19723, variables = c('y', 'x1:x5') ) # Fit model model <- rblimp( list(structure = 'y ~ f', measurement = 'f -> x1:x5'), mydata, chains = 4, seed = 3927, latent = ~ f ) # Post-burn only (default) arr <- as.array(model) dim(arr) dimnames(arr)$parameters # Include warmup, feed to bayesplot with its n_warmup marker ## Not run: if (requireNamespace("bayesplot", quietly = TRUE)) { arr_full <- as.array(model, warmup = "include") bayesplot::mcmc_trace(arr_full, n_warmup = attr(arr_full, "n_warmup")) } ## End(Not run)# Generate Data with `rblimp_sim` mydata <- rblimp_sim( c( 'f ~ normal(0, 1)', 'x1:x5 ~ normal(f, 1)', 'y ~ normal(10 + 0.3*f, 1 - .3^2)' ), n = 500, seed = 19723, variables = c('y', 'x1:x5') ) # Fit model model <- rblimp( list(structure = 'y ~ f', measurement = 'f -> x1:x5'), mydata, chains = 4, seed = 3927, latent = ~ f ) # Post-burn only (default) arr <- as.array(model) dim(arr) dimnames(arr)$parameters # Include warmup, feed to bayesplot with its n_warmup marker ## Not run: if (requireNamespace("bayesplot", quietly = TRUE)) { arr_full <- as.array(model, warmup = "include") bayesplot::mcmc_trace(arr_full, n_warmup = attr(arr_full, "n_warmup")) } ## End(Not run)
blimp_syntax to character vectorConvert blimp_syntax to character vector
## S3 method for class 'blimp_syntax' as.character(x, ...)## S3 method for class 'blimp_syntax' as.character(x, ...)
x |
A |
... |
Additional arguments (unused) |
A character string containing the formatted 'Blimp' input syntax with a header comment and all commands.
Convert blimp_obj to data.frame
## S4 method for signature 'blimp_obj' as.data.frame(x, row.names = NULL, optional = FALSE, ...)## S4 method for signature 'blimp_obj' as.data.frame(x, row.names = NULL, optional = FALSE, ...)
x |
A |
row.names |
NULL or a character vector giving row names |
optional |
Logical. If TRUE, setting row names is optional |
... |
Additional arguments passed to as.data.frame |
A data.frame containing the MCMC iterations from the model.
Convert blimp_obj to matrix
## S4 method for signature 'blimp_obj' as.matrix(x, ...)## S4 method for signature 'blimp_obj' as.matrix(x, ...)
x |
A |
... |
Additional arguments (unused) |
A numeric matrix containing the MCMC iterations from the model.
blimp_obj or blimp_bygroup to a mitml.list
Coerces a blimp_obj or blimp_bygroup to a mitml.list
as.mitml(object)as.mitml(object)
object |
|
A list of class "mitml.list" containing the imputed data sets,
suitable for use with functions from the 'mitml' package.
Used inside simple_plot and jn_plot formulas to restrict the SIMPLE
rows to those whose moderator was evaluated at the given label(s).
at(...)at(...)
... |
named arguments where each name is a moderator in the SIMPLE
command and each value is a single label or a vector of labels
(e.g., |
Calling at() outside a formula raises an error; the function only
has meaning when it appears in a simple_plot() or jn_plot() formula.
simple_plot, jn_plot, jn_map, join
## Not run: # Pin `m2` at the SIMPLE label "0", leaving `m1` to color the lines simple_plot(y ~ x | m1 + at(m2 = "0"), model) # Pin to a subset (vector of labels) -- `m2` still faces over those values simple_plot(y ~ x | m1 + at(m2 = c("Q25", "Q75")), model) # Use inside `jn_plot()` the same way jn_plot(y ~ x | m1 + at(m2 = "+1 SD"), model) # In a 4-way fit, pin one moderator and map the slope surface over the other two jn_map(y ~ x | m1 + m2 + at(m3 = "0"), four_way_model) ## End(Not run)## Not run: # Pin `m2` at the SIMPLE label "0", leaving `m1` to color the lines simple_plot(y ~ x | m1 + at(m2 = "0"), model) # Pin to a subset (vector of labels) -- `m2` still faces over those values simple_plot(y ~ x | m1 + at(m2 = c("Q25", "Q75")), model) # Use inside `jn_plot()` the same way jn_plot(y ~ x | m1 + at(m2 = "+1 SD"), model) # In a 4-way fit, pin one moderator and map the slope surface over the other two jn_map(y ~ x | m1 + m2 + at(m3 = "0"), four_way_model) ## End(Not run)
Result object from comparing two Blimp models.
tableMatrix of comparison results
model0The reference blimp_obj model
modelThe comparison blimp_obj model
greaterThanLogical indicating direction of comparison
The main result object containing Blimp model estimates, iterations, and output.
callThe function call that created the object
estimatesMatrix of parameter estimates
burnList of burn-in information for each chain
iterationsData frame of MCMC iterations
psrData frame of Potential Scale Reduction values
imputationsList of imputed datasets
average_impData frame of average imputations
variance_impData frame of imputation variances
waldtestData frame of Wald test results
simpleData frame for simple slopes analysis
syntaxThe blimp_syntax object used to run the model
outputThe blimp_out object containing raw output text
rblimp based on grouping variablerblimp will generate the input, run the script, and load most the saved data into an R object. rblimp_fcs
is used to specify the FCS command in Blimp. rblimp_syntax will generate the Blimp syntax file only.
by_group(expr, group)by_group(expr, group)
expr |
a call to |
group |
a character vector or index pointing to grouping variable in data set |
Separates by grouping to run blimp command on each individual sub data set.
a list of all blimp runs by group with class of blimp_bygroup
# Generate Data mydata <- rblimp_sim( c( 'x ~ normal(0, 1)', 'y ~ normal(0, 1)', 'group = uniform(0, 1) < 0.5' ), n = 1000, seed = 10972 ) # Nonsensical model mdl <- rblimp_fcs( c('x', 'y'), mydata, seed = 3927, nimps = 2, ) |> by_group('group') # Analyze each imputation results <- with(mdl, lm(y ~ x)) # use `mitml` package to pool results# Generate Data mydata <- rblimp_sim( c( 'x ~ normal(0, 1)', 'y ~ normal(0, 1)', 'group = uniform(0, 1) < 0.5' ), n = 1000, seed = 10972 ) # Nonsensical model mdl <- rblimp_fcs( c('x', 'y'), mydata, seed = 3927, nimps = 2, ) |> by_group('group') # Analyze each imputation results <- with(mdl, lm(y ~ x)) # use `mitml` package to pool results
Compares two Bayesian models by calculating the proportion of posterior samples where the comparison model's parameters exceed (or fall below) the reference model's summary statistic. This is useful for model comparison and assessing incremental variance explained (e.g., R-squared differences).
compare( model0, model, use = "mean", greaterThan = TRUE, suffixes = c("R2: Coefficients", "R2: Level-2 Random Intercepts", "R2: Level-2 Random Slopes", "R2: Level-3 Random Slopes", "R2: Level-3 Random Intercepts", "R2: Residual Variation", "R2: Level-1 Residual Variation") )compare( model0, model, use = "mean", greaterThan = TRUE, suffixes = c("R2: Coefficients", "R2: Level-2 Random Intercepts", "R2: Level-2 Random Slopes", "R2: Level-3 Random Slopes", "R2: Level-3 Random Intercepts", "R2: Residual Variation", "R2: Level-1 Residual Variation") )
model0 |
A |
model |
A |
use |
Summary statistic to use as the cutpoint from
|
greaterThan |
Logical. If |
suffixes |
Character vector of parameter name suffixes to compare. Defaults to all R-squared values (coefficients, random effects, residual variation). |
The comparison works by:
Computing a summary statistic (e.g., mean) from model0's posterior samples
Calculating what proportion of model's posterior samples exceed this value
Reporting this proportion for each parameter matching the specified suffixes
A blimp_cp object containing a matrix of comparison proportions.
Due to R restrictions, lists of functions will not give useful printed names.
# Generate data mydata <- rblimp_sim( c( 'x1 ~ normal(0, 1)', 'x2 ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x1 + 0.3*x2, 1)' ), n = 200, seed = 123 ) # Fit baseline model (x1 only) model0 <- rblimp( 'y ~ x1', mydata, seed = 123, burn = 1000, iter = 1000 ) # Fit comparison model (x1 + x2) model1 <- rblimp( 'y ~ x1 x2', mydata, seed = 123, burn = 1000, iter = 1000 ) # Compare models - proportion of model1 R-squared > mean(model0 R-squared) compare(model0, model1)# Generate data mydata <- rblimp_sim( c( 'x1 ~ normal(0, 1)', 'x2 ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x1 + 0.3*x2, 1)' ), n = 200, seed = 123 ) # Fit baseline model (x1 only) model0 <- rblimp( 'y ~ x1', mydata, seed = 123, burn = 1000, iter = 1000 ) # Fit comparison model (x1 + x2) model1 <- rblimp( 'y ~ x1 x2', mydata, seed = 123, burn = 1000, iter = 1000 ) # Compare models - proportion of model1 R-squared > mean(model0 R-squared) compare(model0, model1)
jn_plot_func
Convenience Function for computing conditional effects for jn_plot_func
compute_condeff(value1, value2)compute_condeff(value1, value2)
value1 |
The base value |
value2 |
The value to change as a function of moderator |
a function
# Generate Data mydata <- rblimp_sim( c( 'x1 ~ normal(0, 1)', 'x2 ~ normal(0, 1)', 'm ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x1 + 0.5*x2 + m + 0.2*x1*x2 + 0.3*x2*m + 0.1*x1*m + 0.7*x1*x2*m, 1)' ), n = 100, seed = 981273 ) # Run Rblimp m1 <- rblimp( 'y ~ x1 x2 m x1*x2 x1*m x2*m x1*x2*m', mydata, center = ~ m, seed = 10972, burn = 1000, iter = 1000 ) # Get parameter values params <- m1 |> as.matrix() # Generate Plot ( jn_plot_func( compute_condeff(params[,6], params[,9]), xrange = c(-3, 3) ) # Set custom colors + ggplot2::scale_fill_manual( values = c(`FALSE` = '#ca0020', `TRUE` = '#0571b0') ) + ggplot2::labs( title = 'Johnson-Neyman Plot for `x1` * `x2` Moderated by `x2`', subtitle = 'Red area represents 0 within 95% interval', y = 'y ~ x1 * x2', x = 'm' ) + ggplot2::theme_minimal() )# Generate Data mydata <- rblimp_sim( c( 'x1 ~ normal(0, 1)', 'x2 ~ normal(0, 1)', 'm ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x1 + 0.5*x2 + m + 0.2*x1*x2 + 0.3*x2*m + 0.1*x1*m + 0.7*x1*x2*m, 1)' ), n = 100, seed = 981273 ) # Run Rblimp m1 <- rblimp( 'y ~ x1 x2 m x1*x2 x1*m x2*m x1*x2*m', mydata, center = ~ m, seed = 10972, burn = 1000, iter = 1000 ) # Get parameter values params <- m1 |> as.matrix() # Generate Plot ( jn_plot_func( compute_condeff(params[,6], params[,9]), xrange = c(-3, 3) ) # Set custom colors + ggplot2::scale_fill_manual( values = c(`FALSE` = '#ca0020', `TRUE` = '#0571b0') ) + ggplot2::labs( title = 'Johnson-Neyman Plot for `x1` * `x2` Moderated by `x2`', subtitle = 'Red area represents 0 within 95% interval', y = 'y ~ x1 * x2', x = 'm' ) + ggplot2::theme_minimal() )
Extracts the data information section from Blimp output.
datainfo(object)datainfo(object)
object |
A |
A blimp_out object containing data information
describe provides a flexible summary of various R objects,
such as data frames or Blimp model objects. It is an
alternative to summary and is inspired by precis from the rethinking package.
describe( object, depth = 1, pars, prob = 0.95, digits = 2, sort = NULL, decreasing = FALSE, ... ) ## S4 method for signature 'data.frame' describe( object, depth = 1, pars, prob = 0.5, digits = 2, sort = NULL, decreasing = FALSE, hist = TRUE, ... ) ## S4 method for signature 'blimp_obj' describe( object, depth = 1, pars, prob = 0.95, digits = 2, sort = NULL, decreasing = FALSE, hist = TRUE, ... )describe( object, depth = 1, pars, prob = 0.95, digits = 2, sort = NULL, decreasing = FALSE, ... ) ## S4 method for signature 'data.frame' describe( object, depth = 1, pars, prob = 0.5, digits = 2, sort = NULL, decreasing = FALSE, hist = TRUE, ... ) ## S4 method for signature 'blimp_obj' describe( object, depth = 1, pars, prob = 0.95, digits = 2, sort = NULL, decreasing = FALSE, hist = TRUE, ... )
object |
The object to describe. |
depth |
An integer ( |
pars |
An optional character vector of parameter names to include in the summary. |
prob |
The probability mass for the credible interval (e.g., quantile interval). |
digits |
The number of decimal places to display in the output. |
sort |
An optional character string specifying a column name to sort the results by. |
decreasing |
Logical. If |
... |
Additional arguments passed to specific methods. |
hist |
Logical. If |
An object of class describe, which is a data frame containing summary statistics and attributes for printing.
The data.frame method is adapted from Richard McElreath's precis
function in the rethinking package (https://github.com/rmcelreath/rethinking).
The histospark function is by Hadley Wickham (GPL-3).
The blimp_obj method was written for this package.
An S4 class that holds a summary data frame and formatting
information. It extends the data.frame class and is the return type for
the describe() function.
## S4 method for signature 'describe' show(object)## S4 method for signature 'describe' show(object)
object |
An object of class |
digitsA numeric value indicating the number of digits to display.
This function is called when running Blimp and can be used to determine what location
rblimp uses for the Blimp executable.
detect_blimp(prompt = TRUE)detect_blimp(prompt = TRUE)
prompt |
Logical; when |
rblimp resolves Blimp's executable location in this order:
Any location previously set via set_blimp.
The R_BLIMP environment variable.
A managed install created by install_blimp.
The default operating-system install location of the Blimp system installer.
If none are found and the R session is interactive, detect_blimp
offers to download and install Blimp via install_blimp (unless
prompt = FALSE).
A character string of blimp's executable location.
set_blimp to set blimp location, install_blimp
to install Blimp into a managed directory.
# Obtain blimp location detect_blimp()# Obtain blimp location detect_blimp()
Extracts the model estimates section from Blimp output.
estimates(object)estimates(object)
object |
A |
A blimp_out object containing model estimates
Returns a logical if blimp is detected by detect_blimp
has_blimp()has_blimp()
This function uses detect_blimp to determine if blimp is installed.
a logical
# Detect if system has blimp has_blimp()# Detect if system has blimp has_blimp()
Downloads and installs Blimp into a user-writable directory managed by rblimp. This is an alternative to installing Blimp via the system installer from https://www.appliedmissingdata.com/blimp.
install_blimp(dir = NULL, force = FALSE, quiet = FALSE, linux_variant = NULL)install_blimp(dir = NULL, force = FALSE, quiet = FALSE, linux_variant = NULL)
dir |
Install directory. Defaults to a hidden, user-writable
per-OS location ( |
force |
Logical; overwrite an existing managed install, or proceed
even when a system installation of Blimp is detected. Default |
quiet |
Logical; suppress progress messages. Default |
linux_variant |
Optional character; |
The install directory location can be overridden globally via the
R_BLIMP_HOME environment variable.
Set R_BLIMP_PREVENT_INSTALL=true to disable install_blimp (intended for
locked-down environments).
Non-interactive R sessions require force = TRUE.
Path to the installed blimp executable, invisibly.
Calling install_blimp() contacts updates.blimpstats.com over HTTPS
to fetch the Blimp engine. Information may be collected as described
in the privacy policy.
See privacy policy: https://www.blimpstats.com/privacy.
## Not run: # Install Blimp to the default managed location install_blimp() # Install to a custom directory install_blimp(dir = "~/blimp-managed") # Force install on a Linux RHEL 8 system without auto-detection install_blimp(linux_variant = "rhel8") ## End(Not run)## Not run: # Install Blimp to the default managed location install_blimp() # Install to a custom directory install_blimp(dir = "~/blimp-managed") # Force install on a Linux RHEL 8 system without auto-detection install_blimp(linux_variant = "rhel8") ## End(Not run)
A 2-D analogue of jn_plot(). For a model with an outcome ~ focal x m1 x m2
interaction, the conditional slope of outcome on focal is a linear
surface over (m1, m2):
jn_map() recovers those four coefficients per MCMC draw from the
SIMPLE-evaluated points, evaluates them on a dense grid, and renders the
posterior median as a heatmap. A contour line marks the boundary of the
region where the ci posterior credible interval excludes zero – the 2-D
"region of significance".
jn_map(formula, model, ci = 0.95, n_grid = 100, ...)jn_map(formula, model, ci = 0.95, n_grid = 100, ...)
formula |
an object of class |
model |
an |
ci |
a value between 0 and 1 specifying the credible-interval size for
the significance contour. Default |
n_grid |
integer length-1 or length-2. Number of grid points along
|
... |
currently unused. |
a ggplot2::ggplot plot. Attribute "grid" carries the per-cell
data frame (with lower, median, upper, sig columns).
jn_plot, simple_plot, at, join
## Not run: # Generate data with a three-way interaction mydata <- rblimp_sim( c( 'x ~ normal(0, 1)', 'm1 ~ normal(0, 1)', 'm2 ~ normal(0, 1)', 'y ~ normal(10 + 0.4*x + 0.3*m1 - 0.2*m2 + 0.5*x*m1 + 0.3*x*m2 + 0.2*m1*m2 + 0.6*x*m1*m2, 1)' ), n = 500, seed = 981273 ) # Fit the model -- SIMPLE must evaluate both moderators fit <- rblimp( 'y ~ x m1 m2 x*m1 x*m2 m1*m2 x*m1*m2', mydata, center = ~ x + m1 + m2, simple = 'x | m1 @ quantile and m2 @ sd', seed = 1071, burn = 1000, iter = 1000 ) # Default 2-D heatmap of the conditional slope of y on x jn_map(y ~ x | m1 + m2, fit) # Swap axes jn_map(y ~ x | m2 + m1, fit) # Inspect the underlying per-cell data head(attr(jn_map(y ~ x | m1 + m2, fit), 'grid')) ## End(Not run)## Not run: # Generate data with a three-way interaction mydata <- rblimp_sim( c( 'x ~ normal(0, 1)', 'm1 ~ normal(0, 1)', 'm2 ~ normal(0, 1)', 'y ~ normal(10 + 0.4*x + 0.3*m1 - 0.2*m2 + 0.5*x*m1 + 0.3*x*m2 + 0.2*m1*m2 + 0.6*x*m1*m2, 1)' ), n = 500, seed = 981273 ) # Fit the model -- SIMPLE must evaluate both moderators fit <- rblimp( 'y ~ x m1 m2 x*m1 x*m2 m1*m2 x*m1*m2', mydata, center = ~ x + m1 + m2, simple = 'x | m1 @ quantile and m2 @ sd', seed = 1071, burn = 1000, iter = 1000 ) # Default 2-D heatmap of the conditional slope of y on x jn_map(y ~ x | m1 + m2, fit) # Swap axes jn_map(y ~ x | m2 + m1, fit) # Inspect the underlying per-cell data head(attr(jn_map(y ~ x | m1 + m2, fit), 'grid')) ## End(Not run)
rblimp
Generates a Johnson-Neyman Plot based on the posterior summaries from the output of rblimp.
jn_plot(formula, model, ci = 0.95, ...)jn_plot(formula, model, ci = 0.95, ...)
formula |
an object of class |
model |
an |
ci |
a value between 0 and 1 specifying the credible interval size |
... |
passed bounds search algorithm. See |
To change colors use ggplot2's scale system. Both fill and color are used. See
ggplot2::aes_colour_fill_alpha for more information about setting a manual set of colors.
When the model contains a SIMPLE command that evaluates the focal predictor at
multiple values of the formula moderator (e.g., 'x | m @ quantile'), the
conditional slope is reconstructed from the SIMPLE slope draws via a
per-iteration linear fit across the evaluated points. If the same SIMPLE
command also holds one or two additional moderators at multiple values
(e.g., 'x | m @ quantile and z @ sd'), the plot is faceted by those
additional moderators. When SIMPLE has no relevant rows, jn_plot falls
back to building the conditional slope directly from the model's fixed-effect
interaction parameter (the original behavior).
a ggplot2::ggplot plot. The bounding values are saved in the attribute 'bounds'.
jn_plot_func, jn_map, simple_plot, at, join
## Not run: # ---- Basic single-moderator example ---- mydata <- rblimp_sim( c( 'x ~ normal(0, 1)', 'm ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x + m + 0.2*x*m, 1)' ), n = 100, seed = 981273 ) m1 <- rblimp( 'y ~ x m x*m', mydata, center = ~ m, simple = 'x | m', seed = 10972, burn = 1000, iter = 1000 ) jn_plot(y ~ x | m, m1) # Custom significance-region fill ( jn_plot(y ~ x | m, m1) + ggplot2::scale_fill_manual( values = c(`FALSE` = '#ca0020', `TRUE` = '#0571b0') ) ) # ---- Two-moderator fit: first mod = x-axis, second auto-faceted ---- three_way <- rblimp_sim( c( 'x ~ normal(0, 1)', 'm1 ~ normal(0, 1)', 'm2 ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x*m1 + 0.3*x*m2 + 0.6*x*m1*m2, 1)' ), n = 500, seed = 2024 ) fit <- rblimp( 'y ~ x m1 m2 x*m1 x*m2 m1*m2 x*m1*m2', three_way, center = ~ x + m1 + m2, simple = 'x | m1 @ quantile and m2 @ sd', seed = 1071, burn = 1000, iter = 1000 ) jn_plot(y ~ x | m1 + m2, fit) # Pin one moderator via `at()` jn_plot(y ~ x | m1 + at(m2 = "0"), fit) # Restrict to a subset of SIMPLE values jn_plot(y ~ x | m1 + at(m2 = c("-1 SD", "+1 SD")), fit) # Per-facet boundary x-values are also exposed as an attribute attr(jn_plot(y ~ x | m1 + m2, fit), "bounds") ## End(Not run)## Not run: # ---- Basic single-moderator example ---- mydata <- rblimp_sim( c( 'x ~ normal(0, 1)', 'm ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x + m + 0.2*x*m, 1)' ), n = 100, seed = 981273 ) m1 <- rblimp( 'y ~ x m x*m', mydata, center = ~ m, simple = 'x | m', seed = 10972, burn = 1000, iter = 1000 ) jn_plot(y ~ x | m, m1) # Custom significance-region fill ( jn_plot(y ~ x | m, m1) + ggplot2::scale_fill_manual( values = c(`FALSE` = '#ca0020', `TRUE` = '#0571b0') ) ) # ---- Two-moderator fit: first mod = x-axis, second auto-faceted ---- three_way <- rblimp_sim( c( 'x ~ normal(0, 1)', 'm1 ~ normal(0, 1)', 'm2 ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x*m1 + 0.3*x*m2 + 0.6*x*m1*m2, 1)' ), n = 500, seed = 2024 ) fit <- rblimp( 'y ~ x m1 m2 x*m1 x*m2 m1*m2 x*m1*m2', three_way, center = ~ x + m1 + m2, simple = 'x | m1 @ quantile and m2 @ sd', seed = 1071, burn = 1000, iter = 1000 ) jn_plot(y ~ x | m1 + m2, fit) # Pin one moderator via `at()` jn_plot(y ~ x | m1 + at(m2 = "0"), fit) # Restrict to a subset of SIMPLE values jn_plot(y ~ x | m1 + at(m2 = c("-1 SD", "+1 SD")), fit) # Per-facet boundary x-values are also exposed as an attribute attr(jn_plot(y ~ x | m1 + m2, fit), "bounds") ## End(Not run)
Generates a Johnson-Neyman Plot using a function to produce the conditional effect
jn_plot_func(func, xrange, ci = 0.95, ...)jn_plot_func(func, xrange, ci = 0.95, ...)
func |
a |
xrange |
a |
ci |
a value between 0 and 1 specifying the credible interval size |
... |
values passed to internal boundary search algorithm. See Details below. |
To change colors use ggplot2's scale system. Both fill and color are used. See
ggplot2::aes_colour_fill_alpha for more information about setting a manual set of colors.
For ..., the arguments are passed to the internal boundary search algorithm.
This algorithm uses an initial grid search to locate boundaries based on the range
and then a binary search to refine the estimates.
The following arguments are available:
Number of points in the initial coarse grid search used to locate approximate boundary positions. Higher values improve detection of closely-spaced boundaries but increase computation time. Default is 1000.
Tolerance for binary search refinement. The algorithm refines each boundary until the interval width is smaller than this value. Smaller values give higher precision but require more function evaluations. Default is 1e-12.
Maximum number of iterations for binary search refinement per boundary. Prevents infinite loops if tolerance cannot be achieved. Default is 100.
Logical indicating whether to perform additional refinement in regions where boundaries are detected to be closely spaced. When TRUE, uses a finer grid to resolve boundaries that may be missed by the initial coarse grid. Default is TRUE.
a ggplot2::ggplot plot. The bounding values are saved in the attribute 'bounds'.
# Generate Data mydata <- rblimp_sim( c( 'x1 ~ normal(0, 1)', 'x2 ~ normal(0, 1)', 'm ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x1 + 0.5*x2 + m + 0.2*x1*x2 + 0.3*x2*m + 0.1*x1*m + 0.7*x1*x2*m, 1)' ), n = 100, seed = 981273 ) # Run Rblimp m1 <- rblimp( 'y ~ x1 x2 m x1*x2 x1*m x2*m x1*x2*m', mydata, center = ~ m, seed = 10972, burn = 1000, iter = 1000 ) # Get parameter values params <- m1 |> as.matrix() # Generate Plot ( jn_plot_func( compute_condeff(params[,6], params[,9]), xrange = c(-3, 3) ) + ggplot2::labs( title = 'Johnson-Neyman Plot for `x1` * `x2` Moderated by `x2`', subtitle = 'Red area represents 0 within 95% interval', y = 'y ~ x1 * x2', x = 'm' ) + ggplot2::theme_minimal() )# Generate Data mydata <- rblimp_sim( c( 'x1 ~ normal(0, 1)', 'x2 ~ normal(0, 1)', 'm ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x1 + 0.5*x2 + m + 0.2*x1*x2 + 0.3*x2*m + 0.1*x1*m + 0.7*x1*x2*m, 1)' ), n = 100, seed = 981273 ) # Run Rblimp m1 <- rblimp( 'y ~ x1 x2 m x1*x2 x1*m x2*m x1*x2*m', mydata, center = ~ m, seed = 10972, burn = 1000, iter = 1000 ) # Get parameter values params <- m1 |> as.matrix() # Generate Plot ( jn_plot_func( compute_condeff(params[,6], params[,9]), xrange = c(-3, 3) ) + ggplot2::labs( title = 'Johnson-Neyman Plot for `x1` * `x2` Moderated by `x2`', subtitle = 'Red area represents 0 within 95% interval', y = 'y ~ x1 * x2', x = 'm' ) + ggplot2::theme_minimal() )
Used inside simple_plot and jn_plot formulas to treat a set of
SIMPLE moderators as a single conceptual moderator. Each row's color/legend
label combines every component's "name @ value" so that, e.g., the dummy
codes of a nominal predictor render as one set of colored lines instead of
a sparse facet grid.
join(...)join(...)
... |
bare moderator names (at least two) to bundle together. |
Only the first bare term on the right-hand side of | may use join(...)
(that is, the color / x-axis position). Facet positions must remain plain
moderator names or at(...) calls.
Calling join() outside a formula raises an error; it only has
meaning inside simple_plot() / jn_plot() formulas.
simple_plot, jn_plot, jn_map, at
## Not run: # Nominal predictor with dummy codes -- bundle them as one moderator simple_plot(y ~ x | join(group.1, group.2), model) # Combine with `at()` to pin an unrelated moderator simple_plot(y ~ x | join(group.1, group.2) + at(z = "0"), model) # In a 4-way fit, collapse the two facet moderators so the bound # annotation goes back into each strip of the JN plot jn_plot(y ~ x | m1 + join(m2, m3), four_way_model) ## End(Not run)## Not run: # Nominal predictor with dummy codes -- bundle them as one moderator simple_plot(y ~ x | join(group.1, group.2), model) # Combine with `at()` to pin an unrelated moderator simple_plot(y ~ x | join(group.1, group.2) + at(z = "0"), model) # In a 4-way fit, collapse the two facet moderators so the bound # annotation goes back into each strip of the JN plot jn_plot(y ~ x | m1 + join(m2, m3), four_way_model) ## End(Not run)
Generates an APA-formatted HTML table of model estimates that opens in the RStudio Viewer. Provides publication-ready tables with proper formatting for Bayesian model results.
model_table( object, selector, digits = 3, caption = NULL, show_chains = TRUE, show_neff = FALSE )model_table( object, selector, digits = 3, caption = NULL, show_chains = TRUE, show_neff = FALSE )
object |
A |
selector |
Optional character string specifying variable name or block to display. If missing, creates separate tables for each variable. |
digits |
Integer specifying number of decimal places. Default is 3. |
caption |
Optional character string for table caption. |
show_chains |
Logical indicating whether to show chain information. Default is TRUE. |
show_neff |
Logical indicating whether to show effective sample size. Default is FALSE. |
The function creates APA-style tables with the following features:
Proper coefficient formatting with confidence intervals
Organized sections for different parameter types
Publication-ready styling
Responsive design for different screen sizes
Tables are displayed in the RStudio Viewer pane and can be exported or copied.
Invisibly returns the HTML content. Primary purpose is displaying in Viewer.
# Generate Data mydata <- rblimp_sim( c( 'x ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x, 1)' ), n = 100, seed = 10972 ) # Fit model model <- rblimp( 'y ~ x', mydata, seed = 10972, burn = 1000, iter = 1000 ) # Create APA table for all variables model_table(model) # Create table for specific variable model_table(model, "y") # Customize formatting model_table(model, digits = 2, caption = "Bayesian Model Results")# Generate Data mydata <- rblimp_sim( c( 'x ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x, 1)' ), n = 100, seed = 10972 ) # Fit model model <- rblimp( 'y ~ x', mydata, seed = 10972, burn = 1000, iter = 1000 ) # Create APA table for all variables model_table(model) # Create table for specific variable model_table(model, "y") # Customize formatting model_table(model, digits = 2, caption = "Bayesian Model Results")
Extracts the model fit section from Blimp output.
modelfit(object)modelfit(object)
object |
A |
A blimp_out object containing model fit information
Extracts the model information section from Blimp output.
modelinfo(object)modelinfo(object)
object |
A |
A blimp_out object containing model information
blimp_obj objectReturn variable names from blimp_obj object
## S4 method for signature 'blimp_obj' names(x)## S4 method for signature 'blimp_obj' names(x)
x |
A |
A character vector of variable names from the average imputation data.
Extracts the raw Blimp output text from a blimp_obj.
output(object)output(object)
object |
A |
A blimp_out object containing the raw Blimp output text
Generates ggplot2::ggplot plots using ggplot2::geom_density based on the output from rblimp
posterior_plot(model, selector, ...)posterior_plot(model, selector, ...)
model |
an |
selector |
a name of a variable, a name of a parameter, a number of a parameter, or a combination of any of them. If left empty, a plot of all parameters will be returned. See Examples. |
... |
arguments passed to internally called |
To change colors use ggplot2's scale system. Both fill and color are used. See
ggplot2::aes_colour_fill_alpha for more information about setting a manual set of colors.
a ggplot2::ggplot plot
# Generate Data mydata <- rblimp_sim( c( 'x ~ normal(0, 1)', 'm ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x + m + 0.2*x*m, 1)' ), n = 100, seed = 981273 ) # Run Rblimp m1 <- rblimp( c( 'y ~ x m', 'x ~~ m' ), mydata, seed = 10972, burn = 1000, iter = 1000 ) # Generate plot of all parameters with `y` posterior_plot(m1, 'y') + ggplot2::theme_minimal() # Generate plot of all parameters posterior_plot(m1) + ggplot2::theme_minimal() # Generate plot of all parameters for `y` and `x` posterior_plot(m1, c('x', 'y')) + ggplot2::theme_minimal() # Generate Plot of Parameter 5 posterior_plot(m1, 5) + ggplot2::theme_minimal() # Generate plot of `x residual variance` posterior_plot(m1, 'x residual variance') + ggplot2::theme_minimal() # Generate plot of Parameters 7 and 9 posterior_plot(m1, c(7, 9)) + ggplot2::theme_minimal()# Generate Data mydata <- rblimp_sim( c( 'x ~ normal(0, 1)', 'm ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x + m + 0.2*x*m, 1)' ), n = 100, seed = 981273 ) # Run Rblimp m1 <- rblimp( c( 'y ~ x m', 'x ~~ m' ), mydata, seed = 10972, burn = 1000, iter = 1000 ) # Generate plot of all parameters with `y` posterior_plot(m1, 'y') + ggplot2::theme_minimal() # Generate plot of all parameters posterior_plot(m1) + ggplot2::theme_minimal() # Generate plot of all parameters for `y` and `x` posterior_plot(m1, c('x', 'y')) + ggplot2::theme_minimal() # Generate Plot of Parameter 5 posterior_plot(m1, 5) + ggplot2::theme_minimal() # Generate plot of `x residual variance` posterior_plot(m1, 'x residual variance') + ggplot2::theme_minimal() # Generate plot of Parameters 7 and 9 posterior_plot(m1, c(7, 9)) + ggplot2::theme_minimal()
blimp_obj
Predicted scores from blimp_obj
## S4 method for signature 'blimp_obj' predict(object, ...)## S4 method for signature 'blimp_obj' predict(object, ...)
object |
A |
... |
Additional arguments (unused) |
A list of data frames, one per imputation, each containing the predicted values and probability columns from the model.
blimp_syntax
Print blimp_syntax
## S3 method for class 'blimp_syntax' print(x, ...)## S3 method for class 'blimp_syntax' print(x, ...)
x |
A |
... |
Additional arguments (unused) |
The blimp_syntax object (invisibly). Called for its side effect
of printing the formatted syntax to the console.
Extracts the PSR convergence diagnostic section from Blimp output.
psr(object)psr(object)
object |
A |
A blimp_out object containing PSR output
rblimp will generate the input, run the script, and load most the saved data into an R object. rblimp_fcs
is used to specify the FCS command in Blimp. rblimp_syntax will generate the Blimp syntax file only.
rblimp( model, data, burn = 10000, iter = 10000, seed, thin, nimps, latent, randomeffect, parameters, clusterid, timeid, weight, ordinal, nominal, count, center, chains, simple, waldtest, options, transform, dropout, filter, fixed, output, tmpfolder, add_save = TRUE, print_output = TRUE, nopowershell = FALSE ) rblimp_fcs( variables, data, burn = 10000, iter = 10000, seed, thin, nimps, clusterid, ordinal, nominal, chains, options, transform, fixed, output, tmpfolder, add_save = TRUE, print_output = TRUE, nopowershell = FALSE ) rblimp_syntax( model, data, burn, iter, seed, thin, nimps, latent, randomeffect, parameters, clusterid, timeid, weight, ordinal, nominal, count, transform, dropout, filter, fixed, center, chains, simple, waldtest, options, save, output )rblimp( model, data, burn = 10000, iter = 10000, seed, thin, nimps, latent, randomeffect, parameters, clusterid, timeid, weight, ordinal, nominal, count, center, chains, simple, waldtest, options, transform, dropout, filter, fixed, output, tmpfolder, add_save = TRUE, print_output = TRUE, nopowershell = FALSE ) rblimp_fcs( variables, data, burn = 10000, iter = 10000, seed, thin, nimps, clusterid, ordinal, nominal, chains, options, transform, fixed, output, tmpfolder, add_save = TRUE, print_output = TRUE, nopowershell = FALSE ) rblimp_syntax( model, data, burn, iter, seed, thin, nimps, latent, randomeffect, parameters, clusterid, timeid, weight, ordinal, nominal, count, transform, dropout, filter, fixed, center, chains, simple, waldtest, options, save, output )
model |
a character string or vector/list of character strings. Specifies Blimp's MODEL command. See details. |
data |
a |
burn |
an integer. The number of burn-in iterations to be run |
iter |
an integer. The number of post burn-in iterations to be run |
seed |
a positive integer. The seeding value for Blimp's pseudo random number generator |
thin |
an integer. The thinning interval for imputations only. |
nimps |
an integer. The number of imputations to save. |
latent |
a character string, formula, or vector/list of character strings. Specifies Blimp's LATENT command. See details. |
randomeffect |
a character string or vector/list of character strings. Specifies Blimp's RANDOMEFFECTS command. |
parameters |
a character string or vector/list of character strings. Specifies Blimp's MODEL command. See details. |
clusterid |
a character string, formula, or vector/list of character strings. Specifies Blimp's CLUSTERID command. See details. |
timeid |
a character string, formula, or vector/list of character strings. Specifies Blimp's TIMEID command. See details. |
weight |
a character string, formula, or vector/list of character strings. Specifies Blimp's WEIGHT command. See details. |
ordinal |
a character string, formula, or vector/list of character strings. Specifies Blimp's ORDINAL command. See details. |
nominal |
a character string, formula, or vector/list of character strings. Specifies Blimp's NOMINAL command. See details. |
count |
a character string, formula, or vector/list of character strings. Specifies Blimp's COUNT command. See details. |
center |
a character string, formula, or vector/list of character strings. Specifies Blimp's CENTER command. See details. |
chains |
an integer, character string, or vector/list of character strings. Specifies Blimp's CHAINS command. |
simple |
a character string or vector/list of character strings. Specifies Blimp's SIMPLE command. See details. |
waldtest |
a character string or vector/list of character strings. Specifies Blimp's WALDTEST command. See details. |
options |
a character string or vector/list of character strings. Specifies Blimp's OPTIONS command. |
transform |
a character string or vector/list of character strings. Specifies Blimp's TRANSFORM command. |
dropout |
a character string, formula, or vector/list of character strings. Specifies Blimp's DROPOUT command. See details. |
filter |
a character string. Specifies Blimp's FILTER command. |
fixed |
a character string, formula, or vector/list of character strings. Specifies Blimp's FIXED command. See details. |
output |
a character string or vector/list of character strings. Specifies Blimp's OUTPUT command |
tmpfolder |
a character string. If specified |
add_save |
a single logical value or a list of logical values.
If |
print_output |
The type of output printed to the console.
|
nopowershell |
Windows only. Uses cmd.exe with some limited functions (instead of powershell). |
variables |
list of variables to be included in FCS procedure |
save |
Specify SAVE command for blimp syntax |
The above functions require knowledge of specifying Blimp commands. Blimp's syntax commands are documented in the Blimp User Manual
By default, these commands can be inputted as character strings that will be used to generate the syntax.
For multiple lined commands, you can wrap multiple strings into a character vector or a list.
The appropriate semicolons will be specified, so they are not required in any character strings.
If specifying a character vector or a list for the model, providing names to each element
will be used as blocks in Blimp's model syntax. Similarly, specifying named elements can be used
for the center command to specify if you would like centering within a cluster or
grand mean centering. This also works for the latent input when requesting latent
variables at a specific cluster identifier.
See the Blimp User Manual
for more details about types of centering and specifying latent variables.
In addition, R's formula syntax can be used to specify lists of variables
in place of character strings. For example, specifying the CLUSTERID command with
the variable id can be specified as ~ id as opposed to a character string.
Similarly, + can be used to specify multiple variables. For example, to specify
two variables as ordinal the formula would be: ~ x1 + x2. Finally, this can also be
used to specify centering and latent variables. For example, to center x1 and x2
within cluster we can specify: cwc ~ x1 + x2.
Running rblimp will also check if blimp is up to date.
See details in rblimp_source for more information.
SIMULATE() for creating simulated data to fit within rblimp()
rblimp_sim() for generating simulated datasets
# Generate Data with `rblimp_sim` mydata <- rblimp_sim( c( 'f ~ normal(0, 1)', 'x1:x5 ~ normal(f, 1)', 'y ~ normal(10 + 0.3*f, 1 - .3^2)' ), n = 500, seed = 19723, variables = c('y', 'x1:x5') ) # Fit SEM Model model <- rblimp( list( structure = 'y ~ f', measurement = 'f -> x1:x5' ), mydata, seed = 3927, latent = ~ f ) # View results summary(model)# Generate Data with `rblimp_sim` mydata <- rblimp_sim( c( 'f ~ normal(0, 1)', 'x1:x5 ~ normal(f, 1)', 'y ~ normal(10 + 0.3*f, 1 - .3^2)' ), n = 500, seed = 19723, variables = c('y', 'x1:x5') ) # Fit SEM Model model <- rblimp( list( structure = 'y ~ f', measurement = 'f -> x1:x5' ), mydata, seed = 3927, latent = ~ f ) # View results summary(model)
A guide to installing and configuring Blimp software for use with rblimp.
rblimp requires the Blimp engine. The simplest way to install it is from R:
install_blimp()
This downloads the latest Blimp engine into a user-writable directory:
macOS: ~/.blimp/
Windows: %LOCALAPPDATA%/Blimp/
Linux: ~/.blimp/
Override the location with the R_BLIMP_HOME environment variable.
Use uninstall_blimp to remove it.
If you'd rather use Blimp's standalone system installer (downloaded from https://www.appliedmissingdata.com/blimp), rblimp will auto-detect it and use it instead.
If autodetection fails, set the path manually with set_blimp or the
R_BLIMP environment variable.
Privacy: Downloads are recorded for usage statistics.
See privacy policy: https://www.blimpstats.com/privacy.
By default, rblimp automatically checks for Blimp updates when you run models. This ensures you're using the latest version with bug fixes and improvements.
To disable automatic update checks:
# Disable for current session options(check_blimp_update = FALSE) # Or add to .Rprofile for permanent setting: # options(check_blimp_update = FALSE) # Or set environment variable (useful for CI / sysadmin contexts): # R_BLIMP_NO_UPDATE_CHECK=true
You can manually update Blimp at any time:
update_blimp()
For managed installs, this re-downloads the latest Blimp engine. For system installs, this launches the Blimp Updater application.
Model Fitting:
rblimp - Fit Bayesian models
rblimp_fcs - Fully Conditional Specification imputation
rblimp_syntax - Generate Blimp syntax
rblimp_source - Run existing Blimp syntax files
Simulation:
rblimp_sim - Generate simulated datasets
SIMULATE - Create simulation specifications
Output & Analysis:
summary - Model summary
estimates - Extract parameter estimates
describe - Descriptive statistics
psr - Potential Scale Reduction values
output - Raw Blimp output
standardized - Extract standardized parameters only
compare - Compare models
Visualization:
trace_plot - MCMC trace plots
posterior_plot - Posterior density plots
residual_plot - Residual diagnostics
jn_plot - Johnson-Neyman plots
simple_plot - Simple slopes plots
model_table - Publication tables
Utilities:
by_group - Grouped analysis
as.mitml - Convert to mitml format
names - Obtain imputation variable names
with - Evaluate across imputations
write.blimp - Write results to files
Setup & Configuration:
install_blimp - Install Blimp into a managed directory
uninstall_blimp - Remove the managed install
detect_blimp - Detect Blimp installation
set_blimp - Set path to Blimp executable
has_blimp - Check if Blimp is available
update_blimp - Update Blimp installation
GitHub repository: https://github.com/blimp-stats/rblimp
Example files: https://github.com/blimp-stats/rblimp-examples
Blimp User Guide: https://docs.google.com/document/d/1D3MS79CakuX9mVVvGH13B5nRd9XLttp69oGsvrIRK64
Report issues: https://github.com/blimp-stats/rblimp/issues
Generate simulated datasets using Blimp's SIMULATE command. Supports both single-level and multilevel data generation.
rblimp_sim( model, n, seed, define = NULL, variables = NULL, tmpfolder, nopowershell = FALSE )rblimp_sim( model, n, seed, define = NULL, variables = NULL, tmpfolder, nopowershell = FALSE )
model |
Character string or vector specifying data generation equations.
Can be specified as a vector: |
n |
Sample size. For single-level: integer (e.g., |
seed |
Random seed for reproducibility. |
define |
Character vector or string of parameter definitions (optional).
Can be |
variables |
Formula or character string specifying which variables to save (optional). If not specified, all variables from the model are saved. |
tmpfolder |
Character string. Temporary folder path. If not specified, creates one with |
nopowershell |
Windows only. Uses cmd.exe instead of powershell. |
A data.frame with simulated data. The returned object has two attributes:
The Blimp syntax used for simulation (blimp_syntax object)
The raw Blimp output (blimp_out object)
Access these with attr(result, "syntax") and attr(result, "output").
SIMULATE() for creating a simulation specification to use with rblimp()
# Simple regression: y = 10 + 0.5*x + error dat <- rblimp_sim( model = c( "x = normal(0, 1)", "y = normal(10 + x*0.5, 1)" ), n = 1000, seed = 10972 ) # Same thing with full quoted syntax dat <- rblimp_sim( model = "x = normal(0, 1); y = normal(10 + x*0.5, 1);", n = 1000, seed = 10972 ) # With parameter definitions dat <- rblimp_sim( model = c( "x = normal(0, 1)", "y = normal(b0 + b1*x, s2e)" ), n = 1000, define = c("b0 = 10", "b1 = 0.5", "s2e = 1"), seed = 10972 ) # Multilevel model dat <- rblimp_sim( model = list( schools = c( "z = normal(0, 1)", "u0 = normal(10 + z*-0.5, 1.0)" ), students = c( "x = normal(0, 1)", "y = normal(u0 + x*0.5, 1)" ) ), n = list(students = 1000, schools = 100), seed = 198723 ) # Access syntax and output syntax <- attr(dat, "syntax") print(syntax) output <- attr(dat, "output") print(output)# Simple regression: y = 10 + 0.5*x + error dat <- rblimp_sim( model = c( "x = normal(0, 1)", "y = normal(10 + x*0.5, 1)" ), n = 1000, seed = 10972 ) # Same thing with full quoted syntax dat <- rblimp_sim( model = "x = normal(0, 1); y = normal(10 + x*0.5, 1);", n = 1000, seed = 10972 ) # With parameter definitions dat <- rblimp_sim( model = c( "x = normal(0, 1)", "y = normal(b0 + b1*x, s2e)" ), n = 1000, define = c("b0 = 10", "b1 = 0.5", "s2e = 1"), seed = 10972 ) # Multilevel model dat <- rblimp_sim( model = list( schools = c( "z = normal(0, 1)", "u0 = normal(10 + z*-0.5, 1.0)" ), students = c( "x = normal(0, 1)", "y = normal(u0 + x*0.5, 1)" ) ), n = list(students = 1000, schools = 100), seed = 198723 ) # Access syntax and output syntax <- attr(dat, "syntax") print(syntax) output <- attr(dat, "output") print(output)
This function runs 'imp' file with Blimp and captures the output only.
rblimp_source(file, plots = FALSE, output = TRUE, nopowershell = FALSE)rblimp_source(file, plots = FALSE, output = TRUE, nopowershell = FALSE)
file |
a character string to the 'imp' file's location |
plots |
a logical. Setting to |
output |
The type of output printed to the console.
|
nopowershell |
Windows only. Uses cmd.exe with some limited functions (instead of powershell). |
Running rblimp_source will also run a check to see if Blimp is up to date.
If Blimp is not up to date, it will prompt the user if it would like to update or not.
This check will only be performed on the first run in a session and then every ten hours.
This behavior can be disabled by setting the check_blimp_update option to FALSE using options,
or by setting the R_BLIMP_NO_UPDATE_CHECK environment variable to "true".
This check is not performed if R is not being run with an interactive session. See interactive for more information.
a blimp_out object
# Run blimp script ## Not run: output <- rblimp_source("filepath/to/syntax")# Run blimp script ## Not run: output <- rblimp_source("filepath/to/syntax")
blimp_obj
Residuals scores from blimp_obj
## S4 method for signature 'blimp_obj' resid(object, ...)## S4 method for signature 'blimp_obj' resid(object, ...)
object |
A |
... |
Additional arguments passed to residuals |
A list of data frames, one per imputation, each containing the residual columns from the model.
rblimp and SIMPLE commandGenerates a conditional effect plots based on the posterior summaries from the output of rblimp.
residual_plot( model, variable, nsigma = 1, point_col = "black", horz_line = "black", col1 = "#0571b0", col2 = "#ca0020", linewidth = 1.1, ... )residual_plot( model, variable, nsigma = 1, point_col = "black", horz_line = "black", col1 = "#0571b0", col2 = "#ca0020", linewidth = 1.1, ... )
model |
an |
variable |
the name of the outcome for which to create a plot |
nsigma |
the number of standard deviations to produce credible bounds |
point_col |
the color of the points in the plot |
horz_line |
the color of the horiztonal zero line |
col1 |
the color of the loess mean line |
col2 |
the color of the loess credible bound lines |
linewidth |
the linewidth value for the loess lines and its bounds. |
... |
arguments passed to |
All colors are passed into ggplot2. See ggplot2::aes_colour_fill_alpha for details on changing colors.
a ggplot2::ggplot plot
# Generate Data mydata <- rblimp_sim( c( 'x ~ normal(0, 1)', 'm ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x + m + 0.2*x*m, 1)' ), n = 100, seed = 981273 ) # Run Rblimp m1 <- rblimp( 'y ~ x m', mydata, nimps = 10, seed = 10972, burn = 1000, iter = 1000 ) # Generate Plot residual_plot(m1, 'y') + ggplot2::theme_minimal()# Generate Data mydata <- rblimp_sim( c( 'x ~ normal(0, 1)', 'm ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x + m + 0.2*x*m, 1)' ), n = 100, seed = 981273 ) # Run Rblimp m1 <- rblimp( 'y ~ x m', mydata, nimps = 10, seed = 10972, burn = 1000, iter = 1000 ) # Generate Plot residual_plot(m1, 'y') + ggplot2::theme_minimal()
blimp_obj
Residuals scores from blimp_obj
## S4 method for signature 'blimp_obj' residuals(object, ...)## S4 method for signature 'blimp_obj' residuals(object, ...)
object |
A |
... |
Additional arguments (unused) |
A list of data frames, one per imputation, each containing the residual columns from the model.
This function can set the Blimp executable location if it cannot be autodetected.
set_blimp(exec, beta = FALSE)set_blimp(exec, beta = FALSE)
exec |
a character string for the Blimp executable's location |
beta |
a logical value. If true |
By default rblimp tries to determine the location of Blimp's computational engine based on
standard operating system installation locations. This function is useful for non standard installations
or when wanting to use a beta version of blimp's computational engine (which must be installed via the updater).
TRUE if the executable is successfully set; otherwise, it produces an error.
# example code # Use blimp executable at `filepath` location ## Not run: set_blimp('filepath') # Use default blimp location but beta build set_blimp(beta = TRUE)# example code # Use blimp executable at `filepath` location ## Not run: set_blimp('filepath') # Use default blimp location but beta build set_blimp(beta = TRUE)
Show method for blimp_cp
## S4 method for signature 'blimp_cp' show(object)## S4 method for signature 'blimp_cp' show(object)
object |
A |
No return value, called for the side effect of printing the comparison table to the console.
Show method for blimp_obj
## S4 method for signature 'blimp_obj' show(object)## S4 method for signature 'blimp_obj' show(object)
object |
A |
No return value, called for the side effect of printing the model estimates to the console.
rblimp and SIMPLE commandGenerates a conditional effect plots based on the posterior summaries from the output of rblimp.
simple_plot(formula, model, ci = 0.95, xvals, ...)simple_plot(formula, model, ci = 0.95, xvals, ...)
formula |
an object of class |
model |
an |
ci |
a value between 0 and 1 specifying the credible interval size |
xvals |
a list of values to evaluate for the focal variable. If empty, they will automatically be determined |
... |
arguments passed to the internal |
To change colors use ggplot2's scale system. Both fill and color are used. See
ggplot2::aes_colour_fill_alpha for more information about setting a manual set of colors.
For nominal moderators, wrap the dummy codes in join(...) so they are
treated as a single compound moderator (one set of colored lines instead of
separate facets):
join() works for any user-controlled bundling, not just nominal dummies;
however it is only valid in the first (color/legend) position of the formula.
When the SIMPLE command contains more than one moderator (e.g.,
mod1 @ values and mod2 @ value), the right-hand side of the formula may list the
moderators that vary using + (e.g., focal | mod1 + mod2). Moderators that are
omitted from the formula are treated as held-constant context: their value must
be the same across the matched simple effects and is reported in the plot
subtitle.
a ggplot2::ggplot plot
## Not run: # ---- Basic single-moderator example ---- mydata <- rblimp_sim( c( 'x ~ normal(0, 1)', 'm ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x + m + 0.2*x*m, 1)' ), n = 100, seed = 981273 ) m1 <- rblimp( 'y ~ x m x*m', mydata, center = ~ m, simple = 'x | m', seed = 10972, burn = 1000, iter = 1000 ) simple_plot(y ~ x | m, m1) # ---- Two moderators: first colored, second auto-faceted ---- three_way <- rblimp_sim( c( 'x ~ normal(0, 1)', 'm1 ~ normal(0, 1)', 'm2 ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x*m1 + 0.3*x*m2 + 0.6*x*m1*m2, 1)' ), n = 500, seed = 2024 ) fit <- rblimp( 'y ~ x m1 m2 x*m1 x*m2 m1*m2 x*m1*m2', three_way, center = ~ x + m1 + m2, simple = 'x | m1 @ quantile and m2 @ sd', seed = 1071, burn = 1000, iter = 1000 ) simple_plot(y ~ x | m1 + m2, fit) # Pin a moderator to one SIMPLE value via `at()` simple_plot(y ~ x | m1 + at(m2 = "0"), fit) # Restrict to a subset of values simple_plot(y ~ x | m1 + at(m2 = c("-1 SD", "+1 SD")), fit) # Bundle moderators (e.g. nominal dummy codes) via `join()` simple_plot(y ~ x | join(m1, m2), fit) ## End(Not run)## Not run: # ---- Basic single-moderator example ---- mydata <- rblimp_sim( c( 'x ~ normal(0, 1)', 'm ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x + m + 0.2*x*m, 1)' ), n = 100, seed = 981273 ) m1 <- rblimp( 'y ~ x m x*m', mydata, center = ~ m, simple = 'x | m', seed = 10972, burn = 1000, iter = 1000 ) simple_plot(y ~ x | m, m1) # ---- Two moderators: first colored, second auto-faceted ---- three_way <- rblimp_sim( c( 'x ~ normal(0, 1)', 'm1 ~ normal(0, 1)', 'm2 ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x*m1 + 0.3*x*m2 + 0.6*x*m1*m2, 1)' ), n = 500, seed = 2024 ) fit <- rblimp( 'y ~ x m1 m2 x*m1 x*m2 m1*m2 x*m1*m2', three_way, center = ~ x + m1 + m2, simple = 'x | m1 @ quantile and m2 @ sd', seed = 1071, burn = 1000, iter = 1000 ) simple_plot(y ~ x | m1 + m2, fit) # Pin a moderator to one SIMPLE value via `at()` simple_plot(y ~ x | m1 + at(m2 = "0"), fit) # Restrict to a subset of values simple_plot(y ~ x | m1 + at(m2 = c("-1 SD", "+1 SD")), fit) # Bundle moderators (e.g. nominal dummy codes) via `join()` simple_plot(y ~ x | join(m1, m2), fit) ## End(Not run)
Creates a simulation specification that can be passed to rblimp() as the data argument.
Instead of reading existing data, rblimp() will use Blimp's SIMULATE command to generate
data and then fit the specified model to it.
SIMULATE(model, n, define = NULL, variables = NULL)SIMULATE(model, n, define = NULL, variables = NULL)
model |
Character string or vector specifying data generation equations.
Same format as |
n |
Sample size. For single-level: integer. For multilevel: named list. |
define |
Character vector or string of parameter definitions (optional). |
variables |
Formula or character string specifying which variables to save (optional). |
A blimp_simulate object (subclass of blimp_syntax) that can be passed to
rblimp() as the data argument.
rblimp_sim() for directly generating simulated data without fitting a model
# Create simulation specification sim_spec <- SIMULATE( model = c( "x = normal(0, 1)", "y = normal(10 + x*0.5, 1)" ), n = 1000 ) # View the specification print(sim_spec) # Use in rblimp to fit a model to simulated data mdl <- rblimp( model = "y ~ x", data = sim_spec, seed = 123, burn = 5000, iter = 5000 ) summary(mdl)# Create simulation specification sim_spec <- SIMULATE( model = c( "x = normal(0, 1)", "y = normal(10 + x*0.5, 1)" ), n = 1000 ) # View the specification print(sim_spec) # Use in rblimp to fit a model to simulated data mdl <- rblimp( model = "y ~ x", data = sim_spec, seed = 123, burn = 5000, iter = 5000 ) summary(mdl)
Extracts the data information section from Blimp output.
standardized(object)standardized(object)
object |
A |
A base::matrix with standardized solutions
Provides formatted summary output for blimp_obj, optionally allowing selection by variable name or block. Offers cleaner presentation than the raw estimates matrix.
## S4 method for signature 'blimp_obj' summary(object, selector, digits = 3, ...)## S4 method for signature 'blimp_obj' summary(object, selector, digits = 3, ...)
object |
A |
selector |
Optional character string specifying the variable name or block to extract. If missing, shows all estimates. Can be a variable name (e.g., "y1") or a block name (e.g., "between"). |
digits |
Integer specifying the number of decimal places for rounding. Default is 3. |
... |
Additional arguments (for S4 method compatibility). |
If selector is provided: invisibly returns a matrix or list of estimates for the selected variable/block. If no selector: returns the full estimates matrix with improved formatting.
Generates ggplot2::ggplot plots using ggplot2::geom_line based on the output from rblimp
trace_plot(model, selector, ...)trace_plot(model, selector, ...)
model |
an |
selector |
a name of a variable, a name of a parameter, a number of a parameter, or a combination of any of them. If left empty, a plot of all parameters will be returned. See Examples. |
... |
arguments passed to internally called |
To change colors use ggplot2's scale system. Both fill and color are used. See
ggplot2::aes_colour_fill_alpha for more information about setting a manual set of colors.
a ggplot2::ggplot plot
# Generate Data mydata <- rblimp_sim( c( 'x ~ normal(0, 1)', 'm ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x + m + 0.2*x*m, 1)' ), n = 100, seed = 981273 ) # Run Rblimp m1 <- rblimp( c( 'y ~ x m', 'x ~~ m' ), mydata, seed = 10972, burn = 1000, iter = 1000 ) # Generate plot of all parameters with `y` trace_plot(m1, 'y') + ggplot2::theme_minimal() # Generate plot of all parameters with `y` # Add limits to only graph first 250 iterations trace_plot(m1, 'y') + ggplot2::xlim(0, 250) + ggplot2::theme_minimal() # Generate plot of all parameters trace_plot(m1) + ggplot2::theme_minimal() # Generate plot of all parameters for `y` and `x` trace_plot(m1, c('x', 'y')) + ggplot2::theme_minimal() # Generate Plot of Parameter 5 trace_plot(m1, 5) + ggplot2::theme_minimal() # Generate plot of `x residual variance` trace_plot(m1, 'x residual variance') + ggplot2::theme_minimal() # Generate plot of Parameters 7 and 9 trace_plot(m1, c(7, 9)) + ggplot2::theme_minimal()# Generate Data mydata <- rblimp_sim( c( 'x ~ normal(0, 1)', 'm ~ normal(0, 1)', 'y ~ normal(10 + 0.5*x + m + 0.2*x*m, 1)' ), n = 100, seed = 981273 ) # Run Rblimp m1 <- rblimp( c( 'y ~ x m', 'x ~~ m' ), mydata, seed = 10972, burn = 1000, iter = 1000 ) # Generate plot of all parameters with `y` trace_plot(m1, 'y') + ggplot2::theme_minimal() # Generate plot of all parameters with `y` # Add limits to only graph first 250 iterations trace_plot(m1, 'y') + ggplot2::xlim(0, 250) + ggplot2::theme_minimal() # Generate plot of all parameters trace_plot(m1) + ggplot2::theme_minimal() # Generate plot of all parameters for `y` and `x` trace_plot(m1, c('x', 'y')) + ggplot2::theme_minimal() # Generate Plot of Parameter 5 trace_plot(m1, 5) + ggplot2::theme_minimal() # Generate plot of `x residual variance` trace_plot(m1, 'x residual variance') + ggplot2::theme_minimal() # Generate plot of Parameters 7 and 9 trace_plot(m1, c(7, 9)) + ggplot2::theme_minimal()
Deletes the managed install directory created by install_blimp. Does
not affect system installations of Blimp.
uninstall_blimp(quiet = FALSE)uninstall_blimp(quiet = FALSE)
quiet |
Logical; suppress messages and skip the interactive
confirmation prompt. Default |
TRUE if anything was removed, FALSE otherwise (invisibly).
## Not run: uninstall_blimp()## Not run: uninstall_blimp()
If Blimp was installed via install_blimp, the latest version is
re-downloaded into the managed directory. If Blimp was installed via the
system installer, the Blimp Updater application is launched (back-compat).
update_blimp()update_blimp()
If the session is not interactive, the updater is not opened.
For managed installs, the manifest is checked first and the engine is re-downloaded only if a newer version exists.
Logical indicating whether an update was started.
Calling update_blimp() on a managed install contacts
updates.blimpstats.com over HTTPS to check for a newer version of
Blimp, and downloads from it if one is available. Information may be
collected as described in the privacy policy.
See privacy policy: https://www.blimpstats.com/privacy.
# Update Blimp ## Not run: update_blimp()# Update Blimp ## Not run: update_blimp()
mitml package using by_group
Fit Model across imputations with mitml package using by_group
## S4 method for signature 'blimp_bygroup' with(data, expr, ...)## S4 method for signature 'blimp_bygroup' with(data, expr, ...)
data |
A |
expr |
An expression to evaluate on each imputation |
... |
Additional arguments (unused) |
A list of class "mitml.result" containing the results of
evaluating expr on each imputed data set.
mitml packageFit Model across imputations with mitml package
## S4 method for signature 'blimp_obj' with(data, expr, ...)## S4 method for signature 'blimp_obj' with(data, expr, ...)
data |
A |
expr |
An expression to evaluate on each imputation |
... |
Additional arguments (unused) |
A list of class "mitml.result" containing the results of
evaluating expr on each imputed data set.
A function to write out blimp input and output from a model
write.blimp(object, folder = "") ## S4 method for signature 'blimp_syntax' write.blimp(object, folder = "") ## S4 method for signature 'blimp_out' write.blimp(object, folder = "") ## S4 method for signature 'blimp_obj' write.blimp(object, folder = "")write.blimp(object, folder = "") ## S4 method for signature 'blimp_syntax' write.blimp(object, folder = "") ## S4 method for signature 'blimp_out' write.blimp(object, folder = "") ## S4 method for signature 'blimp_obj' write.blimp(object, folder = "")
object |
A |
folder |
a location to a folder to write input and output |
No return value, called for its side effect of writing 'Blimp' input and output files to disk.
write.blimp(blimp_syntax): Write blimp_syntax to file
write.blimp(blimp_out): Write blimp_out to file
write.blimp(blimp_obj): Write blimp_obj files to folder
# Generate Data with `rblimp_sim` mydata <- rblimp_sim( c( 'f ~ normal(0, 1)', 'x1:x5 ~ normal(f, 1)', 'y ~ normal(10 + 0.3*f, 1 - .3^2)' ), n = 500, seed = 19723, variables = c('y', 'x1:x5') ) # Fit SEM Model model <- rblimp( list( structure = 'y ~ f', measurement = 'f -> x1:x5' ), mydata, seed = 3927, latent = ~ f ) # Write out input and output ## Not run: write.blimp(model, "folder_location") ## End(Not run)# Generate Data with `rblimp_sim` mydata <- rblimp_sim( c( 'f ~ normal(0, 1)', 'x1:x5 ~ normal(f, 1)', 'y ~ normal(10 + 0.3*f, 1 - .3^2)' ), n = 500, seed = 19723, variables = c('y', 'x1:x5') ) # Fit SEM Model model <- rblimp( list( structure = 'y ~ f', measurement = 'f -> x1:x5' ), mydata, seed = 3927, latent = ~ f ) # Write out input and output ## Not run: write.blimp(model, "folder_location") ## End(Not run)