Quantcast
Channel: Active questions tagged xts - Stack Overflow
Viewing all articles
Browse latest Browse all 57

how to shift an xts time series to the left in r?

$
0
0

I am trying to shift one column of an xts time series to the left in R (please see the code below). The lag shifts nicely to the right, but I want it to line up with the rolling mean. Using a negative number says: "n must be positive".

#rm(list=ls())#dev.off(dev.list()["RStudioGD"])library(roll) # do not use library(RcppRoll)!getSymbols("^GSPC",src="yahoo",from="2015-01-01",to = "2020-06-04")y<-GSPC$GSPC.Closestandardize<-function(ts) { as.xts(apply(ts, 2, function(x) x / x[1])) }y<-standardize(GSPC$GSPC.Close)n=400y$m<-rollmean(y$GSPC.Close,k=n)y$rolling<-roll_sd(y$GSPC.Close,n)y$lag<-lag(y$rolling,n/2) # how to go left?plot(y)ts.plot(y,col=1:4)

Viewing all articles
Browse latest Browse all 57

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>