<packageNotFoundError/error/condition>
Error in `library(p, character.only = TRUE)`: there is no package called 'INLA'

mydag <- createAbnDag(dag = ~a+b|a, data.df = data.frame("a"=1, "b"=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",
## now build the cache of pre-computed scores accordingly to the structural constraints
dag.banned= ~b1|b2, dag.retained= ~g1|g2, max.parents=max.par)
mydat <- ex1.dag.data ## this data comes with abn see ?ex1.dag.data
mycache <- buildScoreCache(data.df = mydat, data.dists = mydists, max.parents = 2)
heur.res <- searchHeuristic(score.cache = mycache, data.dists = mydists,
start.dag = "random", num.searches = 10,
## this data comes with abn see ?ex1.dag.data
mydists <- list(b1="binomial", p1="poisson", g1="gaussian", b2="binomial",
## Build cache may take some minutes for buildScoreCache()
skip("plotting for abnHillClimber is not up to date with searchHillClimber()'s output.")
## This data comes with `abn` see ?ex1.dag.data
## Setup distribution list for each node:
## Parent limits, for speed purposes quite specific here:
## Now build cache (no constraints in ban nor retain)
mycache <- buildScoreCache(data.df=mydat, data.dists=mydists, max.parents=max.par)
mp.dag <- mostProbable(score.cache=mycache, verbose = FALSE)
myres <- fitAbn(object=mp.dag,create.graph=TRUE, verbose = FALSE)

"plotting for abnHillClimber is not up to date with searchHillClimber()'s output."

Expected `{ ... }` to run without any errors.
i Actually got a <packageNotFoundError> with text:
  there is no package called 'INLA'

nthreads = control[["ncores"]] # single threaded

# We can use the same data as for calc.node.inla.glm() for this rough test.

fctrl.bayes <- fit.control(method = "bayes")
fctrl.mle <- fit.control(method = "mle")
fit.control(method = "bayes", epsilon = 1e-06) # no error if argument and method don't correspond
test_that("fitAbn() wrapper of 'mle' and 'bayes' works", {
dist <- list(Ozone="gaussian", Solar.R="gaussian", Wind="gaussian", Temp="gaussian", Month="gaussian", Day="gaussian")
colnames(d) <- rownames(d) <- names(dist)
## test wrapper of method "mle" and "bayes"
m.0.mle <- abn:::fitAbn.mle(dag=d, data.df=df, data.dists=dist)
m.0.mle.1 <- fitAbn(dag=d, data.df=df, data.dists=dist, method="mle")
m.0.bayes.1 <- fitAbn(dag=d, data.df=df, data.dists=dist, method="bayes")
test_that("fitAbn() works with DAG as formula statement", {
expect_silent(fitAbn(dag=~b1|b2, data.df=ex3.dag.data[,c(1,2)], data.dists=mydists, method = "bayes"))
expect_silent(fitAbn(dag=~b1|b2, data.df=ex3.dag.data[,c(1,2)], data.dists=mydists, method = "mle"))
## test formula statement with correlation
expect_silent(fitAbn(dag=~b1|b2, data.df=ex3.dag.data[,c(1,2,14)], data.dists=mydists, method = "bayes", group.var="group", cor.vars=c("b1","b2")))
expect_silent(fitAbn(dag=~b1|b2, data.df=ex3.dag.data[,c(1,2,14)], data.dists=mydists, method = "mle", group.var="group", cor.vars=c("b1","b2")))
test_that("fitAbn() catches wrong arguments.", {
fitAbn(dag=~b1|b2, data.df=ex3.dag.data[,c(1,2)], data.dists=mydists, centre = TRUE))
fitAbn(dag=~b1|b2, data.df=ex3.dag.data[,c(1,2)], data.dists=mydists, centre = FALSE))
fitAbn(dag=~b1|b2, data.df=ex3.dag.data[,c(1,2)], data.dists=mydists, centre = NULL),
regexp = "'centre' should be either TRUE or FALSE")
# TODO: add tests for other arguments here.
# Test for different method arguments e.g. fitAbn(method="mle", control=fit.control(method="bayes")). -> Consider to remove this from fit.control...
test_that("fitAbn() runs in parallel", {
fitAbn(dag=d, data.df=df, data.dists=dist, method="bayes", control = list(ncores = 1))
fitAbn(dag=d, data.df=df, data.dists=dist, method="bayes", control = list(ncores = 0))
fitAbn(dag=d, data.df=df, data.dists=dist, method="bayes", control = list(ncores = -2))
skip_on_cran() # workaround to not overconsume threads on CRAN. This is related to an issue reported for lme4
fitAbn(dag=d, data.df=df, data.dists=dist, method="bayes", control = list(ncores = 2))
fitAbn(dag=d, data.df=df, data.dists=dist, method="mle", control = list(ncores = 1))
fitAbn(dag=d, data.df=df, data.dists=dist, method="mle", control = list(ncores = 0))
fitAbn(dag=d, data.df=df, data.dists=dist, method="mle", control = list(ncores = -2))
skip_on_cran() # workaround to not overconsume threads on CRAN. This is related to an issue reported for lme4 (https://github.com/lme4/lme4/issues/627)
fitAbn(dag=d, data.df=df, data.dists=dist, method="mle", control = list(ncores = 2))
test_that("fitAbn() works with control arguments", {
# Supplying control arguments as direct arguments is deprecated. They should be passed in a list of 'control=list(max.mode.error=0)'
expect_no_error(fitAbn(dag=~b1|b2, data.df=ex3.dag.data[,c(1,2)], data.dists=mydists, method = "bayes", control=list(max.mode.error=0)))
expect_warning(fitAbn(dag=~b1|b2, data.df=ex3.dag.data[,c(1,2)], data.dists=mydists, method = "mle", control=list(max.mode.error=0)))
## null model - all independent variables
colnames(mydag.empty) <- rownames(mydag.empty) <- names(mydat);
## now repeat but include some dependencies
expect_no_error(fitAbn(dag=mydag,data.df=mydat,data.dists=mydists, method = "bayes", centre=TRUE, compute.fixed=TRUE, control=list(n.grid=100)))
expect_warning(fitAbn(dag=mydag,data.df=mydat,data.dists=mydists, method = "mle", centre=TRUE, compute.fixed=TRUE, control=list(n.grid=100)))
test_that("fitAbn() is backward compatible", {
## now fit the model to calculate its goodness of fit
}) # create.graph argument is deprecated and should be ignored.
## a simple plot of some posterior densities
## the algorithm which chooses density points is very simple any may be
## rather sparse so also recompute the density over an equally spaced
## grid of 100 points between the two end points
myres.c <- fitAbn(dag=mydag,data.df=mydat,data.dists=mydists,centre=TRUE, compute.fixed=TRUE, control=list(n.grid=100))
## repeat but use INLA for the numerics using max.mode.error=100
## as using internal code is the default here rather than INLA
myres.inla <- fitAbn(dag=mydag,data.df=mydat,data.dists=mydists,centre=TRUE, compute.fixed=TRUE, control=list(max.mode.error=100))
### A very simple mixed model example using built-in data
## specify DAG - only two variables using subset of variables from ex3.dag.data
## both variables are assumed to need (separate) adjustment for the group variable
colnames(mydag) <- rownames(mydag) <- names(ex3.dag.data[,c(1,2)]);
## just compute marginal likelihood - use internal code via max.mode.error=0
expect_true(any(myres.inla$used.INLA)) #BUG: this switches back to internal C code eventhough INLA is enforced...
## compare log marginal likelihoods for each node and total DAG - should be very similar
## now for marginals - INLA is strongly preferable for estimating marginals for nodes
## with random effects as it is far faster, but may not be reliable
# INLA's estimates of the marginals, using high n.grid=500
# as this makes the plots smoother - see below.
## this is NOT recommended - marginal density estimation using fitabn in mixed models
## is really just for diagnostic purposes, better to use fitabn.inla() here
### these are very similar although not exactly identical
## use internal code but only to compute a single parameter over a specified grid
## this can be necessary if the simple auto grid finding functions does a poor job
#                 marginal.node=1,marginal.param=3,## precision term in node 1
test_that("fitabn() works with all distributions, grouping and class abnCache", {
mydists <- mydists[-1] # remove grouping variable from distribution list
## buildScoreCache -> mostProbable() -> fitAbn()
mycache.bayes <- buildScoreCache(data.df = df, data.dists = mydists, method = "bayes",
group.var = "Pedigree", adj.vars = NULL, cor.vars = NULL,
#   mp.dag.bayes <- mostProbable(score.cache = mycache.bayes, verbose = FALSE)
# myres.bayes <- fitAbn(object = mp.dag.bayes, data.df = df, data.dists = mydists)
mycache.mle <- buildScoreCache(data.df = df, data.dists = mydists, method = "mle",
mp.dag.mle <- mostProbable(score.cache = mycache.mle, verbose = FALSE)
myres.mle <- fitAbn(object = mp.dag.mle, data.df = df, data.dists = mydists)
}, regexp = "`data.df` and `object` provided but can only accept one of them")
myres.mle <- fitAbn(object = mp.dag.mle, data.dists = mydists)
}, regexp = "`data.dists` and `object` provided but can only accept one of them")
myres.mle <- fitAbn(object = mp.dag.mle, method = "mle")
## buildScoreCache -> searchHillClimber() -> fitAbn()
#   hc.dag.bayes <- searchHillClimber(score.cache = mycache.bayes, verbose = FALSE)
# myres.bayes <- fitAbn(object = hc.dag.bayes, data.df = df, data.dists = mydists)
hc.dag.mle <- searchHillClimber(score.cache = mycache.mle, verbose = FALSE)
myres.mle <- fitAbn(object = hc.dag.mle, data.df = df, data.dists = mydists)
myres.mle <- fitAbn(object = hc.dag.mle, data.dists = mydists)
myres.mle <- fitAbn(object = hc.dag.mle, method = "mle")