# Test for trend and seasonality in time series

This is my data:

x <- c(88, 88, 88, 85, 85, 85, 85, 85, 86, 90, 83, 83, 84, 88, 88, 88, 89,
89, 89, 89, 87, 89, 82, 82, 85, 85, 87, 87, 87, 87, 87, 87, 92, 92,
84, 84)

My purpose here is to check whether x has trend or seasonality. What I have done for now is:

stlm <- stl(ts(x,frequency = 12), s.window = "periodic")
plot(stlm)

After I obtain and view the graph, I want to measure whether trend and seasonality are statistically significantly or not. I would like to get something like a $p$-value in regression analysis.

How can I do that?

Are you sure you want to formally test for the presence of a trend or a seasonal pattern? What are you going to do with the test result?

To get a fair result you would need to formulate a hypothesis before you see the data. For example, $H_0 \colon \text{there is a linear trend}$. Then you would build a model for the data including a linear trend and test whether the corresponding coefficient is zero or not.

However, if you (1) do not have a hypothesis to begin with and (2) take a look at the data to identify the possible shape of the trend and/or the seasonal component and then (3) specify a model allowing for this particular shape, then you will quite likely reject the null of absence of the shape. Think about it: you extract a pattern from the data and then question its presence; of course, you will likely conclude the pattern is present. Therefore, when testing you cannot apply the critical values of the regular null distribution as if the pattern was specified before viewing the data.

• For example, if you (1) you do not know what type of time trend to expect and (2) take a look at the data and see a linear time trend, then (3) specify a model allowing for a linear time trend and then (4) test for its absence, then you will quite likely reject the null hypothesis. This will happen by the design of the testing procedure, rendering it quite unsuitable for inference.

If trend/seasonality is a just a nuisance and your focus will be on some other aspects of the model, you could include trend/seasonality without formal testing. Even if those patterns do not truly belong in the model, presence of irrelevant regressors is normally considered less problematic than absence of relevant ones (although exceptions exist).

Update

Your data series is only three years long, which makes seasonality modelling quite problematic and prone to overfitting. Extra care will be needed there (but that is a separate topic, so I will not expand on it).

You may visualize seasonality using function seasonplot from "forecast" package in R. The function takes a time series object (ts) as an input and plots the development of the series stacked over full seasonal periods. (In the figure, I assumed your series starts in January, but you could change that.)

• @Richad, the x is here my dependent value for multiple regression before building regression model I wanted to check if there is any pattern like trend or seasonality after viewed the graph above ı have some doubt about seasonality because every november the x is decreasing between 6 and 8 points thats way ı want to test power of seasonality and fix it like using dummy variable. Do u have any suggestion about my problem?Thanks in advance – tiko33 Jul 24 '16 at 13:32
• @tiko33, you could include seasonal variables (e.g. dummies) in your model, but it does not seem you would really need to test for presence of seasonality. – Richard Hardy Jul 24 '16 at 14:34
• thanks for your interest ts=ts(uba_down[,2],freq=12) apply(stlm\$time.series, 2, var) / var(ts) after tried this I obtain seasonal trend remainder 0.66415287 0.07936102 0.21969780 do u think it's the prove presence of seasonality and now how can I build up my model with decompositon of seasonality or dummy variables or both. I m confused about that – tiko33 Jul 24 '16 at 19:58
• Building a model is a separate question and should be asked as such. I could only hint that three years is a very short period and thus extra care should be taken to prevent overfitting. Using STL decomposition could probably work alright if you sensibly tune the model parameters (I cannot help with it as I have not used STL much; better refer to the existing literature). After the STL decomposition, you could subtract the fitted seasonal component from your data and further model the modified series (run a regression with the variables of interest or the like). – Richard Hardy Jul 25 '16 at 7:06

Well, you can run your model with a time trend and check if it's significant. If you suspect that the trend is not linear --for instance, quadratic--, you can add a polynomial time trend and check its significance again. I do not know if it is the best strategy, but it may help.