Announcement

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

  • Incorporating Parameters from Estimation Outputs into a Mathematical Formula automatically

    Hello,
    Could you assist me in creating a code that initially computes the means and standard deviations of SBA marks and Exam marks by School_Code, performs a regression of Raw_SBA marks on Exam Scores, and subsequently utilizes the means, standard deviations, and the regression coefficient in a mathematical formula to determine the moderated mark?
    The formula is "Moderated SBA Score = Overal_Mean_Raw_SBA + RegCoef(Sch_mean_Exam - Overal_Mean_Exam) + (Raw_SBA_Student - Sch_mean_SBA)(Sch_SD_Exam/Sch_SD_SBA)"

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float Indentification long School_Code int(Subject_Code Raw_SBA) float Exam
      1 10002 208 37 75
      2 10002 208 38 52
      3 10002 208 38 52
      4 10002 208 40 72
      5 10002 208 37 77
      6 10005 208 32 56
      7 10005 208 34 66
      8 10005 208 34 68
      9 10005 208 34 66
     10 10005 208 34 62
     11 10013 208 36 58
     12 10013 208 38 59
     13 10013 208 36 51
     14 10013 208 36 51
     15 10013 208 38 53
     16 10018 208 39 68
     17 10018 208 38 84
     18 10018 208 34 63
     19 10018 208 35 38
     20 10018 208 39 72
     21 10032 208 34 67
     22 10032 208 38 69
     23 10032 208 37 68
     24 10032 208 34 61
     25 10032 208 36 75
     26 10175 208 36 60
     27 10175 208  0  5
     28 10175 208 36 52
     29 10175 208 34 58
     30 10175 208 36 60
     31 10496 208 37 57
     32 10496 208 37 71
     33 10496 208 37 57
     34 10496 208 35 57
     35 10496 208 36 63
     36 10558 208  0 67
     37 10558 208  0 61
     38 10558 208  0 55
     39 10558 208  0 59
     40 10558 208  0 53
     41 10837 208 32 49
     42 10837 208 13 36
     43 10837 208 36 55
     44 10837 208 33 59
     45 10837 208 36 56
     46 10838 208 32 34
     47 10838 208 32 37
     48 10838 208 35 51
     49 10838 208 36 38
     50 10838 208 36 48
     51 20005 208 31 51
     52 20005 208 30 44
     53 20005 208 29 53
     54 20005 208 31 51
     55 20005 208 29 53
     56 20010 208 39 76
     57 20010 208 36 68
     58 20010 208 34 74
     59 20010 208 37 80
     60 20010 208 33 71
     61 20023 208 36 55
     62 20023 208 36 55
     63 20023 208 36 55
     64 20023 208 36 55
     65 20023 208 36 55
     66 20080 208 36 76
     67 20080 208 39 55
     68 20080 208 40 71
     69 20080 208 36 60
     70 20080 208 36 59
     71 20085 208 37 59
     72 20085 208 37 60
     73 20085 208 36 49
     74 20085 208 38 43
     75 20085 208 36 58
     76 20130 208 37 74
     77 20130 208 35 57
     78 20130 208 32 58
     79 20130 208 37 59
     80 20130 208 34 66
     81 20153 208 33 46
     82 20153 208 24 42
     83 20153 208 33 58
     84 20153 208 24 44
     85 20153 208 33 60
     86 20187 208 33 51
     87 20187 208 34 43
     88 20187 208 30 40
     89 20187 208 34 54
     90 20187 208 33 51
     91 20195 208 30 56
     92 20195 208 29 54
     93 20195 208 34 76
     94 20195 208 34 76
     95 20195 208 29 44
     96 20235 208 38 69
     97 20235 208 36 60
     98 20235 208 38 71
     99 20235 208 35 57
    100 20235 208 36 59
    end

  • #2
    The means and SDs are to be calculated by School_Code and Overall (National).

    Comment


    • #3
      If you need to use more than 1 mean or SD, you use a local macro to hold these:

      Code:
      sysuse auto, clear
      foreach var of varlist mpg turn disp{
          qui summarize `var'
          local mean_`var'= r(mean)
          local SD_`var'= r(sd)
      }
      di `mean_mpg'
      di `SD_turn'
      di `mean_displacement'
      Res.:

      Code:
      . di `mean_mpg'
      21.297297
      
      .
      . di `SD_turn'
      4.3993537
      
      .
      . di `mean_displacement'
      197.2973
      To see how to reference a coefficient, run the regression with the option -coeflegend-, e.g.,

      Code:
      sysuse auto, clear
      regress price mpg weight turn i.rep78, coeflegend
      Res.:

      Code:
      . regress price mpg weight turn i.rep78, coeflegend
      
            Source |       SS           df       MS      Number of obs   =        69
      -------------+----------------------------------   F(7, 61)        =      6.34
             Model |   242819042         7  34688434.6   Prob > F        =    0.0000
          Residual |   333977917        61  5475047.82   R-squared       =    0.4210
      -------------+----------------------------------   Adj R-squared   =    0.3545
             Total |   576796959        68  8482308.22   Root MSE        =    2339.9
      
      ------------------------------------------------------------------------------
             price | Coefficient  Legend
      -------------+----------------------------------------------------------------
               mpg |  -86.24749  _b[mpg]
            weight |    3.39851  _b[weight]
              turn |  -321.7209  _b[turn]
                   |
             rep78 |
                2  |   1143.126  _b[2.rep78]
                3  |   1074.757  _b[3.rep78]
                4  |   1541.853  _b[4.rep78]
                5  |   2813.323  _b[5.rep78]
                   |
             _cons |   9030.873  _b[_cons]
      So after the regression, when the estimation results are active, you can specify your expression, e.g.,

      Code:
      display "wanted = " `=(`mean_mpg'*_b[mpg])+ (`mean_weight'*_b[weight])/ `SD_turn''
      But note that if you have missing values, not all observations are used in the regression, and you will need to run the regression first and compute the statistics using the estimation sample, e.g.,

      Code:
      sysuse auto, clear
      regress price mpg weight turn i.rep78, coeflegend
      mean mpg if e(sample)


      Last edited by Andrew Musau; 18 Oct 2023, 06:04.

      Comment

      Working...
      X