R/apply_ar_on_ts.R

Defines functions apply_ar_on_ts

Documented in apply_ar_on_ts

#' Apply autoregression of order 2 on timeseries
#' This function applies autoregression of order 2 using a numeric vector (predicts next value in series given previous 2)
#' @param s A numeric vector representing the series.
#' @param a1 A coefficient of first previous value in the series.
#' @param a2 A coefficient of second previous value in the series.
#' @export


apply_ar_on_ts = function(s, a1, a2){
  s_pred = rep(0,length(s))
  s_pred[1] = NA
  s_pred[2] = NA
  for (t in 3:length(s)){
    s_pred[t] = a1*s[t-1] + a2*s[t-2]
  }
  s_pred
}
msxakk89/dat documentation built on Aug. 3, 2020, 6:39 p.m.