R/moving_average_pupil.R

Defines functions moving_average_pupil

Documented in moving_average_pupil

#' Moving average function
#'
#' Creates a moving average using a specified averageing window
#' if n is 5, then it uses the target value, 2 preceding and 2 following
#'
#' @param x data
#' @param n sed for moving window
#' @param centered whether moving average window should be centered (TRUE) or trailing (FALSE)
#' @return return sum divided by count
#' @export
moving_average_pupil <- function(x, n=5, centered=TRUE) {

  if (centered) {
    before <- floor  ((n-1)/2)
    after  <- ceiling((n-1)/2)
  } else {
    before <- n-1
    after  <- 0
  }

  # Track the sum and count of number of non-NA items
  s     <- rep(0, length(x))
  count <- rep(0, length(x))

  # Add the centered data
  new <- x
  # Add to count list wherever there isn't a
  count <- count + !is.na(new)
  # Now replace NA_s with 0_s and add to total
  new[is.na(new)] <- 0
  s <- s + new

  # Add the data from before
  i <- 1
  while (i <= before) {
    # This is the vector with offset values to add
    new   <- c(rep(NA, i), x[1:(length(x)-i)])

    count <- count + !is.na(new)
    new[is.na(new)] <- 0
    s <- s + new

    i <- i+1
  }
  #
  # Add the data from after
  i <- 1
  while (i <= after) {
    # This is the vector with offset values to add
    new   <- c(x[(i+1):length(x)], rep(NA, i))

    count <- count + !is.na(new)
    new[is.na(new)] <- 0
    s <- s + new

    i <- i+1
  }

  # return sum divided by count
  s/count
}
dmirman/gazer documentation built on Aug. 1, 2022, 2:02 p.m.