read_hobbs: Read hobbs binary output

View source: R/hobbs.R

read_hobbsR Documentation

Read hobbs binary output

Description

Reads the default binary chain. When an hobbs_run uses declaration-level save=mean, this returns the full draws for the unmarked parameters. Use read_hobbs_mean() for the one-row binary of mean-only parameters.

Usage

read_hobbs(file, dim = NULL, max_records = NULL, param_names = NULL)

Arguments

file

Path to binary chain output.

dim

Number of parameters in the chain. If omitted, it is read from the binary header written by recent hobbs versions.

max_records

Optional maximum number of records to read.

param_names

Optional names for theta columns. Usually supplied automatically when reading a hobbs_run object returned by hobbs().

Value

A data frame with columns iter, accepted, logp, and the saved parameter columns.

Examples

library(hobbs)

set.seed(1)
n <- 200L
p <- 4L
X <- cbind(1, matrix(rnorm(n * (p - 1L)), nrow = n))
beta_true <- c(0.5, 1, -0.75, 0.25)
sigma_true <- 0.75
y <- as.numeric(X %*% beta_true + rnorm(n, sd = sigma_true))

dat <- list(n = n, p = p, X = X, y = y)

model <- '
param beta(p);
param logsigma(1);

func llk() {
  double sigma = exp(logsigma(1));
  for (i = 1:n) {
    y(i) ~ dnorm(mu(i), sigma);
  }
}

block beta(j) {
  beta(j) ~ dnorm(0, 10);
  llk();
} cache mu(n) {
  for (i = 1:n) {
    for (k = 1:p) {
      mu(i) += beta(k) * X(i, k);
    }
  }
} update mu(n) {
  for (i = 1:n) {
    mu(i) += (proposal(beta(j)) - current(beta(j))) * X(i, j);
  }
}

block logsigma(1) {
  logsigma(1) ~ dnorm(0, 2);
  llk();
}
'

out <- tempfile("regression", fileext = ".bin")

fit <- hobbs(
    model = model,
    data = dat,
    samples = 2000,
    burnin = 1000,
    seed = 123,
    out = out
)

draws <- read_hobbs(fit)
colMeans(draws[paste0("beta[", seq_len(p), "]")])


hobbs documentation built on Oct. 6, 2026, 1:07 a.m.