runmain.R

###################################################
# Runmain.R
# misclassification simulation master file
# Emily Peterson, Eli Zaleznik, Miu Bing
# 11202018
###################################################


runsim =FALSE


library(runjags)
library(R2jags)
library(coda)
library(truncnorm)
library(tidyverse)
library(xtable)
library(devtools)
library(testthat)


build() # get a bundled package tar.gz
install() # install your source package
load_all()
document()


simsetting = "alldata"
#simsetting = "nobreakdown"
runname <- simsetting #Name your output folder to describe run setting
MakeDirs(runname = runname, WD = paste(getwd(), "/", sep="") ) #Create your output folder


#Set the true for global values. These are values I am fixing.
global_settings <- c(sensworld = 0.5, #global sens value
                     specworld = 0.9, #global spec value
                     rho.alphabeta = -0.3, #correlation between sens and spec
                     rrho.alphabeta = -0.3,
                     rsigma.alpha = 0,
                     rsigma.beta = 0,
                     sigma.alpha = 0.2, #sd for sens in RW
                     sigma.beta = 0.15, #sd for spec in RW
                     C = 1, #number of countries to simulate
                     Nyears = 15, #number of years to simulate,
                     refyear = 10, #set reference year
                     vrenv = 5000 #starting vr env, vr.ct is binomial given vrenv to generate year specific vrenv
                     )

saveRDS(global_settings, paste(output.dir, "global_settings.RDS", sep=""))
globalvars <- c("sensworld", "specworld", "rho.alphabeta", "sigma.alpha", "sigma.beta")

if(runsim){
#Run the simulation and get MCMC samples and their posterior estimates. Saved samples to specified output folder
get_simu_output(runname = runname,
                global_settings = global_settings,
                output.dir = output.dir,
                nsim = 20)

#Combines simulation results in to one tibble. Saved to output directory
summarize_runsetting(output.dir,
                     simsetting,
                     nsim = nsim)
}

#Specify the models you want to compare
runnames = c("alldata", "nobreakdown")
#Select parameters you want to plot
plotpars <- c("sens.ct[1,10]", "spec.ct[1,10]")
#Get xtable with simulation output
get_output_table
#Get comparison plots saved to figure directory
produce_plots(runnames = runnames,
              plotpars = plotpars,
              fig.dir = fig.dir )

#install.packages("/Users/emily/Dropbox/outputsim_0.1.0.tar.gz", repos = NULL, type = "source")


#Testing
errortib <- readRDS(paste(getwd(), '/tests/', 'errortib.RDS', sep=""))
workingtib <- readRDS(paste(getwd(), '/tests/', 'workingtib.RDS', sep=""))

expect_error(summarize_simulation_error(errortib))
expect_silent(summarize_simulation_error(workingtib))

test_that("function gives error when the data is missing parameters", {
  expect_error(summarize_simulation_error(errortib))
  expect_silent(summarize_simulation_error(workingtib))
})
Enpeterson/outputsim documentation built on May 24, 2019, 9:53 a.m.