***Using Maximum Likelihood Estimation in Practise
*MSc Economics for Development, MLE Class
*Given by Selma Telalagic, based on Simon Quinn's class in the previous year
*Get a feel for the data
clear
use sa_wage_curve_1
sum wphy logwphy edyrs
graph twoway scatter wphy edyrs
graph twoway scatter logwphy edyrs
*Using logs
reg wphy edyrs
rvpplot edyrs
reg logwphy edyrs
rvpplot edyrs
***Using MLE
*Define the PROGRAM
version 9
program MLE
args lfn beta sigma
quietly replace `lfn' = ln(normalden($ML_y1, `beta', `sigma'))
*normalden is the normal distribution function (variable, mean, st dev)
*In above line, you can also directly type in the log-likelihood function
*It is sufficient to specify the log-likelihood for one observation; Stata realises it should sum them
end
*SPECIFY THE MODEL
ml model lf MLE (logwphy = edyrs) /sigma,tech(bfgs 5 dfp 5 nr 5 bhhh 5)
*lf means linear form; the observations are assumed independent
*both derivatives calculated numerically
*two possible options at this stage: "robust" generates heteroscedasticity-corrected standard errors and "cluster(varname)" generates cluster-corrected standard errors
*tech specifies which algorithm to use for maximising the log-likelihood and for how many iterations; optional, can create kinks in log-likelihood function
*CHECK the program for errors (whether it is able to calculated the log-likelihood function and the first and second derivatives)
ml check
*SEARCH for a starting value (Stata will either choose a starting value of 0, or a random number, so either way ml search is better)
ml search
*MAXIMISE the log-likelihood
ml max
*Compare to OLS
reg wphy edyrs
*GRAPH the convergence path
ml model lf MLE (logwphy = edyrs) /sigma,tech(bfgs 5 dfp 5 nr 5 bhhh 5)
ml search
ml max
ml graph
***Wald test
test edyrs
***LR test
*Define constraint
constraint 1 [eq1]edyrs = 0
*Unrestricted model
ml model lf MLE (logwphy = edyrs) /sigma,tech(bfgs 5 dfp 5 nr 5 bhhh 5)
ml search
ml max
lrtest, saving(0)
*Restricted model
ml model lf MLE (logwphy = edyrs) /sigma,tech(bfgs 5 dfp 5 nr 5 bhhh 5) constraint(1)
ml search
ml max
*Run test
lrtest
***Other useful commands
*Drop a program in memory
program drop MLE