📄 sem_panel.m
字号:
results.yhat=x*results.beta+kron(en,results.sfe);
tnvar=nvar+N;
elseif model==2
results.tfe=meanty-meantx*results.beta; % including intercept
results.yhat=x*results.beta+kron(et,results.tfe);
tnvar=nvar+T;
elseif model==3
intercept=mean(y)-mean(x)*results.beta; % intercept calculated separately
results.con=intercept;
results.sfe=meanny-meannx*results.beta-kron(et,intercept);
results.tfe=meanty-meantx*results.beta-kron(en,intercept);
results.yhat=x*results.beta+kron(en,results.sfe)+kron(et,results.tfe)+kron(ent,intercept);
tnvar=nvar+N+T-1;
else
results.yhat=x*results.beta;
tnvar=nvar;
end
results.resid=y-results.yhat;
yme=y-mean(y);
B = (speye(N) - rho*sparse(W));
Be=zeros(nobs,1);
for t=1:T
t1=1+(t-1)*N;t2=t*N;
Be([t1:t2],1)= B*results.resid([t1:t2],1);
end
epe = Be'*Be;
rsqr1=yme'*yme;
results.rsqr=1.0-epe/rsqr1; %rsquared
rsqr3 = epe/(nobs-tnvar);
rsqr2 = rsqr1/(nobs-1.0);
results.rbar = 1 - (rsqr3/rsqr2); % rbar-squared
results.sige = (1/nobs)*epe;
results.tnvar=tnvar;
sigu = epe;
sige = results.sige;
parm = [results.beta
results.rho
results.sige];
results.lik = f2_sempanel(parm,ywith,xwith,W,detval,T); %Elhorst
if N <= 500, % t-stats using information matrix (Anselin, 1982, 1988),
% adjusted by Elhorst for spatial panels
t0 = clock;
B = (speye(N) - rho*sparse(W));
BI = inv(B); WB = W*BI;
pterm = trace(WB'*WB);
xpx = zeros(nvar+2,nvar+2);
xpx(1:nvar,1:nvar) = (1/sige)*xs'*xs;
% rho, rho
xpx(nvar+1,nvar+1) = T*(trace(WB'*WB) + pterm);
% sige, sige
xpx(nvar+2,nvar+2) = nobs/(2*sige*sige);
% rho, sige
xpx(nvar+1,nvar+2) = -(T/sige)*(rho*trace(WB'*WB) - trace(BI'*WB));
xpx(nvar+2,nvar+1) = xpx(nvar+1,nvar+2);
tmp = diag(inv(xpx));
bvec = [results.beta
results.rho];
results.tstat = bvec./(sqrt(tmp(1:nvar+1,1)));
time3 = etime(clock,t0);
elseif N > 500
t0 = clock;
hessn = hessian('f2_sempanel',parm,ywith,xwith,W,detval,T); %Elhorst
if hessn(nvar+2,nvar+2) == 0
hessn(nvar+2,nvar+2) = 1/sige; % this is a hack for very large models that
end; % should not affect inference in these cases
xpxi = inv(-hessn);
xpxi = diag(xpxi(1:nvar+1,1:nvar+1));
zip = find(xpxi <= 0);
if length(zip) > 0
xpxi(zip,1) = 1;
fprintf(1,'sem: negative or zero variance from numerical hessian \n');
fprintf(1,'sem: replacing t-stat with 0 \n');
end;
tmp = [results.beta
results.rho];
results.tstat = tmp./sqrt(xpxi);
if length(zip) ~= 0
results.tstat(zip,1) = 0;
end;
time3 = etime(clock,t0);
end; % end of t-stat calculations
results.lndet = detval;
results.time = etime(clock,timet);
results.time1 = time1;
results.time2 = time2;
results.time3 = time3;
results.time4 = time4;
function [rmin,rmax,convg,maxit,detval,ldetflag,eflag,order,iter,options] = sem_parse(info)
% PURPOSE: parses input arguments for far, far_g models
% ---------------------------------------------------
% USAGE: [rmin,rmax,convg,maxit,detval,ldetflag,eflag,order,iter] = far_parse(info)
% where info contains the structure variable with inputs
% and the outputs are either user-inputs or default values
% ---------------------------------------------------
% set defaults
options = optimset('fminbnd');
options.MaxIter = 500;
eflag = 1; % default to not computing eigenvalues
ldetflag = 1; % default to 1999 Pace and Barry MC determinant approx
order = 50; % there are parameters used by the MC det approx
iter = 30; % defaults based on Pace and Barry recommendation
rmin = -0.99; % use -1,1 rho interval as default
rmax = 0.99;
detval = 0; % just a flag
convg = 0.0001;
maxit = 500;
fields = fieldnames(info);
nf = length(fields);
if nf > 0
for i=1:nf
if strcmp(fields{i},'rmin')
rmin = info.rmin; eflag = 1;
elseif strcmp(fields{i},'rmax')
rmax = info.rmax; eflag = 1;
elseif strcmp(fields{i},'eigs')
eflag = info.eigs; % flag for compute the eigenvalues
elseif strcmp(fields{i},'convg')
options.TolFun = info.convg;
elseif strcmp(fields{i},'maxit')
options.MaxIter = info.maxit;
elseif strcmp(fields{i},'lndet')
detval = info.lndet;
ldetflag = -1;
eflag = 1;
rmin = detval(1,1);
nr = length(detval);
rmax = detval(nr,1);
elseif strcmp(fields{i},'lflag')
tst = info.lflag;
if tst == 0,
ldetflag = 0;
elseif tst == 1,
ldetflag = 1;
elseif tst == 2,
ldetflag = 2;
else
error('sar: unrecognizable lflag value on input');
end;
elseif strcmp(fields{i},'order')
order = info.order;
elseif strcmp(fields{i},'iter')
iter = info.iter;
end;
end;
else, % the user has input a blank info structure
% so we use the defaults
end;
function [rmin,rmax,time2] = sem_eigs(eflag,W,rmin,rmax,n);
% PURPOSE: compute the eigenvalues for the weight matrix
% ---------------------------------------------------
% USAGE: [rmin,rmax,time2] = far_eigs(eflag,W,rmin,rmax,W)
% where eflag is an input flag, W is the weight matrix
% rmin,rmax may be used as default outputs
% and the outputs are either user-inputs or default values
% ---------------------------------------------------
if eflag == 0
t0 = clock;
opt.tol = 1e-3; opt.disp = 0;
lambda = eigs(sparse(W),speye(n),1,'SR',opt);
rmin = 1/lambda;
rmax = 1;
time2 = etime(clock,t0);
else
time2 = 0;
end;
function [detval,time1] = sem_lndet(ldetflag,W,rmin,rmax,detval,order,iter);
% PURPOSE: compute the log determinant |I_n - rho*W|
% using the user-selected (or default) method
% ---------------------------------------------------
% USAGE: detval = far_lndet(lflag,W,rmin,rmax)
% where eflag,rmin,rmax,W contains input flags
% and the outputs are either user-inputs or default values
% ---------------------------------------------------
% do lndet approximation calculations if needed
if ldetflag == 0 % no approximation
t0 = clock;
out = lndetfull(W,rmin,rmax);
time1 = etime(clock,t0);
tt=rmin:.001:rmax; % interpolate a finer grid
outi = interp1(out.rho,out.lndet,tt','spline');
detval = [tt' outi];
elseif ldetflag == 1 % use Pace and Barry, 1999 MC approximation
t0 = clock;
out = lndetmc(order,iter,W,rmin,rmax);
time1 = etime(clock,t0);
results.limit = [out.rho out.lo95 out.lndet out.up95];
tt=rmin:.001:rmax; % interpolate a finer grid
outi = interp1(out.rho,out.lndet,tt','spline');
detval = [tt' outi];
elseif ldetflag == 2 % use Pace and Barry, 1998 spline interpolation
t0 = clock;
out = lndetint(W,rmin,rmax);
time1 = etime(clock,t0);
tt=rmin:.001:rmax; % interpolate a finer grid
outi = interp1(out.rho,out.lndet,tt','spline');
detval = [tt' outi];
elseif ldetflag == -1 % the user fed down a detval matrix
time1 = 0;
% check to see if this is right
if detval == 0
error('sem: wrong lndet input argument');
end;
[n1,n2] = size(detval);
if n2 ~= 2
error('sem: wrong sized lndet input argument');
elseif n1 == 1
error('sem: wrong sized lndet input argument');
end;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -