moving-average

rollapply time series in R (zoo) on backward looking data

廉价感情. 提交于 2019-12-05 14:27:43
I would like to use the zoo function rollapply to apply a function (for example mean) on a time series but only using the last N known points. For example: x = zoo(c(1,2,3,4), order.by=c(10,11,12,13)) rollmean(x,2) Produces: 10 11 12 1.5 2.5 3.5 I would like to produce a series that would have date entries of 11, 12, 13 and values of 1.5, 2.5, 3.5. The values seem correct but the dates that rollmean outputs don't seem to correspond to what I would like. I'm a bit worried about just assigning the dates I want to the zoo object using time(x)<- because I'm not sure that rollapply is actually

rolling average to multiple variables in R using data.table package

跟風遠走 提交于 2019-12-05 03:40:23
I would like to get rolling average for each of the numeric variables that I have. Using data.table package, I know how to compute for a single variable. But how should I revise the code so it can process multiple variables at a time rather than revising the variable name and repeat this procedure for several times? Thanks. Suppose I have other numeric variables named as "V2", "V3", and "V4". require(data.table) setDT(data) setkey(data,Receptor,date) data[ , `:=` ('RollConc' = rollmean(AvgConc, 48, align="left", na.pad=TRUE)) , by=Receptor] A copy of my sample data can be found at: https:/

Rolling comparison between a value and a past window, with percentile/quantile

时间秒杀一切 提交于 2019-12-04 21:16:45
I'd like to compare each value x of an array with a rolling window of the n previous values. More precisely I'd like to see at which percentile this new value x would be, if we added it to the previous window : import numpy as np A = np.array([1, 4, 9, 28, 28.5, 2, 283, 3.2, 7, 15]) print A n = 4 # window width for i in range(len(A)-n): W = A[i:i+n] x = A[i+n] q = sum(W <= x) * 1.0 / n print 'Value:', x, ' Window before this value:', W, ' Quantile:', q [ 1. 4. 9. 28. 28.5 2. 283. 3.2 7. 15. ] Value: 28.5 Window before this value: [ 1. 4. 9. 28.] Quantile: 1.0 Value: 2.0 Window before this

How to take the mean of last 10 values in a column before a missing value using R?

爱⌒轻易说出口 提交于 2019-12-04 13:02:19
I am new to R and having trouble figuring out to go about this. I have data on tree growth rates from dead trees, organized by year. So, my first column is year and the columns to the right are growth rates for individual trees, ending in the year each tree died. After the tree died, the values are "NA" for the remaining years in the dataset. I need to take the mean growth for the 10 years preceding each tree's death, but each tree died in a different year. Does anyone have an idea for how to do this? Here is an example of what a dataset might look like: Year Tree1 Tree2 Tree3 1989 53.00 84.58

Moving average for time series with not-equal intervls

江枫思渺然 提交于 2019-12-04 09:24:36
I have a dataset for price of the ticker on the stock exchange: time - price. But intervals between data points are not equal - from 1 to 2 minutes. What is the best practice to calculate moving average for such case? How to make it in Matlab? I tend to think, that weights of the points should depend on the time interval that was last since previous point. Does we have function in Matlab to calculate moving average with custom weights of the points? Here is an example of the "naive" approach I mentioned in the comments above: % some data (unequally spaced in time, but monotonically non

Why is zoo::rollmean slow compared to a simple Rcpp implementation?

ⅰ亾dé卋堺 提交于 2019-12-04 08:47:03
问题 zoo::rollmean is a helpful function that returns the rolling mean of a time series; for vector x of length n and window size k it returns the vector c(mean(x[1:k]), mean(x[2:(k+1)]), ..., mean(x[(n-k+1):n])) . I noticed that it seemed to be running slowly for some code I was developing, so I wrote my own version using the Rcpp package and a simple for loop: library(Rcpp) cppFunction("NumericVector rmRcpp(NumericVector dat, const int window) { const int n = dat.size(); NumericVector ret(n

LINQ to calculate a moving average of a SortedList<dateTime,double>

十年热恋 提交于 2019-12-04 07:58:30
问题 I have a time series in the form of a SortedList<dateTime,double> . I would like to calculate a moving average of this series. I can do this using simple for loops. I was wondering if there is a better way to do this using linq. my version: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var mySeries = new SortedList<DateTime, double>(); mySeries.Add(new DateTime(2011, 01, 1

SAS: standard deviation on unfixed rolling window

坚强是说给别人听的谎言 提交于 2019-12-03 22:49:47
问题 I think I posted similar question before. But this time I am struggling with data ID. My data looks like date Stock value standard_deviation 01/01/2015 VOD 18 ... 01/01/2015 VOD 15 ... 01/01/2015 VOD 5 ... 03/01/2015 VOD 66 ... 03/01/2015 VOD 7 ... 04/01/2015 VOD 19 ... 04/01/2015 VOD 7 ... 05/01/2015 VOD 3 ... 06/01/2015 VOD 7 ... ..... ... ... ... 01/01/2015 RBS 58 ... 01/01/2015 RBS 445 ... 01/01/2015 RBS 44 ... 03/01/2015 RBS 57 ... I need to work out the moving average/std deviation for

How to efficiently calculate a moving Standard Deviation

倖福魔咒の 提交于 2019-12-03 18:40:28
问题 Below you can see my C# method to calculate Bollinger Bands for each point (moving average, up band, down band). As you can see this method uses 2 for loops to calculate the moving standard deviation using the moving average. It used to contain an additional loop to calculate the moving average over the last n periods. This one I could remove by adding the new point value to total_average at the beginning of the loop and removing the i - n point value at the end of the loop. My question now

Moving average in postgresql

别来无恙 提交于 2019-12-03 16:06:57
问题 I have the following table in my Postgresql 9.1 database: select * from ro; date | shop_id | amount -----------+----------+-------- 2013-02-07 | 1001 | 3 2013-01-31 | 1001 | 2 2013-01-24 | 1001 | 1 2013-01-17 | 1001 | 5 2013-02-10 | 1001 | 10 2013-02-03 | 1001 | 4 2012-12-27 | 1001 | 6 2012-12-20 | 1001 | 8 2012-12-13 | 1001 | 4 2012-12-06 | 1001 | 3 2012-10-29 | 1001 | 3 I am trying to get a moving average comparing data against last 3 Thursdays without including the current Thursday. Here's