Announcement

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

  • Ordering Matrix Columns based on a numerical stub

    I want to compare how well my estimator performs when I change intervention dates. The numerical stubs at the end represent how many periods before the real intervention the model estimated it with. So, 0 = no change in the intervention date (the baseline), cf_itp1 = t-1 and so on and so forth. I have two questions.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int(year gdp7) double normalcf byte relative_0 float diff_0 double cf_itp1 float diff_1 double cf_itp11 float diff_11 double cf_itp2 float diff_2 double cf_itp3 float diff_3
    1960  2284 2344.5752805835496 -30  -60.57528 2276.6414734765826   7.358527  2244.851951782911   39.14805 2285.5111621388987  -1.511162 2149.4583128288814  134.54169
    1961  2388  2410.555300400853 -29   -22.5553  2379.372251968636  8.6277485  2351.595825171148  36.404175 2383.9300873391794  4.0699124 2278.4425831244616  109.55742
    1962  2527  2565.170095355413 -28  -38.17009 2523.9075950755077   3.092405  2499.557629867715   27.44237 2529.0939672926684 -2.0939672 2413.2098552263424  113.79015
    1963  2610 2680.1819865730668 -27 -70.181984  2634.576459501778  -24.57646 2611.5858534236877 -1.5858535   2639.64795097254  -29.64795 2544.9506394299456   65.04936
    1964  2806 2850.2205099133694 -26  -44.22051 2810.8892336840795  -4.889234 2782.7987988582872    23.2012  2815.483727738153  -9.483727 2709.1714691637803   96.82853
    1965  3005 3049.2355450697996 -25  -44.23555 2951.4899655536365   53.51003 2950.8411341922456   54.15887  2961.716943257398   43.28306 2856.6111149280327  148.38889
    1966  3168 3294.6976320281647 -24 -126.69763  3114.825753888938   53.17425  3164.328880968721   3.671119 3132.8916867840503  35.108315 3102.5433034131743  65.456696
    1967  3241  3457.126790346017 -23  -216.1268  3284.359821580163  -43.35982 3361.9635772419924 -120.96358 3301.6066990840786   -60.6067 3290.4571031158825   -49.4571
    1968  3571  3748.937513372169 -22  -177.9375  3542.394351675149   28.60565  3624.067345561626  -53.06734  3562.659916826559   8.340083 3608.4215291249393 -37.421528
    1969  3998 4053.9835620080307 -21  -55.98356  3907.673715416546   90.32629 4012.9955179068884 -14.995518  3921.212088315499   76.78791 4000.5904314458885 -2.5904315
    1970  4367  4313.265847486722 -20   53.73415  4401.813433075546 -34.813435   4357.53514884318   9.464851 4391.6079879405515  -24.60799  4413.008847918387  -46.00885
    1971  4686 4643.6328845354155 -19   42.36712  4752.889042894377 -66.889046   4723.21218045041  -37.21218  4740.357799935831   -54.3578  4762.926313247855  -76.92632
    1972  5055  5067.659012151861 -18 -12.659012  5161.752129468419 -106.75213  5074.093031587983  -19.09303  5149.951690490311  -94.95169  5222.220245633824 -167.22025
    1973  5553  5627.864741147243 -17  -74.86474  5722.387213375465  -169.3872  5573.992557455671  -20.99256  5709.864705226496  -156.8647  5771.458608645211  -218.4586
    1974  6074 6138.4684340114745 -16  -64.46844 6412.3676527099515  -338.3676  6193.318397114159  -119.3184  6380.649853915731  -306.6498  6334.559299155217  -260.5593
    1975  6603  6637.413276120334 -15 -34.413277  6882.521933316244 -279.52194  6703.786623317315 -100.78662  6852.977933755327 -249.97794  6891.695416274375  -288.6954
    1976  7367  7334.825054463951 -14  32.174946  7631.665659047329 -264.66565  7333.681163311273  33.318836 7597.5728634522075 -230.57286  7506.109068892885 -139.10907
    1977  8090  8023.515936373996 -13   66.48406  8153.018577950277  -63.01858  7987.883714076809  102.11629  8135.297305555689  -45.29731   8216.44365217744 -126.44365
    1978  8928   8924.21961297197 -12   3.780387  8944.156736328703 -16.156736  8768.910668867975  159.08932  8938.310238176047 -10.310238  8923.804894679817  4.1951056
    1979 10067   9854.24671087441 -11   212.7533  9867.700283977521   199.2997  9738.060489630489   328.9395  9863.621247425095  203.37875  9932.886355933126  134.11365
    1980 11083 10760.977180535365 -10   322.0228 11141.965284327745  -58.96528 10734.688931265224   348.3111  11101.18320101278   -18.1832 10931.560108737493   151.4399
    1981 12115 11892.800173885393  -9  222.19983 12140.762754003626 -25.762754 11831.720026086914  283.27997 12113.663253502547  1.3367465 11983.711457731633  131.28854
    1982 12761 12369.192922761604  -8   391.8071 12904.054159671497 -143.05415 12825.177857621007  -64.17786 12847.210427423932  -86.21043 12798.603889107393  -37.60389
    1983 13519  13211.47763314891  -7   307.5224 13474.799504878494    44.2005 13511.086509392951   7.913491 13445.040541424662   73.95946 13575.435683753494  -56.43568
    1984 14481 14423.075860809462  -6   57.92414 14345.006666775525  135.99333 14173.744020426862    307.256 14350.900560531225  130.09944 14334.611230691844  146.38876
    1985 15291  15282.67629347721  -5   8.323707 15027.740612064397   263.2594 14808.922910588553   482.0771 15051.400245391114  239.59976 15193.558192843635   97.44181
    1986 15998 15993.358177955923  -4   4.641822 15632.242746025708   365.7573 15402.883869441917  595.11615 15665.717854077833   332.2821 15889.550392775038   108.4496
    1987 16679 16851.079161089652  -3 -172.07916  16406.77723027094  272.22278 16070.825730643563  608.17426 16445.918061013603  233.08194 16781.647186953443 -102.64719
    1988 17786 18042.316421826057  -2 -256.31644 17671.247758017216  114.75224  17233.38276754516  552.61725  17701.79093996754   84.20906 18094.165480361087  -308.1655
    1989 18994  19248.25044872261  -1 -254.25044 18878.848179305558  115.15182 18451.953916224746   542.0461 18907.035397257958    86.9646 19476.562770419605  -482.5628
    1990 20465  20254.86643544301   0  210.13356 20109.221533872013   355.7785 19562.724321482547   902.2757  20112.77665979149   352.2233  20894.71509092117  -429.7151
    1991 21602 20818.459230348755   1   783.5408  21051.03478268218   550.9652 20383.912337548496  1218.0876  21014.06579691887   587.9342 22010.935341014898  -408.9353
    1992 22154 21654.308331137978   2   499.6917 21777.687919138658   376.3121 21150.143111870002  1003.8569  21752.09566467736   401.9043  22755.83559173263  -601.8356
    1993 21878 22290.622004675584   3   -412.622 22020.336194965592  -142.3362 21334.038497707166   543.9615 22035.062829178958 -157.06284 23169.077940390824  -1291.078
    1994 22371 23394.456620703248   4 -1023.4566 23120.569794816485  -749.5698  22153.11277370602   217.8872 23135.977882583473  -764.9779 24080.725681755328 -1709.7257
    1995 23035  24245.63418341099   5 -1210.6342 24074.757464654263 -1039.7574 23051.452302220892 -16.452303 24078.509289104673 -1043.5093 24993.984456863047 -1958.9845
    1996 23742 25206.203243412278   6 -1464.2032  24590.19690625007  -848.1969 23836.706737146244  -94.70673 24636.203034690177  -894.2031 26135.287140408273  -2393.287
    1997 24156 26368.449542536662   7 -2212.4495  25404.94813975198  -1248.948 24980.240113443997  -824.2401 25484.671177607506  -1328.671  26978.63669790658  -2822.637
    1998 24931 27441.339580822205   8 -2510.3396  26225.51988298099   -1294.52 26081.498688399875 -1150.4987 26326.195756118213 -1395.1958  27785.17650185547 -2854.1765
    1999 25755 28673.510595792643   9 -2918.5105 26945.020476190006 -1190.0205 26820.250841849163 -1065.2509 27090.345552552215 -1335.3456  28968.29728223864  -3213.297
    2000 26943 30217.040951444593  10  -3274.041 28860.321105877192  -1917.321 28489.048624819734 -1546.0486 28966.775677303256 -2023.7756 30663.573323168766  -3720.573
    2001 27449 31105.584968837804  11  -3656.585 30257.826641488027  -2808.827   29918.9937802087  -2469.994 30309.359322785338 -2860.3594  31531.88069785482 -4082.8806
    2002 28348  32076.66496251002  12  -3728.665   31621.8036247985  -3273.804  30749.69362657657 -2401.6936 31627.399364777542 -3279.3994 32719.485593028978 -4371.4854
    2003 28855  33222.11056743127  13 -4367.1104  32453.79060907304 -3598.7905 31152.361109357942  -2297.361  32486.67075222959  -3631.671  33845.04974355354   -4990.05
    end
    format %ty year
    label var year "Year" 
    label var gdp7 "Real West Germany" 
    label var normalcf "SCUL West Germany, 1990" 
    label var cf_itp1 "SCUL West Germany, 1989" 
    label var cf_itp11 "SCUL West Germany, 1979" 
    label var cf_itp2 "SCUL West Germany, 1988" 
    label var cf_itp3 "SCUL West Germany, 1987" 
    cls
    
    mkmat gdp if relative < 0, mat(treat)
    
    
    mata A=J(0,1,.)
    tempname e123
    
    foreach x of var *cf* {
    
    mkmat `x' if relative < 0, mat(cont)
    
    mat loss = (treat - cont)' * (treat - cont)
    
    mat loss = loss'/ rowsof(treat)
    
    mata: X = round(sqrt(st_matrix("loss")),.000000001)
    
    mata: st_matrix("loss", X)
    
    loc err =loss[1,1]
    
     matrix `e123'=`err'
    
     mata: A=A\st_matrix("`e123'")
    }
    cls
    mata: st_matrix("B",A)
    
    mata C="RMSE"
    mata C=C,J(1,1," ")
    mata st_matrix("B",A)
    mata st_matrixcolstripe("B",C)
    
    mat l B
    First: Row 3 is 261.55226, or the RMSE for the largest backdate (cf_itp11). Thus, I want this one to be last, since it's the largest backdate that I do. How might I sort the matrix by the numerical suffixes, such that we've got the RMSPE's for 0, 1, 2, 3, 11 in order of each other?

    Additionally.... I use a loop to feed each cf variable to the matrix. Would there be a way to do this in Mata, such that I don't need to loop over each cf variable, like
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int(year gdp7) double normalcf byte relative_0 float diff_0 double cf_itp1 float diff_1 double cf_itp11 float diff_11 double cf_itp2 float diff_2 double cf_itp3 float diff_3
    1960  2284 2344.5752805835496 -30  -60.57528 2276.6414734765826   7.358527  2244.851951782911   39.14805 2285.5111621388987  -1.511162 2149.4583128288814  134.54169
    1961  2388  2410.555300400853 -29   -22.5553  2379.372251968636  8.6277485  2351.595825171148  36.404175 2383.9300873391794  4.0699124 2278.4425831244616  109.55742
    1962  2527  2565.170095355413 -28  -38.17009 2523.9075950755077   3.092405  2499.557629867715   27.44237 2529.0939672926684 -2.0939672 2413.2098552263424  113.79015
    1963  2610 2680.1819865730668 -27 -70.181984  2634.576459501778  -24.57646 2611.5858534236877 -1.5858535   2639.64795097254  -29.64795 2544.9506394299456   65.04936
    1964  2806 2850.2205099133694 -26  -44.22051 2810.8892336840795  -4.889234 2782.7987988582872    23.2012  2815.483727738153  -9.483727 2709.1714691637803   96.82853
    1965  3005 3049.2355450697996 -25  -44.23555 2951.4899655536365   53.51003 2950.8411341922456   54.15887  2961.716943257398   43.28306 2856.6111149280327  148.38889
    1966  3168 3294.6976320281647 -24 -126.69763  3114.825753888938   53.17425  3164.328880968721   3.671119 3132.8916867840503  35.108315 3102.5433034131743  65.456696
    1967  3241  3457.126790346017 -23  -216.1268  3284.359821580163  -43.35982 3361.9635772419924 -120.96358 3301.6066990840786   -60.6067 3290.4571031158825   -49.4571
    1968  3571  3748.937513372169 -22  -177.9375  3542.394351675149   28.60565  3624.067345561626  -53.06734  3562.659916826559   8.340083 3608.4215291249393 -37.421528
    1969  3998 4053.9835620080307 -21  -55.98356  3907.673715416546   90.32629 4012.9955179068884 -14.995518  3921.212088315499   76.78791 4000.5904314458885 -2.5904315
    1970  4367  4313.265847486722 -20   53.73415  4401.813433075546 -34.813435   4357.53514884318   9.464851 4391.6079879405515  -24.60799  4413.008847918387  -46.00885
    1971  4686 4643.6328845354155 -19   42.36712  4752.889042894377 -66.889046   4723.21218045041  -37.21218  4740.357799935831   -54.3578  4762.926313247855  -76.92632
    1972  5055  5067.659012151861 -18 -12.659012  5161.752129468419 -106.75213  5074.093031587983  -19.09303  5149.951690490311  -94.95169  5222.220245633824 -167.22025
    1973  5553  5627.864741147243 -17  -74.86474  5722.387213375465  -169.3872  5573.992557455671  -20.99256  5709.864705226496  -156.8647  5771.458608645211  -218.4586
    1974  6074 6138.4684340114745 -16  -64.46844 6412.3676527099515  -338.3676  6193.318397114159  -119.3184  6380.649853915731  -306.6498  6334.559299155217  -260.5593
    1975  6603  6637.413276120334 -15 -34.413277  6882.521933316244 -279.52194  6703.786623317315 -100.78662  6852.977933755327 -249.97794  6891.695416274375  -288.6954
    1976  7367  7334.825054463951 -14  32.174946  7631.665659047329 -264.66565  7333.681163311273  33.318836 7597.5728634522075 -230.57286  7506.109068892885 -139.10907
    1977  8090  8023.515936373996 -13   66.48406  8153.018577950277  -63.01858  7987.883714076809  102.11629  8135.297305555689  -45.29731   8216.44365217744 -126.44365
    1978  8928   8924.21961297197 -12   3.780387  8944.156736328703 -16.156736  8768.910668867975  159.08932  8938.310238176047 -10.310238  8923.804894679817  4.1951056
    1979 10067   9854.24671087441 -11   212.7533  9867.700283977521   199.2997  9738.060489630489   328.9395  9863.621247425095  203.37875  9932.886355933126  134.11365
    1980 11083 10760.977180535365 -10   322.0228 11141.965284327745  -58.96528 10734.688931265224   348.3111  11101.18320101278   -18.1832 10931.560108737493   151.4399
    1981 12115 11892.800173885393  -9  222.19983 12140.762754003626 -25.762754 11831.720026086914  283.27997 12113.663253502547  1.3367465 11983.711457731633  131.28854
    1982 12761 12369.192922761604  -8   391.8071 12904.054159671497 -143.05415 12825.177857621007  -64.17786 12847.210427423932  -86.21043 12798.603889107393  -37.60389
    1983 13519  13211.47763314891  -7   307.5224 13474.799504878494    44.2005 13511.086509392951   7.913491 13445.040541424662   73.95946 13575.435683753494  -56.43568
    1984 14481 14423.075860809462  -6   57.92414 14345.006666775525  135.99333 14173.744020426862    307.256 14350.900560531225  130.09944 14334.611230691844  146.38876
    1985 15291  15282.67629347721  -5   8.323707 15027.740612064397   263.2594 14808.922910588553   482.0771 15051.400245391114  239.59976 15193.558192843635   97.44181
    1986 15998 15993.358177955923  -4   4.641822 15632.242746025708   365.7573 15402.883869441917  595.11615 15665.717854077833   332.2821 15889.550392775038   108.4496
    1987 16679 16851.079161089652  -3 -172.07916  16406.77723027094  272.22278 16070.825730643563  608.17426 16445.918061013603  233.08194 16781.647186953443 -102.64719
    1988 17786 18042.316421826057  -2 -256.31644 17671.247758017216  114.75224  17233.38276754516  552.61725  17701.79093996754   84.20906 18094.165480361087  -308.1655
    1989 18994  19248.25044872261  -1 -254.25044 18878.848179305558  115.15182 18451.953916224746   542.0461 18907.035397257958    86.9646 19476.562770419605  -482.5628
    1990 20465  20254.86643544301   0  210.13356 20109.221533872013   355.7785 19562.724321482547   902.2757  20112.77665979149   352.2233  20894.71509092117  -429.7151
    1991 21602 20818.459230348755   1   783.5408  21051.03478268218   550.9652 20383.912337548496  1218.0876  21014.06579691887   587.9342 22010.935341014898  -408.9353
    1992 22154 21654.308331137978   2   499.6917 21777.687919138658   376.3121 21150.143111870002  1003.8569  21752.09566467736   401.9043  22755.83559173263  -601.8356
    1993 21878 22290.622004675584   3   -412.622 22020.336194965592  -142.3362 21334.038497707166   543.9615 22035.062829178958 -157.06284 23169.077940390824  -1291.078
    1994 22371 23394.456620703248   4 -1023.4566 23120.569794816485  -749.5698  22153.11277370602   217.8872 23135.977882583473  -764.9779 24080.725681755328 -1709.7257
    1995 23035  24245.63418341099   5 -1210.6342 24074.757464654263 -1039.7574 23051.452302220892 -16.452303 24078.509289104673 -1043.5093 24993.984456863047 -1958.9845
    1996 23742 25206.203243412278   6 -1464.2032  24590.19690625007  -848.1969 23836.706737146244  -94.70673 24636.203034690177  -894.2031 26135.287140408273  -2393.287
    1997 24156 26368.449542536662   7 -2212.4495  25404.94813975198  -1248.948 24980.240113443997  -824.2401 25484.671177607506  -1328.671  26978.63669790658  -2822.637
    1998 24931 27441.339580822205   8 -2510.3396  26225.51988298099   -1294.52 26081.498688399875 -1150.4987 26326.195756118213 -1395.1958  27785.17650185547 -2854.1765
    1999 25755 28673.510595792643   9 -2918.5105 26945.020476190006 -1190.0205 26820.250841849163 -1065.2509 27090.345552552215 -1335.3456  28968.29728223864  -3213.297
    2000 26943 30217.040951444593  10  -3274.041 28860.321105877192  -1917.321 28489.048624819734 -1546.0486 28966.775677303256 -2023.7756 30663.573323168766  -3720.573
    2001 27449 31105.584968837804  11  -3656.585 30257.826641488027  -2808.827   29918.9937802087  -2469.994 30309.359322785338 -2860.3594  31531.88069785482 -4082.8806
    2002 28348  32076.66496251002  12  -3728.665   31621.8036247985  -3273.804  30749.69362657657 -2401.6936 31627.399364777542 -3279.3994 32719.485593028978 -4371.4854
    2003 28855  33222.11056743127  13 -4367.1104  32453.79060907304 -3598.7905 31152.361109357942  -2297.361  32486.67075222959  -3631.671  33845.04974355354   -4990.05
    end
    format %ty year
    label var year "Year" 
    label var gdp7 "Real West Germany" 
    label var normalcf "SCUL West Germany, 1990" 
    label var cf_itp1 "SCUL West Germany, 1989" 
    label var cf_itp11 "SCUL West Germany, 1979" 
    label var cf_itp2 "SCUL West Germany, 1988" 
    label var cf_itp3 "SCUL West Germany, 1987" 
    cls
    
    mkmat gdp if relative < 0, mat(treat)
    
    mkmat *cf* if relative < 0, mat(cont)
    
    mat loss = (treat - cont)' * (treat - cont)
    
    mat loss = loss'/ rowsof(treat)
    
    mata: X = round(sqrt(st_matrix("loss")),.000000001)
    
    mata: st_matrix("loss", X)
    
    loc err =loss[1,1]
    
     matrix `e123'=`err'
    
     mata: A=A\st_matrix("`e123'")
    My goal would be to have the numerical suffix as the row names (0, 1, 2, 3, 11) and the column be its respective RMSPE. How might I do this?
Working...
X