Announcement

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

  • use margins command to predict y when x is not a factor variable

    sysuse auto
    reg price weight rep78


    I want to calculate predicted price for each observation with rep78 equal to the mean and weight equal to the actual value, and make the predicted price a new variable called yhat.
    for example, observation 1: weight=2930, rep78=3 (the mean rep78 is 3.405797 for the whole sample). The predicted price for the first observation is calculated based on weight=2930, rep78=3.405797.

    I tried "margins weight, atmeans" which reports error, because weight should be a factor variable instead, rather than continuous variable as it is.
    I tried "margins, at((asobs) weight) atmeans", but the predicted price is based on weight equal to its mean, which is not what I want.

    What's more, I don't know how to generate a new variable yhat equal to the predicted price.

    Thanks a lot!


  • #2
    Code:
    sysuse auto, clear
    reg price weight rep78
    levelsof weight if e(sample), local(wlevels)
    sum rep78 if e(sample)
    margins, at(rep78=(`r(mean)') weight=(`wlevels'))
    *IN THIS CASE, EQUIVALENT TO
    sum rep78 if e(sample)
    margins, at(rep78=(`r(mean)')) over(weight)
    Res.:

    Code:
    . margins, at(rep78=(`r(mean)') weight=(`wlevels'))
    
    Adjusted predictions                            Number of obs     =         69
    Model VCE    : OLS
    
    Expression   : Linear prediction, predict()
    
    1._at        : weight          =        1760
                   rep78           =    3.405797
    
    2._at        : weight          =        1800
                   rep78           =    3.405797
    
    3._at        : weight          =        1830
                   rep78           =    3.405797
    
    4._at        : weight          =        1930
                   rep78           =    3.405797
    
    5._at        : weight          =        1980
                   rep78           =    3.405797
    
    6._at        : weight          =        1990
                   rep78           =    3.405797
    
    7._at        : weight          =        2020
                   rep78           =    3.405797
    
    8._at        : weight          =        2040
                   rep78           =    3.405797
    
    9._at        : weight          =        2050
                   rep78           =    3.405797
    
    10._at       : weight          =        2070
                   rep78           =    3.405797
    
    11._at       : weight          =        2110
                   rep78           =    3.405797
    
    12._at       : weight          =        2120
                   rep78           =    3.405797
    
    13._at       : weight          =        2130
                   rep78           =    3.405797
    
    14._at       : weight          =        2160
                   rep78           =    3.405797
    
    15._at       : weight          =        2200
                   rep78           =    3.405797
    
    16._at       : weight          =        2240
                   rep78           =    3.405797
    
    17._at       : weight          =        2280
                   rep78           =    3.405797
    
    18._at       : weight          =        2370
                   rep78           =    3.405797
    
    19._at       : weight          =        2410
                   rep78           =    3.405797
    
    20._at       : weight          =        2580
                   rep78           =    3.405797
    
    21._at       : weight          =        2650
                   rep78           =    3.405797
    
    22._at       : weight          =        2670
                   rep78           =    3.405797
    
    23._at       : weight          =        2690
                   rep78           =    3.405797
    
    24._at       : weight          =        2730
                   rep78           =    3.405797
    
    25._at       : weight          =        2750
                   rep78           =    3.405797
    
    26._at       : weight          =        2830
                   rep78           =    3.405797
    
    27._at       : weight          =        2930
                   rep78           =    3.405797
    
    28._at       : weight          =        3170
                   rep78           =    3.405797
    
    29._at       : weight          =        3180
                   rep78           =    3.405797
    
    30._at       : weight          =        3200
                   rep78           =    3.405797
    
    31._at       : weight          =        3210
                   rep78           =    3.405797
    
    32._at       : weight          =        3220
                   rep78           =    3.405797
    
    33._at       : weight          =        3250
                   rep78           =    3.405797
    
    34._at       : weight          =        3260
                   rep78           =    3.405797
    
    35._at       : weight          =        3280
                   rep78           =    3.405797
    
    36._at       : weight          =        3300
                   rep78           =    3.405797
    
    37._at       : weight          =        3310
                   rep78           =    3.405797
    
    38._at       : weight          =        3330
                   rep78           =    3.405797
    
    39._at       : weight          =        3350
                   rep78           =    3.405797
    
    40._at       : weight          =        3370
                   rep78           =    3.405797
    
    41._at       : weight          =        3400
                   rep78           =    3.405797
    
    42._at       : weight          =        3430
                   rep78           =    3.405797
    
    43._at       : weight          =        3470
                   rep78           =    3.405797
    
    44._at       : weight          =        3600
                   rep78           =    3.405797
    
    45._at       : weight          =        3670
                   rep78           =    3.405797
    
    46._at       : weight          =        3690
                   rep78           =    3.405797
    
    47._at       : weight          =        3700
                   rep78           =    3.405797
    
    48._at       : weight          =        3720
                   rep78           =    3.405797
    
    49._at       : weight          =        3740
                   rep78           =    3.405797
    
    50._at       : weight          =        3830
                   rep78           =    3.405797
    
    51._at       : weight          =        3880
                   rep78           =    3.405797
    
    52._at       : weight          =        3900
                   rep78           =    3.405797
    
    53._at       : weight          =        4030
                   rep78           =    3.405797
    
    54._at       : weight          =        4060
                   rep78           =    3.405797
    
    55._at       : weight          =        4080
                   rep78           =    3.405797
    
    56._at       : weight          =        4130
                   rep78           =    3.405797
    
    57._at       : weight          =        4290
                   rep78           =    3.405797
    
    58._at       : weight          =        4330
                   rep78           =    3.405797
    
    59._at       : weight          =        4720
                   rep78           =    3.405797
    
    60._at       : weight          =        4840
                   rep78           =    3.405797
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             _at |
              1  |   3082.997    576.826     5.34   0.000     1931.327    4234.667
              2  |   3179.317    563.154     5.65   0.000     2054.944     4303.69
              3  |   3251.557   552.9735     5.88   0.000      2147.51    4355.604
              4  |   3492.357   519.5451     6.72   0.000     2455.052    4529.662
              5  |   3612.757   503.1579     7.18   0.000      2608.17    4617.344
              6  |   3636.837   499.9094     7.27   0.000     2638.736    4634.938
              7  |   3709.077   490.2251     7.57   0.000     2730.311    4687.843
              8  |   3757.237   483.8221     7.77   0.000     2791.255    4723.219
              9  |   3781.317   480.6371     7.87   0.000     2821.694     4740.94
             10  |   3829.477   474.3015     8.07   0.000     2882.504    4776.451
             11  |   3925.797   461.7738     8.50   0.000     3003.836    4847.758
             12  |   3949.877   458.6732     8.61   0.000     3034.107    4865.648
             13  |   3973.957   455.5857     8.72   0.000     3064.351    4883.563
             14  |   4046.197   446.4042     9.06   0.000     3154.923    4937.472
             15  |   4142.517   434.3618     9.54   0.000     3275.286    5009.749
             16  |   4238.837   422.5656    10.03   0.000     3395.158    5082.517
             17  |   4335.157   411.0366    10.55   0.000     3514.496    5155.818
             18  |   4551.877   386.1966    11.79   0.000     3780.811    5322.944
             19  |   4648.197   375.7066    12.37   0.000     3898.075     5398.32
             20  |   5057.557   335.7728    15.06   0.000     4387.165    5727.949
             21  |   5226.117   321.9603    16.23   0.000     4583.303    5868.932
             22  |   5274.277   318.3439    16.57   0.000     4638.683    5909.871
             23  |   5322.437   314.8836    16.90   0.000     4693.752    5951.123
             24  |   5418.757   308.4524    17.57   0.000     4802.912    6034.603
             25  |   5466.917   305.4918    17.90   0.000     4856.983    6076.852
             26  |   5659.558   295.4648    19.15   0.000     5069.643    6249.472
             27  |   5900.358   287.3467    20.53   0.000     5326.651    6474.064
             28  |   6478.278   289.6729    22.36   0.000     5899.927    7056.628
             29  |   6502.358   290.4399    22.39   0.000     5922.476     7082.24
             30  |   6550.518   292.1277    22.42   0.000     5967.266    7133.769
             31  |   6574.598   293.0476    22.44   0.000     5989.509    7159.686
             32  |   6598.678   294.0175    22.44   0.000     6011.653    7185.703
             33  |   6670.918   297.2227    22.44   0.000     6077.493    7264.342
             34  |   6694.998   298.3877    22.44   0.000     6099.247    7290.748
             35  |   6743.158   300.8595    22.41   0.000     6142.472    7343.843
             36  |   6791.318   303.5163    22.38   0.000     6185.328    7397.307
             37  |   6815.398   304.9125    22.35   0.000      6206.62    7424.175
             38  |   6863.558   307.8377    22.30   0.000      6248.94    7478.175
             39  |   6911.718   310.9356    22.23   0.000     6290.915    7532.521
             40  |   6959.878   314.2011    22.15   0.000     6332.555      7587.2
             41  |   7032.118   319.4022    22.02   0.000     6394.411    7669.825
             42  |   7104.358   324.9513    21.86   0.000     6455.572    7753.144
             43  |   7200.678    332.861    21.63   0.000     6536.099    7865.256
             44  |   7513.718   362.1401    20.75   0.000     6790.682    8236.754
             45  |   7682.278   379.8435    20.22   0.000     6923.896     8440.66
             46  |   7730.438   385.1159    20.07   0.000     6961.529    8499.347
             47  |   7754.518   387.7854    20.00   0.000     6980.279    8528.756
             48  |   7802.678   393.1887    19.84   0.000     7017.651    8587.704
             49  |   7850.838   398.6749    19.69   0.000     7054.858    8646.818
             50  |   8067.558   424.3015    19.01   0.000     7220.413    8914.703
             51  |   8187.958   439.1329    18.65   0.000     7311.201    9064.715
             52  |   8236.118   445.1718    18.50   0.000     7347.304    9124.932
             53  |   8549.158   485.7198    17.60   0.000     7579.387    9518.929
             54  |   8621.398   495.3597    17.40   0.000      7632.38    9610.416
             55  |   8669.558   501.8385    17.28   0.000     7667.605    9671.511
             56  |   8789.958   518.2065    16.96   0.000     7755.325    9824.591
             57  |   9175.238   572.0086    16.04   0.000     8033.186    10317.29
             58  |   9271.558   585.7497    15.83   0.000     8102.071    10441.05
             59  |   10210.68   724.0918    14.10   0.000     8764.982    11656.37
             60  |   10499.64   767.8462    13.67   0.000     8966.584    12032.69
    ------------------------------------------------------------------------------
    
    .

    What's more, I don't know how to generate a new variable yhat equal to the predicted price.

    Code:
    sysuse auto, clear
    reg price weight rep78
    predict yhat, xb
    See

    Code:
    help predict
    Last edited by Andrew Musau; 24 May 2022, 08:42.

    Comment


    • #3
      Cross-posted at https://stackoverflow.com/questions/...actor-variable

      Please note our policy on cross-posting, which is that you are asked to tell us about it. (The same is good practice anywhere, regardless of whether it is explicit policy.)

      Comment

      Working...
      X