##########################################################################
## Access simulated data set #############################################
##########################################################################
#require(sourceR)
data(sim_SA)
##########################################################################
## Set priors ############################################################
##########################################################################
priors <- list(a = 1, r = 1, theta = c(0.01, 0.00001))
##########################################################################
## Run model #############################################################
##########################################################################
res <- saBayes(formula = Human~Source1+Source2+Source3+Source4+Source5,
time=~Time, location=~Location, type=~Type,
data=sim_SA$data, priors = priors,
alpha_conc = 1, prev = sim_SA$prev,
likelihood_dist = "pois", n_iter = 20)
## or
## Run model using all parameters
res <- saBayes(formula = Human~Source1+Source2+Source3+Source4+Source5,
time=~Time, location=~Location, type=~Type,
data=sim_SA$data, priors = priors,
alpha_conc = 1, prev = sim_SA$prev,
likelihood_dist = "pois", n_iter = 5000,
params_fix = list(type_effects = FALSE, r = FALSE),
mcmc_params = list(save_lambda = TRUE, burn_in = 1,
thin = 1, n_r = 200))
##########################################################################
#### Results #############################################################
##########################################################################
## Example: Look at data #################################################
res$posterior$cluster
## Example: Convert rates into proportions, example lambda j #############
lambda_j_proportion_t1_l1 <- apply(res$posterior$lj[[1]][[1]], 1,
function(x) x/sum(x))
## Example: Trace plot ###################################################
# trace plot for lambda (type = 3, time = 1, location = 2)
plot(res$posterior$li$time1$locationB[, "3"], type="l")
## or
plot(res$posterior$li[[1]][[2]][, 3], type="l")
## Example: Density plot #################################################
# density plot for type effect (type = 3)
plot(density(res$posterior$q[,"3"]))
## Example: Residual plot ################################################
summary_li <- summary(res)$li
median_li <- c(summary_li$time1$locationA[, "median"],
summary_li$time1$locationB[, "median"],
summary_li$time2$locationA[, "median"],
summary_li$time2$locationB[, "median"])
plot(median_li, sim_SA$data[, "Human"],
xlab="lambda i", ylab="Human cases")
abline(0,1)
## Example: Box plots showing the type effect grouping ###################
require(ggplot2)
library(cluster)
# subset your data by removing a burn in (of 1000) from the total number
# of iterations (5000) and thinning to have a maximum of 10000 values as
# this can be very slow
sub_hclust <- seq(1000, 5000, by=ceiling(5000/10000))
groups <- as.data.frame(t(res$posterior$cluster))
groups <- as.data.frame(apply(groups, 2, function(x) as.factor(x)))
# compute dissimilarity matrix for the type effect clusters
disim_clust_g <- daisy(groups[,sub_hclust])
clu <- hclust(disim_clust_g, method="complete")
# hclust_groups contains the group id for each type effect,
# if they were split into 3 groups using the tree generated by the
# hclust command above
hclust_groups <- cutree(clu, k=3)
# Put the data in the format ggplot2 requires it to be in
# i.e. a column for the values for q at each iteration (thinned)
# a column for the factor name (type i) so that it will make boxplots
# and a column for the group
q_full <- data.frame(qi=c(as.vector(res$posterior$q[sub_hclust,])),
i=as.factor(rep(1:50, each=dim(res$posterior$q[sub_hclust,])[1])),
group=as.factor(rep(as.vector(hclust_groups),
each=dim(res$posterior$q[sub_hclust,])[1])))
g <- ggplot(q_full, aes(x=i,y=qi, fill=group)) +
geom_boxplot(outlier.size=0.5) +
theme_classic()+ ylab("density") + xlab("Type effect i")
print(g)
## Example: Dendrogram of type effect grouping ###########################
library(gplots)
library(cluster)
# subset your data by removing a burn in (of 1000) and thinning to
# have a maximum of 10000 values as this can be very slow
sub_hclust <- seq(1000, 5000, by=ceiling(5000/10000))
groups <- as.data.frame(t(res$posterior$cluster))
groups <- as.data.frame(apply(groups, 2, function(x) as.factor(x)))
rownames(groups) <- 1:50
# compute dissimilarity matrix for the type effect clusters
disim_clust_g <- daisy(groups[,sub_hclust])
clu <- hclust(disim_clust_g, method="complete")
dend <- as.dendrogram(clu)
# OPTIONAL: change the colour of the heatmap. The lighter the colour,
# the less likely two type effects are in the same group
hmcols <- colorRampPalette(c("blue","white"))(299)
# OPTIONAL: change the layout of the heatmap and dendrogram
lmat <- rbind(c(0,3),c(2,1),c(0,4))
lwid <- c(0.1,4)
lhei <- c(1.5,4,0.4)
layout(mat=lmat, widths=lwid, heights=lhei)
heatmap_data <- as.matrix(disim_clust_g)
rownames(heatmap_data) <- 1:50
colnames(heatmap_data) <- 1:50
heatmap.2(heatmap_data,
density.info="none", # turns off density plot inside color legend
trace="none", # turns off trace lines inside the heat map
col=hmcols, # use on color palette defined earlier
dendrogram="col", # only draw a row dendrogram
Colv=dend,
Rowv=dend,
lmat=lmat, lwid=lwid, lhei=lhei
)Run the code above in your browser using DataLab