% This Matlab program illustrates the use of the Newton-Raphson algorithm % to obtain maximum likelihood estimates of a logistic regression. The data % and much of the code are taken from Anders Swensen, "Non-linear regression," % www.math.uio.no/avdc/kurs/ST110/materiale/opti_30.ps. % First, load and transform data: load 'beetle.dat'; % load data m=length(beetle(:,1)) % count the rows in the data matrix x=[]; % create empty vectors y=[]; for j=1:m % expand group data into individual data x=[x,beetle(j,1)*ones(1,beetle(j,2))]; y=[y,ones(1,beetle(j,3)),zeros(1,beetle(j,2)-beetle(j,3))]; end beetle2=[x;y]'; % Next, specify starting points for iteration on parameter values: beta0 = [0; 0] % Finally, call the function NR_logistic and use its output [betaml,Jbar] = NR_logistic(beetle2,beta0) covmat = inv(Jbar) stderr = sqrt(diag(covmat))