# Test to identify change in median in a time-series

the question is trivial, but is there any statistical test (can be done in R) to show that the median is changing in a time-series?? For example, if you go to the following link, you would notice that, after the first few samples (6 or 7), the time series takes a sharp rise. I was wondering if there is a formal statistical way of saying it!

## migrated from stackoverflow.comMay 9 '13 at 20:01

This question came from our site for professional and enthusiast programmers.

• Why the median and not the mean? A test for a change in the latter is called the Chow test and I'm sure it's not hard to find. – RoyalTS May 9 '13 at 18:26
• Asking about statistical methodologies is Off Topic for StackOverflow. – joran May 9 '13 at 18:29

As Nick has pointed out, you must take into account any auto-correlative structure that exists in the data. You might want to review Automatic detection of level changes in series of prices which discusses testing for level shifts in the mean. Whether you use the mean or the median is irrelevant to that discussion. The only "advantage" of using the median is that pulse and seasonal pulse anomalies are effectively suppressed when using the median while level/step shifts and local time trends are not.

There is a test called Mood's median test for this. However the R version of it is a bit complicated. See the discussion here: https://stat.ethz.ch/pipermail/r-help/2010-April/234387.html

• Standard tests for comparing medians (or means for that matter) are of little use here unless modified to match the dependence structure of time series. – Nick Cox Aug 8 '13 at 8:58

There is a new R package, changepoint.np which detects changes in the empirical distribution function. A minimal working example (demonstrated using a Normal distribution but it is a nonparametric test) is:

set.seed(1)
x=c(rnorm(100),rnorm(100,2))
out=cpt.np(x) # runs the ecdf changepoint method
plot(out) # plots the data with changepoints marked
cpts(out) # lists the changepoints identified


Alternatively, if you want a change in mean and are willing to make distributional assumptions the changepoint package contains the cpt.mean function or cpt.meanvar if the variance is changing too.

If you have dependence in the data then you just need to inflate the default penalty otherwise you get spurious changes that are just due to the dependence structure. For that you might want to use the CROPS (changepoints for a range of penalties) options available in both packages.