---
title: "checkAssumptions"
output: 
	pdf_document
	word_document
---
```{r, echo=FALSE}
if(input$format=="PDF") {
    align="center"
} else {
    align="default"
}
```

# Distributional assumptions 
```{r,echo=FALSE}
# Defines the number of parameters 
if(input$from=="Spreadsheet" || input$from=="R"){
  n.params <- dim(param())[2]
  width.figs <- c(4.6,3,3)
  height.figs <- c(4.0,3,3)
}
if(input$from=="BUGS"){
  n.params <- dim(param2())[2]
}
```

This sections presents graphical and tabular summaries to check the distributional assumptions used for the $n=`r n.params`$ parameters included in the economic model.  For each parameter, a histogram of the distribution is presented together with a summary table, reporting some relevant statistics.



```{r,echo=FALSE, fig.width=4.6, fig.height=4.0,results='asis',fig.align=align}
if (input$from=="Spreadsheet" || input$from=="R") {
params.names <- colnames(param())


for (i in 1:n.params) {
if (var(param()[,i])!=0) {
hist(param()[,i], breaks= seq(min(param()[,i]),max(param()[,i]),l = input$bins+1),
main = paste('Histogram of',params.names[i]),xlab = params.names[i], ylim = NULL)
points(c(quantile(param()[,i],0.025),quantile(param()[,i],0.025),quantile(param()[,i],0.975)),c(0,0,0),lwd=5,type='l')
cat("  \n")

mu <- mean(param()[,i]) #mean value
sd <- sd(param()[,i])   #sd value
q1<-quantile(param()[,i],0.025, names = FALSE) 
q2<-quantile(param()[,i],0.975, names = FALSE) 
med <- quantile(param()[,i],0.5, names = FALSE)
mcse <- sqrt(-((sum(param()[,i])/length(param()[,i]))^2 - 
	sum((param()[,i]^2)/length(param()[,i]))))/sqrt(length(param()[,i]))
summary2<-data.frame(mu,sd,q1,med,q2,mcse) 
#colnames(summary2) <- c("Mean","  Standard deviation","  2.5%","Median"," 97.5%","Monte Carlo SE")
#rownames(summary2) <- params.names[i]
## see discussion here: https://github.com/yihui/knitr/issues/886
print(knitr::kable(summary2,align=c('l','l','l','l','l','l'),
                   col.names=c('Mean','Standard deviation','2.5%','Median','97.5%','Monte Carlo SE')))
cat('  \n')
}
}
}
```

```{r,echo=FALSE, fig.width=4.3, fig.height=4.0,results='asis',fig.align=align}
if (input$from=="BUGS") {
cat(paste0("In addition, we also report some 'convergence' diagnostics plots. \nThe leftmost graph shows a _traceplot_ of the ",obj1()$n.chains," monitored Markov Chains, while the rightmost graph shows an _autocorrelation_ plot.\n\n"))
params.names <- colnames(param2())

for (i in 1:n.params) {
if (var(param2()[,i])!=0) {
cat(paste0("## Summary statistics for parameter ",params.names[i],"\n"))
hist(param2()[,i], breaks= seq(min(param2()[,i]),max(param2()[,i]),l = input$bins+1),
#main = paste('Histogram of',params.names[i]),
main="",xlab = params.names[i], ylim = NULL)
points(c(quantile(param2()[,i],0.025),quantile(param2()[,i],0.025),quantile(param2()[,i],0.975)),c(0,0,0),lwd=5,type='l')
cat("  \n")



cat("  \n")
tmp <- obj1()$BUGSoutput$summary[i,]
summary22 <- data.frame(t(tmp))[-c(4,6)]
if(input$nchains>1){
  summary22[8] <- obj1()$BUGSoutput$summary[i,"sd"]/sqrt(obj1()$BUGSoutput$summary[i,"n.eff"])
  print(knitr::kable(summary22,align=c('l','l','l','l','l','l','l','l'),
                   col.names=c('Mean','Standard deviation','2.5%','Median','97.5%','Rhat','n.eff','Monte Carlo SE')))
} else {
    print(knitr::kable(summary22,align=c('l','l','l','l','l','l','l'),
                   col.names=c('Mean','Standard deviation','2.5%','Median','97.5%')))
}
cat('  \n')


cat("  \n")
cat("### Diagnostic plots\n")
mytraceplot(params.names[i],obj1())

acf.plot(obj1(),i,title="")
cat("  \n\n")
}
}
}
```
