EWMA chart in R

I have a dataset of 4948 daily return from 2000 to 2019 (4948 records)
The relevant variables are the yield and the position (1,2,3,...)

I want to calculate the exponentially weighted moving average chart (EWMA) with R.
There is a function in the package qcc that do it:

ewma(data, sizes, center, std.dev, lambda = 0.2, nsigmas = 3, 
     data.name, labels, newdata, newsizes, newlabels, 
     plot = TRUE, ...)

The problem is that I can't decipher some of their arguments:

- data: the dataframe, matrix or vector. (O.K.)
- sizes: we need to suppose there are 4948 groups (one for each day) of a size of one each?
- center: this is the media of each group or the media of the all observations?
- lambda: the parameter to weight each observation in the way the latest are of more weight. (O.K.)
- nsigmas: is this the standard deviation of all the observations or a arbitrary number?

Also, if the relevant variables are the position (1,2,3,..) and the yield how can I put the ticks in x axis as the years (2000,2001,2002,...)

The qcc package is intended for making process control charts for manufacturing. Imagine you are making batches of 100 wooden rods and from each batch you measure the length of 10 of them to monitor whether the process is controlling the length sufficiently well.

sizes would be 10. If you do not provide the value of sizes, the ewma function would derive the number from the number of measurements in each row of the data matrix or data frame. That is, your data would have 10 columns.
center is the intended length of the rods.
nsigmas determines how tightly you want to control the process. A data point will be labeled as "out of control" if it is more than nsigmas from the center.

None of that has much to do with your needs, I think. I suppose sizes would be 1 in your case. center determines where the target line on the plot is drawn and you can pick a value that works for your data.

The only way I know of to add axis annotations for these plots is to draw another x axis. I experimented with the pistonrings data used in the example of ewma() and found that the x axis is approximately plotted from 0 to 1 with offsets of 0.0375 on each end. Below is an example of adding some year labels to the pistonrings plot.

Perhaps qcc is not very well matched to what you want to do.

library(qcc)
data(pistonrings)
attach(pistonrings)
diameter <- qcc.groups(diameter, sample)

q <- ewma(diameter[1:25,], lambda=0.2, nsigmas=3)
axis(side = 3, at = 0.0375 + 0.925*(c(1,7,22,25) - 1)/24, labels = 2000:2003)

Hi,

Yes, I think qcc might not be the correct package and even R might not be the tool needed.

Probably I would need to do it manually in Excel. It is a lot of work, because I need to apply successive formula to earlier results.

Thank you anyway

I believe that you will find these models, specifically in the time series context, covered in a very clear form in Forecasting Principles and Practice, 3rd Edition: chapter 8: https://otexts.com/fpp3/expsmooth.html

If the data come from a market that is closed on known days, there is probably some necessary wrangling to get an index without missing values; if you search trading_day in their text, there is a walkthrough of how to do this with equities data.

I would maintain, in response to your final observation that, with R, the question is not if, only how.....

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.