Announcement

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

  • fitted lines after qreg?

    Dear All, Given the following data (taken from https://cran.r-project.org/web/packa...gnettes/rq.pdf)
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int id float(income foodexp)
      1 420.15765 255.83943
      2  541.4117  310.9587
      3  901.1575    485.68
      4  639.0802  402.9973
      5  750.8756  495.5608
      6   945.799  633.7978
      7  829.3979  630.7566
      8  979.1649  700.4409
      9  1309.879  830.9586
     10 1492.3988  815.3602
     11   502.839  338.0014
     12  616.7169  412.3613
     13  790.9225  520.0006
     14 555.87866  452.4015
     15  713.4412 512.72003
     16  838.7561  658.8395
     17 535.07666  392.5995
     18  596.4408  443.5586
     19  924.5619  640.1164
     20  487.7583  333.8394
     21  692.6397  466.9583
     22   997.877  543.3969
     23  506.9995 317.71985
     24  654.1587  424.3209
     25  933.9193  518.9617
     26  433.6813  338.0014
     27  587.5962  419.6412
     28  896.4746    476.32
     29  454.4782  386.3602
     30  584.9989 423.27835
     31   800.799  503.3572
     32  502.4369  354.6389
     33  713.5197  497.3182
     34  906.0006  588.5195
     35  880.5969  654.5972
     36  796.8289  550.7274
     37   854.879 528.37695
     38 1167.3716  640.4813
     39 523.80005  401.3203
     40  670.7792   435.999
     41  377.0584  276.5606
     42   851.543  588.3488
     43 1121.0938  664.1978
     44  625.5179  444.8602
     45  805.5377  462.8995
     46  558.5812  377.7792
     47  884.4005 553.15045
     48  1257.499  810.8962
     49  2051.179 1067.9541
     50  1466.333 1049.8788
     51  730.0989 522.70123
     52   800.799  572.0807
     53 1245.6964   907.397
     54 1201.0002  811.5776
     55  634.4002  427.7975
     56  956.2314  649.9985
     57 1148.6011  860.6002
     58 1768.8236 1143.4211
     59  2822.533 2032.6792
     60  922.3549 590.61835
     61  2293.192  1570.391
     62  627.4726    483.48
     63  889.9809  600.4804
     64    1162.2  696.2021
     65 1197.0793  774.7962
     66  530.7972  390.5984
     67 1142.1526  612.5619
     68 1088.0039  708.7621
     69  484.6612  296.9192
     70   1536.02 1071.4626
     71  678.8974  496.5976
     72  671.8802  503.3974
     73  690.4683  357.6411
     74  860.6948  430.3376
     75  873.3095   624.699
     76  894.4598 582.54126
     77  1148.647 580.22156
     78  926.8762 543.88074
     79  839.0414  588.6372
     80  829.4974  627.9999
     81 1264.0043  712.1012
     82  1937.977   968.395
     83  698.8317  482.5816
     84  920.4199  593.1694
     85  1897.571 1033.5658
     86  891.6824  693.6794
     87  889.6783  693.6794
     88 1221.4818  761.2791
     89  544.5991  361.3981
     90 1031.4491  628.4522
     91 1462.9497  771.4485
     92  830.4353  757.1187
     93  975.0414   821.597
     94 1337.9983 1022.3202
     95  867.6427  679.4407
     96  725.7459 538.74915
     97  989.0056  679.9981
     98 1525.0005  977.0033
     99   672.196  561.2015
    100  923.3977  728.3997
    101  472.3215  372.3186
    102  590.7601   361.521
    103  940.9218  517.9196
    104  643.3571  459.8177
    105 2551.6616  863.9199
    106 1795.3226  831.4407
    107 1165.7734 534.76105
    108  815.6212  392.0502
    109 1264.2065  934.9752
    110 1095.4056  813.3081
    111  447.4478    263.71
    112 1178.9741  769.0839
    113  975.8023  630.5863
    114 1017.8522  645.9874
    115  423.8798  319.5584
    116 558.77673  348.4518
    117  943.2487  614.5068
    118 1348.3003  662.0096
    119 2340.6174 1504.3707
    120 587.17914   406.218
    121  1540.974  692.1689
    122  1115.848   588.137
    123 1044.6843  511.2609
    124  1389.793    700.56
    125  2497.786  1301.145
    126  1585.381   879.066
    127  1862.044  912.8851
    128 2008.8546  1509.781
    129  697.3099 484.06055
    130  571.2518  399.6703
    131  598.3465  444.1001
    132  461.0977  248.8101
    133  977.1107 527.80133
    134  883.9849 500.63135
    135  718.3594  436.8107
    136 543.89703   374.799
    137  1587.348  726.3922
    138  4957.813    1827.2
    139  969.6838  523.4911
    140   419.998  334.9998
    141 561.99896  473.2009
    142  689.5988 581.20294
    143 1398.5203   929.754
    144  820.8168  591.1974
    145  875.1716  637.5483
    146   1392.45  674.9509
    147 1256.3174  776.7589
    148  1362.859   959.517
    149 1999.2552 1250.9644
    150  1209.473  737.8201
    151 1125.0356  810.6772
    152  1827.401  983.0009
    153  1014.154  708.8969
    154  880.3944    633.12
    155  2432.391 1424.8047
    156 1177.8547  830.9586
    157  1222.594  925.5795
    158  1519.581 1162.0024
    159  687.6638   383.458
    160  953.1192  621.1173
    161  953.1192  621.1173
    162  953.1192  621.1173
    163  939.0418  548.6002
    164 1283.4025  745.2353
    165  1511.579  837.8005
    166  1342.582  795.3403
    167   511.798  418.5976
    168  689.7988  508.7975
    169 1532.3074   883.278
    170 1056.0808  742.5276
    171  387.3195  242.3202
    172  387.3195  242.3202
    173  410.9987 266.00098
    174  832.7554  614.7588
    175  614.9986  385.3184
    176  887.4658    515.62
    177 1024.8176  708.4787
    178 1006.4354  734.2357
    179       726   433.001
    180  494.4174  327.4188
    181  748.6414 429.03995
    182  987.6417  619.6408
    183  788.0961   400.799
    184  831.7983  620.8007
    185 1139.4945  819.9965
    186  507.5169   360.878
    187  576.1972  395.7608
    188  696.5991  442.0001
    189   650.818  404.0384
    190  949.5802  670.7993
    191  497.1193 297.57016
    192  570.1674  353.4882
    193  724.7306  383.9376
    194  408.3399  284.8008
    195  638.6713     431.1
    196  1225.789  801.3517
    197  715.3701  448.4513
    198  800.4708  577.9111
    199  975.5974   570.521
    200 1613.7566  865.3206
    201  608.5019  444.5578
    202  958.6634  680.4198
    203  835.9426  576.2779
    204  873.7375  631.7982
    205  951.4432 608.64185
    206  473.0022  300.9999
    207 601.00305  377.9984
    208  713.9979  397.0015
    209  829.2983  588.5195
    210  959.7953  681.7616
    211 1212.9613  807.3603
    212  958.8743  696.8011
    213 1129.4431  811.1962
    214  1943.042   1305.72
    215  539.6388  442.0001
    216   463.599  353.6013
    217    562.64  468.0008
    218  736.7584  526.7573
    219  1415.446   890.239
    220 2208.7898 1318.8032
    221  636.0009 331.00055
    222   759.401  416.4015
    223 1078.8383  596.8406
    224   499.751  408.4992
    225 1020.0225  775.0209
    226  1595.161  1138.162
    227  776.5958  485.5198
    228 1230.9236  772.7612
    229  1807.952   993.963
    230  415.4407   305.439
    231  440.5174  306.5191
    232  541.2006 299.19934
    233 581.35986  468.0008
    234  743.0773  522.6019
    235 1057.6768  750.3202
    end
    How can I draw graph like this?
    Click image for larger version

Name:	engel.png
Views:	1
Size:	52.3 KB
ID:	1482912


    We can run the following regressions:
    Code:
    // OLS
    reg foodexp income
    // Quantile = 0.05
    qreg foodexp income, q(0.05)
    // Quantile = 0.1
    qreg foodexp income, q(0.1)
    // Quantile = 0.25
    qreg foodexp income, q(0.25)
    // Quantile = 0.5 (Median)
    qreg foodexp income, q(0.5)
    // Quantile = 0.75
    qreg foodexp income, q(0.75)
    // Quantile = 0.95
    qreg foodexp income, q(0.95)
    But, how to graph them in one figure? Thanks.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    Code:
    regress foodexp income
    predict xb_ols
    label var xb_ols "Least Squares"
    
    foreach q of numlist 5 10 25 50 75 95 {
        qreg foodexp income, q(`=`q'/100')
        predict xb_q`q'
        label var xb_q`q' "`q'th percentile"
    }
    
    graph twoway line xb_* income, sort || scatter foodexp income
    will get you started. Add options to the -graph- command to style the graph to your liking.

    Comment


    • #3
      Dear Clyde, Many thanks. It really helps.
      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment

      Working...
      X