%% Test Time Series Data for Unit Root
% This example shows how to test a univariate time series for a unit root. It uses wages data (1900-1970) in the manufacturing sector. The series is in the Nelson-Plosser data set.
% Load the Nelson-Plosser data. Extract the nominal wages data.

load Data_NelsonPlosser
wages = DataTable.WN;

%
% Trim the NaN values from the series and the corresponding dates (this step is optional because the test ignores NaN values).

wDates = dates(isfinite(wages));
wages = wages(isfinite(wages));

%
% Plot the data to look for trends.

plot(wDates,wages)
title('Wages')

% The plot suggests exponential growth.
%
% Transform the data using the log function to linearize the series.

logWages = log(wages);
plot(wDates,logWages)
title('Log Wages')

% The plot suggests that time series has a linear trend.
%
% Test the null hypothesis that there is no unit root (trend stationary) against the alternative hypothesis that the series is a unit root process with a trend (difference stationary). Set 'Lags',7:2:11, as suggested in Kwiatkowski et al., 1992.

[h1,pValue1] = kpsstest(logWages,'Lags',7:2:11)

% kpsstest fails to reject the null hypothesis that the wage series is trend stationary.
% Test the null hypothesis that the series is a unit root process (difference stationary) against the alternative hypothesis that the series is trend stationary.

[h2,pValue2] = adftest(logWages,'Model','ts')

% adftest fails to reject the null hypothesis that the wage series is a unit root process.
% Because the results of the two tests are inconsistent, it is unclear that the wage series has a unit root. This is a typical result of tests on many macroeconomic series.
% kpsstest has a limited set of calculated critical values. When it calculates a test statistic that is outside this range, the test reports the p-value at the appropriate endpoint. So, in this case, pValue reflects the closest tabulated value. When a test statistic lies inside the span of tabulated values, kpsstest linearly interpolates the p-value.
% Copyright 2015 The MathWorks, Inc.