rollapply

Calculate running sum/decay value in time series

余生长醉 提交于 2019-12-24 18:31:10
问题 I'm looking to calculate a specific type of 'diminishing' sum or decay value within a time series. For instance, say I have some simple data: thedata <- data.frame(magicseeds = c(30,20,10,40,20) ,week = seq(from = 1, to = 5, by = 1)) I would like to calculate the value of 'magicseeds', such that it has a diminishing impact over time. For instance, lets say each value has 20% of the value the following week, so the first value in the magicseeds column (30) would be 30*0.2+6*0.2 and so on until

Wavelet correlation using rolling window

北慕城南 提交于 2019-12-24 12:18:04
问题 I have 3 time series which I can apply the wavelet transform to using a rolling window. The rolling window takes a single time series of length 200 and applies the waveslim::modwt function to it over the first 30 samples. This outputs 5 lists of which I am only interested in (d1,d2,d3,d4) and these each have a length of 30. A simple example can be found here: library(waveslim) J <- 4 #no. of levels in decomposition data(ar1) ar1.modwt <- modwt(ar1, "la8", J) @G. Grothendieck has kindly

VaR calculation with complete missing column

我只是一个虾纸丫 提交于 2019-12-24 04:34:03
问题 I need to calculate rolling VaR of stock returns. From this post: Using rollapply function for VaR calculation using R , I understand that columns having complete missing cases will give error. But since the starting date and end date of stock returns for various firms are different, it creates missing values when data is converted from long to wide format. Estimation can be done using only rows with no missing values but this leads to serious loss of data. Thus, is there any way to perform

Rolling Count of Events Over Time Series

こ雲淡風輕ζ 提交于 2019-12-23 20:16:32
问题 I'm trying to calculate a rolling count/sum of occurrences by group over the series of a time frame. I have a data frame with some sample data like this: dates = as.Date(c("2011-10-09", "2011-10-15", "2011-10-16", "2011-10-18", "2011-10-21", "2011-10-22", "2011-10-24")) group1=c("A", "C", "A", "A", "L", "F", "A") group2=c("D", "A", "B", "H", "A", "A", "E") df1 <- data.frame(dates, group1, group2) I iterate individual data frames for each unique 'group', so for example this is how the group

Rolling prediction in a data frame using dplyr and rollapply

拈花ヽ惹草 提交于 2019-12-23 18:33:34
问题 My first question here :) My goal is: Given a data frame with predictors (each column a predictor / rows observations) fit a regression using lm and then predict the value using the last observation using a rolling window. The data frame looks like: > DfPredictor[1:40,] Y X1 X2 X3 X4 X5 1 3.2860 192.5115 2.1275 83381 11.4360 8.7440 2 3.2650 190.1462 2.0050 88720 11.4359 8.8971 3 3.2213 192.9773 2.0500 74130 11.4623 8.8380 4 3.1991 193.7058 2.1050 73930 11.3366 8.7536 5 3.2224 193.5407 2.0275

How to calculate the average slope within a moving window in R

允我心安 提交于 2019-12-23 16:41:26
问题 My dataset contains 2 variables y and t [05s] . y was measured every 05 seconds. I am trying to calculate the average slope within a moving 20-second-window , i.e. after calculating the first 20-second slope value the window moves forward one time unit (05 seconds) and calculates the next 20-second-window, producing successive 20-second slope values at 05-second increments . I thought that calculating a rolling regression with rollapply (zoo package) would do the trick, but I get the same

Genome coverage as sliding window

别等时光非礼了梦想. 提交于 2019-12-23 01:00:08
问题 I mapped my reads to my assembly using the bwa mem algorithm and extracted the number of reads per base (= coverage) using samtools depth . The resulting file is the following: 1091900001 1 236 1091900001 2 245 1091900001 3 265 1091900001 4 283 1091900001 5 288 1091900002 1 297 1091900002 2 312 1091900002 3 327 1091900002 4 338 1091900002 5 348 with three columns: name of the contig (since it is a multi-contig file, this ID changes) - position (base) - number of reads that mapped (coverage).

Sum over rows (rollapply) with time decay

前提是你 提交于 2019-12-20 07:25:12
问题 This is a follow on question to a question I posted earlier (see Sum over rows with multiple changing conditions R data.table for more details). I want to calculate how many times the 3 subjects have experienced an event in the last 5 years. So have been summing over a rolling window using rollapply from the zoo package. This assumes that the experience 5 years ago is as important as the experience 1 year ago (same weighting), so now I want to include a time decay for the experience that

How to rollapply over a multi column data table

亡梦爱人 提交于 2019-12-20 04:56:11
问题 I would like to use the rollapply function over a multi column datatable, namely I would like to be able to use each column independantly for instance let's consider the following datable : > DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9) > DT x y v 1: a 1 1 2: a 3 2 3: a 6 3 4: b 1 4 5: b 3 5 6: b 6 6 7: c 1 7 8: c 3 8 9: c 6 9 Then I would like to use rollapply as a rolling subset in order to work out the rolling mean over 3 element of columns 2 and 3 and store them into

Efficient way to perform running total in the last 365 day window

坚强是说给别人听的谎言 提交于 2019-12-19 03:44:21
问题 This is what my data frame looks like: library(data.table) df <- fread(' Name EventType Date SalesAmount RunningTotal Runningtotal(prior365Days) John Email 1/1/2014 0 0 0 John Sale 2/1/2014 10 10 10 John Sale 7/1/2014 20 30 30 John Sale 4/1/2015 30 60 50 John Webinar 5/1/2015 0 60 50 Tom Email 1/1/2014 0 0 0 Tom Sale 2/1/2014 15 15 15 Tom Sale 7/1/2014 10 25 25 Tom Sale 4/1/2015 25 50 35 Tom Webinar 5/1/2015 0 50 35 ') df[,Date:= as.Date(Date, format="%m/%d/%Y")] The last column was my