## Generate example data
t <- runif(1000, -10, 10)
true_y <- 2*sin(t) + -0.06*t^2
y <- rnorm(length(true_y), true_y, 1)
## Fit model (using unstandardized expansions for consistent inference)
model_fit <- lgspline(t, y,
K = 7,
standardize_expansions_for_fitting = FALSE)
## Compare Wald (= t-intervals here) to Monte Carlo credible intervals
# Get Wald intervals
wald <- wald_univariate(model_fit,
cv = qt(0.975, df = model_fit$trace_XUGX))
wald_bounds <- cbind(wald[["interval_lb"]], wald[["interval_ub"]])
## Generate posterior samples (uniform prior)
post_draws <- generate_posterior(model_fit,
theta_1 = -1,
theta_2 = 0,
num_draws = 2000)
## Convert to matrix and compute credible intervals
post_mat <- Reduce('cbind',
lapply(post_draws$post_draw_coefficients,
function(x) Reduce("rbind", x)))
post_bounds <- t(apply(post_mat, 1, quantile, c(0.025, 0.975)))
## Compare intervals
print(round(cbind(wald_bounds, post_bounds), 4))
Run the code above in your browser using DataLab