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]
st: mle of panel data with fixed effect
From
王武俊 <[email protected]>
To
[email protected]
Subject
st: mle of panel data with fixed effect
Date
Fri, 28 Oct 2011 20:08:17 +0800 (CST)
Hello. I want to estimate the model: y_it=x_it'*beta1+sqrt(exp(x_it'*beta2))*epislon_it+alfa_i. i stands for individual and t stands for time. epsilon_it~N(0,1), alfa_it~N(0,sigma). beta1 and beta2 are the parameters I want to get. First I sum the above equation within i and divide it by T.Then I use the above equation to subtract the the equation we just get to get an eqution without alfa_i.That is:
y_it-summation(y_is)/T=x_it'*beta-summation(x_is'*beta1)/T+sqrt(exp(x_it'*beta2))*epislon_it-summation{sqrt(exp(x_is'*beta2))*epislon_is}/T s=1,,T
And I drop the last observation of each i.So t=1,,,T-1. Below is the mle I have written:
program mlefe
version 11
args todo b lnf
tempvar theta1 theta2 //theta1=xb1,theta2=xb2
mleval `theta1'=`b',eq(1)
mleval `theta2'=`b',eq(2)
local y "$ML_y1"
tempvar u usum u1 a idnum tnum lnl last
tempname ma mu1 diag t1 t2 t3 V omega inv uvu mat1
sort $panel $year
gen double `u'=`y'-`theta1'
by $panel: egen double `usum'=mean(`u')
gen double `u1'=`u'-`usum' //demean
gen double `a'=exp(`theta2')
sort $year $panel // generate N
by $year: gen `idnum'=_n
sum `idnum'
local N=r(max)
sort $panel $year // generate T
by $panel: gen `tnum'=_n
sum `tnum'
local T=r(max)
sort $panel $year
gen double `lnl'=.
local i=1
while `i'<`N'+1 {
mkmat `a' `u1' if `idnum'==`i',mat(`mat1')
matrix `ma'=`mat1'[1...,1]
matrix `mu1'=`mat1'[1..`T'-1,2]
matrix `diag'=diag(`ma')
matrix `t1'=J(1,`T',1)#`ma'
matrix `t2'=-(`t1'+`t1'')/`T'
matrix `t3'=J(`T',`T',1)#(J(1,`T',1)*`ma'/(`T'^2))
matrix `V'=`diag'+`t2'+`t3'
matrix `omega'=`V'[1..`T'-1,1..`T'-1] //variance matix omega
matrix `inv'=invsym(`omega')
matrix `uvu'=`mu1''*`inv'*`mu1'
matrix list `uvu'
replace `lnl'=-0.5*(`T'-1)*ln(2*3.1415926)-0.5*ln(det(`omega'))-0.5*trace(`uvu') if `idnum'==`i' //log-likelihood of the ith panel
local i=`i'+1
}
by $panel: gen byte `last'=(_n==_N)
mlsum `lnf'=`lnl' if `last' // put log-likelihood of each panel on the last observation within the panel
end
clear
set more off
set obs 100
set seed 12345
gen x = invnormal(uniform())
gen id = 1 + floor((_n - 1)/10)
bys id: gen fe = invnormal(uniform())
bys id: replace fe = fe[1]
bys id: gen year=_n
gen y = 3*x+3 + fe + invnormal(uniform())*exp(0.3*x+0.3)^0.5 //generate simulation data
global panel="id"
global year="year"
ml model d0 mlefe (y =x) (x)
ml check
ml search
ml maximize
After the execution, the result is:
Test 1: Calling mlefe to check if it computes log likelihood and
does not alter coefficient vector...
Passed.
Test 2: Calling mlefe again to check if the same log likelihood value is
returned...
Passed.
Test 3: Calling mlefe to check if 1st derivatives are computed...
test not relevant for type d0 evaluators.
Test 4: Calling mlefe again to check if the same 1st derivatives are
returned...
test not relevant for type d0 evaluators.
Test 5: Calling mlefe to check if 2nd derivatives are computed...
test not relevant for type d0 evaluators.
Test 6: Calling mlefe again to check if the same 2nd derivatives are
returned...
test not relevant for type d0 evaluators.
------------------------------------------------------------------------------
Searching for alternate values for the coefficient vector to verify that mlefe
returns different results when fed a different coefficient vector:
Searching...
initial: log likelihood = -<inf> (could not be evaluated)
searching for feasible values +
feasible: log likelihood = -225.61221
improving initial values ......+...
improve: log likelihood = -225.0945
continuing with tests...
------------------------------------------------------------------------------
Test 7: Calling mlefe to check log likelihood at the new values...
FAILED; mlefe returned error 504.
Can anybody help me with it? Thank you in advance!
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/