Announcement

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

  • Define the scalar variable when categorical variable is used in regression

    Hi everyone,

    Kindly help me on how I can define the content of the scalar variable i.e. "sca b_ `x' " after estimating a regression. I have provided the codes that I am running using the "sysuse auto" data set to illustrate what I am trying to do.

    sysuse auto
    global X " mpg weight i.rep78"
    reg price $X

    foreach x of global X {
    sca b_`x' = _b[`x']
    }

    When I run the codes, stata(15) gives me this error " b_i.rep78 invalid name" . If also tried " sca b_ `x' = _b[`x' , 2.`x' .. 5. `x' ] still does not work.


  • #2
    You are close
    You just need to call the betas the correct way:
    Code:
    sysuse auto
    global X " mpg weight i.rep78"
    fvexpand $X
    global X2 "`r(varlist)'"
    reg price $X
    
    foreach x of global X2 {
    local cnt=`cnt'+1
    sca b_`cnt' = _b[`x']
    }
    scalar list

    Comment


    • #3
      Thank you FernandoRios.

      I have tried the suggested codes, and they works but they limit me to progress with further arguments in my loop. When I run the codes i get this error " 1 invalid name"

      Code:
      global X " mpg weight i.rep78"
      fvexpand $X
      global X2 "`r(varlist)'"
      reg price $X
      sum price 
      sca mean_`y' =r(mean)
      
      foreach x of global X2 {
      local cnt=`cnt'+1
      sca b_`cnt' = _b[`x']
      corr length `cnt' ,c
      sca cov_`cnt' = r(cov_12)
      sum `cnt'  
      sca elas_`cnt' = (b_`cnt'*r(mean))/mean_`y'
      }

      Comment

      Working...
      X