------------------------------------------------------------------------------- help formvcorr-------------------------------------------------------------------------------

Generate moving-window correlations in time series or panel

mvcorrtsvar1 tsvar2[ifexp] [inrange],generate(newvar)window(#)[endforce]

mvcorris for use with time-series data. You musttssetyour data before usingmvcorr; see help tsset.

varnamemay contain time-series operators; see help varlist.

Description

mvcorrcomputes a moving-window correlation coefficient fortsvar1andtsvar2which must be time series variables under the aegis oftsset. If a panel calendar is in effect, the coefficient is calculated for each time series within the panel. The correlation coefficient is placed in a new variable, specified with thegenerate()option. Althoughmvcorrworks with unbalanced panels (where the start and/or end points differ across units),mvcorrdoes not allow gaps within the observations of a time series; that is, the value of an observation for a given period may be missing, but the observation itself must be defined. Gaps in time series may be dealt with via thetsfillcommand.By using time-series operators,

mvcorrmay also be used to generate moving autocorrelations.

Optionsgenerate(newvar)specifies the name of a new variable in which the results are to be placed. This is a required option.

window(#)specifies the width of the window for computation of the statistics, which must be an integer and at least 3. By default, results for odd-length windows are placed in the middle of the window and results for even-length windows are placed at the end of the window. The first default can be over-ridden by theendoption. This is a required option.

endforces results to be placed at the end of the window in the case where the window width is an odd number.

forceforces results to be computed when some of a particular window's values are missing.

RemarksOccasionally people want to use

ifand/orinwhen calculating moving correlations, but that raises a complication not usually encountered. What would you expect from a moving correlation calculated with either kind of restriction? Let us identify two possibilities:Weak interpretation: I don't want to see any results for the excluded observations.

Strong interpretation: I don't even want you to use the values for the excluded observations.

Here is a concrete example. Suppose as a consequence of some restriction, observations 1-42 are included, but not observations 43 on. But the moving correlation for 42 will depend, among other things, on the value for observation 43 if the summary extends backwards and forwards and is of length at least 3, and it will similarly depend on some of the observations 44 onwards in some circumstances.

Our guess is that most people would go for the weak interpretation, which is employed in

mvcorr. If not, you should ignore what you don't want or even set unwanted values to missing afterwards by usingreplace.

Examples. webuse grunfeld . mvcorr invest mvalue, win(5) gen(rho)

. mvcorr invest L.invest, win(5) gen(acf) end

AuthorsChristopher F Baum, Boston College, USA baum@bc.edu

Nicholas J. Cox, Durham University, U.K. n.j.cox@durham.ac.uk

AcknowledgementsThis routine is based on Cox's

movsummand the authors'mvsummandstatsmat. Its development was encouraged by a query from Nathalie Carcenac.

Also seeOn-line: correlate, tsset, tsfill