# \donttest{
# Load the example dataset
library(hbsaems)
data("data_fhnorm")
# Prepare the dataset
data <- data_fhnorm
# Fit the Basic Model
model <- hbm(
formula = bf(y ~ x1 + x2 + x3), # Formula model
hb_sampling = "gaussian", # Gaussian family for continuous outcomes
hb_link = "identity", # Identity link function (no transformation)
data = data, # Dataset
chains = 4, # Number of MCMC chains
iter = 4000, # Total MCMC iterations
warmup = 2000, # Number of warmup iterations
cores = 2 # Parallel processing
)
summary(model)
# Fit the Basic Model With Defined Random Effect
model_with_defined_re <- hbm(
formula = bf(y ~ x1 + x2 + x3), # Formula model
hb_sampling = "gaussian", # Gaussian family
hb_link = "identity", # Identity link
re = ~(1 | group), # Defined random effect
data = data,
chains = 4,
iter = 4000,
warmup = 2000,
cores = 2
)
summary(model_with_defined_re)
# Fit the Model with Missing Data
# a. Handling missing by deletion
data_miss <- data
data_miss$y[3:5] <- NA
model_deleted <- hbm(
formula = bf(y ~ x1 + x2 + x3),
hb_sampling = "gaussian",
hb_link = "identity",
re = ~(1 | group),
data = data,
handle_missing = "deleted",
chains = 4,
iter = 4000,
warmup = 2000,
cores = 2
)
summary(model_deleted)
# b. Handling missing using multiple imputation
model_multiple <- hbm(
formula = bf(y ~ x1 + x2 + x3),
hb_sampling = "gaussian",
hb_link = "identity",
re = ~(1 | group),
data = data,
handle_missing = "multiple",
chains = 4,
iter = 4000,
warmup = 2000,
cores = 2
)
summary(model_multiple)
# c. Handling missing during modeling
data_miss$y[3:5] <- NA
data_miss$x1[6:7] <- NA
model_model <- hbm(
formula = bf(y | mi() ~ mi(x1) + x2 + x3) +
bf(x1 | mi() ~ x2 + x3),
hb_sampling = "gaussian",
hb_link = "identity",
re = ~(1 | group),
data = data,
handle_missing = "model",
chains = 4,
iter = 4000,
warmup = 2000,
cores = 2
)
summary(model_model)
# Fit the Model with Spatial Effect
# a. CAR (Conditional Autoregressive)
data("adjacency_matrix_car")
adjacency_matrix_car
model_spatial_car <- hbm(
formula = bf(y ~ x1 + x2 + x3 ),
hb_sampling = "gaussian",
hb_link = "identity",
data = data,
sre = "sre",
sre_type = "car",
M = adjacency_matrix_car,
chains = 4,
iter = 4000,
warmup = 2000,
cores = 2
)
summary(model_spatial_car)
# b. SAR (Simultaneous Autoregressive)
data("spatial_weight_sar")
spatial_weight_sar
model_spatial_sar <- hbm(
formula = bf(y ~ x1 + x2 + x3 ),
hb_sampling = "gaussian",
hb_link = "identity",
data = data,
sre_type = "sar",
M = spatial_weight_sar,
chains = 4,
iter = 4000,
warmup = 2000,
cores = 2
)
# }
Run the code above in your browser using DataLab