sma: Simple Moving Average

View source: R/RcppExports.R

smaR Documentation

Simple Moving Average

Description

Smoothes a series by means of a windowed mean. If x is an input series and y is the output filtered series, then:

y[i] = mean(x[(i-window_size+1) : i])

Usage

sma(x, window_size, recentre = TRUE, miss = "single")

Arguments

x

Input time-series as a vector.

window_size

Window size as an integer

recentre

Whether to recentre the window around the middle point in the window so as to avoid adding lag. I.e. for an odd window_length y[i] = mean(x[(i - floor(window_length/2)) : (i + floor(window_length/2))]) For an even window length the output is located more closely to earlier points, i.e. y[i] = mean(x[(i - window_length/2 + 1) : (i + window_length/2)])

miss

How to handle missing values. 'single' only returns NA for y[i] if x[i] = NA. 'all' returns NA for y[i] if any value in the window is missing, and 'none' omits NAs from the window, so will only return NA for y[i] if all windowed values are also NA.

Value

A filtered time-series the same length as x.


stulacy/filters documentation built on March 19, 2022, 7:25 a.m.