rollapply

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

。_饼干妹妹 提交于 2019-12-06 19:25:29
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 : Factor w/ 5 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ... $ vial : Factor w/ 4 levels "1","2",

R rollapply bottom to top?

烈酒焚心 提交于 2019-12-06 10:35:53
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), .Dimnames = list(c("2008", "2009", "2010","2011", "2012","RBH"), c("Tree001", "Tree002", "Tree003")))

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

假如想象 提交于 2019-12-05 18:29:20
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 to have a column added to data, that gives the moving average over 3 days for each sp. So the following

R: Grouped rolling window linear regression with rollapply and ddply

佐手、 提交于 2019-12-04 05:10:55
问题 I have a data set with several grouping variables on which I want to run a rolling window linear regression. The ultimate goals is to extract the 10 linear regressions with the lowest slopes and average them together to provide a mean minimum rate of change. I have found examples using rollapply to calculate rolling window linear regressions, but I have the added complication that I would like to apply these linear regressions to groups within the data set. Here is a sample data set and my

R: Grouped rolling window linear regression with rollapply and ddply

穿精又带淫゛_ 提交于 2019-12-02 04:40:06
I have a data set with several grouping variables on which I want to run a rolling window linear regression. The ultimate goals is to extract the 10 linear regressions with the lowest slopes and average them together to provide a mean minimum rate of change. I have found examples using rollapply to calculate rolling window linear regressions, but I have the added complication that I would like to apply these linear regressions to groups within the data set. Here is a sample data set and my current code which is close and isn't quite working. dat<-data.frame(w=c(rep(1,27), rep(2,27),rep(3,27)),

Using rollapply function for VaR calculation using R

橙三吉。 提交于 2019-12-02 00:23:49
I did the following for calculating Value at Risk (VaR) over 20 period rolling window: require(PerformanceAnalytics); require(zoo) data(edhec) class(edhec) # [1] "xts" "zoo" class(edhec$CTAGlobal) # "NULL" var1<-rollapply(edhec,width=20,FUN=function(edhec) VaR(R=edhec,p=.95,method="modified"),by.column=TRUE) It produces the desired output, and then I tried the same on another data: data(managers) class(managers) # [1] "xts" "zoo" class(managers$HAM4) # [1] "xts" "zoo" var2<-rollapply(managers,width=20,FUN=function(managers) VaR(R=managers,p=.95,method="modified"),by.column=TRUE) But I am

Sum pairs of columns by group

﹥>﹥吖頭↗ 提交于 2019-12-01 21:57:23
问题 I wish to sum pairs of columns by group. In the example below I wish to sum pairs ( v1 and v2 ), ( v3 and v4 ), and ( v5 and v6 ), each by r1 , r2 and r3 . I can do this using the sapply statement below and I get the correct answer. However, the required code is complex. Could someone show me how to do the same operation perhaps in package data.table or with rollapply and/or other options? I have not yet explored those options. Sorry if this is a duplicate. my.data <- read.table(text= " r1 r2

Estimation of rolling Value at Risk (VaR) using R

不想你离开。 提交于 2019-12-01 02:09:25
问题 I need to perform rolling VaR estimation of daily stock returns. At first I did the following: library(PerformanceAnalytics) data(edhec) sample<-edhec[,1:5] var605<-rollapply(as.zoo(sample),width=60,FUN=function(x) VaR(R=x,p=.95,method="modified",invert=T),by.column=TRUE,fill=NA) It performs the computation and returns a zoo object but gives a series of warnings as follows: VaR calculation produces unreliable result (inverse risk) for column: 1 : -0.00030977098532231 Then, I tried the same

the rolling regression in R using roll apply

时间秒杀一切 提交于 2019-11-30 19:49:11
My imported data contains 7 variables: Y and X1 , X2 , X3 , X4 , X5 , X6 . I tried applying the rollapply function in zoo in order to run a rolling regression within an in-sample with a window of 262 obs. (work days in a year). date Y X1 X2 1 10/1/07 -0.0080321720 4.690734e-03 3.333770e-03 2 10/2/07 0.0000000000 -2.818413e-03 5.418223e-03 3 10/3/07 0.0023158650 -4.178744e-03 -3.821100e-04 4 10/4/07 -0.0057491710 -5.071030e-03 -8.321550e-04 5 10/5/07 0.0073570500 3.065045e-03 5.179574e-03 6 10/8/07 0.0127708010 -7.278513e-03 1.145395e-03 7 10/9/07 0.0032661980 9.692267e-03 6.514035e-03 8 10/10

Efficiently perform row-wise distribution test

与世无争的帅哥 提交于 2019-11-30 19:38:35
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,000 rows for a single example, and I have a lot of examples. So I'd like this to be fast. The Kolmogorov