Skip to content

Instantly share code, notes, and snippets.

@josephsdavid
Created June 18, 2019 05:39
Show Gist options
  • Save josephsdavid/ce782d54e11c771788221a2da914956d to your computer and use it in GitHub Desktop.
Save josephsdavid/ce782d54e11c771788221a2da914956d to your computer and use it in GitHub Desktop.
R function for basic AR() forecasting
arpgen <- function(phi,vec,l,mu){
v <- vec[length(vec):(length(vec)-(length(phi)-1))]
f <- sum(phi*(v)) + mu*(1-sum((phi)))
if (l==1) return(f)
return(arpgen(phi,append(vec,f),l-1,mu))
}
arp <- function(phi,vec,l,mu){
as.numeric(lapply(1:l, arpgen, phi = phi, vec = vec, mu = mu))
}
# usage
x <- c(27.7,23.4,21.2,21.1,22.7)
p <- c(1.6,-.8)
avg <- 29.4
arp(p,x,5,avg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment