if (FALSE) {
# Load example data and fit a model
library(abn)
data(ex1.dag.data)
# Define distributions
mydists <- list(b1 = "binomial", p1 = "poisson", g1 = "gaussian",
b2 = "binomial", p2 = "poisson", g2 = "gaussian",
b3 = "binomial", g3 = "gaussian")
# Build score cache
mycache <- buildScoreCache(data.df = ex1.dag.data,
data.dists = mydists,
method = "mle",
max.parents = 2)
# Find most probable DAG
mp_dag <- mostProbable(score.cache = mycache)
# Fit the model
myfit <- fitAbn(object = mp_dag, method = "mle")
# Export to JSON string with metadata
json_export <- export_abnFit(myfit,
scenario_id = "example_model_v1",
label = "Example ABN Model")
# View the structure
library(jsonlite)
parsed <- fromJSON(json_export)
str(parsed, max.level = 2)
# Export to file
export_abnFit(myfit,
file = "my_abn_model.json",
scenario_id = "example_model_v1",
label = "Example ABN Model",
pretty = TRUE)
# Export with compact formatting
compact_json <- export_abnFit(myfit, pretty = FALSE)
# ---
# Mixed-effects model example
# (Requires data with grouping structure)
# Add grouping variable
ex1.dag.data$group <- rep(1:5, length.out = nrow(ex1.dag.data))
# Build cache with grouping
mycache_grouped <- buildScoreCache(data.df = ex1.dag.data,
data.dists = mydists,
method = "mle",
group.var = "group",
max.parents = 2)
# Fit grouped model
myfit_grouped <- fitAbn(object = mp_dag,
method = "mle",
group.var = "group")
# Export grouped model (includes random effects)
json_grouped <- export_abnFit(myfit_grouped,
scenario_id = "grouped_model_v1",
label = "Mixed Effects ABN")
}
Run the code above in your browser using DataLab