Search code examples
time-seriesmoving-averagegretl

Weighted Moving Avarage in Gretl


I have a question on gretl and how I can compute the filter of moving avarage.

I have a time series and I want to calculate the weighted moving avarage centered in 5 with these weights: 0.15, 0.2, 0.3, 0.2, 0.15.

In the main page of gretl we have the Variabile window where I can select Filter but there's no option for what I want to do, only, for example, simple moving avarage.

In R I would do something like this:

c<-as.vector()
for (in in 3:(T-2)){ 
c<-rbind(c, 0.15*x[i-2]+0.2*x[i-1]+0.3*x[i]+0.2*x[i+1]+0.15*x[i+2]}

where x is my time seriee and T is the number of observations.

But my questions are:

  1. Does it exist an user-friendly way to do it in gretl?
  2. If not, what is the best way to do it in the console? Does it exist a specific function?

Solution

  • Well I don't know what exactly you call user friendly, but since you want to have those specific weights, I guess there's no way around typing in some numbers, right? So if I understand you correctly, and given your series x (in a dataset which is declared and recognized as a time series), then you simply would need to type the formula:

    series weighma =  0.15 * x(+2) + 0.2 * x(+1) + 0.3 * x + 0.2 * x(-1) + 0.15 * x(-2)
    

    (Instead of 'series' you could also type in 'genr' or just omit it, but I recommend this explicit variant. The same goes for the + signs inside the parentheses to indicate leads instead of lags.) The name 'weighma' is of course arbitrary.

    There are at least two places where you could type in that formula: Either choose Add /Define new variable from the menus, which gives you a dialog window with a formula field, or open the gretl console (or a script editor window).

    A solution which would perhaps be more flexible in a script could use a gretl list of variables and the 'lincomb' function, something like this:

    maxlead = 2
    matrix weights = {0.15, 0.2, 0.3, 0.2, 0.15}
    list xx = lags( nelem(weights), x(maxlead + 1) )
    series weighma = lincomb(xx, weights)
    

    The correct maxlead value could also be inferred from the length of the weights vector under the assumption of a centered MA, but I leave it at that.