rblimp_sim: Simulate data using Blimp

View source: R/rblimp_sim.R

rblimp_simR Documentation

Simulate data using Blimp

Description

Generate simulated datasets using Blimp's SIMULATE command. Supports both single-level and multilevel data generation.

Usage

rblimp_sim(
  model,
  n,
  seed,
  define = NULL,
  variables = NULL,
  tmpfolder,
  nopowershell = FALSE
)

Arguments

model

Character string or vector specifying data generation equations. Can be specified as a vector: c("x = normal(0, 1)", "y = normal(10, 1)") OR as a single string with newlines/semicolons: "x = normal(0, 1); y = normal(10, 1);" For multilevel: use named list with arbitrary level names (e.g., list(schools = ..., students = ...)). The sample size determines the level hierarchy (largest = level-1, smallest = highest level).

n

Sample size. For single-level: integer (e.g., 1000). For multilevel: named list with sample sizes (e.g., list(students = 1000, schools = 100)). Names must match the names used in model list. Sample sizes must be unique.

seed

Random seed for reproducibility.

define

Character vector or string of parameter definitions (optional). Can be c("b0 = 10", "b1 = 0.5") OR "b0 = 10; b1 = 0.5;".

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 tempdir.

nopowershell

Windows only. Uses cmd.exe instead of powershell.

Value

A data.frame with simulated data. The returned object has two attributes:

syntax

The Blimp syntax used for simulation (blimp_syntax object)

output

The raw Blimp output (blimp_out object)

Access these with attr(result, "syntax") and attr(result, "output").

See Also

SIMULATE() for creating a simulation specification to use with rblimp()

Examples


# 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)


rblimp documentation built on May 18, 2026, 9:07 a.m.