Announcement

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

  • Correlation between two variables for every time unit

    Hello everyone, I am completely new to Stata and I am trying to run an analysis for my dissertation.

    I have daily data for two market indices, SPX500 and FTSEMIB, across a time period that goes from 2006 to 2008.
    I would like to plot the correlation coefficients between the two indices across this time period. I have tried running a loop in order to have the correlation between the indices at each time t, but I am not sure of the syntax I need to use.

    Ideally, I would like to have a new variable containing the correlation between the two at each time t. I hope my explanation was clear.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(FTSEMIB SPX500 t)
        19460    890.64   1
      19313.5    880.03   2
    19256.334  874.4067   3
        19177    868.07   4
        19349  874.2233   5
    19487.334  881.5967   6
    19530.666  892.5267   7
    19403.334    900.96   8
    19236.666    895.39   9
    19177.334    887.16  10
        19303  873.9633  11
    19560.666  884.1866  12
    19709.334  887.1667  13
        19588  899.2033  14
    18967.334    891.48  15
        18731  876.9966  16
    18669.334    864.01  17
    19047.334  854.9233  18
        18989  845.2534  19
    19274.334  853.7534  20
    19540.666    866.71  21
        19888  880.4366  22
    19859.334  865.6266  23
    19403.334    836.41  24
    19185.334  801.4267  25
        19064    786.35  26
    19592.666  806.0467  27
    19911.666  838.8167  28
    20343.666  861.0533  29
        20442  878.4434  30
    20490.666    878.96  31
        20450  887.5133  32
    21004.334  890.1533  33
    21565.666  916.3834  34
        21865    918.36  35
        22092  929.5467  36
        22485  954.4667  37
    22490.666    974.94  38
        22027  980.2667  39
        21253  963.0467  40
        20867  950.9767  41
        19954  941.5634  42
    19395.334  906.5067  43
    19199.666  888.7333  44
    20010.666  877.9333  45
    20677.666  893.8867  46
    21342.334    919.98  47
        21716  945.7433  48
        21900  960.3333  49
    21513.666    957.46  50
    21526.666  931.6067  51
    22135.334    950.76  52
        22778  969.7333  53
    22140.666    966.86  54
    21607.334  937.4966  55
    21486.666    931.36  56
        22591  963.6967  57
        23226 1012.6866  58
    24436.334 1050.7833  59
    24981.666 1090.1333  60
    25616.666   1124.86  61
    25489.666 1147.2367  62
    25671.334 1144.6067  63
    26162.334   1161.92  64
    26843.334 1176.1934  65
    27218.666 1202.6866  66
    27189.666 1194.4233  67
    27144.666 1193.7267  68
    27460.666 1216.7966  69
        26993 1222.8966  70
        26487 1205.9967  71
    26057.666 1192.1666  72
        26614   1187.56  73
    27431.334   1219.33  74
    27882.334   1231.15  75
        28126 1244.2633  76
    28089.666    1235.2  77
        28252 1241.4467  78
    28086.334   1244.87  79
    28076.334 1248.9767  80
        28287 1251.3733  81
        28834   1263.13  82
    29019.334 1278.4634  83
        28874   1287.03  84
    28457.334   1288.39  85
    28186.666 1284.6167  86
        27945 1273.3367  87
        27989   1276.85  88
        27916   1278.92  89
    27998.666 1281.4867  90
    28054.334 1272.9833  91
        28424 1273.2766  92
        28634 1279.4066  93
        28801 1285.7866  94
        28972   1289.45  95
    29231.666   1293.58  96
        29342 1297.0767  97
    29266.666 1289.2367  98
    29085.666   1283.86  99
    28973.666 1280.0466 100
    end
    Thank you for your help!

    Best,
    Irene

  • #2
    At each time t you have just one data point and so the correlation is indeterminate. So, what else do you seek? You can get moving correlations for moving windows, e.g. with rangestat from SSC.

    Comment


    • #3
      You are right Nick, thank you! So, if I wanted to create a one-month moving average of the correlation, how could I do that? Apologies but I am not familiar with that function.

      Comment


      • #4
        The rangestat command (not a function) is to be found on SSC and so must be installed with

        Code:
        ssc install rangestat
        That done you should consult the help

        Code:
        help rangestat
        For example, with your sample dataset and a decision that "one month" means 30 days, then this


        Code:
        rangestat (corr) FTSEMIB SPX500 , interval(t -29 0)
        is for a window of length 30 that ends with today. Here's a token plot of results for your example data. Evidently at the start of the series you need to wait 30 days before you have a full window.


        Click image for larger version

Name:	rangestat.png
Views:	1
Size:	21.3 KB
ID:	1630024

        Comment


        • #5
          That is perfect, thank you very much!

          Comment

          Working...
          X