Open mthelm85 opened 5 years ago
Hi @mthelm85, Thanks for this report, I will dig into the details later. (quite busy at this moment)
I stumbled over this problem a few days ago as well. To be honest, I thought a centred moving average would be the default behaviour; it just seems very odd to me that applying a moving average to a function (e.g. a sine) should result in a shifted phase. My (stupidly simple) workaround is to simply shift the timestamps by half the width of the moving average's window. This will probably be fairly problematic for non-equidistant timestamps, but, oh, well. As a MWE:
using Statistics
using Plots
# generate some data
t = collect(-2*pi:0.01:2*pi)
x = sin.(t)
dt = Array{DateTime}(undef, length(x))
for i in 1:length(x)
dt[i] = DateTime(1900,1,1) + Second(i)
end
# generate TimeArrays
mw = 50
ta = TimeArray(dt, x)
tb = moving(mean, ta, mw*2+1, padding=true)
tc = TimeArray(timestamp(tb)[1:end-mw], values(tb)[1+mw:end])
# plot TimeArrays
plot(ta, label="raw data")
plot!(tb, label="moving average")
plot!(tc, label="shifted average")
It's pretty easy to compute a one-sided moving average with the
mean
function passed tomoving
, but it would be nice to be able to compute a centered moving average. It would be nice if this could be done by simply passing acenter::Bool
keyword argument tomoving
as is done in R's forecast package with thema
function.I wrote a function for computing it that looks like this (I'm a Julia newbie):
Anyways, just a thought. I love the package, thanks!