##
## use built-in simulated data set
mydat<-ex0.dag.data[,c("b1","b2","b3","g1","b4","p2","p4")];## take a subset of cols
## setup distribution list for each node
mydists<-list(b1="binomial",
b2="binomial",
b3="binomial",
g1="gaussian",
b4="binomial",
p2="poisson",
p4="poisson"
);
## null model - all independent variables
mydag.empty<-matrix(data=c(
0,0,0,0,0,0,0, #
0,0,0,0,0,0,0, #
0,0,0,0,0,0,0, #
0,0,0,0,0,0,0, #
0,0,0,0,0,0,0, #
0,0,0,0,0,0,0, #
0,0,0,0,0,0,0 #
), byrow=TRUE,ncol=7);
colnames(mydag.empty)<-rownames(mydag.empty)<-names(mydat);
## fit model to get goodness of fit
myres.inla<-fitabn.inla(dag.m=mydag.empty,data.df=mydat,data.dists=mydists,centre=TRUE,
compute.fixed=FALSE);
print(myres.inla$mlik);## log marginal likelihood goodness of fit for complete DAG
## now repeat but include some dependencies
mydag<-mydag.empty;
mydag["b1","b2"]<-1; # b1<-b2
mydag["b2","p4"]<-1; # b2<-p4
mydag["b2","g1"]<-1; # b2<-g1
mydag["g1","p2"]<-1; # g1<-p2
mydag["b3","g1"]<-1; # b3<-g1
mydag["b4","b1"]<-1; # b4<-b1
mydag["p4","g1"]<-1; # p4<-g1
myres.inla<-fitabn.inla(dag.m=mydag,data.df=mydat,data.dists=mydists,centre=TRUE,
compute.fixed=FALSE);
print(myres.inla$mlik);## a much poorer fit than full independence DAG
## now also plot the graph via graphviz
myres.inla<-fitabn.inla(dag.m=mydag,data.df=mydat,data.dists=mydists,centre=TRUE,
create.graph=TRUE,compute.fixed=FALSE);
## ok for quick visualisation - but not publication quality
plot(myres.inla$graph);# see ?plot.graph for details.
## for publication quality may be better to use graphviz direct
tographviz(dag.m=mydag,data.df=mydat,data.dists=mydists,outfile="graph.dot");
## and then process using graphviz tools e.g. on linux
system("dot -Tpdf -o graph.pdf graph.dot")
system("evince graph.pdf");
## a simple plot of some posterior densities
myres.inla<-fitabn.inla(dag.m=mydag,data.df=mydat,data.dists=mydists,centre=TRUE,
compute.fixed=TRUE,create.graph=TRUE);
## compare with non-INLA version
myres.c<-fitabn(dag.m=mydag,data.df=mydat,data.dists=mydists,centre=TRUE,
compute.fixed=TRUE,n.grid=100);
print(names(myres.c$marginals));## gives all the different parameter names
## the plot below shows only red as the lines are virtually on top of each other
par(mfrow=c(2,2));
plot(myres.c$marginals[["b1|(Intercept)"]],type="b",xlab="b1|(Intercept)",
main="Posterior Marginal Density",pch="+");
points(myres.inla$marginals[["b1|(Intercept)"]],col="red");
plot(myres.c$marginals[["b2|p4"]],type="b",xlab="b2|p4",
main="Posterior Marginal Density",pch="+");
points(myres.inla$marginals[["b2|p4"]],col="red");
plot(myres.c$marginals[["g1|precision"]],type="b",xlab="g1|precision",
main="Posterior Marginal Density",pch="+");
points(myres.inla$marginals[["g1|precision"]],col="red");
plot(myres.c$marginals[["b4|b1"]],type="b",xlab="b4|b1",
main="Posterior Marginal Density",pch="+");
points(myres.inla$marginals[["b4|b1"]],col="red");
### A very simple mixed model example using built-in data
## specify dag - only two variables using subset of variables from ex3.dag.data
mydag<-matrix(data=c(
0,1, # b1
0,0 # b2
), byrow=TRUE,ncol=2);
colnames(mydag)<-rownames(mydag)<-names(ex3.dag.data[,c(1,2)]);
## specific distributions
mydists<-list(b1="binomial",
b2="binomial"
);
## the variable "group" in grouped.dag.data contains the group membership information
## (indices 1 through 50
## fit model to node 1 e.g. b1 as a binary mixed model with single covariate (e.g. glmm)
## for node 2 e.g. g1 just fit standard gaussian glm
## just compute marginal likelihood
myres.inla<-fitabn.inla(dag.m=mydag,data.df=ex3.dag.data[,c(1,2,14)],data.dists=mydists,
group.var="group",cor.vars=c("b1","b2"),
centre=TRUE,compute.fixed=TRUE);
## what marginals do we have?
print(names(myres.inla$marginals));
plot(myres.inla$marginals[["b1|Groups-precision"]]);# marg. density for precision of random effects
## the x range here is too wide here but this determined by INLA and not the abn libraryRun the code above in your browser using DataLab