0

I am programming a function in order to get an over the sample simulation. My function is the following:

oos <- function(alpha, rho,rv){
  ar_oos <-(alpha + rho*rv)
  return(ar_oos)
}

I define now arbitrary the values of alpha and rho:

rho <-0.4
alpha <- 45

Lets create a matrix to save our results:

results <- matrix(NA, nrow=(1239), ncol=1)

The first 240 values will be a stationary time series:

results[1:240] <- rnorm(240, 0, 2)

What I need now is a loop that takes the previous value and recalculate the funcion oos until I fill the 1,239 values of my matrix:

results[241] <- oos(alpha, rho, rv[240])
results[242] <- oos(alpha, rho, rv[241])
results[243] <- oos(alpha, rho, rv[242])
results[244] <- oos(alpha, rho, rv[243])

Does anyone have an idea?

Thank you very much !

1 Answers1

1

You can write it in a simple for-loop:

for (i in 241:length(results)) {
  results[i] <- oos(alpha, rho, rv[i-1])
}
koolmees
  • 2,725
  • 9
  • 23
  • 1
    You would need to fill `rv` with values first. I don't know what distribution it has nor how it is defined but if we follow the last code in the original question this should be the loop. – koolmees Sep 08 '21 at 14:10