R/tst.buildmod.r

Defines functions model.create model.append model.sim

# Helper functions to create and append model

# Create model text file
model.create <- function(modelname,directory = getwd()){
  
  mymod <- paste0(directory,"/",modelname,".txt")
  sink(mymod)
  sink()
  
}

# append model
model.append <-  function(modelname, equation ,directory){
  
  directory <- getwd()
  
  mymod <- paste0(directory,"/",modelname,".txt")

  write(equation, file = mymod, append = TRUE)
  
  
}


# simualte model
model.sim <-  function(modelname,dat = dat , smpl = NA ,directory){
  
  directory <- getwd()
  
  mymod <- paste0(directory,"/",modelname,".txt")
  
  if(!is.na(smpl)){
    
    smpl <- paste(smpl, xts::last(dat$Date),"quarter")
    
  }else{
    
    smpl <-  paste(dat$Date[1],xts::last(dat$Date),"quarter")
    
  }
  
  write(c("#3. Timeline",paste0("timeline ",smpl)), file = mymod, append = TRUE)
  
  mymod <- tst.model(mymod, dataFile = dat, smpl = smpl)
  
  mod <- simulate.tst(mymod)
  
  fcst_start <- substr(smpl, start = 1, stop = 10)
  fcst_end <- substr(smpl, start = 12, stop = 21)
  
  fcast <- data.frame(mod$baseline[,mymod$endogenous[,1]],
                     Date = seq(dat$Date[1], as.Date(fcst_end), by = "quarter"))

  
  mod <- mymod

  mod <- list(mod = mymod,
            fcast = fcast)
  
  return(mod)
  
}
AdamElderfield/tst_package documentation built on Dec. 5, 2019, 2:08 a.m.