I am managing a stock daily dataset. Here is the sample code:
A <- cumsum(rnorm(200))
Date <- Sys.Date() + cumsum(sample(1:5, 200, replace = TRUE)) # unequaly spaced time series
data<-data.frame(Date,A)
data<-data%>%
mutate(Year_Month=as.yearmon(Date))
My objective is to calculate a new monthly variable called B. It is calculated as mean from daily data A over a 12-month period that ends in month. For example, 2021-07-03 belongs to July of 2021, I need the set the window over previous 12 months, from July of 2020 to June of 2021 and I calculate the average using all daily data A within this window. Therefore, for [2021-07-03,2021-07-31], the outcome B is the same.
I've tried to use rollapply and runner function, but the difficulty is the window is not constant since the number of days in each month are not constant. I'd like to achieve this goal under dplyr context.
The expected sample output looks like this:
Date Year_Month A B
2021-07-03 July 2021 3.3 2.3
2021-07-08 July 2021 1.5 2.3
2021-07-11 July 2021 4.3 2.3
...
2021-08-04 Aug 2021 2.2 3.2
2021-08-07 Aug 2021 5.7 3.2
2021-08-09 Aug 2021 4.2 3.2
My new dataset is a pure time series with 348 monthly observations:
Date A
2021-07-01 3.1
2021-08-01 4.5
2021-09-01 5.5
...
2021-10-01 4.4
2021-11-01 2.4
2021-12-01 5.5
I am calculating shock by doing AR(2) model over rolling window of 60 months that end in month.The shock in month n+1 is the difference between the actual value of the series and its predicted value (residuals) using the slope coefficients estimated over the preceding 60 months.
I am writing an function and call it inside the slider.
AR_2<-function(x){
arima(x,order=c(1,0,0))$residuals
}
MILIQ_unexpected<-MILIQ%>%
mutate(Shock= slide_index_dbl(A, Date, AR_2, .before = months(60), .after = months(-1), .complete = T))
I got the following error:
Problem with `mutate()` input `B`.
x In iteration 61, the result of `.f` had size 59, not 1.
i Input `B` is `slide_index_dbl(...)`.
Backtrace:
1. `%>%`(...)
11. slider:::stop_not_all_size_one(61L, 59L)
12. slider:::glubort("In iteration {iteration}, the result of `.f` had size {size}, not 1.")
sliderpackage to do this: davisvaughan.github.io/slider. You might want to check it out. - p0bsarima(rnorm(50), order = c(1,0,0))$residualsreturn a vector of length 50. So how can a vector of size n>1 be assigned to single value unless you further summarise it. Think of it and your problem will be solved. - AnilGoyalrollingsumorcumsumbut notrolling-cumsum. - AnilGoyal