I'd like to use vectorization to take a 4 point moving window on standard deviation on the close column and create another variable (st.dev) in the dataframe. Here's the dataframe head(xyz) Date Close 1 2011-01-28 56.42 2 2011-01-27 57.37 3 2011-01-26 56.48 4 2011-01-25 56.39 5 2011-01-24 55.74 6 2011-01-21 55.46 So the first 3 elements to the new st.dev column would be zero (c(rep(0,3)), then the 4th element of the new std.dev column would be standard deviation of the first 4 closes. Next element would be sd of Close[5]:Close[1], then sd of Close[6]: Close[2] ...and so on until the last row of xyz. There must be an easy vetorized way to do this but I don't see it. Sorry for the basic question but continuing to figure this new language out. Thanks in advance for the help -- View this message in context: http://r.789695.n4.nabble.com/How-to-do-a-moving-window-on-standard-deviation-tp3247566p3247566.html Sent from the R help mailing list archive at Nabble.com.
One way is to convert your data into a zoo object and use the rollapply function # Your data lines = "Date Close 2011-01-28 56.42 2011-01-27 57.37 2011-01-26 56.48 2011-01-25 56.39 2011-01-24 55.74 2011-01-21 55.46" d = read.table(textConnection(lines), header = TRUE) # create zoo object d.z=zoo(d[,-1],order.by=as.Date(d$Date)) # generate rolling std devs sd.z = rollapply(d.z,5,sd,align="right") # you wanted a data frame df = as.data.frame(merge(d.z,sd.z,all=TRUE)) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/How-to-do-a-moving-window-on-standard-deviation-tp3247566p3247592.html Sent from the R help mailing list archive at Nabble.com.
On Sun, Jan 30, 2011 at 1:39 PM, eric <ericstrom at aol.com> wrote:> > I'd like to use vectorization to take a 4 point moving window on standard > deviation on the close column and create another variable (st.dev) in the > dataframe. Here's the dataframe > > > head(xyz) > ? ? ? ?Date Close > 1 2011-01-28 56.42 > 2 2011-01-27 57.37 > 3 2011-01-26 56.48 > 4 2011-01-25 56.39 > 5 2011-01-24 55.74 > 6 2011-01-21 55.46 > > So the first 3 elements to the new st.dev column would be zero (c(rep(0,3)),Technically, they should be NA because the standard deviation for those periods is unknown, not zero. Also, you may be introducing look-ahead bias because your series is in descending order (the 4th element will contain future periods in the standard deviation calculation).> then the 4th element of the new std.dev column would be standard deviation > of the first 4 closes. Next element would be sd of Close[5]:Close[1], then > sd of Close[6]: Close[2] ...and so on until the last row of xyz.Please see the help pages for the indexing (?"[") operator, the sequence (?":") operator, and "Operator Syntax and Precedence" (?Syntax). They will help you understand why Close[6]:Close[2] doesn't work.> > There must be an easy vetorized way to do this but I don't see it. Sorry for > the basic question but continuing to figure this new language out. > > Thanks in advance for the help >TTR::runSD will do what you want and the same call will work on data.frame, matrix, vector, xts/zoo, or any other time-series object. Note that time-series objects will generally put time periods in ascending order. library(TTR) d$st.dev <- runSD(d$Close,4) z <- zoo(d[,2,FALSE],as.Date(d[,1])) z$st.dev <- runSD(z$Close,4) x <- as.xts(z) x$st.dev <- runSD(x$Close,4) HTH, -- Joshua Ulrich | FOSS Trading: www.fosstrading.com> -- > View this message in context: http://r.789695.n4.nabble.com/How-to-do-a-moving-window-on-standard-deviation-tp3247566p3247566.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
How about considering 'embed':> dDate Close 1 2011-01-28 56.42 2 2011-01-27 57.37 3 2011-01-26 56.48 4 2011-01-25 56.39 5 2011-01-24 55.74 6 2011-01-21 55.46> x <- embed(d$Close, 4) > x[,1] [,2] [,3] [,4] [1,] 56.39 56.48 57.37 56.42 [2,] 55.74 56.39 56.48 57.37 [3,] 55.46 55.74 56.39 56.48> apply(x, 1, sd)[1] 0.4714870 0.6700497 0.4968149>On Sun, Jan 30, 2011 at 2:39 PM, eric <ericstrom at aol.com> wrote:> > I'd like to use vectorization to take a 4 point moving window on standard > deviation on the close column and create another variable (st.dev) in the > dataframe. Here's the dataframe > > > head(xyz) > ? ? ? ?Date Close > 1 2011-01-28 56.42 > 2 2011-01-27 57.37 > 3 2011-01-26 56.48 > 4 2011-01-25 56.39 > 5 2011-01-24 55.74 > 6 2011-01-21 55.46 > > So the first 3 elements to the new st.dev column would be zero (c(rep(0,3)), > then the 4th element of the new std.dev column would be standard deviation > of the first 4 closes. Next element would be sd of Close[5]:Close[1], then > sd of Close[6]: Close[2] ...and so on until the last row of xyz. > > There must be an easy vetorized way to do this but I don't see it. Sorry for > the basic question but continuing to figure this new language out. > > Thanks in advance for the help > > -- > View this message in context: http://r.789695.n4.nabble.com/How-to-do-a-moving-window-on-standard-deviation-tp3247566p3247566.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?