if (FALSE) {
#################################################################
## Example 1
#################################################################
## Subset of the build-in dataset, see ?ex0.dag.data
mydat <- ex0.dag.data[,c("b1","b2","g1","g2","b3","g3")] ## take a subset of cols
## setup distribution list for each node
mydists <- list(b1="binomial", b2="binomial", g1="gaussian",
g2="gaussian", b3="binomial", g3="gaussian")
# Structural constraints
# ban arc from b2 to b1
# always retain arc from g2 to g1
## parent limits
max.par <- list("b1"=2, "b2"=2, "g1"=2, "g2"=2, "b3"=2, "g3"=2)
## now build the cache of pre-computed scores accordingly to the structural constraints
res.c <- buildScoreCache(data.df=mydat, data.dists=mydists,
dag.banned= ~b1|b2, dag.retained= ~g1|g2, max.parents=max.par)
## repeat but using R-INLA. The mlik's should be virtually identical.
## now build cache:
if(requireNamespace("INLA", quietly = TRUE)){
res.inla <- buildScoreCache(data.df=mydat, data.dists=mydists,
dag.banned= ~b1|b2, dag.retained= ~g1|g2, max.parents=max.par,
control=list(max.mode.error=100))
## comparison - very similar
difference <- res.c$mlik - res.inla$mlik
}
## Comparison Bayes with MLE (unconstrained):
res.mle <- buildScoreCache(data.df=mydat, data.dists=mydists,
max.parents=3, method="mle")
res.abn <- buildScoreCache(data.df=mydat, data.dists=mydists,
max.parents=3, method="Bayes")
## of course different, but smame order:
plot(-res.mle$bic, res.abn$mlik)
#################################################################
## Example 2 - mle with several cores
###################################################################
## Many variables, few observations
mydat <- ex0.dag.data
mydists <- as.list(rep(c("binomial", "gaussian", "poisson"), each=10))
names(mydists) <- names(mydat)
# system.time( {
# res.mle1 <- buildScoreCache(data.df=mydat, data.dists=mydists,
# max.parents=2, method="mle", ncores=2) })
# system.time( {
# res.mle2 <- buildScoreCache(data.df=mydat, data.dists=mydists,
# max.parents=2, method="mle") })
#################################################################
## Example 3 - grouped data - random effects example e.g. glmm
###################################################################
mydat <- ex3.dag.data ## this data comes with abn see ?ex3.dag.data
mydists <- list(b1="binomial", b2="binomial", b3="binomial",
b4="binomial", b5="binomial", b6="binomial", b7="binomial",
b8="binomial", b9="binomial", b10="binomial",b11="binomial",
b12="binomial", b13="binomial" )
max.par <- 2
## in this example INLA is used as default since these are glmm nodes
## when running this at node-parent combination 71 the default accuracy check on the
## INLA modes is exceeded (default is a max. of 10 percent difference from
## modes estimated using internal code) and a message is given that internal code
## will be used in place of INLA's results.
# mycache <- buildScoreCache(data.df=mydat, data.dists=mydists, group.var="group",
# cor.vars=c("b1","b2","b3","b4","b5","b6","b7",
# "b8","b9","b10","b11","b12","b13"),
# max.parents=max.par, which.nodes=c(1))
}
Run the code above in your browser using DataLab