Linear filters
The first step in time series analysis is to decompose the time series in trend, seasonality, and so on.
One of the methods of extracting trend from the time series is linear filters.
One of the basic examples of linear filters is moving average with equal weights.
Examples of linear filters are weekly average, monthly average, and so on.
The function used for finding filters is given as follows:
Filter(x,filter)
Here, x
is the time series data and filter
is the coefficients needed to be given to find the moving average.
Now let us convert the Adj.Close
of our StockData
in time series and find the weekly and monthly moving average and plot it. This can be done by executing the following code:
> StockData <- read.zoo("DataChap4.csv",header = TRUE, sep = ",",format="%m/%d/%Y") >PriceData<-ts(StockData$Adj.Close, frequency = 5) > plot(PriceData,type="l") > WeeklyMAPrice <- filter(PriceData,filter=rep(1/5,5)) > monthlyMAPrice <- filter(PriceData,filter=rep...