R/tst.addEqu.R

Defines functions tst.addEqu

#' Add equation.
#' 
#' Add a single equation to a tst object.
#' 
#' @param model an tst object.
#' @param var the varaibles in the eqauation.
#' @param eq the equations to be added to the tst object.
#' @param desc a descriptions of the equation.
#' @return an tst object.
#' 

tst.addEqu<-function(model=stop("Need a model"),var=stop("Need a variable name!"),eq=stop("Need an equation!"),desc=""){
  
  variable = gsub("\\bin\\b","inv",var)
  variable<-trim(variable)
  equation = gsub("\\bin\\b","inv",eq)
  equation<-trim(equation)
  #replacing all lags. This might be an issue if there are a lag of 2 but not of 1.
  lag=1
  while(grepl("^.*?\\(-[0-9]+\\).*$",equation)){
    temp<-substr(equation,start=(regexpr("\\(-[0-9]+\\)",equation)+2),stop=nchar(equation))
    lag<-as.numeric(substr(temp,start=1,stop=(regexpr("\\)",temp)-1)))
    equation=gsub(paste("(-",lag,")",sep=""), paste("_",lag,sep=""),equation , fixed = T)
  }
  
  
  if(is.null(model$equations)){
    equations<-matrix(nrow=1,ncol=3,dimnames=list(NULL,c("endogenous value","equation","description")))
    equations[1,1]<-variable
    equations[1,2]<-equation
    equations[1,3]<-desc
  }else{
    equations<-model$equations
    equations<-rbind(equations,c(variable,equation,desc))
  }
  ind=tst.getIndex(model,end=variable)
  if(ind<0){
    model<-tst.addEnd(model,var=var,init=NA,lag=0,desc=desc)
  }
  model$equations<-equations
  return(model)
}
AdamElderfield/tst_package documentation built on Dec. 5, 2019, 2:08 a.m.