Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Two seemingly similar NL estimations give different results
From
Michael Boehm <[email protected]>
To
[email protected]
Subject
Re: st: Two seemingly similar NL estimations give different results
Date
Thu, 23 Jan 2014 10:14:41 +0100
Dear Nick,
Thanks for your answer. I am not quite sure I understand it though.
All the parameters we estimate are free to vary in both
implementations of the -nl- command (more detailed code below), but
the estimates are different. Or do I misunderstand something?
program nlexppremia
version 13.1
syntax varlist(min=4 max=4) [aw fw iw] if, at(name)
// Retrieve variables
local lhs: word 1 of `varlist'
local s: word 2 of `varlist'
local j: word 3 of `varlist'
local e: word 4 of `varlist'
// Retrieve parameters, stored in "at"
tempname bG pi lambdaL1_0 lambdaL2_0 lambdaEL1_0 lambdaEL2_0
lambdaL1_1 lambdaL2_1 lambdaEL1_1 lambdaEL2_1
scalar `bG' = `at'[1,1]
scalar `pi' = `at'[1,2]
scalar `lambdaL1_0' = `at'[1,3]
scalar `lambdaL2_0' = `at'[1,4]
scalar `lambdaEL1_0' = `at'[1,5]
scalar `lambdaEL2_0' = `at'[1,6]
scalar `lambdaL1_1' = `at'[1,7]
scalar `lambdaL2_1' = `at'[1,8]
scalar `lambdaEL1_1' = `at'[1,9]
scalar `lambdaEL2_1' = `at'[1,10]
// replace the lhs directly:
replace `lhs' = `bG'+(`lambdaL1_0'*j+`lambdaL2_0'*j^2) +
log(1+`pi'*exp(`lambdaEL1_0'*j+`lambdaEL2_0'*j^2)*`e') if `s'==0
replace `lhs' = `bG'+(`lambdaL1_1'*j+`lambdaL2_1'*j^2) +
log(1+`pi'*exp(`lambdaEL1_1'*j+`lambdaEL2_1'*j^2)*`e') if `s'==1
end
nl exppremia @ lhs s j e, nparameters(10)
********************************************************************
program nlexppremia
version 13.1
syntax varlist(min=4 max=4) [aw fw iw] if, at(name)
// Retrieve variables
local lhs: word 1 of `varlist'
local s: word 2 of `varlist'
local j: word 3 of `varlist'
local e: word 4 of `varlist'
// Retrieve parameters, stored in "at"
tempname bG pi lambdaL1_0 lambdaL2_0 lambdaEL1_0 lambdaEL2_0
lambdaL1_1 lambdaL2_1 lambdaEL1_1 lambdaEL2_1
scalar `bG' = `at'[1,1]
scalar `pi' = `at'[1,2]
scalar `lambdaL1_0' = `at'[1,3]
scalar `lambdaL2_0' = `at'[1,4]
scalar `lambdaEL1_0' = `at'[1,5]
scalar `lambdaEL2_0' = `at'[1,6]
scalar `lambdaL1_1' = `at'[1,7]
scalar `lambdaL2_1' = `at'[1,8]
scalar `lambdaEL1_1' = `at'[1,9]
scalar `lambdaEL2_1' = `at'[1,10]
// Compute RHS of regression
tempvar RHS
qui gen `RHS'=.
replace `RHS' = `bG'+(`lambdaL1_0'*j+`lambdaL2_0'*j^2) +
log(1+`pi'*exp(`lambdaEL1_0'*j+`lambdaEL2_0'*j^2)*`e') if `s'==0
replace `RHS' = `bG'+(`lambdaL1_1'*j+`lambdaL2_1'*j^2) +
log(1+`pi'*exp(`lambdaEL1_1'*j+`lambdaEL2_1'*j^2)*`e') if `s'==1
//Assign lhs=rhs
replace `lhs' = `RHS'
end
nl exppremia @ lhs s j e, nparameters(10)
********************************************************************
On Thu, Jan 23, 2014 at 9:50 AM, Nick Cox <[email protected]> wrote:
> The key question is what in your code is free to vary? Only parameters
> that are free to vary can be estimated by -nl-; the others are fixed
> constants.
>
> Nick
> [email protected]
>
>
> On 23 January 2014 08:44, Michael Boehm <[email protected]> wrote:
>> Dear all,
>>
>> We are struggling with nonlinear least square regression with a
>> function evaluator programme. We don’t understand why assigning to the
>> left-hand side variable, lhs, directly an expression for the
>> right-hand side is different to first computing the right-hand side
>> variable, rhs, and then assigning lhs=rhs.
>> Both residual SS and parameter estimates differ across the two
>> scenarios. A sketch of the two codes, which we thought would give the
>> same results are:
>>
>> program nltest_1
>>
>> version 13.1
>> syntax varlist(min=4 max=4) [aw fw iw]
>> if, at(name)
>>
>> // Retrieve variables
>> local lhs: word 1 of `varlist'
>> local s: word 2 of `varlist'
>> local j: word 3 of `varlist'
>> local e: word 4 of `varlist'
>>
>> // replace the lhs directly:
>> replace `lhs'= <Function of Parameters
>> and Variables> if `s'==0
>> replace `lhs'= <Function of Parameters
>> and Variables> if `s'==1
>> end
>>
>> ********************************************************************
>> program nltest_1
>>
>> version 13.1
>> syntax varlist(min=4 max=4) [aw fw iw]
>> if, at(name)
>>
>> // Retrieve variables
>> local lhs: word 1 of `varlist'
>> local s: word 2 of `varlist'
>> local j: word 3 of `varlist'
>> local e: word 4 of `varlist'
>>
>> // Compute RHS of regression
>> tempvar RHS
>> replace `RHS'= <Function of Parameters
>> and Variables> if `s'==0
>> replace `RHS'= <Function of Parameters
>> and Variables> if `s'==1
>>
>> //Assign lhs=rhs.
>> replace `lhs' = `RHS'
>>
>> end
>> ********************************************************************
>>
>> *
>> * For searches and help try:
>> * http://www.stata.com/help.cgi?search
>> * http://www.stata.com/support/faqs/resources/statalist-faq/
>> * http://www.ats.ucla.edu/stat/stata/
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/faqs/resources/statalist-faq/
> * http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/