Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Nonlinear equation


From   smerryman@kc.rr.com
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Nonlinear equation
Date   Tue, 31 Jan 2006 12:03:58 -0600

One possibility would to -expand 2- the dataset and add a small amount 
of noise to the dependent variable.  For example:

clear
set obs 10
gen id = _n
gen double X = .
gen double be = 10 + 10*uniform()
gen double la = 2.5 + uniform()
gen double wespe = 2.5 + uniform()
gen double wvari = 2.5 + uniform()


expand 2
bysor id: replace be = be + .00000001 if _n == 2
count

forv i = 2(2) `=r(N)' {
qui {
    local j = `i' - 1
    
    nl (be = {X=1} - (la*((wespe*exp(0.5*wvari)*norm(ln({X} ) /// 
    -ln(wespe)-wvari))/wvari^.5) - ({X}*norm(ln({X})) -  /// 
    {X}*norm((ln({X})-ln(wespe))/wvari^0.5))  ))  in `j'/`i'
    
    replace X = _b[/X] in `j'/`i'
    }
}

duplicates drop id, force

gen double test = X - be - (la*((wespe*exp(0.5*wvari)*norm(ln(X) /// 
    -ln(wespe)-wvari))/wvari^.5) - (X*norm(ln(X)) - /// 
    X*norm((ln(X)-ln(wespe))/wvari^0.5)) )
l, noobs



. clear

. set obs 10
obs was 0, now 10

. gen id = _n

. gen double X = .
(10 missing values generated)

. gen double be = 10 + 10*uniform()

. gen double la = 2.5 + uniform()

. gen double wespe = 2.5 + uniform()

. gen double wvari = 2.5 + uniform()

. 
. 
. expand 2
(10 observations created)

. bysor id: replace be = be + .00000001 if _n == 2
(10 real changes made)

. count
   20

. 
. forv i = 2(2) `=r(N)' {
  2. qui {
  3.         local j = `i' - 1
  4.         nl (be = {X=1} - (la*((wespe*exp(0.5*wvari)*norm(ln
({X} ) /// 
>         -ln(wespe)-wvari))/wvari^.5) - ({X}*norm(ln({X})) -  /// 
>         {X}*norm((ln({X})-ln(wespe))/wvari^0.5))  ))  in `j'/`i'
  5.         replace X = _b[/X] in `j'/`i'
  6.         }
  7. }

. 
. duplicates drop id, force

Duplicates in terms of id

(10 observations deleted)

. //Test
. gen double test =    X - be - (la*((wespe*exp(0.5*wvari)*norm(ln
(X) /// 
>         -ln(wespe)-wvari))/wvari^.5) - (X*norm(ln(X)) - /// 
>         X*norm((ln(X)-ln(wespe))/wvari^0.5)) )

. l, noobs

  +--------------------------------------------------------------------
--------+
  | id           X          be          la       wespe       
wvari        test |
  |--------------------------------------------------------------------
--------|
  |  1   13.243148   13.101069   2.7776166    3.123684   2.5789619   
5.000e-09 |
  |  2    12.91888   14.241573   2.8396291   2.8949698   3.1121321   
5.000e-09 |
  |  3   12.367111   14.218242   3.3797715   2.7676705   3.4745156   
4.989e-09 |
  |  4   24.262301   19.075531   3.3698243   3.0092984   2.5247363   
5.000e-09 |
  |  5   19.050771   18.731844   2.9128101   3.1045931   2.9258794   
5.000e-09 |
  |--------------------------------------------------------------------
--------|
  |  6    12.60056   14.491571   2.7960302   2.8251088   3.3989561   
4.993e-09 |
  |  7   8.6867876   10.236076   3.4760952   2.5443994   3.2996814   
4.996e-09 |
  |  8    16.66885   18.424199   2.9968444   3.1121078   3.3819863   
5.000e-09 |
  |  9   10.029968   11.252171   3.2660736   3.2177827   2.8250082   
5.000e-09 |
  | 10   14.449689   16.639751   3.3539697   3.3019271   3.4364441   
4.962e-09 |
  +--------------------------------------------------------------------
--------+

. 


Hope this helps,

Scott


----- Original Message -----
From: cortega <cortega@fedea.es>
Date: Tuesday, January 31, 2006 9:47 am
Subject: st: Nonlinear equation

> I would like to solve the following nonlinear equation for each 
> individual in my sample. 
>  
> 0=X-be-(la/r)*(wespe*exp(0.5*wvari)*norm(ln(X)-ln(wespe)-
> wvari)/((wvari)^(0.5)))-(X)*norm((ln(X)-
> ln(wespe))/((wvari)^(0.5)))) 
>  
> where "X" is the parameter I want to obtain and "be", "la", 
> "wespe", "wvari" are variables that vary across individuals. 
> I have tried using the nl command. The problem is that I only have 
> one observation per individual and it could not solve the 
> equation. 
>  
> Thank you very much,
>  
> Carolina Ortega 
>  


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2021 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index