Performing variance when the time window is not constant

I am trying to calculate a moving variance with a window of, say, 4 years, for each of names A, Band C. Data weekly:

> head(data1, 17)
         date name       value
1  1985-01-01    A -0.44008233
2  1985-01-01    B          NA #Observe that there are some NA's
3  1985-01-01    C  0.38682496
4  1985-01-08    A  0.41806540
5  1985-01-08    B -0.05460831
6  1985-01-08    C -0.52051435
7  1985-01-15    A  1.25769395
8  1985-01-15    B  0.80272053
9  1985-01-15    C -0.34501742
10 1985-01-22    A -0.43401839
11 1985-01-22    B  0.91113966
12 1985-01-22    C  1.07131717
13 1985-01-29    A -1.55395857
14 1985-01-29    B -0.43281709
15 1985-01-29    C  0.98034779
16 1985-02-05    A  1.70557396
17 1985-02-05    B  0.44688788

So far my approach is dcastdata and then run rollapply()( zoo) with a moving window 192 = 4 * 12 * 4:

v <- dcast(data1, date ~ name, value.var = "value")
var <- rollapply(v[-1], width=4*12*4, var, fill=NA, by.column = T)
var <- cbind(v$date, var)
var[,1] <- as.Date(var[,1])
However, I realized that for several months I have four observations (for example, February 7, 14, 21, 28), and for some I have five weekly observations (for example, 1, 8, 15, 22 and 29 January), therefore, the use of the observation window 4 years * 12 months * 4 weeksis incorrect. I was thinking of adding these additional observations to the time window ( width), but I'm not sure how (or if possible), since they vary depending on how many 5 weeks per month and how many Observations for 4 weeks per month are inside the time window .

, NA, NA ( var()), . , , - . , , , , .

:

set.seed(486)
date <- rep(seq(as.Date("1985-01-01"), as.Date("2010-01-1"), by="weeks"), each=3)
N <- length(date)
name <- c("A","B","C")
value <- rnorm(N)
i<-which(value %in% sample(value, 25)) ;i
j<-which(value %in% sample(value, 150)) ;j
value[i] <- NA
value[j] <- 0
data1 <- data.frame(date, name, value)
+4
2

, , . , . , - 2 :

library(data.table)
library(zoo)
setDT(data1)[,var := {
           v1 <- rollapplyr(value,width=4*12*4, var, fill=N)
           v2 <- rollapplyr(value,width=4*12*5, var, fill=N)
           (v1+v2)/2},  name]

PS: data.table, ( ) .

, . , . . , na.locf.

library(data.table)
library(zoo)
ID <- 
data.table(
  date = seq(as.Date("1985-01-01"), as.Date("2010-01-1"), by="days"))
setkey(ID,date)

setDT(data1)[,date:=as.Date(date)][, 
        {
          merge(ID,.SD,all.x=TRUE)[,value := na.locf(value)]
        },

        name]
+2

4 208 5 , . 209 , 2 4 , .

data1 "zoo" , . z A, B C. , rollapplyr

library(zoo)
z <- read.zoo(data1, split = 2) # 1305 x 3 
var0 <- function(x) var(x[x != 0])
r <- rollapplyr(z, 209, var0)

, 4- Index, A, B C:

fortify.zoo(r)
+2

All Articles