Dear Stata users,

I am trying nlsur command using function evaluator program to estimate coefficients of QUAIDS model in Stata 14.
The thing is that when I try nlsur command, the error message occurs: could not evaluate equation 1
starting values invalid or some RHS variables have missing values
.

I sure that there is no missing variables.
I hereby attach code and function evaluator program.
I am afraid the codes are too long to go over, but I could not understand where the problem comes from.

Hope someone can tell me how to deal with "starting values invalid" message when using nlsur command in general.

Many thanks!


Code:
 

{/* function evaluator program */

    do "$dofiles\0_funcev_QUAIDS.do"
}



{/* estimation */

    use "$datain\data_korea_prices.dta", clear
    tab state, gen(r)
    
    timer on 1
    
    {    /* 1st step: Probit model */
 
    forv i=1/$neqnm2{          
                gen d`i'=0 if w`i'==0
                replace d`i'=1 if w`i'>0
                tab d`i'
 
                probit d`i' p1-p10 lnm z1 z2 i.z3 z4 z5, iter(10)
                
                mat PC`i' = (_b[p1], _b[p2], _b[p3], _b[p4], _b[p5], _b[p6], _b[p7], _b[p8])
                
                predict lipr`i', xb
                gen pdf`i'=normalden(lipr`i')
                gen cdf`i'=normal(lipr`i')
                label var lipr`i' "linear prediction for d`i'"
                label var pdf`i' "univariate standard normal probability density function for d`i'"
                label var cdf`i' "cumulative distribution function for d`i'"
                drop lipr`i' d`i'
                sum pdf`i' cdf`i'
    }

    mat PC = (PC1 \ PC2 \ PC3 \PC4\ PC5\ PC6\ PC7\ PC8)
    mat list PC
    
    * put matrix to excel
    putexcel set "$results\probit", sheet("pcoeff", replace) modify
    
    #delimit;
    putexcel A1 = ("Probit price coeffcieints  ")
    
             B2 = matrix(PC)
    ;
    #delimit cr    
    
    
    gen pdf$neqn = 0
    gen cdf$neqn = 1

    gen pdf$neqnm1 = 0
    gen cdf$neqnm1 = 1
    
    sum cdf* pdf*
    
    forv i=1/$neqn {
        drop if cdf`i' == .
        drop if pdf`i' == .
    }
    
    }
        
    {/* 2nd step: system estimation */
    
    nlsur quaidscensored @ $shares $prices $logexp $cdfs $pdfs r1-r16, fgnls nequations($neqn) ///
        param(a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 ///
        g11 g12 g13 g14 g15 g16 g17 g18 g19 g110 g22 g23 g24 g25 g26 g27 g28 g29 g210 ///
        g33 g34 g35 g36 g37 g38 g39 g310 g44 g45 g46 g47 g48 g49 g410 ///
        g55 g56 g57 g58 g59 g510 g66 g67 g68 g69 g610 g77 g78 g79 g710 ///
        g88 g89 g810 g99 g910 g1010  ///
        b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 l1 l2 l3 l4 l5 l6 l7 l8 l9 l10 ///
        d1a d2a d3a d4a d5a d6a d7a d8a d9a d10a ///
        k1d1 k2d1 k3d1 k4d1 k5d1 k6d1 k7d1 k8d1 k9d1 k10d1 ///
        k1d2 k2d2 k3d2 k4d2 k5d2 k6d2 k7d2 k8d2 k9d2 k10d2 ///
        k1d3 k2d3 k3d3 k4d3 k5d3 k6d3 k7d3 k8d3 k9d3 k10d3 ///
        k1d4 k2d4 k3d4 k4d4 k5d4 k6d4 k7d4 k8d4 k9d4 k10d4 ///
        k1d5 k2d5 k3d5 k4d5 k5d5 k6d5 k7d5 k8d5 k9d5 k10d5 ///
        k1d6 k2d6 k3d6 k4d6 k5d6 k6d6 k7d6 k8d6 k9d6 k10d6 ///
        k1d7 k2d7 k3d7 k4d7 k5d7 k6d7 k7d7 k8d7 k9d7 k10d7 ///
        k1d8 k2d8 k3d8 k4d8 k5d8 k6d8 k7d8 k8d8 k9d8 k10d8 ///
        k1d9 k2d9 k3d9 k4d9 k5d9 k6d9 k7d9 k8d9 k9d9 k10d9 ///
        k1d10 k2d10 k3d10 k4d10 k5d10 k6d10 k7d10 k8d10 k9d10 k10d10 ///
        k1d11 k2d11 k3d11 k4d11 k5d11 k6d11 k7d11 k8d11 k9d11 k10d11 ///
        k1d12 k2d12 k3d12 k4d12 k5d12 k6d12 k7d12 k8d12 k9d12 k10d12 ///
        k1d13 k2d13 k3d13 k4d13 k5d13 k6d13 k7d13 k8d13 k9d13 k10d13 ///
        k1d14 k2d14 k3d14 k4d14 k5d14 k6d14 k7d14 k8d14 k9d14 k10d14 ///
        k1d15 k2d15 k3d15 k4d15 k5d15 k6d15 k7d15 k8d15 k9d15 k10d15 ///
        k1d16 k2d16 k3d16 k4d16 k5d16 k6d16 k7d16 k8d16 k9d16 k10d16)
        
    }

    timer off 1
    timer list 1
    estimates save "$datain\est_regfe", replace
}

    forv i = 1/$neqn {
        predict res`i', equation(#`i') residuals
        predict w`i'_0, equation(#`i') yhat
    }

And the function evaluator program

Code:
program nlsurquaidscensored
    
    version 14

    syntax varlist (min=57  max=57) if, at(name)
    tokenize `varlist'
    args w1 w2 w3 w4 w5 w6 w7 w8 w9 w10 ///
         lnp1 lnp2 lnp3 lnp4 lnp5 lnp6 lnp7 lnp8 lnp9 lnp10 ///
         lnm cdf1 cdf2 cdf3 cdf4 cdf5 cdf6 cdf7 cdf8 cdf9 cdf10 ///
         pdf1 pdf2 pdf3 pdf4 pdf5 pdf6 pdf7 pdf8 pdf9 pdf10 ///
         r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 r14 r15 r16
    
    /* We have 10 goods, imposing symmetry and homogeneity constraints there are
                
    parameters than can be estimated in the censored QUAIDS model. Below, we extract those parameters from the `at' vector and
    impose constraints as we go along */
    
    {/* a, constants */
    tempname a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
    scalar `a1' = `at'[1,1]
    scalar `a2' = `at'[1,2]
    scalar `a3' = `at'[1,3]
    scalar `a4' = `at'[1,4]    
    scalar `a5' = `at'[1,5]
    scalar `a6' = `at'[1,6]
    scalar `a7' = `at'[1,7]
    scalar `a8' = `at'[1,8]
    scalar `a9' = `at'[1,9]
    scalar `a10' = `at'[1,10]
    
    }
    {/* g, price coefficients */
    tempname g11 g12 g13 g14 g15 g16 g17 g18 g19 g110
    tempname g21 g22 g23 g24 g25 g26 g27 g28 g29 g210
    tempname g31 g32 g33 g34 g35 g36 g37 g38 g39 g310
    tempname g41 g42 g43 g44 g45 g46 g47 g48 g49 g410
    tempname g51 g52 g53 g54 g55 g56 g57 g58 g59 g510
    tempname g61 g62 g63 g64 g65 g66 g67 g68 g69 g610
    tempname g71 g72 g73 g74 g75 g76 g77 g78 g79 g710
    tempname g81 g82 g83 g84 g85 g86 g87 g88 g89 g810
    tempname g91 g92 g93 g94 g95 g96 g97 g98 g99 g910
    tempname g101 g102 g103 g104 g105 g106 g107 g108 g109 g1010
    
    scalar `g11' = `at'[1,11]
    scalar `g12' = `at'[1,12]
    scalar `g13' = `at'[1,13]
    scalar `g14' = `at'[1,14]
    scalar `g15' = `at'[1,15]
    scalar `g16' = `at'[1,16]
    scalar `g17' = `at'[1,17]
    scalar `g18' = `at'[1,18]
    scalar `g19' = `at'[1,19]
    scalar `g110' = `at'[1,20]
    
    scalar `g21' = `g12'
    scalar `g22' = `at'[1,21]
    scalar `g23' = `at'[1,22]
    scalar `g24' = `at'[1,23]
    scalar `g25' = `at'[1,24]
    scalar `g26' = `at'[1,25]
    scalar `g27' = `at'[1,26]
    scalar `g28' = `at'[1,27]
    scalar `g29' = `at'[1,28]
    scalar `g210' = `at'[1,29]

    scalar `g31' = `g13'
    scalar `g32' = `g23'
    scalar `g33' = `at'[1,30]
    scalar `g34' = `at'[1,31]
    scalar `g35' = `at'[1,32]
    scalar `g36' = `at'[1,33]
    scalar `g37' = `at'[1,34]
    scalar `g38' = `at'[1,35]
    scalar `g39' = `at'[1,36]
    scalar `g310' = `at'[1,37]
    
    scalar `g41' = `g14'
    scalar `g42' = `g24'
    scalar `g43' = `g34'
    scalar `g44' = `at'[1,38]
    scalar `g45' = `at'[1,39]
    scalar `g46' = `at'[1,40]
    scalar `g47' = `at'[1,41]
    scalar `g48' = `at'[1,42]
    scalar `g49' = `at'[1,43]
    scalar `g410' = `at'[1,44]
    
    scalar `g51' = `g15'
    scalar `g52' = `g25'
    scalar `g53' = `g35'
    scalar `g54' = `g45'
    scalar `g55' = `at'[1,45]
    scalar `g56' = `at'[1,46]
    scalar `g57' = `at'[1,47]
    scalar `g58' = `at'[1,48]
    scalar `g59' = `at'[1,49]
    scalar `g510' = `at'[1,50]
    
    scalar `g61' = `g16'
    scalar `g62' = `g26'
    scalar `g63' = `g36'
    scalar `g64' = `g46'
    scalar `g65' = `g56'
    scalar `g66' = `at'[1,51]
    scalar `g67' = `at'[1,52]
    scalar `g68' = `at'[1,53]
    scalar `g69' = `at'[1,54]
    scalar `g610' = `at'[1,55]
    
    scalar `g71' = `g17'
    scalar `g72' = `g27'
    scalar `g73' = `g37'
    scalar `g74' = `g47'
    scalar `g75' = `g57'
    scalar `g76' = `g67'
    scalar `g77' = `at'[1,56]
    scalar `g78' = `at'[1,57]
    scalar `g79' = `at'[1,58]
    scalar `g710' = `at'[1,59]
    
    scalar `g81' = `g18'
    scalar `g82' = `g28'
    scalar `g83' = `g38'
    scalar `g84' = `g48'
    scalar `g85' = `g58'
    scalar `g86' = `g68'
    scalar `g87' = `g78'
    scalar `g88' = `at'[1,60]
    scalar `g89' = `at'[1,61]
    scalar `g810' = `at'[1,62]
    
    scalar `g91' = `g19'
    scalar `g92' = `g29'
    scalar `g93' = `g39'
    scalar `g94' = `g49'
    scalar `g95' = `g59'
    scalar `g96' = `g69'
    scalar `g97' = `g79'
    scalar `g98' = `g89'
    scalar `g99' = `at'[1,63]
    scalar `g910' = `at'[1,64]
    
    scalar `g101' = `g110'
    scalar `g102' = `g210'
    scalar `g103' = `g310'
    scalar `g104' = `g410'
    scalar `g105' = `g510'
    scalar `g106' = `g610'
    scalar `g107' = `g710'
    scalar `g108' = `g810'
    scalar `g109' = `g910'
    scalar `g1010' = `at'[1,65]
    
    }
    
    {/* b, income coefficients */
    tempname b1 b2 b3 b4 b5 b6 b7 b8 b9 b10
    scalar `b1' = `at'[1,66]
    scalar `b2' = `at'[1,67]
    scalar `b3' = `at'[1,68]
    scalar `b4' = `at'[1,69]
    scalar `b5' = `at'[1,70]
    scalar `b6' = `at'[1,71]
    scalar `b7' = `at'[1,72]
    scalar `b8' = `at'[1,73]
    scalar `b9' = `at'[1,74]
    scalar `b10' = `at'[1,75]
    
    }
    
    {/* l, squared income coefficients */
    tempname l1 l2 l3 l4 l5 l6 l7 l8 l9 l10
    scalar `l1' = `at'[1,76]
    scalar `l2' = `at'[1,77]
    scalar `l3' = `at'[1,78]                                            
    scalar `l4' = `at'[1,79]
    scalar `l5' = `at'[1,80]
    scalar `l6' = `at'[1,81]
    scalar `l7' = `at'[1,82]                                            
    scalar `l8' = `at'[1,83]
    scalar `l9' = `at'[1,84]
    scalar `l10' = `at'[1,85]
    }

    {/* d`i'a, pdf coefficients */
    tempname d1a d2a d3a d4a d5a d6a d7a d8a d9a d10a
    scalar `d1a' = `at'[1,86]
    scalar `d2a' = `at'[1,84]
    scalar `d3a' = `at'[1,88]
    scalar `d4a' = `at'[1,89]
    scalar `d5a' = `at'[1,90]
    scalar `d6a' = `at'[1,91]
    scalar `d7a' = `at'[1,92]
    scalar `d8a' = `at'[1,93]
    scalar `d9a' = `at'[1,94]
    scalar `d10a' = `at'[1,95]
    }
    
    {/* k, region dummies */
    * region 1
    tempname k1d1 k2d1 k3d1 k4d1 k5d1 k6d1 k7d1 k8d1 k9d1 k10d1  
    scalar `k1d1' = `at'[1,96]
    scalar `k2d1' = `at'[1,97]
    scalar `k3d1' = `at'[1,98]
    scalar `k4d1' = `at'[1,99]
    scalar `k5d1' = `at'[1,100]
    scalar `k6d1' = `at'[1,101]
    scalar `k7d1' = `at'[1,102]
    scalar `k8d1' = `at'[1,103]
    scalar `k9d1' = `at'[1,104]
    scalar `k10d1' = `at'[1,105]
    
    * region 2
    tempname k1d2 k2d2 k3d2 k4d2 k5d2 k6d2 k7d2 k8d2 k9d2 k10d2  
    scalar `k1d2' = `at'[1,106]
    scalar `k2d2' = `at'[1,107]
    scalar `k3d2' = `at'[1,108]
    scalar `k4d2' = `at'[1,109]
    scalar `k5d2' = `at'[1,110]
    scalar `k6d2' = `at'[1,111]
    scalar `k7d2' = `at'[1,112]
    scalar `k8d2' = `at'[1,113]
    scalar `k9d2' = `at'[1,114]
    scalar `k10d2' = `at'[1,115]
    
    * region 3
    tempname k1d3 k2d3 k3d3 k4d3 k5d3 k6d3 k7d3 k8d3 k9d3 k10d3  
    scalar `k1d3' = `at'[1,116]
    scalar `k2d3' = `at'[1,117]
    scalar `k3d3' = `at'[1,118]
    scalar `k4d3' = `at'[1,119]
    scalar `k5d3' = `at'[1,120]
    scalar `k6d3' = `at'[1,121]
    scalar `k7d3' = `at'[1,122]
    scalar `k8d3' = `at'[1,123]
    scalar `k9d3' = `at'[1,124]
    scalar `k10d3' = `at'[1,125]
    
    * region 4
    tempname k1d4 k2d4 k3d4 k4d4 k5d4 k6d4 k7d4 k8d4 k9d4 k10d4  
    scalar `k1d4' = `at'[1,126]
    scalar `k2d4' = `at'[1,127]
    scalar `k3d4' = `at'[1,128]
    scalar `k4d4' = `at'[1,129]
    scalar `k5d4' = `at'[1,130]
    scalar `k6d4' = `at'[1,131]
    scalar `k7d4' = `at'[1,132]
    scalar `k8d4' = `at'[1,133]
    scalar `k9d4' = `at'[1,134]
    scalar `k10d4' = `at'[1,135]
    
    * region 5
    tempname k1d5 k2d5 k3d5 k4d5 k5d5 k6d5 k7d5 k8d5 k9d5 k10d5  
    scalar `k1d5' = `at'[1,136]
    scalar `k2d5' = `at'[1,137]
    scalar `k3d5' = `at'[1,138]
    scalar `k4d5' = `at'[1,139]
    scalar `k5d5' = `at'[1,140]
    scalar `k6d5' = `at'[1,141]
    scalar `k7d5' = `at'[1,142]
    scalar `k8d5' = `at'[1,143]
    scalar `k9d5' = `at'[1,144]
    scalar `k10d5' = `at'[1,145]
    
    * region 6
    tempname k1d6 k2d6 k3d6 k4d6 k5d6 k6d6 k7d6 k8d6 k9d6 k10d6  
    scalar `k1d6' = `at'[1,146]
    scalar `k2d6' = `at'[1,147]
    scalar `k3d6' = `at'[1,148]
    scalar `k4d6' = `at'[1,149]
    scalar `k5d6' = `at'[1,150]
    scalar `k6d6' = `at'[1,151]
    scalar `k7d6' = `at'[1,152]
    scalar `k8d6' = `at'[1,153]
    scalar `k9d6' = `at'[1,154]
    scalar `k10d6' = `at'[1,155]
    
    * region 7
    tempname k1d7 k2d7 k3d7 k4d7 k5d7 k6d7 k7d7 k8d7 k9d7 k10d7  
    scalar `k1d7' = `at'[1,156]
    scalar `k2d7' = `at'[1,157]
    scalar `k3d7' = `at'[1,158]
    scalar `k4d7' = `at'[1,159]
    scalar `k5d7' = `at'[1,160]
    scalar `k6d7' = `at'[1,161]
    scalar `k7d7' = `at'[1,162]
    scalar `k8d7' = `at'[1,163]
    scalar `k9d7' = `at'[1,164]
    scalar `k10d7' = `at'[1,165]
    
    * region 8
    tempname k1d8 k2d8 k3d8 k4d8 k5d8 k6d8 k7d8 k8d8 k9d8 k10d8  
    scalar `k1d8' = `at'[1,166]
    scalar `k2d8' = `at'[1,167]
    scalar `k3d8' = `at'[1,168]
    scalar `k4d8' = `at'[1,169]
    scalar `k5d8' = `at'[1,170]
    scalar `k6d8' = `at'[1,171]
    scalar `k7d8' = `at'[1,172]
    scalar `k8d8' = `at'[1,173]
    scalar `k9d8' = `at'[1,174]
    scalar `k10d8' = `at'[1,175]
    
    * region 9
    tempname k1d9 k2d9 k3d9 k4d9 k5d9 k6d9 k7d9 k8d9 k9d9 k10d9  
    scalar `k1d9' = `at'[1,176]
    scalar `k2d9' = `at'[1,177]
    scalar `k3d9' = `at'[1,178]
    scalar `k4d9' = `at'[1,179]
    scalar `k5d9' = `at'[1,180]
    scalar `k6d9' = `at'[1,181]
    scalar `k7d9' = `at'[1,182]
    scalar `k8d9' = `at'[1,183]
    scalar `k9d9' = `at'[1,184]
    scalar `k10d9' = `at'[1,185]
    
    * region 10
    tempname k1d10 k2d10 k3d10 k4d10 k5d10 k6d10 k7d10 k8d10 k9d10 k10d10  
    scalar `k1d10' = `at'[1,186]
    scalar `k2d10' = `at'[1,187]
    scalar `k3d10' = `at'[1,188]
    scalar `k4d10' = `at'[1,189]
    scalar `k5d10' = `at'[1,190]
    scalar `k6d10' = `at'[1,191]
    scalar `k7d10' = `at'[1,192]
    scalar `k8d10' = `at'[1,193]
    scalar `k9d10' = `at'[1,194]
    scalar `k10d10' = `at'[1,195]
    
    * region 11
    tempname k1d11 k2d11 k3d11 k4d11 k5d11 k6d11 k7d11 k8d11 k9d11 k10d11  
    scalar `k1d11' = `at'[1,196]
    scalar `k2d11' = `at'[1,197]
    scalar `k3d11' = `at'[1,198]
    scalar `k4d11' = `at'[1,199]
    scalar `k5d11' = `at'[1,200]
    scalar `k6d11' = `at'[1,201]
    scalar `k7d11' = `at'[1,202]
    scalar `k8d11' = `at'[1,203]
    scalar `k9d11' = `at'[1,204]
    scalar `k10d11' = `at'[1,205]
    
    * region 12
    tempname k1d12 k2d12 k3d12 k4d12 k5d12 k6d12 k7d12 k8d12 k9d12 k10d12  
    scalar `k1d12' = `at'[1,206]
    scalar `k2d12' = `at'[1,207]
    scalar `k3d12' = `at'[1,208]
    scalar `k4d12' = `at'[1,209]
    scalar `k5d12' = `at'[1,210]
    scalar `k6d12' = `at'[1,211]
    scalar `k7d12' = `at'[1,212]
    scalar `k8d12' = `at'[1,213]
    scalar `k9d12' = `at'[1,214]
    scalar `k10d12' = `at'[1,215]
    
    * region 13
    tempname k1d13 k2d13 k3d13 k4d13 k5d13 k6d13 k7d13 k8d13 k9d13 k10d13  
    scalar `k1d13' = `at'[1,216]
    scalar `k2d13' = `at'[1,217]
    scalar `k3d13' = `at'[1,218]
    scalar `k4d13' = `at'[1,219]
    scalar `k5d13' = `at'[1,220]
    scalar `k6d13' = `at'[1,221]
    scalar `k7d13' = `at'[1,222]
    scalar `k8d13' = `at'[1,223]
    scalar `k9d13' = `at'[1,224]
    scalar `k10d13' = `at'[1,225]
    
    * region 14
    tempname k1d14 k2d14 k3d14 k4d14 k5d14 k6d14 k7d14 k8d14 k9d14 k10d14  
    scalar `k1d14' = `at'[1,226]
    scalar `k2d14' = `at'[1,227]
    scalar `k3d14' = `at'[1,228]
    scalar `k4d14' = `at'[1,229]
    scalar `k5d14' = `at'[1,230]
    scalar `k6d14' = `at'[1,231]
    scalar `k7d14' = `at'[1,232]
    scalar `k8d14' = `at'[1,233]
    scalar `k9d14' = `at'[1,234]
    scalar `k10d14' = `at'[1,235]
    
    * region 15
    tempname k1d15 k2d15 k3d15 k4d15 k5d15 k6d15 k7d15 k8d15 k9d15 k10d15  
    scalar `k1d15' = `at'[1,236]
    scalar `k2d15' = `at'[1,237]
    scalar `k3d15' = `at'[1,238]
    scalar `k4d15' = `at'[1,239]
    scalar `k5d15' = `at'[1,240]
    scalar `k6d15' = `at'[1,241]
    scalar `k7d15' = `at'[1,242]
    scalar `k8d15' = `at'[1,243]
    scalar `k9d15' = `at'[1,244]
    scalar `k10d15' = `at'[1,245]
    
    * region 16
    tempname k1d16 k2d16 k3d16 k4d16 k5d16 k6d16 k7d16 k8d16 k9d16 k10d16  
    scalar `k1d16' = `at'[1,246]
    scalar `k2d16' = `at'[1,247]
    scalar `k3d16' = `at'[1,248]
    scalar `k4d16' = `at'[1,259]
    scalar `k5d16' = `at'[1,250]
    scalar `k6d16' = `at'[1,251]
    scalar `k7d16' = `at'[1,252]
    scalar `k8d16' = `at'[1,253]
    scalar `k9d16' = `at'[1,254]
    scalar `k10d16' = `at'[1,255]    
        
    }
    
 
    // price indices and expenditure shares    
    
    quietly {
        // First get the price index
        // I set a_0 = 5
        
        // demand shifters
        
        
        
        
        tempvar lnpindex
        gen double `lnpindex' = 5
        forvalues i = 1/$neqn {
            replace `lnpindex' = `lnpindex' + ((`a`i'' + ///
            `k`i'd1'*`r1' + `k`i'd2'*`r2' + `k`i'd3'*`r3' + `k`i'd4'*`r4' + ///
            `k`i'd5'*`r5' + `k`i'd6'*`r6' + `k`i'd7'*`r7' + `k`i'd8'*`r8' + ///
            `k`i'd9'*`r9' + `k`i'd10'*`r10' + `k`i'd11'*`r11' + `k`i'd12'*`r12' + ///
            `k`i'd13'*`r13' + `k`i'd14'*`r14' + `k`i'd15'*`r15' + `k`i'd16'*`r16')*`lnp`i'')
        }
        forvalues i = 1/$neqn {
            forvalues j = 1/$neqn {
                replace `lnpindex' = `lnpindex' + 0.5*`g`i'`j''*`lnp`i''*`lnp`j''
            }
        }
    
        
        // The b(p) term in the QUAIDS model
        tempvar bofp
        gen double `bofp' = 0
        forvalues i = 1/$neqn {
            replace `bofp' = `bofp' + `lnp`i''*`b`i''
        }
        replace `bofp' = exp(`bofp')
    
        // Finally, the expenditure shares for 6 goods system
        // do demographics and region fixed effects become part of the price index? How to deal with it?
        
        forvalues i = 1/$neqn {
            replace `w`i'' = `cdf`i''*[`a`i'' + ///
            `k`i'd1'*`r1' + `k`i'd2'*`r2' + `k`i'd3'*`r3' + `k`i'd4'*`r4' + `k`i'd5'*`r5' ///
            + `k`i'd6'*`r6' + `k`i'd7'*`r7' + `k`i'd8'*`r8' + `k`i'd9'*`r9' + `k`i'd10'*`r10' ///
            + `k`i'd11'*`r11' + `k`i'd12'*`r12' + `k`i'd13'*`r13' + `k`i'd14'*`r14' + `k`i'd15'*`r15' ///
            + `k`i'd16'*`r16' + `g`i'1'*`lnp1' + `g`i'2'*`lnp2' + `g`i'3'*`lnp3' ///
            + `g`i'4'*`lnp4' + `g`i'5'*`lnp5' + `g`i'6'*`lnp6' + `g`i'7'*`lnp7' ///
            + `g`i'8'*`lnp8' +`g`i'9'*`lnp9' + `g`i'10'*`lnp10' ///
            +`b`i''*(`lnm' - `lnpindex') + `l`i''/`bofp'*(`lnm' - `lnpindex')^2] + `d`i'a'*`pdf`i''
        }
        
    }

end