0
$\begingroup$

Let's suppose I have a time-series of 100 daily values and I want to compute a 5-day moving average of this time series.

I would do as follows:

library(igraph)
df=data.frame(x=rnorm(100)) # main time-series
df_mv_avg=data.frame(x=running_mean(df$x,5)) # time-series smoothed

It is almost obvious that df_mv_avg now contains 4 observations less (96) than df. However, in most examples of smoothed time-series, these time-series have the same length of the original (non-smoothed) time-series (in my case 100 obs).

How can I smooth a time-series (by n values) and at the same time keep the whole length of observations of the original time-series?

Thanks for any help

$\endgroup$
0
$\begingroup$

The same problem occurs in image processing with mean filtering or other convolution operations, and it is solved in different ways by different "border treatments", which can be, e.g., repeat, reflect, zero, or wrap around.

As the igraph library function running_mean does not support a "border treatment" option, you must implement it your self and, e.g, repeat the edge values, reflect them, or whatever the wanted behaviour is, e.g.

x <- running_mean(c(rep(x[0],n-1), x, rep(x[length(x)],n-1)), n)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.