/* LOG_FGLS.G: feasible GLS estimation of binary logit model using first stage nonlinear least squares estimates of parameters in file log_nls.fmt By John Rust, Yale University, April, 1998 */ proc (4)=log_fgls(dt,dloc,iloc,cloc,q,ll,dll,ls,lle,h,hll); local y,x,p,i,wts,q_init; if (0 == files("log_nls.fmt",0)); "Error: parameter file log_nls.fmt not found"; "Run estimate.gpr choosing logit and nls first, then rerun"; "choosing logit and fgls"; retp(-1,-1,-1,-1); else; load q_init=log_nls; endif; y=dt[.,dloc]; /* dependent variable in y */ y=.not(y); /* reverse entries so 1-phi(xq)=1/(1+exp(xq)) */ x=dt[.,iloc']; /* independent variables in x */ p=1/(1+exp(x*q)); wts=1/(1+exp(x*q_init)); wts=wts.*(1-wts); ll=ll+sumc((-(y-p)^2)./wts)/2; /* cumulate log-likelihood */ dll=dll-sumc((y-p).*p.*(1-p).*x./wts); /* derivative of log-likelihood */ if ls > 0; goto skip; endif; /* don't compute hessian in linesearch */ if lle == -1; /* last likelihood evaluation iteration */ /* cumulate outer product of 1st derivs */ h=h+moment((y-p).*p.*(1-p).*x./wts,1); endif; /* cumulate hessian of likelihood */ i=1; do until i > rows(x); hll=hll-((p[i]*(1-p[i]))^2)*x[i,.]'x[i,.]/wts[i]+ (y[i]-p[i])*p[i]*(1-p[i])*(1-2*p[i]).*x[i,.]'x[i,.]/wts[i]; i=i+1; endo; skip: retp(ll,dll,h,hll); endp;