First, we install the packages.

remotes::install_github("DavisVaughan/slider") # Install slider package 
if(!require(tidyverse)){install.packages("tidyverse")}

Then, we set the parameters & sample the processes.

library(tidyverse)     # Data science package
library(slider)        # Sliding windows
set.seed(42)
depth <- 10            # Below: all the parameters
n_sample <- 10^4
rho_1 <- 0.95          # For levels of autocorrelations
rho_2 <- 0.85
rho_3 <- 0.5
rho_4 <- 0.05
sd_X <- 0.1
sd_C <- 0.05
sd_r <- 0.2
b <- 1
a <- 0
# Below, we simulate the processes
X <- arima.sim(n = n_sample, list(ar = rho_1), sd = sd_X) + 1
C <- arima.sim(n = n_sample, list(ar = rho_1), sd = sd_C)
r1 <- arima.sim(n = n_sample, list(ar = rho_1), sd = sd_X*sqrt(1-rho_1^2))
r2 <- arima.sim(n = n_sample, list(ar = rho_2), sd = sd_X*sqrt(1-rho_2^2))
r3 <- arima.sim(n = n_sample, list(ar = rho_3), sd = sd_X*sqrt(1-rho_3^2))
r4 <- arima.sim(n = n_sample, list(ar = rho_4), sd = sd_X*sqrt(1-rho_4^2))

data.frame(time = 1:n_sample, r1, r2, r3, X)[1:100,] %>%
    gather(key = process, value = value, -time) %>%
    ggplot(aes(x = time, y = value, color = process)) +
    geom_line()

pred <- function(x,y){                    # Regression function
    data <- data.frame(x = x, y = y)
    N <- length(y)
    fit <- lm(y ~ x, data = data[1:(N-1),])
    return((y[N] - predict(fit, newdata = data[N,]))^2)
}

pred_depth <- function(depth, X, r){      # Automation of sliding
 return(slide2_dbl(.x = as.vector(X), 
       .y = as.vector(r),
       .f = pred, 
       .before = depth,
       .complete = TRUE) %>% mean(na.rm=T))
}

dpth <- 4:25
e1 <- dpth %>% map_dbl(pred_depth, X = X, r = r1)
e2 <- dpth %>% map_dbl(pred_depth, X = X, r = r2)
e3 <- dpth %>% map_dbl(pred_depth, X = X, r = r3)
e4 <- dpth %>% map_dbl(pred_depth, X = X, r = r4)

data.frame(depth = dpth, e1, e2, e3, e4) %>%
    gather(key = key, value = MSE, -depth) %>%
    ggplot(aes(x = depth, y = MSE, color = key)) + geom_line() + geom_point() +
    coord_fixed(500)

    
# ggsave("AR_test.pdf")
e <-data.frame(depth = dpth, e1, e2, e3, e4) 
save(e ,file="e.RData")   # Save, if need be.
LS0tCnRpdGxlOiBTaW1wbGUgc2ltdWxhdGlvbnMKb3V0cHV0OiBodG1sX25vdGVib29rCi0tLQoKCkZpcnN0LCB3ZSBpbnN0YWxsIHRoZSBwYWNrYWdlcy4KCmBgYHtyfQpyZW1vdGVzOjppbnN0YWxsX2dpdGh1YigiRGF2aXNWYXVnaGFuL3NsaWRlciIpICMgSW5zdGFsbCBzbGlkZXIgcGFja2FnZSAKaWYoIXJlcXVpcmUodGlkeXZlcnNlKSl7aW5zdGFsbC5wYWNrYWdlcygidGlkeXZlcnNlIil9CmBgYAoKVGhlbiwgd2Ugc2V0IHRoZSBwYXJhbWV0ZXJzICYgc2FtcGxlIHRoZSBwcm9jZXNzZXMuCgpgYGB7ciwgbWVzc2FnZSA9IEZBTFNFLCB3YXJuaW5nID0gRkFMU0V9CmxpYnJhcnkodGlkeXZlcnNlKSAgICAgIyBEYXRhIHNjaWVuY2UgcGFja2FnZQpsaWJyYXJ5KHNsaWRlcikgICAgICAgICMgU2xpZGluZyB3aW5kb3dzCnNldC5zZWVkKDQyKQpkZXB0aCA8LSAxMCAgICAgICAgICAgICMgQmVsb3c6IGFsbCB0aGUgcGFyYW1ldGVycwpuX3NhbXBsZSA8LSAxMF40CnJob18xIDwtIDAuOTUgICAgICAgICAgIyBGb3IgbGV2ZWxzIG9mIGF1dG9jb3JyZWxhdGlvbnMKcmhvXzIgPC0gMC44NQpyaG9fMyA8LSAwLjUKcmhvXzQgPC0gMC4wNQpzZF9YIDwtIDAuMQpzZF9DIDwtIDAuMDUKc2RfciA8LSAwLjIKYiA8LSAxCmEgPC0gMAojIEJlbG93LCB3ZSBzaW11bGF0ZSB0aGUgcHJvY2Vzc2VzClggPC0gYXJpbWEuc2ltKG4gPSBuX3NhbXBsZSwgbG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