# How to obtain detrended data and demeaned data in Stata?

I would like to run KSS test (KAPETANIOS, G. – SHIN, Y. – SNELL, A. 2003: Testing for a UnitRoot in the Nonlinear STAR Framework) , but I don't know how to obtain demeaned data and detrended data. Could you describe me the process in STATA?

-

You can demean a variable as follows:

summarize x
generate x_demeaned = x - r(mean)


If you have a batch of variables, you can use a loop. Suppose that you have three variables called peter, paul and mary:

foreach v of varlist peter paul mary {
summarize v'
generate v'_demeaned = v' - r(mean)
}


If you have missing values and you want to use the variables in a regression, you might want to do listwise deletion. This means that you don't take into account obsevrations that have missings for any of the variables.

egen anymiss = rowmiss(peter paul mary)
foreach v of varlist peter paul mary {
summarize v' if anymiss == 0
generate v'_demeaned = v' - r(mean) if anymiss == 0
}


For detrending you have several possibilities. If you want to remove a linear trend from a variable y, you could do the following, supposing that t is the time index:

regress y t
predict y_detrended , resid


You can also have a look at

help tssmooth


as well as the following presentation: http://ideas.repec.org/p/boc/asug06/2.html

-