library(tidyverse)
library(bayesPop)
library(bayesMig)


DAT <- read_tsv(us.mig.file)
DAT[,2] %>% t
sum(DAT$`2001`)

# Toy simulation for US states
dir.create("Output")
sim.dir <- "./Output"
us.mig.file <- file.path(find.package("bayesMig"), "extdata", "USmigrates.txt")
m <- run.mig.mcmc(nr.chains = 2, iter = 100000, thin = 1, my.mig.file = us.mig.file, 
                   output.dir = sim.dir, present.year = 2017, annual = TRUE)
pred <- mig.predict(sim.dir = sim.dir, burnin = 5, end.year = 2050)
mig.trajectories.plot(pred, "Wyoming", pi = 80, ylim = c(-0.03, 0.03)) 

load("./Output/bayesMig.mcmc.meta.rda")
#############
#subnat example
data.dir <- "./extdata"
dir.create("Pop_Output")
sim.dir <- "./Pop_Output"
dir.create("Output")
example(pop.predict.subnat)
read_tsv(file.path(data.dir, "CANlocations.txt"))
pred <- pop.predict.subnat(output.dir = sim.dir,  locations = file.path(data.dir, "CANlocations.txt"),
                   inputs = list(popM = file.path(data.dir, "CANpopM.txt"),popF = file.path(data.dir, "CANpopF.txt")), 
                   verbose = TRUE)
pop.pyramid(pred, "Ontario")
?pop.pyramid
summary(pred)
write.pop.trajectories(pred)
load("Pop_Output/predictions/prediction.rda")