Announcement

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

  • Regression - for specific observations separately, save coefficients as variables

    Hi there,

    I'm currently working on some regressions at STATA. For a range of companies I have both sales and cost values for several years. I Need to regress sales (as independant variable) on costs (dependant variable) for each company separately and save the regression coefficients for each company's regression.

    I'm quite new to STATA and am a liztle bit lost with how to start. How can I tell STATA to do the regression for each company separately? And as a next step, how can I save the coefficients I get from each regression as a new variable?

    Thank you so much in advance for your help!

    KR
    Sarah

  • #2
    Hello Sarah. See the first example in the help for statsby.

    Code:
    help statsby
    Code:
    clear *
    sysuse auto
    statsby, by(foreign): regress mpg gear turn
    Use the saving option if you want to save the coefficients to disk rather than replace the working dataset.

    HTH.
    --
    Bruce Weaver
    Email: [email protected]
    Version: Stata/MP 18.5 (Windows)

    Comment


    • #3
      To follow up Bruce's helpful advice, let's compare the results in his problem of statsby with those of rangestat (SSC). The first produces a new reduced dataset. The second adds new variables alongside existing variables.

      Code:
      . clear *
      
      . sysuse auto
      (1978 Automobile Data)
      
      . statsby, by(foreign): regress mpg gear turn
      (running regress on estimation sample)
      
            command:  regress mpg gear turn
                 by:  foreign
      
      Statsby groups
      ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5
      ..
      
      . list
      
           +--------------------------------------------+
           |  foreign   _b_gea~o     _b_turn    _b_cons |
           |--------------------------------------------|
        1. | Domestic   3.814768   -.7819742   41.52745 |
        2. |  Foreign   6.566766   -2.327916   84.17068 |
           +--------------------------------------------+
      
      .
      . sysuse auto, clear
      (1978 Automobile Data)
      
      . rangestat (reg) mpg gear turn , int(foreign 0 0)
      
      .
      . ds
      make          headroom      turn          reg_nobs      b_turn        se_cons
      price         trunk         displacement  reg_r2        b_cons
      mpg           weight        gear_ratio    reg_adj_r2    se_gear_ra~o
      rep78         length        foreign       b_gear_ratio  se_turn
      
      .
      . tabdisp foreign, c(b_*)
      
      ----------------------------------------------------------------------------
       Car type |         b_gear_ratio                b_turn                b_cons
      ----------+-----------------------------------------------------------------
       Domestic |            3.8147681            -.78197421             41.527445
        Foreign |            6.5667664            -2.3279161             84.170678
      ----------------------------------------------------------------------------
      Some small points about #1.

      1. Please see https://www.statalist.org/forums/help#spelling for why we should write Stata not STATA.

      2. If you're predicting sales from costs then you're regressing sales on costs and sales is dependent and costs independent. Or vice versa.

      Comment

      Working...
      X