# Integer valued AR 1 INAR = function(lambda,phi,n0,n){ m = (n+n0) y <- rep(0,m) w <- rpois(m,lambda) tmp <- rep(0,m) for(i in c(2:m)){ tmp[i] = rbinom(n=1,size=y[i-1],p=phi) y[i] = tmp[i] + w[i] } y = y[-c(1:n0)] return(y) } # For the INAR2 you need to be careful how to specify the parameters phi1 and phi2 INAR2 = function(lambda,phi1,phi2,n0,n){ m = (n+n0) y <- c(1,1,rep(0,(m-2))) w <- rpois(m,lambda) tmp <- rep(0,m) tmp2 <-rep(0,m) for(i in c(3:m)){ tmp[i] = rbinom(n=1,size=y[i-1],p=phi1) tmp2[i] = rbinom(n=1,size=y[i-2],p=phi2) y[i] = (tmp[i] + tmp2[i] + w[i]) } y = y[-c(1:n0)] return(y) } test1 = INAR(lambda = 5,phi = 0.1, n0=100, n=100) test2 = INAR(lambda = 5,phi = 0.5, n0=100, n=100) test3 = INAR(lambda = 5,phi = 0.95, n0=100, n=100) test4 = INAR2(lambda=5, phi1=0.5, phi2=0.5, n0=100,n=100) # Make acf plots to check the correlation #par(mfrow=c(2,1)) plot(c(1:100),test1) plot(c(1:100),test2) plot(c(1:100),test3)