Q: Repeat a for loop in R Hey I am now doing a simulation study and would like to repeat the following for loop for 5000 times. I would like the output to be a matrix. THX!
mu_v1=0.025
phi_v1=0.119
sigma_v1=0.013

v1=rep(1:35)
for(i in 1){
  v1[i]=0.059634
}
for(i in 2:35){
  v1[i]=mu_v1*(1-phi_v1)+phi_v1*v1[i-1]+sigma_v1*rnorm(1)
}


A: mu_v1=0.025
phi_v1=0.119
sigma_v1=0.013
n = 5000
res <- matrix(NA, n, 35)
for(h in seq_len(n)){
  v1=rep(1:35)
  for(i in 1){
    v1[i]=0.059634
  }
  for(i in 2:35){
    v1[i]=mu_v1*(1-phi_v1)+phi_v1*v1[i-1]+sigma_v1*rnorm(1)
  }
  res[h, ] <- v1
}

