%\VignetteEngine{knitr::knitr}
%\VignetteIndexEntry{BYM with PC priors}

\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\usepackage{caption}
\usepackage{subcaption}
\providecommand{\subfloat}[2][need a sub-caption]{\subcaptionbox{#1}{#2}}
\title{BYM with PC priors}
\author{Patrick Brown}
\date{April 2016}

\begin{document}

\maketitle

<<knitr, include=FALSE>>=
require('knitr')
opts_chunk$set(out.width='0.48\\textwidth', fig.align='default', fig.height=3, fig.width=6)
if(Sys.info()['sysname'] =='Linux' &
  requireNamespace("INLA")) {   
  INLA::inla.setOption(inla.call = 
      system.file(paste(
          "bin/linux/",          
          ifelse(
            .Machine$sizeof.pointer == 4, 
            "32", "64"),
          'bit/inla.static', sep=''),
        package="INLA")) 
}
@    

<<packages>>=
require('diseasemapping')
data('kentucky')
@

\section*{Incidence rates}

<<rates, tidy=TRUE>>=
if(FALSE) {
	# must have an internet connection to do the following
	larynxRates= cancerRates("USA", year=1998:2002,site="Larynx")
	dput(larynxRates)
} else {
	larynxRates = structure(c(0, 0, 0, 0, 1e-06, 6e-06, 2.3e-05, 4.5e-05, 9.9e-05, 
					0.000163, 0.000243, 0.000299, 0.000343, 0.000308, 0.000291, 0.000217, 
					0, 0, 0, 1e-06, 1e-06, 3e-06, 8e-06, 1.3e-05, 2.3e-05, 3.5e-05, 
					5.8e-05, 6.8e-05, 7.5e-05, 5.5e-05, 4.1e-05, 3e-05), .Names = c("M_10", 
					"M_15", "M_20", "M_25", "M_30", "M_35", "M_40", "M_45", "M_50", 
					"M_55", "M_60", "M_65", "M_70", "M_75", "M_80", "M_85", "F_10", 
					"F_15", "F_20", "F_25", "F_30", "F_35", "F_40", "F_45", "F_50", 
					"F_55", "F_60", "F_65", "F_70", "F_75", "F_80", "F_85"))
	
}
@



<<smr>>=
# get rid of under 10's
larynxRates = larynxRates[grep("_(0|5)$",names(larynxRates), invert=TRUE)]
# compute Sexpected
kentucky = diseasemapping::getSMR(
    popdata=kentucky, 
    model = larynxRates, 
    casedata=larynx, 
    regionCode="County")
@



\section*{The BYM model}

The Besag, York and Mollie model for Poisson distributed case counts is:

\begin{align*}
Y_i \sim & \text{Poisson}(O_i \lambda_i)\\
\log(\mu_i) = &X_i \beta + U_i\\
U_i \sim & \text{BYM}(\sigma_1^2 , \sigma_2^2)\\
\end{align*}

\begin{itemize}
\item $Y_i$ is the response variable for region $i$
\item $O_i$ is the 'baseline' expected count, which is specified 
\item  $X_i$ are covariates
\item $U_i$ is a spatial random effect with a spatially structured variance parameter $\sigma_1^2$
and a spatially independent variance $\sigma_2^2$
\end{itemize}






\section*{Gamma priors on precision}




<<bymGamma, tidy=TRUE>>=
kBYM = kBYMpc = try(bym(
		formula = observed ~ offset(logExpected) + poverty,
    data=kentucky,
    priorCI = list(sdSpatial=c(0.1, 5), sdIndep=c(0.1, 5)),
		region.id="County"
))
@


<<bymTry, include=FALSE>>=
if(class(kBYM) == 'try-error') 
	kBYM = list()
@

Above, Gamma priors are assigned to 
$1/\sigma_1^2$ and $1/\sigma_2^2$, with the shape and scale parameters 
set to produce 
2.5\% to 97.5\% prior intervals of $(0.1, 5)$ for each standard deviation
parameter.




<<summary>>=
if(!is.null(kBYM$parameters))
	knitr::kable(kBYM$parameters$summary[,c(1,3,5)], digits=3)
@



<<priorPost, fig.cap="gamma priors sd parameters", fig.height=4, fig.width=3, fig.subcap=c("spatial", "indep"), echo=FALSE>>=

if(!is.null(kBYM$parameters)) {
	
plot(kBYM$parameters$sdSpatial$posterior, type='l', 
		xlim=c(0,1))
lines(kBYM$parameters$sdSpatial$prior, col='blue')
legend('topright', lty=1, col=c('black','blue'), legend=c('posterior','prior'))

plot(kBYM$parameters$sdIndep$posterior, type='l', 
		xlim=c(0,1))
lines(kBYM$parameters$sdIndep$prior, col='blue')
legend('topright', lty=1, col=c('black','blue'), legend=c('posterior','prior'))
} else {
	plot(1:10, type='n')
	text(5,5,'inla is not installed')
	plot(1:10, type='n')
	text(5,5,'inla is not installed')
}
@




\section*{BYM with penalised complexity prior}

`propSpatial = c(u=0.5, alpha=0.8)` means $pr(\phi < 0.5) = 0.8$, which is different from the specification of `pc.prec`


<<bymPc, tidy=TRUE>>=
kBYMpc = try(
bym(
	formula = observed ~ offset(logExpected) + poverty,
    kentucky,
	prior = list(
      sd=c(u=1, alpha=0.05), 
      propSpatial = c(u=0.5, alpha=0.8)),
  verbose=TRUE), silent=TRUE)
@

<<bymPcTry, include=FALSE>>=
if(class(kBYMpc) == 'try-error') 
	kBYMpc = list()
@


Here penalized complexity priors are used with
$pr(\sqrt{\sigma_1^2+\sigma_2^2} > 1) = 0.05$ and
$$
pr(\sigma_1/\sqrt{\sigma_1^2 + \sigma_2^2} < 0.5) = 0.8.
$$

<<summaryPc>>=
if(!is.null(kBYMpc$parameters))
	knitr::kable(kBYMpc$parameters$summary[,c(1,3,5)], digits=3)
@



<<priorPostPc, fig.cap="PC priors variance parameters", fig.height=4, fig.width=3, fig.subcap=c("sd","prop spatial"), echo=FALSE>>=

if(!is.null(kBYMpc$parameters)) {
	
plot(kBYMpc$parameters$sd$posterior, type='l', 
		xlim=c(0,1))
lines(kBYMpc$parameters$sd$prior, col='blue')
abline(v=kBYMpc$parameters$sd$params.intern[1], lty=3)
legend('topright', lty=1, col=c('black','blue'), legend=c('posterior','prior'), bg='white')

plot(kBYMpc$parameters$propSpatial$posterior, 
  type='l', 
		xlim=c(0, 1))
lines(kBYMpc$parameters$propSpatial$prior, col='blue')
abline(v=kBYMpc$parameters$propSpatial$params.intern[1], lty=3)
legend('topright', lty=1, col=c('black','blue'), legend=c('posterior','prior'))
} else {
	plot(1:10, type='n')
	text(5,5,'inla is not installed')
	plot(1:10, type='n')
	text(5,5,'inla is not installed')
}
@

<<checkPropSpatial, eval=FALSE, include=FALSE, purl=FALSE>>=

noData = bym(
	formula = observed ~ offset(logExpected) + poverty,
    kentucky,
	prior = list(
      sd=c(u=1, alpha=0.05), 
      propSpatial = c(u=0.25, alpha=0.8)))

	plot(noData$inla$marginals.hyperpar$Phi, type='l')
	lines(noData$parameters$propSpatial$prior, col='red', lty=2)
	abline(v=0.25)




	theCdf = data.frame(
		propSpatial = kBYMpc$parameters$propSpatial$prior[-1,1],
		cdf = cumsum(kBYMpc$parameters$propSpatial$prior[-1,2]*diff(kBYMpc$parameters$propSpatial$prior[,1]))
		)
	approx(theCdf$propSpatial, theCdf$cdf, kBYMpc$parameters$propSpatial$params.intern[1])
@

<<maps, fig.cap='Random effects and fitted values', fig.subcap=c('gamma, fitted','pc fitted','gamma random','pc random'), echo=FALSE>>=

if(require('mapmisc', quietly=TRUE) & !is.null(kBYMpc$parameters)) {
	
thecex=0.6	
	
colFit = colourScale(kBYM$data$fitted.exp,
		breaks=6, dec=1, style='equal')


map.new(kBYM$data)
plot(kBYM$data, col=colFit$plot, add=TRUE)
legendBreaks('topleft', colFit, cex=thecex)

colFitPc = colourScale(kBYMpc$data$fitted.exp,
		breaks=colFit$breaks, style='fixed',
		col=colFit$col)

map.new(kBYM$data)
plot(kBYMpc$data, col=colFitPc$plot, add=TRUE)
legendBreaks('topleft', colFitPc, cex=thecex)

colR = colourScale(kBYM$data$random.mean,
		breaks=12, dec=-log10(0.05), style='equal')

map.new(kBYM$data)
plot(kBYM$data, col=colR$plot, add=TRUE)
legendBreaks('topleft', colR, cex=thecex)

colRpc = colourScale(kBYMpc$data$random.mean,
		breaks=colR$breaks, col=colR$col, 
		style='fixed')

map.new(kBYM$data)
plot(kBYMpc$data, col=colRpc$plot, add=TRUE)
legendBreaks('topleft', colRpc, cex=thecex)


} else {
	plot(1:10, type='n')
	text(5,5,'inla is not installed')
	plot(1:10, type='n')
	text(5,5,'inla is not installed')
	plot(1:10, type='n')
	text(5,5,'inla is not installed')
	plot(1:10, type='n')
	text(5,5,'inla is not installed')
}
@


\end{document}
  