Announcement

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

  • Imposing Constraints in a Nonlinear Least-Squares Estimation Function Evaluator Program with Fixed Effects

    I am using the nl function to estimate a nonlinear function with a large number of fixed effects and have written a function evaluator program for the same. The code is

    Code:
    cap program drop nlmyeval
    program nlmyeval
      version 17.0
      syntax varlist(min=621 max=621) [if], at(name)
      
      local E: word 1 of `varlist'
      local hc: word 2 of `varlist'
      local l: word 3 of `varlist'
      local a: word 4 of `varlist'
      local b: word 5 of `varlist'
      local id: word 6 of `varlist'
      local inc: word 7 of `varlist'
      local hsize: word 8 of `varlist'
      local temp: word 9 of `varlist'
      local urban: word 10 of `varlist'
      forval i=1/611 {
       local inext=`i'+10
       local d`i' : word `inext' of `varlist'
      }
          
      tempname bl_c bl_inc bl_hsize bl_temp bl_urban
      tempname ba_c ba_inc ba_hsize ba_temp ba_urban
      tempname bb_c bb_inc bb_hsize bb_temp bb_urban
    
      scalar `bl_c' = `at'[1,1]
      scalar `bl_inc' = `at'[1,2]
      scalar `bl_hsize' = `at'[1,3]
      scalar `bl_temp' = `at'[1,4]
      scalar `bl_urban' = `at'[1,5]
      scalar `ba_c' = `at'[1,6]
      scalar `ba_inc' = `at'[1,7]
      scalar `ba_hsize' = `at'[1,8]
      scalar `ba_temp' = `at'[1,9]
      scalar `ba_urban' = `at'[1,10]
      scalar `bb_c' = `at'[1,11]
      scalar `bb_inc' = `at'[1,12]
      scalar `bb_hsize' = `at'[1,13]
      scalar `bb_temp' = `at'[1,14]
      scalar `bb_urban' = `at'[1,15]
      
      tempvar fe
      gen `fe' = 0 `if'
      forvalues i = 1(1)611{
            tempname bd`i'
            scalar `bd`i'' = `at'[1,15+`i']
            replace  `fe' = `fe' + `bd`i''*d`i'  `if'
      }
      
      tempvar exp_l exp_a exp_b exp_total
      gen `exp_l' = exp(`bl_c'+`bl_inc'*`inc'+`bl_hsize'*`hsize'+`bl_temp'*`temp'+`bl_urban'*`urban') `if'
      gen `exp_a' = exp(`ba_c'+`ba_inc'*`inc'+`ba_hsize'*`hsize'+`ba_temp'*`temp'+`ba_urban'*`urban') `if'
      gen `exp_b' = exp(`bb_c'+`bb_inc'*`inc'+`bb_hsize'*`hsize'+`bb_temp'*`temp'+`bb_urban'*`urban') `if'
      gen `exp_total' = 1+`exp_l'+`exp_a'+`exp_b' `if'
      
      replace `E' = 1/`exp_total'*`hc' + `exp_l'/`exp_total'*`l' + `exp_a'/`exp_total'*`a' + `exp_b'/`exp_total'*`b'+`fe' `if'
      
    end
    
    local np = `n'+5*3
    mat M = J(1,`np',0.6)
    
    nl myeval @ E hc-d`n', parameters(bl_c bl_inc bl_hsize bl_temp bl_urban ba_c ba_inc ba_hsize ba_temp ba_urban bb_c bb_inc bb_hsize bb_temp bb_urban `names') initial(M) noconstant log
    I wish to impose the following constraints,
    Code:
     constraint 1 _b[bl_inc] = 0
    constraint 2 _b[bl_hsize] = 0
    constraint 3 _b[bl_temp] = 0
    constraint 4 _b[bl_urban] = 0
    constraint 5 _b[ba_inc] = 0
    constraint 6 _b[ba_hsize] = 0
    constraint 7 _b[ba_temp] = 0
    constraint 8 _b[ba_urban] = 0
    constraint 9 _b[bb_inc] = 0
    constraint 10 _b[bb_hsize] = 0
    constraint 11 _b[bb_temp] = 0
    constraint 12 _b[bb_urban] = 0
    With these constraints the estimation effectively becomes a linear regression. The reason I want to do this is to zero out every coefficient associated with the covariates, get just the constant values, and then use that to inform my estimation. However, specifying
    Code:
    constraints(1/12)
    does not seem to work (it generates the 198 error code with the message "nlmyeval returned 198
    verify that nlmyeval is a function evaluator program"). Writing out the entire model instead of using the function evaluator program is also not feasible because of the large number of fixed effects. I also tried to rewrite the function evaluator program to remove all the above twelve variables, but that also generated the 480 error code "starting values invalid or some RHS variables have missing values".

    Is there any way I can impose these constraints? Or maybe another way I can go about performing this restricted estimation? Any help would be appreciated. Thank you so much!
Working...
X