rollapply

Efficiently perform row-wise distribution test

依然范特西╮ 提交于 2019-12-19 02:37:14
问题 I have a matrix in which each row is a sample from a distribution. I want to do a rolling comparison of the distributions using ks.test and save the test statistic in each case. The simplest way to implement this conceptually is with a loop: set.seed(1942) mt <- rbind(rnorm(5), rnorm(5), rnorm(5), rnorm(5)) results <- matrix(as.numeric(rep(NA, nrow(mt)))) for (i in 2 : nrow(mt)) { results[i] <- ks.test(x = mt[i - 1, ], y = mt[i, ])$statistic } However, my real data has ~400 columns and ~300

Using rollapply and lm over multiple columns of data

非 Y 不嫁゛ 提交于 2019-12-13 15:26:34
问题 I have a data frame similar to the following with a total of 500 columns: Probes <- data.frame(Days=seq(0.01, 4.91, 0.01), B1=5:495,B2=-100:390, B3=10:500,B4=-200:290) I would like to calculate a rolling window linear regression where my window size is 12 data points and each sequential regression is separated by 6 data points. For each regression, "Days" will always be the x component of the model, and the y's would be each of the other columns (B1, followed by B2, B3, etc). I would then

Moving average with changing period in R

三世轮回 提交于 2019-12-13 02:28:04
问题 I have a data frame named abc on which I'm doing moving average using rollapply . The following code works: forecast <- rollapply(abc, width=12, FUN=mean, align = "right", fill=NA) Now, I want to do the same thing with the width being variable, i.e. for the 1st month, it'll be empty, for the second month, first month's value will come. For the third month, it'll be (first month+second month/2), i.e. for the ith month, if i<=12 , the value will be (sum(1:i-1)/(i-1)) and for i>=12 it will be

Rolling lagged differences

雨燕双飞 提交于 2019-12-12 03:58:18
问题 Ok so I am looking to create rolling lagged differences in R. vec <- c(43.79979, 44.04865, 44.17308, 44.54638, 44.79524, 44.79524, 44.79524, 44.42195, 44.54638, 44.79524, 44.42195, 43.30206, 43.30206, 43.17764, 43.30206) > length(vec) [1] 15 This is what I have tried so far: vec1 <- rollapply(vec, width = 2, fill = NA, FUN = diff) This gives this output: [1] 0.24886 0.12443 0.37330 0.24886 0.00000 0.00000 -0.37329 0.12443 0.24886 -0.37329 -1.11989 0.00000 -0.12442 0.12442 NA > length(vec1) [1

How to calculate moving average by specified grouping and deal with NAs

六月ゝ 毕业季﹏ 提交于 2019-12-11 19:04:09
问题 I have a data.table which needs a moving average to be calculated on the previous n days of data (let's use n=2 for simplicity, not incl. current day) for a specified grouping (ID1, ID2). The moving average should attempt to include the last 2 days of values for each ID1-ID2 pair. I would like to calculate moving average to handle NAs two separate ways: 1. Only calculate when there are 2 non-NA observations, otherwise avg should be NA (e.g. first 2 days within an ID1-ID2 will always have NAs)

CAPM.beta rollapply

喜欢而已 提交于 2019-12-08 13:31:41
问题 I have already successfully calculated my rolling correlations in my xts object with x <- cbind(market_return,stock_returns) rollcor_3year <- rollapplyr( x, width=width_cor,function(x) cor(x[,1],x[,-1], use="pairwise.complete.obs"),by.column=FALSE) The correlation was later used to calculate rolling Betas. Now I found the function CAPM.beta from the PerformanceAnalytics package and I wonder why I cannot use beta <- rollapplyr(x,width=width_cor,function(x) CAPM.beta(x[,1],x[,-1]),by.column

R: rollapplyr and lm factor error: Does rollapplyr change variable class?

狂风中的少年 提交于 2019-12-08 03:40:07
问题 This question builds upon a previous one which was nicely answered for me here. R: Grouped rolling window linear regression with rollapply and ddply Wouldn't you know that the code doesn't quite work when extended to the real data rather than the example data? I have a somewhat large dataset with the following characteristics. str(T0_satData_reduced) 'data.frame': 45537 obs. of 5 variables: $ date : POSIXct, format: "2014-11-17 08:47:35" "2014-11-17 08:47:36" "2014-11-17 08:47:37" ... $ trial

R rollapply bottom to top?

大憨熊 提交于 2019-12-07 19:01:32
问题 I'm trying to run rollapply from the bottom of my data.frame up to the top of my data.frame. Basically the last row in the data.frame (RBH) is the final measurement for a given subject in 2012. I then need to subtract each annual measurement in the previous years to calculate what the individual's size would have been each year prior. Sample data.frame: df1 <- structure(c(1.62, 3.96, 4.89, 6.61, 8.79, 57.15, 2.43, 5.58, 7.2, 9.3, 11.87, 66.6, 1.47, 3.49, 4.32, NA, NA, 60.75), .Dim = c(6L, 3L)

use rollapply and zoo to calculate rolling average of a column of variables

放肆的年华 提交于 2019-12-07 12:41:44
问题 I want to calculate the rolling mean for all variables in column "sp". This is a sample of my data: the_date sp wins 01-06--2012 1 305 02-06--2012 1 276 03-06--2012 1 184 04-06--2012 1 248 05-06--2012 1 243 06-06--2012 1 363 07-06--2012 1 272 01-06--2012 2 432 02-06--2012 2 369 03-06--2012 2 302 04-06--2012 2 347 05-06--2012 2 357 06-06--2012 2 331 07-06--2012 2 380 01-06--2012 3 1 02-06--2012 3 2 03-06--2012 3 3 04-06--2012 3 2 05-06--2012 3 0 06-06--2012 3 2 07-06--2012 3 0 What I want, is

Clueless about this error: wrong sign in 'by' argument

江枫思渺然 提交于 2019-12-06 19:54:29
I have the following dataset: >k1[1:10,] id web_name first_name second_name position date team1 team2 game_week points home_away team_scored team_conceded minutes goals assists 1 1 Fabianski Lukasz Fabianski Goalkeeper 17/08/13 ARS AVL 1 0 H 1 3 0 0 0 2 1 Fabianski Lukasz Fabianski Goalkeeper 24/08/13 ARS FUL 2 0 A 3 1 0 0 0 3 1 Fabianski Lukasz Fabianski Goalkeeper 01/09/13 ARS TOT 3 0 H 1 0 0 0 0 4 1 Fabianski Lukasz Fabianski Goalkeeper 14/09/13 ARS SUN 4 0 A 3 1 0 0 0 5 1 Fabianski Lukasz Fabianski Goalkeeper 22/09/13 ARS STK 5 0 H 3 1 0 0 0 6 1 Fabianski Lukasz Fabianski Goalkeeper 28/09