Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating a vector of mobile variances of a time series

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)]))
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading