Announcement

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

  • #16
    There were three mistakes:

    1. You need to restore the estimates after the call to -nlcom- at the end. This call is used to display the results, but it changes the coefficient names from "_b[a1]" to "[a1]_cons". The elasticities code is supposed to work with coefficients written as _b[a1]
    2. Your loops need to go until 4 when you calculate the elasticities, it's a 4 goods system.
    3. You need to refer to lnexpmean and not to lnexp in the elasticity code.


    I'm rather tired of fixing these nlsur routines. The only reason why people keep using this code is because Brian Poi's code does not handle censoring. I'd be happy to work together in incorporating censoring corrections into this code.

    Code:
    cap program drop nlsurquaidsNNP
    program nlsurquaidsNNP
    
    version 12.1
    
    syntax varlist(min=16 max=16) if, at(name)
    
    tokenize `varlist'
    args w1 w2 w3 lnp1 lnp2 lnp3 lnp4 lnm x1 x2 pdf1 pdf2 pdf3 cdf1 cdf2 cdf3
    
    // With four goods, there are 15 parameters that can be 
    // estimated, after eliminating one of the goods and 
    // imposing adding up, symmetry, and homogeneity
    // constraints, in the QUAIDS model
    // Here, we extract those parameters from the `at'
    // vector, and impose constraints as we go along
    
    tempname a1 a2 a3 a4
    scalar `a1' = `at'[1,1] 
    scalar `a2' = `at'[1,2] 
    scalar `a3' = `at'[1,3] 
    scalar `a4' = 1 - `a1' - `a2' - `a3'
    
    tempname b1 b2 b3 b4
    scalar `b1' = `at'[1,4]
    scalar `b2' = `at'[1,5]
    scalar `b3' = `at'[1,6]
    scalar `b4' = -`b1' - `b2' - `b3'
    
    tempname g11 g12 g13 g14
    tempname g21 g22 g23 g24
    tempname g31 g32 g33 g34
    tempname g41 g42 g43 g44
    scalar `g11' = `at'[1,7]
    scalar `g12' = `at'[1,8]
    scalar `g13' = `at'[1,9]
    scalar `g14' = -`g11' - `g12' - `g13'
    
    scalar `g21' = `g12'
    scalar `g22' = `at'[1,10]
    scalar `g23' = `at'[1,11]
    scalar `g24' = -`g21' - `g22' - `g23'
    
    scalar `g31' = `g13'
    scalar `g32' = `g23'
    scalar `g33' = `at'[1,12]
    scalar `g34' = -`g31' - `g32' - `g33'
    
    scalar `g41' = `g14'
    scalar `g42' = `g24'
    scalar `g43' = `g34'
    scalar `g44' = -`g41' - `g42' - `g43'
    
    tempname l1 l2 l3 l4
    scalar `l1' = `at'[1,13]
    scalar `l2' = `at'[1,14]
    scalar `l3' = `at'[1,15]
    scalar `l4' = -`l1' - `l2' - `l3'
    
    // constant and household demographics
    tempname r11 r12
    tempname r21 r22
    tempname r31 r32
    
    scalar `r11' = `at'[1,16]
    scalar `r12' = `at'[1,17]
    scalar `r21' = `at'[1,18]
    scalar `r22' = `at'[1,19]
    scalar `r31' = `at'[1,20]
    scalar `r32' = `at'[1,21]
    
    // pdf
    tempname d1 d2 d3
    scalar `d1' = `at'[1,22]
    scalar `d2' = `at'[1,23]
    scalar `d3' = `at'[1,24]
    
    // Okay, now that we have all the parameters, we can 
    // calculate the expenditure shares. 
    quietly {
    // First get the price index
    // I set a_0 = 5
    tempvar lnpindex
    gen double `lnpindex' = 5 + `a1'*`lnp1' + `a2'*`lnp2' ///
    + `a3'*`lnp3' + `a4'*`lnp4'
    forvalues i = 1/4 {
    forvalues j = 1/4 {
    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/4 {
    replace `bofp' = `bofp' + `lnp`i''*`b`i''
    }
    replace `bofp' = exp(`bofp')
    // Finally, the expenditure shares for 3 of the 4
    // goods (the fourth is dropped to avoid singularity)
    replace `w1' = (`a1' + `g11'*`lnp1' + `g12'*`lnp2' + ///
    `g13'*`lnp3' + `g14'*`lnp4' + ///
    `b1'*(`lnm' - `lnpindex') + ///
    `l1'/`bofp'*(`lnm' - `lnpindex')^2 + ///
    `r11'*`x1' +`r12'*`x2')*`cdf1'+ ///
    `d1'*`pdf1'
    
    replace `w2' = (`a2' + `g21'*`lnp1' + `g22'*`lnp2' + ///
    `g23'*`lnp3' + `g24'*`lnp4' + ///
    `b2'*(`lnm' - `lnpindex') + ///
    `l2'/`bofp'*(`lnm' - `lnpindex')^2 + ///
    `r21'*`x1' +`r22'*`x2')*`cdf2' + ///
    `d2'*`pdf2'
    
    replace `w3' = (`a3' + `g31'*`lnp1' + `g32'*`lnp2' + ///
    `g33'*`lnp3' + `g34'*`lnp4' + ///
    `b3'*(`lnm' - `lnpindex') + ///
    `l3'/`bofp'*(`lnm' - `lnpindex')^2 + ///
    `r31'*`x1' +`r32'*`x2')*`cdf3' + ///
    `d3'*`pdf3' 
    }
    
    end
    
    webuse food, clear
    
    * TEST modified model gives same results
    
    set trace off
    
    
    ****************************************
    
    *** create additional variables
    generate x1 = int(runiform()*4) // generate number of kinds
    generate x2 = (runiform() > 0.7) // generate rural vs urban binary variables
    
    * Nigussie's approaches 
    gen cdf1=1
    gen cdf2=1
    gen cdf3=1
    
    gen pdf1=0
    gen pdf2=0
    gen pdf3=0
    
    nlsur quaidsNNP @ w1 w2 w3 lnp1 lnp2 lnp3 lnp4 lnexp ///
    x1 x2 pdf1 pdf2 pdf3 cdf1 cdf2 cdf3, ifgnls nequations(3) ///
    param(a1 a2 a3 ///
    g11 g21 g31 ///
    g22 g32 ///
    g33 ///
    b1 b2 b3 ///
    l1 l2 l3 ///
    r1 r2 ///
    m11 m12 m13 ///
    m21 m22 m23 ///
    d1 d2 d3 ) nolog 
    
    est store quaidsNNP2
    
    *set trace on
    *set tracedepth 4
    
    *Share, price and total expenditure means 
    quietly {
    foreach x of varlist w* lnp* lnexp {
    sum `x'
    scalar `x'mean=r(mean)
    }
    * Price indexes
    glo asum "_b[a1]*lnp1mean"
    forv i=2(1)4 {
    glo asum "${asum} + _b[a`i']*lnp`i'mean"
    }
    glo gsum ""
    forv i=1(1)4 {
    forv j=1(1)4 {
    glo gsum "${gsum} + 0.5*_b[g`i'`j']*lnp`i'mean*lnp`j'mean"
    }
    }
    glo ap "10.0 + ${asum} ${gsum}"
    glo bp "_b[b1]*lnp1mean"
    forv i=2(1)4 {
    glo bp "${bp} + _b[b`i']*lnp`i'mean"
    }
    glo bp "(exp(${bp}))"
    * Mus
    forv i=1(1)4 {
    glo mu`i' "_b[b`i'] + 2*_b[l`i']/${bp}*(lnexpmean-(${ap}))"
    }
    forv j=1(1)4 {
    glo gsum2`j' ""
    forv k=1(1)4 {
    glo gsum2`j' "${gsum2`j'} + _b[g`j'`k']*lnp`k'mean"
    }
    }
    }
    nlcom (a1:_b[/a1])(a2:_b[/a2])(a3:_b[/a3])(a4:1-_b[/a1]-_b[/a2]-_b[/a3]) ///
    (b1:_b[/b1])(b2:_b[/b2])(b3:_b[/b3])(b4:-_b[/b1]-_b[/b2]-_b[/b3]) ///
    (g11:_b[/g11])(g12:_b[/g21])(g13:_b[/g31]) ///
    (g21:_b[/g21])(g22:_b[/g22])(g23:_b[/g32]) ///
    (g31:_b[/g31])(g32:_b[/g32])(g33:_b[/g33]) ///
    (g14:-_b[/g11]-_b[/g21]-_b[/g31]) ///
    (g24:-_b[/g21]-_b[/g22]-_b[/g32]) ///
    (g34:-_b[/g31]-_b[/g32]-_b[/g33]) ///
    (g41:-_b[/g11]-_b[/g21]-_b[/g31]) ///
    (g42:-_b[/g21]-_b[/g22]-_b[/g32]) ///
    (g43:-_b[/g31]-_b[/g32]-_b[/g33]) ///
    (g44:-(-_b[/g11]-_b[/g21]-_b[/g31])-(-_b[/g21]-_b[/g22]-_b[/g32])-(-_b[/g31]-_b[/g32]-_b[/g33])) ///
    (l1:_b[/l1])(l2:_b[/l2])(l3:_b[/l3])(l4:-_b[/l1]-_b[/l2]-_b[/l3]), post noheader
    
    * est store quaidsNNP2
    
    
    forv i=1(1)4 {
    forv j=1(1)4 {
    glo delta=cond(`i'==`j',1,0)
    glo mu`i'`j' "_b[g`i'`j'] - ${mu`i'}*(_b[a`j'] ${gsum2`j'})-_b[l`i']*_b[b`j']/${bp}*(lnexpmean - (${ap}))^2"
    cap nlcom (elasexp`i': ${mu`i'}/w`i'mean + 1) (mu`i'`j': ${mu`i'`j'}), post noheader
    if _rc {
    est restore quaidsNNP2
    qui nlcom (elasexp`i': ${mu`i'}/w`i'mean + 1) (mu`i'`j'f: (1e+2)*(${mu`i'`j'})), post noheader
    qui nlcom (elasexp`i': _b[elasexp`i']) (mu`i'`j':_b[mu`i'`j'f]/(1e+2)), post noheader
    }
    * Uncompensated price elasticity
    nlcom (elasexp`i': _b[elasexp`i']) (elu`i'`j':_b[mu`i'`j']/w`i'mean - ${delta}) , post noheader
    * Compensated price elasticity
    nlcom (elc`i'`j': _b[elu`i'`j'] + _b[elasexp`i']*w`j'mean), noheader
    qui est restore quaidsNNP2
    }
    }


    Jorge Eduardo Pérez Pérez
    www.jorgeperezperez.com

    Comment


    • #17
      Thanks a lot Jorge,
      It perfectly works now.

      Comment


      • #18
        For users of this routine in their estimation, it is worth to note that the above system of equations are simply uses for test using as if all PDF is 1 and CDF is 0 (as if it is non censored regression). In the actual censored regression those values are different from 1 and 0, respectively. This in turn leads to another questions (1) one of the demand property, in particular the additive property doesn't holds. Thus, the last demand equation should be treated using different approaches (see literature) rather than the usual approaches (2) expenditures and incomes elasticity as well as prices elasticity should have to be adjusted using estimated parameters for CDF (denoted by d in nlsurquaids).

        Accordingly, the last section of the routine need to be corrected as follows (in red):

        nlcom (a1:_b[/a1])(a2:_b[/a2])(a3:_b[/a3])(a4:1-_b[/a1]-_b[/a2]-_b[/a3]) ///
        (b1:_b[/b1])(b2:_b[/b2])(b3:_b[/b3])(b4:-_b[/b1]-_b[/b2]-_b[/b3]) ///
        (g11:_b[/g11])(g12:_b[/g21])(g13:_b[/g31]) ///
        (g21:_b[/g21])(g22:_b[/g22])(g23:_b[/g32]) ///
        (g31:_b[/g31])(g32:_b[/g32])(g33:_b[/g33]) ///
        (g14:-_b[/g11]-_b[/g21]-_b[/g31]) ///
        (g24:-_b[/g21]-_b[/g22]-_b[/g32]) ///
        (g34:-_b[/g31]-_b[/g32]-_b[/g33]) ///
        (g41:-_b[/g11]-_b[/g21]-_b[/g31]) ///
        (g42:-_b[/g21]-_b[/g22]-_b[/g32]) ///
        (g43:-_b[/g31]-_b[/g32]-_b[/g33]) ///
        (g44:-(-_b[/g11]-_b[/g21]-_b[/g31])-(-_b[/g21]-_b[/g22]-_b[/g32])-(-_b[/g31]-_b[/g32]-_b[/g33])) ///
        (l1:_b[/l1])(l2:_b[/l2])(l3:_b[/l3])(l4:-_b[/l1]-_b[/l2]-_b[/l3]) ///
        (d1:_b[/d1]) (d2:_b[/d2]) (d3:_b[/d3]), post noheader

        * est store quaidsNNP2

        forv i=1(1)4 {
        forv j=1(1)4 {
        glo delta=cond(`i'==`j',1,0)
        glo mu`i'`j' "_b[g`i'`j'] - ${mu`i'}*(_b[a`j'] ${gsum2`j'})-_b[l`i']*_b[b`j']/${bp}*(lnexpmean - (${ap}))^2"
        cap nlcom (elasexp`i': ${mu`i'}/w`i'mean + 1) (mu`i'`j': ${mu`i'`j'}), post noheader
        if _rc {
        qui nlcom (elasexp`i': ${mu`i'}/w`i'mean + 1) (mu`i'`j'f: (1e+2)*(${mu`i'`j'})), post noheader
        qui nlcom (elasexp`i': _b[elasexp`i']) (mu`i'`j':_b[mu`i'`j'f]/(1e+2)), post noheader
        }
        * Uncompensated price elasticity
        nlcom (elasexp`i': _b[elasexp`i']*_b[d`i']) (elu`i'`j':(_b[mu`i'`j']/w`i'mean - ${delta})*_b[d`i']) , post noheader
        * Compensated price elasticity
        nlcom (elc`i'`j': _b[elu`i'`j'] + _b[elasexp`i']*w`j'mean), noheader
        qui est restore quaidsNNP2
        }
        }
        Last edited by Nigussie Tefera; 17 Sep 2015, 18:12.

        Comment


        • #19
          Hi Nigussie,
          I am interested to know if you ever got your code to work, particularly your latest amendment. looking through the code you have specified the forvalue i/j loops going up to 4 (as you should) but do not estimate d4 (i.e. cdf parameter) in the previous nlcom.

          Thanks,
          Tareq

          Comment


          • #20
            Hi Nigussie and others of course,

            I just adapted your 4 food QUAIDS code to estimate 5 foods model in Stata 14. However, it shows the error message "nlsurqaids returned 198 verify that nlsurqaids is a function evaluator program". Can any of you please check where I have gone wrong in my routine? When I run the same codes of Nigussie using "webuse food" data, it runs perfectly. Quick response is very much appreciated as I have to present some findings towards the end of the week. I have attached a part of the data as well.


            cap program drop nlsurquaidsNNP
            program nlsurquaidsNNP


            version 14.0


            syntax varlist(min=20 max=20) if, at(name)


            tokenize `varlist'
            args w1 w2 w3 w4 lnp1 lnp2 lnp3 lnp4 lnp5 lnm x1 x2 pdf1 pdf2 pdf3 pdf4 cdf1 cdf2 cdf3 cdf4


            // With four goods, there are 15 parameters that can be
            // estimated, after eliminating one of the goods and
            // imposing adding up, symmetry, and homogeneity
            // constraints, in the QUAIDS model
            // Here, we extract those parameters from the `at'
            // vector, and impose constraints as we go along


            tempname a1 a2 a3 a4 a5
            scalar `a1' = `at'[1,1]
            scalar `a2' = `at'[1,2]
            scalar `a3' = `at'[1,3]
            scalar `a4' = `at'[1,4]
            scalar `a5' = 1 - `a1' - `a2' - `a3' - `a4'


            tempname b1 b2 b3 b4 b5
            scalar `b1' = `at'[1,5]
            scalar `b2' = `at'[1,6]
            scalar `b3' = `at'[1,7]
            scalar `b4' = `at'[1,8]
            scalar `b5' = -`b1' - `b2' - `b3' - `b4'

            tempname g11 g12 g13 g14 g15
            tempname g21 g22 g23 g24 g25
            tempname g31 g32 g33 g34 g35
            tempname g41 g42 g43 g44 g45
            tempname g51 g52 g53 g54 g55


            scalar `g11' = `at'[1,9]
            scalar `g12' = `at'[1,10]
            scalar `g13' = `at'[1,11]
            scalar `g14' = `at'[1,12]
            scalar `g15' = -`g11' - `g12' - `g13' - `g14'

            scalar `g21' = `g12'
            scalar `g22' = `at'[1,13]
            scalar `g23' = `at'[1,14]
            scalar `g24' = `at'[1,15]
            scalar `g25' = -`g21' - `g22' - `g23' - `g24'

            scalar `g31' = `g13'
            scalar `g32' = `g23'
            scalar `g33' = `at'[1,16]
            scalar `g34' = `at'[1,17]
            scalar `g35' = -`g31' - `g32' - `g33' - `g34'

            scalar `g41' = `g14'
            scalar `g42' = `g24'
            scalar `g43' = `g34'
            scalar `g44' = `at'[1,18]
            scalar `g45' = -`g41' - `g42' - `g43' - `g44'

            scalar `g51' = `g15'
            scalar `g52' = `g25'
            scalar `g53' = `g35'
            scalar `g54' = `g45'
            scalar `g55' = -`g51' - `g52' - `g53' - `g54'

            tempname l1 l2 l3 l4 15
            scalar `l1' = `at'[1,19]
            scalar `l2' = `at'[1,20]
            scalar `l3' = `at'[1,21]
            scalar `l4' = `at'[1,22]
            scalar `l5' = -`l1' - `l2' - `l3' - `l4'

            // constant and household demographics
            tempname r11 r12
            tempname r21 r22
            tempname r31 r32
            tempname r41 r42


            scalar `r11' = `at'[1,23]
            scalar `r12' = `at'[1,24]
            scalar `r21' = `at'[1,25]
            scalar `r22' = `at'[1,26]
            scalar `r31' = `at'[1,27]
            scalar `r32' = `at'[1,28]
            scalar `r41' = `at'[1,29]
            scalar `r42' = `at'[1,30]

            // pdf
            tempname d1 d2 d3 d4
            scalar `d1' = `at'[1,31]
            scalar `d2' = `at'[1,32]
            scalar `d3' = `at'[1,33]
            scalar `d4' = `at'[1,34]


            // Okay, now that we have all the parameters, we can
            // calculate the expenditure shares.
            quietly {
            // First get the price index
            // I set a_0 = 5
            tempvar lnpindex
            gen double `lnpindex' = 5 + `a1'*`lnp1' + `a2'*`lnp2' ///
            + `a3'*`lnp3' + `a4'*`lnp4' + `a5'*`lnp5'
            forvalues i = 1/5 {
            forvalues j = 1/5 {
            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/5 {
            replace `bofp' = `bofp' + `lnp`i''*`b`i''
            }
            replace `bofp' = exp(`bofp')
            // Finally, the expenditure shares for 4 of the 5
            // goods (the fifth is dropped to avoid singularity)
            replace `w1' = (`a1' + `g11'*`lnp1' + `g12'*`lnp2' + ///
            `g13'*`lnp3' + `g14'*`lnp4' + `g15'*`lnp5' + ///
            `b1'*(`lnm' - `lnpindex') + ///
            `l1'/`bofp'*(`lnm' - `lnpindex')^2 + ///
            `r11'*`x1' +`r12'*`x2')*`cdf1'+ ///
            `d1'*`pdf1'


            replace `w2' = (`a2' + `g21'*`lnp1' + `g22'*`lnp2' + ///
            `g23'*`lnp3' + `g24'*`lnp4' + `g25'*`lnp5' + ///
            `b2'*(`lnm' - `lnpindex') + ///
            `l2'/`bofp'*(`lnm' - `lnpindex')^2 + ///
            `r21'*`x1' +`r22'*`x2')*`cdf2' + ///
            `d2'*`pdf2'


            replace `w3' = (`a3' + `g31'*`lnp1' + `g32'*`lnp2' + ///
            `g33'*`lnp3' + `g34'*`lnp4' + `g35'*`lnp5' + ///
            `b3'*(`lnm' - `lnpindex') + ///
            `l3'/`bofp'*(`lnm' - `lnpindex')^2 + ///
            `r31'*`x1' +`r32'*`x2')*`cdf3' + ///
            `d3'*`pdf3'


            replace `w4' = (`a4' + `g41'*`lnp1' + `g42'*`lnp2' + ///
            `g43'*`lnp3' + `g44'*`lnp4' + `g45'*`lnp5' + ///
            `b4'*(`lnm' - `lnpindex') + ///
            `l4'/`bofp'*(`lnm' - `lnpindex')^2 + ///
            `r41'*`x1' +`r42'*`x2')*`cdf4' + ///
            `d4'*`pdf4'
            }


            end



            set trace off


            ****************************************


            nlsur quaidsNNP @ w1 w2 w3 w4 lnp1 lnp2 lnp3 lnp4 lnp5 lnm ///
            x1 x2 pdf1 pdf2 pdf3 pdf4 cdf1 cdf2 cdf3 cdf4, ifgnls nequations(4) ///
            param(a1 a2 a3 a4 ///
            g11 g21 g31 g41 ///
            g22 g32 g42 ///
            g33 g34 g44 ///
            b1 b2 b3 b4 ///
            l1 l2 l3 l4 ///
            r1 r2 r3 ///
            m11 m12 m13 m14 ///
            m21 m22 m23 m24 ///
            d1 d2 d3 d4 ) nolog


            *This is the place where I get the error message


            est store quaidsNNP2

            *set trace on
            *set tracedepth 4


            *Share, price and total expenditure means
            quietly {
            foreach x of varlist w* lnp* lnexp {
            sum `x'
            scalar `x'mean=r(mean)
            }
            * Price indexes
            glo asum "_b[a1]*lnp1mean"
            forv i=2(1)5 {
            glo asum "${asum} + _b[a`i']*lnp`i'mean"
            }
            glo gsum ""
            forv i=1(1)5 {
            forv j=1(1)5 {
            glo gsum "${gsum} + 0.5*_b[g`i'`j']*lnp`i'mean*lnp`j'mean"
            }
            }
            glo ap "10.0 + ${asum} ${gsum}"
            glo bp "_b[b1]*lnp1mean"
            forv i=2(1)5 {
            glo bp "${bp} + _b[b`i']*lnp`i'mean"
            }
            glo bp "(exp(${bp}))"
            * Mus
            forv i=1(1)5 {
            glo mu`i' "_b[b`i'] + 2*_b[l`i']/${bp}*(lnexpmean-(${ap}))"
            }
            forv j=1(1)5 {
            glo gsum2`j' ""
            forv k=1(1)5 {
            glo gsum2`j' "${gsum2`j'} + _b[g`j'`k']*lnp`k'mean"
            }
            }
            }
            nlcom (a1:_b[/a1])(a2:_b[/a2])(a3:_b[/a3])(a4:_b[/a4]) (a5:1-_b[/a1]-_b[/a2]-_b[/a3]-_b[/a4]) ///
            (b1:_b[/b1])(b2:_b[/b2])(b3:_b[/b3])(b4:_b[/b4])(b5:-_b[/b1]-_b[/b2]-_b[/b3]-_b[/b4]) ///
            (g11:_b[/g11])(g12:_b[/g21])(g13:_b[/g31])(g14:_b[/g41]) ///
            (g21:_b[/g21])(g22:_b[/g22])(g23:_b[/g32])(g24:_b[/g42]) ///
            (g31:_b[/g31])(g32:_b[/g32])(g33:_b[/g33](g34:_b[/g43]) ///
            (g15:-_b[/g11]-_b[/g21]-_b[/g31]-_b[/g41]) ///

            (g25:-_b[/g21]-_b[/g22]-_b[/g32]-_b[/g42]) ///
            (g35:-_b[/g31]-_b[/g32]-_b[/g33]-_b[/g34]) ///
            (g51:-_b[/g11]-_b[/g21]-_b[/g31]-_b[/g41]) ///
            (g52:-_b[/g21]-_b[/g22]-_b[/g32]-_b[/g42]) ///
            (g53:-_b[/g31]-_b[/g32]-_b[/g33]-_b[/g34]) ///
            (g54:-_b[/g41]-_b[/g42]-_b[/g43]-_b[/g44]) ///
            (g55:-(-_b[/g11]-_b[/g21]-_b[/g31]-_b[/g41])-(-_b[/g21]-_b[/g22]-_b[/g32]-_b[/g42])-(-_b[/g31]-_b[/g32]-_b[/g33]-_b[/g34])-(-_b[/g41]-_b[/g42]-_b[/g43]-_b[/g44])) ///

            (l1:_b[/l1])(l2:_b[/l2])(l3:_b[/l3])(l4:_b[/l4])(l5:-_b[/l1]-_b[/l2]-_b[/l3]-_b[/l4]) ///
            (d1:_b[/d1]) (d2:_b[/d2]) (d3:_b[/d3]) (d4:_b[/d4]), post noheader


            * est store quaidsNNP2


            forv i=1(1)5 {
            forv j=1(1)5 {
            glo delta=cond(`i'==`j',1,0)
            glo mu`i'`j' "_b[g`i'`j'] - ${mu`i'}*(_b[a`j'] ${gsum2`j'})-_b[l`i']*_b[b`j']/${bp}*(lnexpmean - (${ap}))^2"
            cap nlcom (elasexp`i': ${mu`i'}/w`i'mean + 1) (mu`i'`j': ${mu`i'`j'}), post noheader
            if _rc {
            est restore quaidsNNP2
            qui nlcom (elasexp`i': ${mu`i'}/w`i'mean + 1) (mu`i'`j'f: (1e+2)*(${mu`i'`j'})), post noheader
            qui nlcom (elasexp`i': _b[elasexp`i']) (mu`i'`j':_b[mu`i'`j'f]/(1e+2)), post noheader
            }
            * Uncompensated price elasticity
            nlcom (elasexp`i': _b[elasexp`i']*_b[d`i']) (elu`i'`j': (_b[mu`i'`j']/w`i'mean - ${delta})*_b[d`i']) , post noheader
            * Compensated price elasticity
            nlcom (elc`i'`j': _b[elu`i'`j'] + _b[elasexp`i']*w`j'mean), noheader
            qui est restore quaidsNNP2
            }
            }
            Attached Files
            Last edited by Mindika911; 09 May 2016, 03:59.

            Comment


            • #21
              Mindika



              Please here I correct your codes it works properly



              Code:
              
              
              set more off
              program nlsuraids
              version 13.1
              syntax varlist(min=20 max=20) if, at(name)
              tokenize `varlist'
              args w1 w2 w3 w4 lnp1 lnp2 lnp3 lnp4 lnp5 lnm z1 z2 pdf1 pdf2 pdf3 pdf4 cdf1 cdf2 cdf3 cdf4
               
              tempname a1 a2 a3 a4 a5
              scalar `a1' = `at'[1,1]
              scalar `a2' = `at'[1,2]
              scalar `a3' = `at'[1,3]
              scalar `a4' = `at'[1,4]
              scalar `a5' = 1-`a1'-`a2'-`a3'-`a4'
              **********************************************************
              tempname b1 b2 b3 b4 b5
              scalar `b1' = `at'[1,5]
              scalar `b2' = `at'[1,6]
              scalar `b3' = `at'[1,7]
              scalar `b4' = `at'[1,8]
              scalar `b5' = -`b1'-`b2'-`b3'-`b4'
              **********************************************************
              tempname g11 g12 g13 g14 g15
              tempname g21 g22 g23 g24 g25
              tempname g31 g32 g33 g34 g35
              tempname g41 g42 g43 g44 g45
              tempname g51 g52 g53 g54 g55
              ********************************************************
              scalar `g11' = `at'[1,9]
              scalar `g12' = `at'[1,10]
              scalar `g13' = `at'[1,11]
              scalar `g14' = `at'[1,12]
              scalar `g15' = -`g11'-`g12'-`g13'-`g14'
              *********************************************************
              scalar `g21' = `g12'
              scalar `g22' = `at'[1,13]
              scalar `g23' = `at'[1,14]
              scalar `g24' = `at'[1,15]
              scalar `g25' = -`g21'-`g22'-`g23'-`g24'
              *********************************************************
              scalar `g31' = `g13'
              scalar `g32' = `g23'
              scalar `g33' = `at'[1,16]
              scalar `g34' = `at'[1,17]
              scalar `g35' = -`g31'-`g32'-`g33'-`g34'
              *********************************************************
              scalar `g41' = `g14'
              scalar `g42' = `g24'
              scalar `g43' = `g34'
              scalar `g44' = `at'[1,18]
              scalar `g45' = -`g41'-`g42'-`g43'-`g44'
              ******************************************************************
              scalar `g51' = `g15'
              scalar `g52' = `g25'
              scalar `g53' = `g35'
              scalar `g54' = `g45'
              scalar `g55' = -`g51'-`g52'-`g53'-`g54'
              *****************************************************************
              
              
              tempname l1 l2 l3 l4 l5
              scalar `l1' = `at'[1,19]
              scalar `l2' = `at'[1,20]
              scalar `l3' = `at'[1,21]
              scalar `l4' = `at'[1,22]
              scalar `l5' = -`l1'-`l2'-`l3'-`l4'
              
              
              **********Household demographics***************************************
              tempname eta11 eta21
              tempname eta12 eta22
              tempname eta13 eta23
              tempname eta14 eta24
              tempname eta15 eta25
              
              
              scalar `eta11' = `at'[1,23]
              scalar `eta12' = `at'[1,24]
              scalar `eta13' = `at'[1,25]
              scalar `eta14' = `at'[1,26]
              scalar `eta15' = - `eta11'-`eta12'-`eta13'-`eta14'
              
              
              scalar `eta21' = `at'[1,27]
              scalar `eta22' = `at'[1,28]
              scalar `eta23' = `at'[1,29]
              scalar `eta24' = `at'[1,30]
              scalar `eta25' = - `eta21'-`eta22'-`eta23'-`eta24'
              
              
              
              **********************************************************************************
              tempname d1 d2 d3 d4
              scalar `d1' = `at'[1,31]
              scalar `d2' = `at'[1,32]
              scalar `d3' = `at'[1,33]
              scalar `d4' = `at'[1,34]
              
              
              ************************************************
              quietly {
              tempvar lnpindex
              gen double `lnpindex' = 5 + `a1'*`lnp1' + `a2'*`lnp2' + `a3'*`lnp3' + `a4'*`lnp4' + `a5'*`lnp5'
              forvalues i = 1/5 {
              forvalues j = 1/5 {
              replace `lnpindex' = `lnpindex' + 0.5*`g`i'`j''*`lnp`i''*`lnp`j''
              }
              }
              
              
              **** The b(p,z) term in the QUAIDS model:
              tempvar bofp
              gen double `bofp' = 0
              forvalues i = 1/5 {
              replace `bofp' = `bofp' + `lnp`i''*`b`i''
              }
              
              
              replace `bofp' = exp(`bofp')
              
              
              *****************************************************************************
              replace `w1' = (`a1' + `g11'*`lnp1' + `g12'*`lnp2' + `g13'*`lnp3' + `g14'*`lnp4' + `g15'*`lnp5' + ///
              `b1'*(`lnm' - `lnpindex') + `l1'/`bofp'*(`lnm' - `lnpindex')^2 + `z1'*`eta11' + `z2'*`eta21')*`cdf1' + `d1'*`pdf1'
              
              
              replace `w2' = (`a2' + `g21'*`lnp1' + `g22'*`lnp2' + `g23'*`lnp3' + `g24'*`lnp4' + `g25'*`lnp5' + ///
              `b2'*(`lnm' - `lnpindex') + `l2'/`bofp'*(`lnm' - `lnpindex')^2 + `z1'*`eta12' + `z2'*`eta22')*`cdf2' + `d2'*`pdf2'
              
              
              replace `w3' = (`a3' + `g31'*`lnp1' + `g32'*`lnp2' + `g33'*`lnp3' + `g34'*`lnp4' + `g35'*`lnp5' + ///
              `b3'*(`lnm' - `lnpindex') + `l3'/`bofp'*(`lnm' - `lnpindex')^2 + `z1'*`eta13' + `z2'*`eta23')*`cdf3' + `d3'*`pdf3'
              
              
              replace `w4' = (`a4' + `g41'*`lnp1' + `g42'*`lnp2' + `g43'*`lnp3' + `g44'*`lnp4' + `g45'*`lnp5' + ///
              `b4'*(`lnm' - `lnpindex') + `l4'/`bofp'*(`lnm' - `lnpindex')^2 + `z1'*`eta14' + `z2'*`eta24')*`cdf4' + `d4'*`pdf4'
              }
              
              
              end
              
              
               
              nlsur aids @ w1 w2 w3 w4 lnp1 lnp2 lnp3 lnp4 lnp5 lnm z1 z2 ///
              pdf1 pdf2 pdf3 pdf4 cdf1 cdf2 cdf3 cdf4, ifgnls neq(4) parameters(a1 a2 a3 a4 b1 b2 b3 b4 g11 g12 g13 ///
              g14 g22 g23 g24 g33 g34 g44 l1 l2 l3 l4 ///
              eta11 eta12 eta13 eta14 eta21 eta22 eta23 eta24 d1 d2 d3 d4)
              
              
              ​
              Last edited by David Achiles; 09 May 2016, 18:27.

              Comment


              • #22
                please changes version 13.1 to 14

                Comment


                • #23
                  Thanks. David. Code works well. Now I am getting few problems in elasticity estimation part. I used the below routine.

                  est store quaidsNNP2

                  *set trace on
                  *set tracedepth 4

                  quietly {
                  foreach x of varlist w* lnp* lnm {
                  sum `x'
                  scalar `x'mean=r(mean)
                  }
                  * Price indexes
                  glo asum "_b[a1]*lnp1mean"
                  forv i=2(1)5 {
                  glo asum "${asum} + _b[a`i']*lnp`i'mean"
                  }
                  glo gsum ""
                  forv i=1(1)5 {
                  forv j=1(1)5 {
                  glo gsum "${gsum} + 0.5*_b[g`i'`j']*lnp`i'mean*lnp`j'mean"
                  }
                  }
                  glo ap "10.0 + ${asum} ${gsum}"
                  glo bp "_b[b1]*lnp1mean"
                  forv i=2(1)5 {
                  glo bp "${bp} + _b[b`i']*lnp`i'mean"
                  }
                  glo bp "(exp(${bp}))"
                  * Mus
                  forv i=1(1)5 {
                  glo mu`i' "_b[b`i'] + 2*_b[l`i']/${bp}*(lnmmean-(${ap}))"
                  }
                  forv j=1(1)5 {
                  glo gsum2`j' ""
                  forv k=1(1)5 {
                  glo gsum2`j' "${gsum2`j'} + _b[g`j'`k']*lnp`k'mean"
                  }
                  }
                  }

                  nlcom (a1:_b[/a1])(a2:_b[/a2])(a3:_b[/a3])(a4:_b[/a4])(a5:1-_b[/a1]-_b[/a2]-_b[/a3]-_b[/a4]) ///
                  (b1:_b[/b1])(b2:_b[/b2])(b3:_b[/b3])(b4:_b[/b4])(b5:-_b[/b1]-_b[/b2]-_b[/b3]-_b[/b4]) ///
                  (g11:_b[/g11])(g12:_b[/g12])(g13:_b[/g13])(g14:_b[/g14]) ///
                  (g21:_b[/g12])(g22:_b[/g22])(g23:_b[/g23])(g24:_b[/g24]) ///
                  (g31:_b[/g13])(g32:_b[/g23])(g33:_b[/g33])(g34:_b[/g34]) ///
                  (g41:_b[/g14])(g42:_b[/g24])(g43:_b[/g34])(g44:_b[/g44]) ///
                  (g15:-_b[/g11]-_b[/g12]-_b[/g13]-_b[/g14]) ///
                  (g25:-_b[/g12]-_b[/g22]-_b[/g23]-_b[/g24]) ///
                  (g35:-_b[/g13]-_b[/g23]-_b[/g33]-_b[/g34]) ///
                  (g45:-_b[/g14]-_b[/g24]-_b[/g34]-_b[/g44]) ///
                  (g51:-_b[/g11]-_b[/g12]-_b[/g13]-_b[/g14]) ///
                  (g52:-_b[/g12]-_b[/g22]-_b[/g23]-_b[/g24]) ///
                  (g53:-_b[/g13]-_b[/g23]-_b[/g33]-_b[/g34]) ///
                  (g54:-_b[/g14]-_b[/g24]-_b[/g34]-_b[/g44]) ///
                  (g55:-(-_b[/g11]-_b[/g12]-_b[/g13]-_b[/g14])-(-_b[/g12]-_b[/g22]-_b[/g23]-_b[/g24])-(-_b[/g13]-_b[/g23]-_b[/g33]-_b[/g34])-(-_b[/g14]-_b[/g24]-_b[/g34]-_b[/g44])) ///
                  (l1:_b[/l1])(l2:_b[/l2])(l3:_b[/l3])(l4:_b[/l4])(l5:-_b[/l1]-_b[/l2]-_b[/l3]-_b[/l4]) ///
                  (d1:_b[/d1]) (d2:_b[/d2]) (d3:_b[/d3]) (d4:_b[/d4]), post noheader


                  This works fine up to this point.

                  est store quaidsNNP2


                  forv i=1(1)5 {
                  forv j=1(1)5 {
                  glo delta=cond(`i'==`j',1,0)
                  glo mu`i'`j' "_b[g`i'`j'] - ${mu`i'}*(_b[a`j'] ${gsum2`j'})-_b[l`i']*_b[b`j']/${bp}*(lnmmean - (${ap}))^2"
                  cap nlcom (elasm`i': ${mu`i'}/w`i'mean + 1) (mu`i'`j': ${mu`i'`j'}), post noheader
                  if _rc {
                  est restore quaidsNNP2
                  qui nlcom (elasm`i': ${mu`i'}/w`i'mean + 1) (mu`i'`j'f: (1e+2)*(${mu`i'`j'})), post noheader
                  qui nlcom (elasm`i': _b[elasm`i']) (mu`i'`j':_b[mu`i'`j'f]/(1e+2)), post noheader
                  }
                  * Uncompensated price elasticity
                  nlcom (elasm`i': _b[elasm`i']*_b[d`i'])(elu`i'`j: (_b[mu`i'`j']/w`i'mean - ${delta})*_b[d`i']) , post noheader
                  * Compensated price elasticity
                  nlcom (elc`i'`j': _b[elu`i'`j'] + _b[elasm`i']*w`j'mean), noheader
                  qui est restore quaidsNNP2
                  }
                  }

                  However the above routine for the elasticity estimates gives the error message "[d1] not found".

                  Also, when I tried to include demographic coefficients (eta) in the nlcom command, it gives the error "unknown function ()
                  r(133)".



                  nlcom (a1:_b[/a1])(a2:_b[/a2])(a3:_b[/a3])(a4:_b[/a4])(a5:1-_b[/a1]-_b[/a2]-_b[/a3]-_b[/a4]) ///
                  (b1:_b[/b1])(b2:_b[/b2])(b3:_b[/b3])(b4:_b[/b4])(b5:-_b[/b1]-_b[/b2]-_b[/b3]-_b[/b4]) ///
                  (g11:_b[/g11])(g12:_b[/g12])(g13:_b[/g13])(g14:_b[/g14]) ///
                  (g21:_b[/g12])(g22:_b[/g22])(g23:_b[/g23])(g24:_b[/g24]) ///
                  (g31:_b[/g13])(g32:_b[/g23])(g33:_b[/g33])(g34:_b[/g34]) ///
                  (g41:_b[/g14])(g42:_b[/g24])(g43:_b[/g34])(g44:_b[/g44]) ///
                  (g15:-_b[/g11]-_b[/g12]-_b[/g13]-_b[/g14]) ///
                  (g25:-_b[/g12]-_b[/g22]-_b[/g23]-_b[/g24]) ///
                  (g35:-_b[/g13]-_b[/g23]-_b[/g33]-_b[/g34]) ///
                  (g45:-_b[/g14]-_b[/g24]-_b[/g34]-_b[/g44]) ///
                  (g51:-_b[/g11]-_b[/g12]-_b[/g13]-_b[/g14]) ///
                  (g52:-_b[/g12]-_b[/g22]-_b[/g23]-_b[/g24]) ///
                  (g53:-_b[/g13]-_b[/g23]-_b[/g33]-_b[/g34]) ///
                  (g54:-_b[/g14]-_b[/g24]-_b[/g34]-_b[/g44]) ///
                  (g55:-(-_b[/g11]-_b[/g12]-_b[/g13]-_b[/g14])-(-_b[/g12]-_b[/g22]-_b[/g23]-_b[/g24])-(-_b[/g13]-_b[/g23]-_b[/g33]-_b[/g34])-(-_b[/g14]-_b[/g24]-_b[/g34]-_b[/g44])) ///
                  (eta11:_b[/eta11])(eta12:_b[/eta12])(eta13:_b[/eta13])(eta14:_b[/eta14])(eta15:-_b[/eta11]-_b[/eta12]-_b[/eta13]-_b[/eta14])///
                  (eta21:_b[/eta21])(eta22:_b[/eta22])(eta23:_b[/eta23])(eta24:_b[/eta24])(eta25:-_b[/eta21]-_b[/eta22]-_b[/eta23]-_b[/eta24])///

                  (l1:_b[/l1])(l2:_b[/l2])(l3:_b[/l3])(l4:_b[/l4])(l5:-_b[/l1]-_b[/l2]-_b[/l3]-_b[/l4]) ///
                  (d1:_b[/d1]) (d2:_b[/d2]) (d3:_b[/d3]) (d4:_b[/d4]), post noheader


                  Appreciate a quick response on this.

                  Comment


                  • #24
                    Code:
                    nlcom (a1:_b[/a1])(a2:_b[/a2])(a3:_b[/a3])(a4:_b[/a4])(a5:1-_b[/a1]-_b[/a2]-_b[/a3]-_b[/a4]) ///
                    (b1:_b[/b1])(b2:_b[/b2])(b3:_b[/b3])(b4:_b[/b4])(b5:-_b[/b1]-_b[/b2]-_b[/b3]-_b[/b4]) ///
                    (g11:_b[/g11])(g12:_b[/g12])(g13:_b[/g13])(g14:_b[/g14]) ///
                    (g21:_b[/g12])(g22:_b[/g22])(g23:_b[/g23])(g24:_b[/g24]) ///
                    (g31:_b[/g13])(g32:_b[/g23])(g33:_b[/g33])(g34:_b[/g34]) ///
                    (g41:_b[/g14])(g42:_b[/g24])(g43:_b[/g34])(g44:_b[/g44]) ///
                    (g15:-_b[/g11]-_b[/g12]-_b[/g13]-_b[/g14]) ///
                    (g25:-_b[/g12]-_b[/g22]-_b[/g23]-_b[/g24]) ///
                    (g35:-_b[/g13]-_b[/g23]-_b[/g33]-_b[/g34]) ///
                    (g45:-_b[/g14]-_b[/g24]-_b[/g34]-_b[/g44]) ///
                    (g51:-_b[/g11]-_b[/g12]-_b[/g13]-_b[/g14]) ///
                    (g52:-_b[/g12]-_b[/g22]-_b[/g23]-_b[/g24]) ///
                    (g53:-_b[/g13]-_b[/g23]-_b[/g33]-_b[/g34]) ///
                    (g54:-_b[/g14]-_b[/g24]-_b[/g34]-_b[/g44]) ///
                    (g55:-(-_b[/g11]-_b[/g12]-_b[/g13]-_b[/g14])-(-_b[/g12]-_b[/g22]-_b[/g23]-_b[/g24])-(-_b[/g13]-_b[/g23]-_b[/g33]-_b[/g34])-(-_b[/g14]-_b[/g24]-_b[/g34]-_b[/g44])) ///
                    (eta11:_b[/eta11])(eta12:_b[/eta12])(eta13:_b[/eta13])(eta14:_b[/eta14])(eta15:-_b[/eta11]-_b[/eta12]-_b[/eta13]-_b[/eta14]) ///
                    (eta21:_b[/eta21])(eta22:_b[/eta22])(eta23:_b[/eta23])(eta24:_b[/eta24])(eta25:-_b[/eta21]-_b[/eta22]-_b[/eta23]-_b[/eta24]) ///
                    (l1:_b[/l1])(l2:_b[/l2])(l3:_b[/l3])(l4:_b[/l4])(l5:-_b[/l1]-_b[/l2]-_b[/l3]-_b[/l4]) ///
                    (d1:_b[/d1]) (d2:_b[/d2]) (d3:_b[/d3]) (d4:_b[/d4]), post noheader

                    I think, now it should work

                    Comment


                    • #25
                      Thanks David. All good. I tried to update the case for 6 food. I get the below error message. I have checked all through the codes, but couldn't find the exact reason.Appreciate if you could figure out. Please find below the trace and the codes.



                      ------------------------------------------------------------------- begin nlsuraids ---
                      - version 14.0
                      - syntax varlist(min=24 max=24) if, at(name)
                      - tokenize `varlist'
                      = tokenize __00000D __00000E __00000F __00000G __00000H lnp1 lnp2 lnp3 lnp4 lnp5 lnp6 l
                      > nm z1 z2 pdf1 pdf2 pdf3 pdf4 pdf5 cdf1 cdf2 cdf3 cdf4 cdf5
                      - args w1 w2 w3 w4 w5 lnp1 lnp2 lnp3 lnp4 lnp5 lnp6 lnm z1 z2 pdf1 pdf2 pdf3 pdf4 pdf5
                      > cdf1 cdf2 cdf3 cdf4 cdf5
                      - set trace on
                      - tempname a1 a2 a3 a4 a5 a6
                      - scalar `a1' = `at'[1,1]
                      = scalar __00000I = __000003[1,1]
                      - scalar `a2' = `at'[1,2]
                      = scalar __00000J = __000003[1,2]
                      - scalar `a3' = `at'[1,3]
                      = scalar __00000K = __000003[1,3]
                      - scalar `a4' = `at'[1,4]
                      = scalar __00000L = __000003[1,4]
                      - scalar `a5' = `at'[1,5]
                      = scalar __00000M = __000003[1,5]
                      - scalar `a6' = 1-`a1'-`a2'-`a3'-`a4'-`a5'
                      = scalar __00000N = 1-__00000I-__00000J-__00000K-__00000L-__00000M
                      - tempname b1 b2 b3 b4 b5 b6
                      - scalar `b1' = `at'[1,6]
                      = scalar __00000O = __000003[1,6]
                      - scalar `b2' = `at'[1,7]
                      = scalar __00000P = __000003[1,7]
                      - scalar `b3' = `at'[1,8]
                      = scalar __00000Q = __000003[1,8]
                      - scalar `b4' = `at'[1,9]
                      = scalar __00000R = __000003[1,9]
                      - scalar `b5' = `at'[1,10]
                      = scalar __00000S = __000003[1,10]
                      - scalar `b6' = -`b1'-`b2'-`b3'-`b4'-`b5'
                      = scalar __00000T = -__00000O-__00000P-__00000Q-__00000R-__00000S
                      - tempname g11 g12 g13 g14 g15 g16
                      - tempname g21 g22 g23 g24 g25 g26
                      - tempname g31 g32 g33 g34 g35 g36
                      - tempname g41 g42 g43 g44 g45 g46
                      - tempname g51 g52 g53 g54 g55 g56
                      - tempname g61 g62 g63 g64 g65 g66
                      - scalar `g11' = `at'[1,11]
                      = scalar __00000U = __000003[1,11]
                      - scalar `g12' = `at'[1,12]
                      = scalar __00000V = __000003[1,12]
                      - scalar `g13' = `at'[1,13]
                      = scalar __00000W = __000003[1,13]
                      - scalar `g14' = `at'[1,14]
                      = scalar __00000X = __000003[1,14]
                      - scalar `g15' = `at'[1,15]
                      = scalar __00000Y = __000003[1,15]
                      - scalar `g16' = -`g11'-`g12'-`g13'-`g14'-`g15'
                      = scalar __00000Z = -__00000U-__00000V-__00000W-__00000X-__00000Y
                      - scalar `g21' = `g12'
                      = scalar __000010 = __00000V
                      - scalar `g22' = `at'[1,16]
                      = scalar __000011 = __000003[1,16]
                      - scalar `g23' = `at'[1,17]
                      = scalar __000012 = __000003[1,17]
                      - scalar `g24' = `at'[1,18]
                      = scalar __000013 = __000003[1,18]
                      - scalar `g25' = `at'[1,19]
                      = scalar __000014 = __000003[1,19]
                      - scalar `g26' = -`g21'-`g22'-`g23'-`g24'-`g25'
                      = scalar __000015 = -__000010-__000011-__000012-__000013-__000014
                      - scalar `g31' = `g13'
                      = scalar __000016 = __00000W
                      - scalar `g32' = `g23'
                      = scalar __000017 = __000012
                      - scalar `g33' = `at'[1,20]
                      = scalar __000018 = __000003[1,20]
                      - scalar `g34' = `at'[1,21]
                      = scalar __000019 = __000003[1,21]
                      - scalar `g35' = `at'[1,22]
                      = scalar __00001A = __000003[1,22]
                      - scalar `g36' = -`g31'-`g32'-`g33'-`g34'-`g35'
                      = scalar __00001B = -__000016-__000017-__000018-__000019-__00001A
                      - scalar `g41' = `g14'
                      = scalar __00001C = __00000X
                      - scalar `g42' = `g24'
                      = scalar __00001D = __000013
                      - scalar `g43' = `g34'
                      = scalar __00001E = __000019
                      - scalar `g44' = `at'[1,23]
                      = scalar __00001F = __000003[1,23]
                      - scalar `g45' = `at'[1,24]
                      = scalar __00001G = __000003[1,24]
                      - scalar `g46' = -`g41'-`g42'-`g43'-`g44'-`g45'
                      = scalar __00001H = -__00001C-__00001D-__00001E-__00001F-__00001G
                      - scalar `g51' = `g15'
                      = scalar __00001I = __00000Y
                      - scalar `g52' = `g25'
                      = scalar __00001J = __000014
                      - scalar `g53' = `g35'
                      = scalar __00001K = __00001A
                      - scalar `g54' = `g45'
                      = scalar __00001L = __00001G
                      - scalar `g55' = `at'[1,25]
                      = scalar __00001M = __000003[1,25]
                      - scalar `g56' = -`g51'-`g52'-`g53'-`g54'-`g55'
                      = scalar __00001N = -__00001I-__00001J-__00001K-__00001L-__00001M
                      - scalar `g61' = `g16'
                      = scalar __00001O = __00000Z
                      - scalar `g62' = `g26'
                      = scalar __00001P = __000015
                      - scalar `g63' = `g36'
                      = scalar __00001Q = __00001B
                      - scalar `g64' = `g46'
                      = scalar __00001R = __00001H
                      - scalar `g65' = `g56'
                      = scalar __00001S = __00001N
                      - scalar `g66' = -`g61'-`g62'-`g63'-`g64'-`g65'
                      = scalar __00001T = -__00001O-__00001P-__00001Q-__00001R-__00001S
                      - tempname l1 l2 l3 l4 l5 16
                      - scalar `l1' = `at'[1,26]
                      = scalar __00001U = __000003[1,26]
                      - scalar `l2' = `at'[1,27]
                      = scalar __00001V = __000003[1,27]
                      - scalar `l3' = `at'[1,28]
                      = scalar __00001W = __000003[1,28]
                      - scalar `l4' = `at'[1,29]
                      = scalar __00001X = __000003[1,29]
                      - scalar `l5' = `at'[1,30]
                      = scalar __00001Y = __000003[1,30]
                      - scalar `l6' = -`l1'-`l2'-`l3'-`l4'-`l5'
                      = scalar = -__00001U-__00001V-__00001W-__00001X-__00001Y
                      --------------------------------------------------------------------- end nlsuraids ---
                      - }
                      - if _rc {
                      - di as error "nlsur`eqn' returned " _rc
                      = di as error "nlsuraids returned " _rc
                      nlsuraids returned 198
                      - di as error "verify that nlsur`eqn' is a function evaluator program"
                      = di as error "verify that nlsuraids is a function evaluator program"
                      verify that nlsuraids is a function evaluator program
                      - exit _rc
                      }
                      }
                      ------------------------------------------------------------------ end nlsur.Estimate ---
                      ----------------------------------------------------------------------------- end nlsur ---
                      r(198);

                      end of do-file

                      r(198);



                      The codes

                      ************************************************** ********
                      set trace on
                      set more off
                      program nlsuraids
                      version 14.0
                      syntax varlist(min=24 max=24) if, at(name)
                      tokenize `varlist'
                      args w1 w2 w3 w4 w5 lnp1 lnp2 lnp3 lnp4 lnp5 lnp6 lnm z1 z2 pdf1 pdf2 pdf3 pdf4 pdf5 cdf1 cdf2 cdf3 cdf4 cdf5

                      set trace on

                      tempname a1 a2 a3 a4 a5 a6
                      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' = 1-`a1'-`a2'-`a3'-`a4'-`a5'
                      ************************************************** ********
                      tempname b1 b2 b3 b4 b5 b6
                      scalar `b1' = `at'[1,6]
                      scalar `b2' = `at'[1,7]
                      scalar `b3' = `at'[1,8]
                      scalar `b4' = `at'[1,9]
                      scalar `b5' = `at'[1,10]
                      scalar `b6' = -`b1'-`b2'-`b3'-`b4'-`b5'
                      ************************************************** ********
                      tempname g11 g12 g13 g14 g15 g16
                      tempname g21 g22 g23 g24 g25 g26
                      tempname g31 g32 g33 g34 g35 g36
                      tempname g41 g42 g43 g44 g45 g46
                      tempname g51 g52 g53 g54 g55 g56
                      tempname g61 g62 g63 g64 g65 g66

                      ************************************************** ******
                      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' = -`g11'-`g12'-`g13'-`g14'-`g15'
                      ************************************************** *******
                      scalar `g21' = `g12'
                      scalar `g22' = `at'[1,16]
                      scalar `g23' = `at'[1,17]
                      scalar `g24' = `at'[1,18]
                      scalar `g25' = `at'[1,19]
                      scalar `g26' = -`g21'-`g22'-`g23'-`g24'-`g25'
                      ************************************************** *******
                      scalar `g31' = `g13'
                      scalar `g32' = `g23'
                      scalar `g33' = `at'[1,20]
                      scalar `g34' = `at'[1,21]
                      scalar `g35' = `at'[1,22]
                      scalar `g36' = -`g31'-`g32'-`g33'-`g34'-`g35'
                      ************************************************** *******
                      scalar `g41' = `g14'
                      scalar `g42' = `g24'
                      scalar `g43' = `g34'
                      scalar `g44' = `at'[1,23]
                      scalar `g45' = `at'[1,24]
                      scalar `g46' = -`g41'-`g42'-`g43'-`g44'-`g45'
                      ************************************************** ****************
                      scalar `g51' = `g15'
                      scalar `g52' = `g25'
                      scalar `g53' = `g35'
                      scalar `g54' = `g45'
                      scalar `g55' = `at'[1,25]
                      scalar `g56' = -`g51'-`g52'-`g53'-`g54'-`g55'
                      ************************************************** ***************
                      scalar `g61' = `g16'
                      scalar `g62' = `g26'
                      scalar `g63' = `g36'
                      scalar `g64' = `g46'
                      scalar `g65' = `g56'
                      scalar `g66' = -`g61'-`g62'-`g63'-`g64'-`g65'
                      ************************************************** ***************

                      tempname l1 l2 l3 l4 l5 16
                      scalar `l1' = `at'[1,26]
                      scalar `l2' = `at'[1,27]
                      scalar `l3' = `at'[1,28]
                      scalar `l4' = `at'[1,29]
                      scalar `l5' = `at'[1,30]
                      scalar `l6' = -`l1'-`l2'-`l3'-`l4'-`l5'

                      **********Household demographics************************************** *
                      tempname eta11 eta21
                      tempname eta12 eta22
                      tempname eta13 eta23
                      tempname eta14 eta24
                      tempname eta15 eta25
                      tempname eta16 eta26

                      scalar `eta11' = `at'[1,31]
                      scalar `eta12' = `at'[1,32]
                      scalar `eta13' = `at'[1,33]
                      scalar `eta14' = `at'[1,34]
                      scalar `eta15' = `at'[1,35]
                      scalar `eta16' = - `eta11'-`eta12'-`eta13'-`eta14'-`eta15'

                      scalar `eta21' = `at'[1,36]
                      scalar `eta22' = `at'[1,37]
                      scalar `eta23' = `at'[1,38]
                      scalar `eta24' = `at'[1,39]
                      scalar `eta25' = `at'[1,40]
                      scalar `eta26' = - `eta21'-`eta22'-`eta23'-`eta24'-`eta25'

                      ************************************************** ********************************
                      tempname d1 d2 d3 d4 d5
                      scalar `d1' = `at'[1,41]
                      scalar `d2' = `at'[1,42]
                      scalar `d3' = `at'[1,43]
                      scalar `d4' = `at'[1,44]
                      scalar `d5' = `at'[1,45]

                      ************************************************
                      quietly {
                      tempvar lnpindex
                      gen double `lnpindex' = 5 + `a1'*`lnp1' + `a2'*`lnp2' + `a3'*`lnp3' + `a4'*`lnp4' + `a5'*`lnp5' + `a6'*`lnp6'
                      forvalues i = 1/6 {
                      forvalues j = 1/6 {
                      replace `lnpindex' = `lnpindex' + 0.5*`g`i'`j''*`lnp`i''*`lnp`j''
                      }
                      }


                      **** The b(p,z) term in the QUAIDS model:
                      tempvar bofp
                      gen double `bofp' = 0
                      forvalues i = 1/6 {
                      replace `bofp' = `bofp' + `lnp`i''*`b`i''
                      }


                      replace `bofp' = exp(`bofp')


                      ************************************************** ***************************
                      replace `w1' = (`a1' + `g11'*`lnp1' + `g12'*`lnp2' + `g13'*`lnp3' + `g14'*`lnp4' + `g15'*`lnp5' + `g16'*`lnp6' + ///
                      `b1'*(`lnm' - `lnpindex') + `l1'/`bofp'*(`lnm' - `lnpindex')^2 + `z1'*`eta11' + `z2'*`eta21')*`cdf1' + `d1'*`pdf1'


                      replace `w2' = (`a2' + `g21'*`lnp1' + `g22'*`lnp2' + `g23'*`lnp3' + `g24'*`lnp4' + `g25'*`lnp5' + `g26'*`lnp6' + ///
                      `b2'*(`lnm' - `lnpindex') + `l2'/`bofp'*(`lnm' - `lnpindex')^2 + `z1'*`eta12' + `z2'*`eta22')*`cdf2' + `d2'*`pdf2'


                      replace `w3' = (`a3' + `g31'*`lnp1' + `g32'*`lnp2' + `g33'*`lnp3' + `g34'*`lnp4' + `g35'*`lnp5' + `g36'*`lnp6' + ///
                      `b3'*(`lnm' - `lnpindex') + `l3'/`bofp'*(`lnm' - `lnpindex')^2 + `z1'*`eta13' + `z2'*`eta23')*`cdf3' + `d3'*`pdf3'


                      replace `w4' = (`a4' + `g41'*`lnp1' + `g42'*`lnp2' + `g43'*`lnp3' + `g44'*`lnp4' + `g45'*`lnp5' + `g46'*`lnp6' + ///
                      `b4'*(`lnm' - `lnpindex') + `l4'/`bofp'*(`lnm' - `lnpindex')^2 + `z1'*`eta14' + `z2'*`eta24')*`cdf4' + `d4'*`pdf4'

                      replace `w5' = (`a5' + `g51'*`lnp1' + `g52'*`lnp2' + `g53'*`lnp3' + `g54'*`lnp4' + `g55'*`lnp5' + `g56'*`lnp6' + ///
                      `b5'*(`lnm' - `lnpindex') + `l5'/`bofp'*(`lnm' - `lnpindex')^2 + `z1'*`eta15' + `z2'*`eta25')*`cdf5' + `d5'*`pdf5'
                      }


                      end

                      nlsur aids @ w1 w2 w3 w4 w5 lnp1 lnp2 lnp3 lnp4 lnp5 lnp6 lnm z1 z2 ///
                      pdf1 pdf2 pdf3 pdf4 pdf5 cdf1 cdf2 cdf3 cdf4 cdf5, ifgnls neq(5) parameters(a1 a2 a3 a4 a5 b1 b2 b3 b4 b5 g11 g12 g13 ///
                      g14 g15 g22 g23 g24 g25 g33 g34 g35 g44 g45 g55 l1 l2 l3 l4 l5 ///
                      eta11 eta12 eta13 eta14 eta15 eta21 eta22 eta23 eta24 eta25 d1 d2 d3 d4 d5)


                      est store quaidsNNP2

                      *set trace on
                      *set tracedepth 4

                      quietly {
                      foreach x of varlist w* lnp* lnm {
                      sum `x'
                      scalar `x'mean=r(mean)
                      }
                      * Price indexes
                      glo asum "_b[a1]*lnp1mean"
                      forv i=2(1)6 {
                      glo asum "${asum} + _b[a`i']*lnp`i'mean"
                      }
                      glo gsum ""
                      forv i=1(1)6 {
                      forv j=1(1)6 {
                      glo gsum "${gsum} + 0.5*_b[g`i'`j']*lnp`i'mean*lnp`j'mean"
                      }
                      }
                      glo ap "10.0 + ${asum} ${gsum}"
                      glo bp "_b[b1]*lnp1mean"
                      forv i=2(1)6 {
                      glo bp "${bp} + _b[b`i']*lnp`i'mean"
                      }
                      glo bp "(exp(${bp}))"
                      * Mus
                      forv i=1(1)6 {
                      glo mu`i' "_b[b`i'] + 2*_b[l`i']/${bp}*(lnmmean-(${ap}))"
                      }
                      forv j=1(1)6 {
                      glo gsum2`j' ""
                      forv k=1(1)6 {
                      glo gsum2`j' "${gsum2`j'} + _b[g`j'`k']*lnp`k'mean"
                      }
                      }
                      }
                      nlcom (a1:_b[/a1])(a2:_b[/a2])(a3:_b[/a3])(a4:_b[/a4])(a5:_b[/a5])(a6:1-_b[/a1]-_b[/a2]-_b[/a3]-_b[/a4]-_b[/a5]) ///
                      (b1:_b[/b1])(b2:_b[/b2])(b3:_b[/b3])(b4:_b[/b4])(b5:_b[/b5])(b6:-_b[/b1]-_b[/b2]-_b[/b3]-_b[/b4]-_b[/b5]) ///
                      (g11:_b[/g11])(g12:_b[/g12])(g13:_b[/g13])(g14:_b[/g14])(g15:_b[/g15]) ///
                      (g21:_b[/g12])(g22:_b[/g22])(g23:_b[/g23])(g24:_b[/g24])(g25:_b[/g25]) ///
                      (g31:_b[/g13])(g32:_b[/g23])(g33:_b[/g33])(g34:_b[/g34])(g35:_b[/g35]) ///
                      (g41:_b[/g14])(g42:_b[/g24])(g43:_b[/g34])(g44:_b[/g44])(g45:_b[/g45]) ///
                      (g51:_b[/g15])(g52:_b[/g25])(g53:_b[/g35])(g54:_b[/g45])(g55:_b[/g55]) ///
                      (g16:-_b[/g11]-_b[/g12]-_b[/g13]-_b[/g14]-_b[/g15]) ///
                      (g26:-_b[/g12]-_b[/g22]-_b[/g23]-_b[/g24]-_b[/g25]) ///
                      (g36:-_b[/g13]-_b[/g23]-_b[/g33]-_b[/g34]-_b[/g35]) ///
                      (g46:-_b[/g14]-_b[/g24]-_b[/g34]-_b[/g44]-_b[/g45]) ///
                      (g56:-_b[/g15]-_b[/g25]-_b[/g35]-_b[/g45]-_b[/g55]) ///
                      (g61:-_b[/g11]-_b[/g12]-_b[/g13]-_b[/g14]-_b[/g15]) ///
                      (g62:-_b[/g12]-_b[/g22]-_b[/g23]-_b[/g24]-_b[/g25]) ///
                      (g63:-_b[/g13]-_b[/g23]-_b[/g33]-_b[/g34]-_b[/g35]) ///
                      (g64:-_b[/g14]-_b[/g24]-_b[/g43]-_b[/g44]-_b[/g45]) ///
                      (g65:-(-_b[/g11]-_b[/g12]-_b[/g13]-_b[/g14]-_b[/g15])-(-_b[/g12]-_b[/g22]-_b[/g23]-_b[/g24]-_b[/g25])-(-_b[/g13]-_b[/g23]-_b[/g33]-_b[/g34]-_b[/g35])-(-_b[/g14]-_b[/g24]-_b[/g34]-_b[/g44]-_b[/g45])) ///
                      (eta11:_b[/eta11])(eta12:_b[/eta12])(eta13:_b[/eta13])(eta14:_b[/eta14])(eta15:_b[/eta15])(eta16:-_b[/eta11]-_b[/eta12]-_b[/eta13]-_b[/eta14]-_b[/eta15]) ///
                      (eta21:_b[/eta21])(eta22:_b[/eta22])(eta23:_b[/eta23])(eta24:_b[/eta24])(eta25:_b[/eta25])(eta26:-_b[/eta21]-_b[/eta22]-_b[/eta23]-_b[/eta24]-_b[/eta25]) ///
                      (l1:_b[/l1])(l2:_b[/l2])(l3:_b[/l3])(l4:_b[/l4])(l5:_b[/l5])(l6:-_b[/l1]-_b[/l2]-_b[/l3]-_b[/l4]-_b[/l5]) ///
                      (d1:_b[/d1]) (d2:_b[/d2]) (d3:_b[/d3]) (d4:_b[/d4])(d5:_b[/d5])(d6:-_b[/d1]-_b[/d2]-_b[/d3]-_b[/d4]-_b[/d5]), post noheader


                      est store quaids

                      forv i=1(1)6 {
                      forv j=1(1)6 {
                      glo delta=cond(`i'==`j',1,0)
                      glo mu`i'`j' "_b[g`i'`j'] - ${mu`i'}*(_b[a`j'] ${gsum2`j'})-_b[l`i']*_b[b`j']/${bp}*(lnmmean - (${ap}))^2"
                      cap nlcom (elasm`i': ${mu`i'}/w`i'mean + 1) (mu`i'`j': ${mu`i'`j'}) (d`i': _b[d`i']), post noheader
                      if _rc {
                      qui nlcom (elasm`i': ${mu`i'}/w`i'mean + 1) (mu`i'`j'f1e+2)*(${mu`i'`j'})) (d`i': _b[d`i']) , post noheader
                      qui nlcom (elasm`i': _b[elasm`i']) (mu`i'`j':_b[mu`i'`j'f]/(1e+2)) (d`i': _b[d`i']) , post noheader
                      }

                      * Uncompensated price elasticity
                      if _b[d`i']!=0{
                      nlcom (elasm`i': _b[elasm`i']*_b[d`i']) (elu`i'`j'_b[mu`i'`j']/w`i'mean - ${delta})*_b[d`i']), post noheader
                      }
                      else {
                      display _b[d`i']
                      nlcom (elasm`i': _b[elasm`i']) (elu`i'`j'_b[mu`i'`j']/w`i'mean - ${delta})), post noheader
                      }
                      * Compensated price elasticity
                      nlcom (elc`i'`j': _b[elu`i'`j'] + _b[elasm`i']*w`j'mean), noheader
                      qui est restore quaids
                      }
                      }

                      Comment


                      • #26
                        I agree with Jorge, we could work into a more general routine for Shonkwiler and Yen method.
                        Last edited by Juan Caro; 17 May 2016, 08:54.

                        Comment


                        • #27
                          Please here I corrected codes with 6 products

                          Code:
                           set more off
                          program nlsuraids
                          version 13.1
                          syntax varlist(min=24 max=24) if, at(name)
                          tokenize `varlist'
                          args w1 w2 w3 w4 w5 lnp1 lnp2 lnp3 lnp4 lnp5 lnp6 lnm z1 z2 pdf1 pdf2 pdf3 pdf4 pdf5 cdf1 cdf2 cdf3 cdf4 cdf5
                             
                            tempname a1 a2 a3 a4 a5 a6
                          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' = 1-`a1'-`a2'-`a3'-`a4'-`a5'
                          ************************************************** ********
                          tempname b1 b2 b3 b4 b5 b6
                          scalar `b1' = `at'[1,6]
                          scalar `b2' = `at'[1,7]
                          scalar `b3' = `at'[1,8]
                          scalar `b4' = `at'[1,9]
                          scalar `b5' = `at'[1,10]
                          scalar `b6' = -`b1'-`b2'-`b3'-`b4'-`b5'
                          ************************************************** ********
                          tempname g11 g12 g13 g14 g15 g16
                          tempname g21 g22 g23 g24 g25 g26
                          tempname g31 g32 g33 g34 g35 g36
                          tempname g41 g42 g43 g44 g45 g46
                          tempname g51 g52 g53 g54 g55 g56
                          tempname g61 g62 g63 g64 g65 g66
                             
                            ************************************************** ******
                          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' = -`g11'-`g12'-`g13'-`g14'-`g15'
                           ************************************************** *******
                          scalar `g21' = `g12'
                          scalar `g22' = `at'[1,16]
                          scalar `g23' = `at'[1,17]
                          scalar `g24' = `at'[1,18]
                          scalar `g25' = `at'[1,19]
                          scalar `g26' = -`g21'-`g22'-`g23'-`g24'-`g25'
                          ************************************************** *******
                          scalar `g31' = `g13'
                          scalar `g32' = `g23'
                          scalar `g33' = `at'[1,20]
                          scalar `g34' = `at'[1,21]
                          scalar `g35' = `at'[1,22]
                          scalar `g36' = -`g31'-`g32'-`g33'-`g34'-`g35'
                          ************************************************** *******
                          scalar `g41' = `g14'
                          scalar `g42' = `g24'
                          scalar `g43' = `g34'
                          scalar `g44' = `at'[1,23]
                          scalar `g45' = `at'[1,24]
                          scalar `g46' = -`g41'-`g42'-`g43'-`g44'-`g45'
                          ************************************************** ****************
                          scalar `g51' = `g15'
                          scalar `g52' = `g25'
                          scalar `g53' = `g35'
                          scalar `g54' = `g45'
                          scalar `g55' = `at'[1,25]
                          scalar `g56' = -`g51'-`g52'-`g53'-`g54'-`g55'
                          ************************************************** ***************
                          scalar `g61' = `g16'
                          scalar `g62' = `g26'
                          scalar `g63' = `g36'
                          scalar `g64' = `g46'
                          scalar `g65' = `g56'
                          scalar `g66' = -`g61'-`g62'-`g63'-`g64'-`g65'
                           ************************************************** ***************
                             
                            tempname l1 l2 l3 l4 l5 l6
                          scalar `l1' = `at'[1,26]
                          scalar `l2' = `at'[1,27]
                          scalar `l3' = `at'[1,28]
                          scalar `l4' = `at'[1,29]
                          scalar `l5' = `at'[1,30]
                          scalar `l6' = -`l1'-`l2'-`l3'-`l4'-`l5'
                             
                             **********Household demographics************************************** *
                          tempname eta11 eta21
                          tempname eta12 eta22
                          tempname eta13 eta23
                          tempname eta14 eta24
                          tempname eta15 eta25
                          tempname eta16 eta26
                             
                            scalar `eta11' = `at'[1,31]
                          scalar `eta12' = `at'[1,32]
                          scalar `eta13' = `at'[1,33]
                          scalar `eta14' = `at'[1,34]
                          scalar `eta15' = `at'[1,35]
                          scalar `eta16' = - `eta11'-`eta12'-`eta13'-`eta14'-`eta15'
                             
                            scalar `eta21' = `at'[1,36]
                          scalar `eta22' = `at'[1,37]
                          scalar `eta23' = `at'[1,38]
                          scalar `eta24' = `at'[1,39]
                          scalar `eta25' = `at'[1,40]
                          scalar `eta26' = - `eta21'-`eta22'-`eta23'-`eta24'-`eta25'
                             
                             ************************************************** ********************************
                          tempname d1 d2 d3 d4 d5
                          scalar `d1' = `at'[1,41]
                          scalar `d2' = `at'[1,42]
                          scalar `d3' = `at'[1,43]
                          scalar `d4' = `at'[1,44]
                          scalar `d5' = `at'[1,45]
                             
                             ************************************************
                          quietly {
                          tempvar lnpindex
                          gen double `lnpindex' = 5 + `a1'*`lnp1' + `a2'*`lnp2' + `a3'*`lnp3' + `a4'*`lnp4' + `a5'*`lnp5' + `a6'*`lnp6'
                          forvalues i = 1/6 {
                          forvalues j = 1/6 {
                          replace `lnpindex' = `lnpindex' + 0.5*`g`i'`j''*`lnp`i''*`lnp`j''
                          }
                          }
                             
                            
                          **** The b(p,z) term in the QUAIDS model:
                          tempvar bofp
                          gen double `bofp' = 0
                          forvalues i = 1/6 {
                          replace `bofp' = `bofp' + `lnp`i''*`b`i''
                          }
                             
                            
                          replace `bofp' = exp(`bofp')
                             
                             ************************************************** ***************************
                          replace `w1' = (`a1' + `g11'*`lnp1' + `g12'*`lnp2' + `g13'*`lnp3' + `g14'*`lnp4' + `g15'*`lnp5' + `g16'*`lnp6' + ///
                          `b1'*(`lnm' - `lnpindex') + `l1'/`bofp'*(`lnm' - `lnpindex')^2 + `z1'*`eta11' + `z2'*`eta21')*`cdf1' + `d1'*`pdf1'
                             
                            
                          replace `w2' = (`a2' + `g21'*`lnp1' + `g22'*`lnp2' + `g23'*`lnp3' + `g24'*`lnp4' + `g25'*`lnp5' + `g26'*`lnp6' + ///
                          `b2'*(`lnm' - `lnpindex') + `l2'/`bofp'*(`lnm' - `lnpindex')^2 + `z1'*`eta12' + `z2'*`eta22')*`cdf2' + `d2'*`pdf2'
                             
                            
                          replace `w3' = (`a3' + `g31'*`lnp1' + `g32'*`lnp2' + `g33'*`lnp3' + `g34'*`lnp4' + `g35'*`lnp5' + `g36'*`lnp6' + ///
                          `b3'*(`lnm' - `lnpindex') + `l3'/`bofp'*(`lnm' - `lnpindex')^2 + `z1'*`eta13' + `z2'*`eta23')*`cdf3' + `d3'*`pdf3'
                             
                            
                          replace `w4' = (`a4' + `g41'*`lnp1' + `g42'*`lnp2' + `g43'*`lnp3' + `g44'*`lnp4' + `g45'*`lnp5' + `g46'*`lnp6' + ///
                          `b4'*(`lnm' - `lnpindex') + `l4'/`bofp'*(`lnm' - `lnpindex')^2 + `z1'*`eta14' + `z2'*`eta24')*`cdf4' + `d4'*`pdf4'
                             
                            replace `w5' = (`a5' + `g51'*`lnp1' + `g52'*`lnp2' + `g53'*`lnp3' + `g54'*`lnp4' + `g55'*`lnp5' + `g56'*`lnp6' + ///
                          `b5'*(`lnm' - `lnpindex') + `l5'/`bofp'*(`lnm' - `lnpindex')^2 + `z1'*`eta15' + `z2'*`eta25')*`cdf5' + `d5'*`pdf5'
                          }
                             
                            
                          end
                             
                            nlsur aids @ w1 w2 w3 w4 w5 lnp1 lnp2 lnp3 lnp4 lnp5 lnp6 lnm z1 z2 ///
                          pdf1 pdf2 pdf3 pdf4 pdf5 cdf1 cdf2 cdf3 cdf4 cdf5, ifgnls neq(5) parameters(a1 a2 a3 a4 a5 b1 b2 b3 b4 b5 g11 g12 g13 ///
                          g14 g15 g22 g23 g24 g25 g33 g34 g35 g44 g45 g55 l1 l2 l3 l4 l5 ///
                          eta11 eta12 eta13 eta14 eta15 eta21 eta22 eta23 eta24 eta25 d1 d2 d3 d4 d5)
                             
                             
                           est store quaids
                             
                             *set trace on
                           *set tracedepth 4
                             
                             quietly {
                           foreach x of varlist w* lnp* lnm {
                           sum `x'
                           scalar `x'mean=r(mean)
                           }
                           * Price indexes
                           glo asum "_b[a1]*lnp1mean"
                           forv i=2(1)6 {
                           glo asum "${asum} + _b[a`i']*lnp`i'mean"
                           }
                           glo gsum ""
                           forv i=1(1)6 {
                           forv j=1(1)6 {
                           glo gsum "${gsum} + 0.5*_b[g`i'`j']*lnp`i'mean*lnp`j'mean"
                           }
                           }
                           glo ap "5.0 + ${asum} ${gsum}"
                           glo bp "_b[b1]*lnp1mean"
                           forv i=2(1)6 {
                           glo bp "${bp} + _b[b`i']*lnp`i'mean"
                           }
                           glo bp "(exp(${bp}))"
                           * Mus
                           forv i=1(1)6 {
                           glo mu`i' "_b[b`i'] + 2*_b[l`i']/${bp}*(lnmmean-(${ap}))"
                           }
                           forv j=1(1)6 {
                           glo gsum2`j' ""
                           forv k=1(1)6 {
                           glo gsum2`j' "${gsum2`j'} + _b[g`j'`k']*lnp`k'mean"
                           }
                           }
                           }
                           nlcom (a1:_b[/a1])(a2:_b[/a2])(a3:_b[/a3])(a4:_b[/a4])(a5:_b[/a5])(a6:1-_b[/a1]-_b[/a2]-_b[/a3]-_b[/a4]-_b[/a5]) ///
                           (b1:_b[/b1])(b2:_b[/b2])(b3:_b[/b3])(b4:_b[/b4])(b5:_b[/b5])(b6:-_b[/b1]-_b[/b2]-_b[/b3]-_b[/b4]-_b[/b5]) ///
                           (g11:_b[/g11])(g12:_b[/g12])(g13:_b[/g13])(g14:_b[/g14])(g15:_b[/g15]) ///
                           (g21:_b[/g12])(g22:_b[/g22])(g23:_b[/g23])(g24:_b[/g24])(g25:_b[/g25]) ///
                           (g31:_b[/g13])(g32:_b[/g23])(g33:_b[/g33])(g34:_b[/g34])(g35:_b[/g35]) ///
                           (g41:_b[/g14])(g42:_b[/g24])(g43:_b[/g34])(g44:_b[/g44])(g45:_b[/g45]) ///
                           (g51:_b[/g15])(g52:_b[/g25])(g53:_b[/g35])(g54:_b[/g45])(g55:_b[/g55]) ///
                           (g16:-_b[/g11]-_b[/g12]-_b[/g13]-_b[/g14]-_b[/g15]) ///
                           (g26:-_b[/g12]-_b[/g22]-_b[/g23]-_b[/g24]-_b[/g25]) ///
                           (g36:-_b[/g13]-_b[/g23]-_b[/g33]-_b[/g34]-_b[/g35]) ///
                           (g46:-_b[/g14]-_b[/g24]-_b[/g34]-_b[/g44]-_b[/g45]) ///
                           (g56:-_b[/g15]-_b[/g25]-_b[/g35]-_b[/g45]-_b[/g55]) ///
                           (g61:-_b[/g11]-_b[/g12]-_b[/g13]-_b[/g14]-_b[/g15]) ///
                           (g62:-_b[/g12]-_b[/g22]-_b[/g23]-_b[/g24]-_b[/g25]) ///
                           (g63:-_b[/g13]-_b[/g23]-_b[/g33]-_b[/g34]-_b[/g35]) ///
                           (g64:-_b[/g14]-_b[/g24]-_b[/g43]-_b[/g44]-_b[/g45]) ///
                           (g65:-(-_b[/g11]-_b[/g12]-_b[/g13]-_b[/g14]-_b[/g15])- ///
                           (-_b[/g12]-_b[/g22]-_b[/g23]-_b[/g24]-_b[/g25])- ///
                           (-_b[/g13]-_b[/g23]-_b[/g33]-_b[/g34]-_b[/g35])- ///
                           (-_b[/g14]-_b[/g24]-_b[/g34]-_b[/g44]-_b[/g45])) ///
                           (eta11:_b[/eta11])(eta12:_b[/eta12])(eta13:_b[/eta13])(eta14:_b[/eta14])(eta15:_b[/eta15]) ///
                           (eta16:-_b[/eta11]-_b[/eta12]-_b[/eta13]-_b[/eta14]-_b[/eta15]) ///
                           (eta21:_b[/eta21])(eta22:_b[/eta22])(eta23:_b[/eta23])(eta24:_b[/eta24])(eta25:_b[/eta25]) ///
                           (eta26:-_b[/eta21]-_b[/eta22]-_b[/eta23]-_b[/eta24]-_b[/eta25]) ///
                           (l1:_b[/l1])(l2:_b[/l2])(l3:_b[/l3])(l4:_b[/l4])(l5:_b[/l5]) ///
                           (l6:-_b[/l1]-_b[/l2]-_b[/l3]-_b[/l4]-_b[/l5]) ///
                           (d1:_b[/d1]) (d2:_b[/d2]) (d3:_b[/d3]) (d4:_b[/d4])(d5:_b[/d5]), post noheader
                             
                            
                           est store quaids
                             
                             forv i=1(1)6 {
                           forv j=1(1)6 {
                           glo delta=cond(`i'==`j',1,0)
                           glo mu`i'`j' "_b[g`i'`j'] - ${mu`i'}*(_b[a`j'] ${gsum2`j'})-_b[l`i']*_b[b`j']/${bp}*(lnmmean - (${ap}))^2"
                           cap nlcom (elasm`i': ${mu`i'}/w`i'mean + 1) (mu`i'`j': ${mu`i'`j'}) (d`i': _b[d`i']), post noheader
                           if _rc {
                           qui nlcom (elasm`i': ${mu`i'}/w`i'mean + 1) (mu`i'`j'f1e+2)*(${mu`i'`j'})) (d`i': _b[d`i']) , post noheader
                           qui nlcom (elasm`i': _b[elasm`i']) (mu`i'`j':_b[mu`i'`j'f]/(1e+2)) (d`i': _b[d`i']) , post noheader
                           }
                             
                             * Uncompensated price elasticity
                           if _b[d`i']!=0{
                           nlcom (elasm`i': _b[elasm`i']*_b[d`i']) (elu`i'`j'_b[mu`i'`j']/w`i'mean - ${delta})*_b[d`i']), post noheader
                           }
                           else {
                           display _b[d`i']
                           nlcom (elasm`i': _b[elasm`i']) (elu`i'`j'_b[mu`i'`j']/w`i'mean - ${delta})), post noheader
                           }
                           * Compensated price elasticity
                           nlcom (elc`i'`j': _b[elu`i'`j'] + _b[elasm`i']*w`j'mean), noheader
                           qui est restore quaids
                           }
                           }

                          Comment


                          • #28
                            For those who got problems in QUAIDS with censoring and demographic variables, the above 2 code sets for 5 food and 6 food with 2 demographic variables corrected by DAVID would be useful. I checked with my data and these codes are working fine. However, incorporating more variables (either food or demographics) would give memory problems in Stata. For your information, these elasticity codes have to be updated with demographic coefficients. If anyone has already done it, please share it. I have included my routines for generating pdf and cdf for censoring.

                            *Probit regressions for censoring
                            gen pw1=w1
                            replace pw1=1 if pw1>0

                            gen pw2=w2
                            replace pw2=1 if pw2>0

                            gen pw3=w3
                            replace pw3=1 if pw3>0

                            gen pw4=w4
                            replace pw4=1 if pw4>0

                            gen pw5=w5
                            replace pw5=1 if pw5>0

                            gen pw6=w6
                            replace pw6=1 if pw6>0

                            probit pw1 sex age hhsize nchild gov semi pvt emp own fam oth D1-D18 M1-M11 rural estate aginc debt samurdi
                            predict predw1, xb
                            gen pdf1 = normalden(predw1)
                            gen cdf1 = normprob(predw1)

                            probit pw2 sex age hhsize nchild gov semi pvt emp own fam oth D1-D18 M1-M11 rural estate aginc debt samurdi
                            predict predw2, xb
                            gen pdf2 = normalden(predw2)
                            gen cdf2 = normprob(predw2)

                            probit pw3 sex age hhsize nchild gov semi pvt emp own fam oth D1-D18 M1-M11 rural estate aginc debt samurdi
                            predict predw3, xb
                            gen pdf3 = normalden(predw3)
                            gen cdf3 = normprob(predw3)

                            probit pw4 sex age hhsize nchild gov semi pvt emp own fam oth D1-D18 M1-M11 rural estate aginc debt samurdi
                            predict predw4, xb
                            gen pdf4 = normalden(predw4)
                            gen cdf4 = normprob(predw4)

                            probit pw5 sex age hhsize nchild gov semi pvt emp own fam oth D1-D18 M1-M11 rural estate aginc debt samurdi
                            predict predw5, xb
                            gen pdf5 = normalden(predw5)
                            gen cdf5 = normprob(predw5)

                            probit pw6 sex age hhsize nchild gov semi pvt emp own fam oth D1-D18 M1-M11 rural estate aginc debt samurdi
                            predict predw6, xb
                            gen pdf6 = normalden(predw6)
                            gen cdf6 = normprob(predw6)

                            Comment


                            • #29
                              I agree with Mindika911 and Jorge, however, I think all codes have to be corrected and updated in term incorporating the two-step censoring and demographic variables ( Ray's approach,1983), because posted code above is not considered based on Ray's approach.

                              ​I am working in this issue and hope soon will finish it

                              Comment


                              • #30
                                Hello everyone,

                                I was looking on the elasticities formulas on the code above (Shonkwiler and Yen approach), and I have trouble understanding how you reach to that reduced form. In particular, in the article: A Food Demand System Estimation for Uganda (Ole Boysen, 2012), there are the formulas for elasticities under censored model (eq 24 - 26) and I'm having a hard time trying to match. Did anyone derived the formula explicitly?

                                Comment

                                Working...
                                X