| read_hobbs | R Documentation |
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.
read_hobbs(file, dim = NULL, max_records = NULL, param_names = NULL)
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 |
A data frame with columns iter, accepted, logp, and the saved parameter columns.
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), "]")])
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.