clear all;
%sp.mat 中数据已取对数
format long
format compact
load sp.mat;
% dimensions of the problem
T=13; % number of time periods
N=29; % number of regions
% row-normalize W
W=normw(w1); % function of LeSage
y=sp(:,[1]); % column number in the data matrix that corresponds to the dependent variable
x=sp(:,[2:8]); % column numbers in the data matrix that correspond to the independent variables
xconstant=ones(N*T,1);
[nobs K]=size(x);
% ----------------------------------------------------------------------------------------
% ols estimation(一般面板模型)
results=ols(y,[xconstant x]);
vnames=char('EF','C','UR','TR','DA','PCG','RD','OD','GOV');
prt_reg(results,vnames,1);
sige=results.sige*((nobs-K)/nobs);
loglikols=-nobs/2*log(2*pi*sige)-1/(2*sige)*results.resid'*results.resid
LMsarsem_panel(results,W,y,[xconstant x]); % (Robust) LM tests
%moran 检验
I=eye(T);W1=kron(I,W);
res=moran(y,[xconstant x],W1);
prt(res);
% ----------------------------------------------------------------------------------------
% spatial fixed effects + (robust) LM tests for spatial lag and spatial error model(空间固定效应模型)
% fixed effects, within estimator
% demeaning of the y and x variables
model=1;
[ywith,xwith,meanny,meannx,~,meantx]=demean(y,x,N,T,model);
results=ols(ywith,xwith);
vnames=char('EF','UR','TR','DA','PCG','RD','OD','GOV'); % should be changed if x is changed
prt_reg(results,vnames);
FE=meanny-meannx*results.beta; % including the constant term
yme = y - mean(y);
ee=ones(T,1);
error=y-kron(ee,FE)-x*results.beta;
rsqr1 = error'*error;
rsqr2 = yme'*yme;
FE_rsqr2 = 1.0 - rsqr1/rsqr