Announcement

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

  • forval loop to generate Inverse Mills' Ratio for panel data

    I wanted to generate Inverse Mills' Ratio for a panel data. Following Wooldridge (2010 pp. 835), I tried to use forval loop to do probit for each single year. However, the following code sometimes works but most of the time encounters errors such as "no observations" "last estimates not found". Can anybody point out what happened?

    Code:
    gen imr_sic1=.
    gen imr_sic2=.
    forval i=2005/2010 {
        forval j=1/2 {
        capture probit dependent_variable ${control_variable_sic`j'} if year==`i'
        predict xb_`i'_`j' if e(sample), xb
        replace imr_sic`j'=normalden(xb_`i'_`j')/normal(xb_`i'_`j') if f_fyear==`i'  
        }
        }
    Last edited by Cooper Felix; 28 Apr 2018, 14:19.

  • #2
    Hi,
    I guess you have a problem when you have no observations in some years.
    In that case, the probit is not estimated, but then you want to predict the xb anyway.

    You should run the code only if you have observations for the year.

    Code:
    gen imr_sic1=.
    gen imr_sic2=.
    forval i=2005/2010 {
    
        sum year if year==`i', meanonly
        if r(N)>0 {
    
            forval j=1/2 {
                probit dependent_variable ${control_variable_sic`j'} if year==`i'
                predict xb_`i'_`j' if e(sample), xb
                replace imr_sic`j'=normalden(xb_`i'_`j')/normal(xb_`i'_`j') if f_fyear==`i'
            }
            
        }
    }
    Raffaele


    Comment


    • #3
      Originally posted by Raffaele Grotti View Post
      Hi,
      I guess you have a problem when you have no observations in some years.
      In that case, the probit is not estimated, but then you want to predict the xb anyway.

      You should run the code only if you have observations for the year.

      Code:
      gen imr_sic1=.
      gen imr_sic2=.
      forval i=2005/2010 {
      
      sum year if year==`i', meanonly
      if r(N)>0 {
      
      forval j=1/2 {
      probit dependent_variable ${control_variable_sic`j'} if year==`i'
      predict xb_`i'_`j' if e(sample), xb
      replace imr_sic`j'=normalden(xb_`i'_`j')/normal(xb_`i'_`j') if f_fyear==`i'
      }
      
      }
      }
      Raffaele

      Thank you so much, your code indeed solved the problem.

      Comment

      Working...
      X