* Open log file log using ML_in_MATA.log, replace * File-Name: OLS_in_MATA.do * Date: February 8, 2009 * Author: Alex Herzog * Purpose: Estimating OLS with ML in MATA * Data Used: tab9s (Jacobson and Dimock) * Output File: - * Data Output: - * Machine: Alex's MacBook * Load dataset into Stata use "${DATA_IN}tab9s.dta", clear * Run a regression of cv92 on two variables and a constant reg cv92 po lnce92 * Now using the log likelihood for a regression use Mata to estimate the same model via ml mata mata clear // Read data into Mata // ------------------- st_view(y=., ., "cv92") st_view(x=., ., ("po", "lnce92")) n = rows(x) n // Define parameters, data, and function // ------------------------------------- void eval(todo, p, y, x, n, ll, g, H) { b1 = p[1] b2 = p[2] b3 = p[3] s = p[4] ll = - (n/2) * log(s) :- 1/(2*s) * (y :- b1 :- x[,1]*b2' :- x[,2]*b3')'(y :- b1 :- x[,1]*b2' :- x[,2]*b3') } // Define optimization problem // --------------------------- S = optimize_init() // Specify the function to be called to evaluate // --------------------------------------------- optimize_init_evaluator(S, &eval()) // Set the starting values for the first iteration // ----------------------------------------------- optimize_init_params(S, (1,1,1,1)) // Define data pointers // -------------------- optimize_init_argument(S, 1, y) optimize_init_argument(S, 2, x) optimize_init_argument(S, 3, n) // Start optimization // ------------------ b = optimize(S) b // Get variance-covariance-matrix // (remember: is inverse of information matrix) // -------------------------------------------- V = optimize_result_V_oim(S) V // Calculate std.errors from V // --------------------------- se = sqrt(diagonal(V)') se // Show results // ------------ (b \ se \ b:/se) // End MATA // -------- end * Re-run regression to compare results reg cv92 po lnce92 log close exit