Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Rolling window Spearman correlation

    Dear Stata Users,

    I want to generate a 5 year (min 3 years) rolling Spearman correlation. I have come with the following codes:

    Code:
    1.  rangestat (corr) x1 x2, int(fyear 0 -4) by(firm_id) 
    
    2. tsset firm_id fyear 
      rolling correlation=r(rho), window(5) saving(f2, replace): corr x1 x2
    The problem with these codes is that: 1. they dont allow to use minimum window (of 3 years) 2.For "rangestat" I am not sure if it is a spearman correlation 3. I have a huge dataset, so "rolling" is to slow.

    Can you suggest me any alternative way in terms of using minimum window requirement and speed of execution?

    Best regards,
    Alberto

  • #2
    The correlation in both cases will not be Spearman correlations. Even if your variables x1 x2 are ranks, they would need to re-ranked within the subset used to be correct for input to a Pearson correlation routine.

    Other way round, it is explicit in the help for rangestat (SSC, as you are asked to explain) that the correlation it produces may be (should be in cases of doubt) checked against the results of correlate, and in turn that command produces Pearson correlations.

    It is a general convention in statistics that correlation means Pearson correlation unless it is explicit that some other kind of correlation is meant.


    As often explained here, you can count the non-missing observations in any interval with rangestat and then decide which results to ignore. Perhaps your data are monthly, for example, in which case you might want to ignore results (set them to missing afterwards) if based on fewer than 36 observations.

    There can some puzzlement at arbitrary rules such as results being acceptable if based on 36 observations but not on 35.

    But if these are yearly data, as the syntax for rolling does imply, 3 observations is extraordinarily low for anything much.

    If you want Spearman correlation with rangestat, that is programmable in principle with a Mata function you write yourself. Otherwise rolling with a call to spearman is indicated. Again, you can save the number of observations used.

    Comment


    • #3
      Thank you for the reply! One clarification - Isnt the code below produce Spearman correlation?

      Code:
       
        rolling correlation=r(rho), window(5) saving(f2, replace): corr x1 x2

      Comment


      • #4
        Why do you think that produces Spearman correlations?

        As already explained, corr -- meaning correlate -- produces Pearson correlations.

        Comment

        Working...
        X