Reputation: 271
I am totally new to R and have just started using it. I have three years of weekly data. I want to decompose this time series data into trend, seasonal and other components. I have following doubts:
ts()
or decompose()
Please correct me if I am wrong, the frequency is 52.
Thanks in Advance. I would really appreciate any kind of help.
Upvotes: 3
Views: 5269
Reputation: 99321
Welcome to R!
Yes, the frequency is 52.
If the data is not already classed as time-series, you will need both ts()
and decompose()
. To find the class of the dataset, use class(data)
. And if it returns "ts"
, your data is already a time-series as far as R is concerned. If it returns something else, like "data.frame"
, then you will need to change it to time-series. Assign a variable to ts(data)
and check the class again to make sure.
There is a monthly time-series dataset sunspot.month
already loaded into R that you can practice on. Here's an example. You can also read the help file for decompose
by writing ?decompose
class(sunspot.month)
[1] "ts"
> decomp <- decompose(sunspot.month)
> summary(decomp)
Length Class Mode
x 2988 ts numeric
seasonal 2988 ts numeric
trend 2988 ts numeric
random 2988 ts numeric
figure 12 -none- numeric
type 1 -none- character
> names(decomp)
[1] "x" "seasonal" "trend" "random" "figure" "type"
> plot(decomp) # to see the plot of the decomposed time-series
The call to names
indicates that you can also access the individual component data. This can be done with the $
operator. For example, if you want to look at the seasonal component only, use decomp$seasonal
.
Upvotes: 9