Announcement

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

  • Foreach loop

    Dear all,

    I need to do a regression for each firm with reg ret mktret, where ret is return of the firm and mktret is the return of the market that the firm is in. The market is described by the variable sic. The specific firm is defined by cusip. Then, for every firm I want to replace alpha with the constant of that regression, and beta with the coefficient of mktret.

    Can someone help me and tell why this is wrong?

    gen int sicint = sic

    levelsof sicint, local(sicintlist)

    foreach cus of local sicintlist{
    qui reg ret mktret if cusip =="`cus'"
    replace alpha = _b[_cons]
    replace beta = _b[mktret]
    }

    Thanks in advance!

  • #2
    Code:
    statsby alpha=_b[_cons] beta=_b[mktret], by(cusip ) saving(alpha_beta, replace): reg ret mktreg
    merge m:1 using alpha_beta
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      What Maarten proposes would be probably faster. The loop you have on mind should look like this: gen alpha = . gen beta = . levelsof cusip, local(cusiplist) foreach cus of local cusiplist { qui reg ret mktret if cusip =="`cus'" replace alpha = _b[_cons] if cusip =="`cus'" replace beta = _b[mktret] if cusip =="`cus'" }What Maarten proposes would be probably faster.

      The loop you have on mind should look like this:

      gen alpha = .
      gen beta = .

      levelsof cusip, local(cusiplist)

      foreach cus of local cusiplist {
      qui reg ret mktret if cusip =="`cus'"
      replace alpha = _b[_cons] if cusip =="`cus'"
      replace beta = _b[mktret] if cusip =="`cus'"
      }

      Comment

      Working...
      X