1
#' @include ccTable.R
2

3
interpolateVec <- function(v, lead, lag, FUN="mean", ...) {
4 1
    v <- suppressWarnings(as.numeric(as.character(v)))
5 1
    na.ind <- which(is.na(v))
6 1
    if (length(na.ind) > 0) { # do interpolation if NA is found. 
7 1
        v2 <- c(rep(NA, lead), v, rep(NA, lag))
8 1
        n_x <- sapply(na.ind, 
9 1
                      function(i) {
10 1
                          do.call(FUN,
11 1
                                  c(list(x=v2[i + lead + seq(-lead, lag)]),
12 1
                                    as.list(substitute(list(...)))[-1L]))
13
                      })
14 1
        v[na.ind] <- n_x
15
    }
16 1
    v
17
}
18

19

20
ccTable$methods(
21
    imputation = function() {
22 1
    "Filling missing data to a time series data by performing a given imputation
23 1
    method on a selected window period nearby the missing data."
24 1
        imputation_columns <- function(sd) {
25 1
            for (i in names(.self$conf)) {
26 1
                imwin <- .self$conf[[i]][['missingness']][['impute']]
27 1
                if (!is.null(imwin)) {
28 1
                    fun <- imwin[['fun']]
29 1
                    lead <- imwin[['lead']]
30 1
                    lag <- imwin[['lag']]
31 1
                    if (fun %in% c("mean", "median", "max", "min", "sum"))
32 1
                        sd[[i]] <- interpolateVec(v=sd[[i]], lead=lead, lag=lag, FUN=fun, na.rm=TRUE)
33
                    else 
34 0
                        sd[[i]] <- interpolateVec(v=sd[[i]], lead=lead, lag=lag, FUN=fun)
35
                }
36
            }
37 1
            return(sd)
38
        }
39 1
        .self$tclean <- .self$tclean[, imputation_columns(.SD), by=c("episode_id", "site")]
40
})

Read our documentation on viewing source code .

Loading