help for mvcorr

Generate moving-window correlations in time series or panel

mvcorr tsvar1 tsvar2 [if exp] [in range] , generate(newvar) window(#) [ end force ]

mvcorr is for use with time-series data. You must tsset your data before using mvcorr; see help tsset.

varname may contain time-series operators; see help varlist.


mvcorr computes a moving-window correlation coefficient for tsvar1 and tsvar2 which must be time series variables under the aegis of tsset. 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 the generate() option. Although mvcorr works with unbalanced panels (where the start and/or end points differ across units), mvcorr does 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 the tsfill command.

By using time-series operators, mvcorr may also be used to generate moving autocorrelations.

Options generate(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 the end option. This is a required option.

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

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


Occasionally people want to use if and/or in when 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 using replace.


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

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


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

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


This routine is based on Cox's movsumm and the authors' mvsumm and statsmat. Its development was encouraged by a query from Nathalie Carcenac.

Also see

On-line: correlate, tsset, tsfill