Home > database >  Creating a vector of mobile variances of a time series
Creating a vector of mobile variances of a time series

Time:02-14

I am trying to create a vector of 50 trading days variances of a time series. With the following for cycle (which works fine for the mean) I am getting a vector of "NULL":

for(i in 1:1742){
  TSLA$mean50[i] = mean(TSLA$rendimenti[i:i 49])
}

for(i in 1:1742){
  TSLA$var50[i] = as.numeric(var(TSLA$rendimenti[i:i 49]))
}

This is what my XTS data looks like

For some reasons if I add values manually (by replacing the index i with a specific value) the code works perfectly fine.

CodePudding user response:

for(i in 1:1742){
  TSLA$mean50[i] = mean(TSLA$rendimenti[i:(i 49)])
}

for(i in 1:1742){
  TSLA$var50[i] = as.numeric(var(TSLA$rendimenti[i:(i 49)]))
}
  • Related