全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 MATLAB等数学软件专版
6082 12
2013-01-16
利用matlab进行空间计量模型检验中,出现了空间权重矩阵是奇异矩阵的现象(Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.261189e-017.),导致数据无法进一步的运算(Warning: Divide by zero)。空间权重矩阵是通过geoda生成转化而来的,请问如何解决这个问题?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2013-1-16 09:36:33
首先看你的权重矩阵是否设置有误,如果没有错误则将矩阵求逆(inv)换成广义逆(pinv)
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-1-16 13:04:30
deng203 发表于 2013-1-16 09:36
首先看你的权重矩阵是否设置有误,如果没有错误则将矩阵求逆(inv)换成广义逆(pinv)
你好,我才开始研究这个软件,我是在通过别人写好的代码改写了一下用于我的分析,代码如下:
clear all;

A=xlsread('C:\MATLAB7\95.xls');
W1=xlsread('C:\MATLAB7\qz.xls');
% Dataset downloaded from www.wiley.co.uk/baltagi/
% Spatial weights matrix constructed by Elhorst
%
% written by: J.Paul Elhorst summer 2008
% University of Groningen
% Department of Economics
% 9700AV Groningen
% the Netherlands
% j.p.elhorst@rug.nl
%
% REFERENCES:
% Elhorst JP (2009) Spatial Panel Data Models. In Fischer MM, Getis A (Eds.)
% Handbook of Applied Spatial Analysis, Ch. C.2. Springer: Berlin Heidelberg New York.
%
% dimensions of the problem
T=1; % number of time periods
N=30; % number of regions
% row-normalize W
W=normw(W1); % function of LeSage
y=A(:,[7]); % column number in the data matrix that corresponds to the dependent variable
x=A(:,[1,2,3,4,5,6]); % 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=strvcat('logc','intercept','log1','log2','lo3','log4','log5','log6');
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
% ----------------------------------------------------------------------------------------
% 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,meanty,meantx]=demean(y,x,N,T,model);
results=ols(ywith,xwith);
vnames=strvcat('logc','intercept','log1','log2','log3','log4','log5','log6'); % 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/rsqr2 % r-squared including fixed effects
sige=results.sige*((nobs-K)/nobs);
loglikfe=-nobs/2*log(2*pi*sige)-1/(2*sige)*results.resid'*results.resid
LMsarsem_panel(results,W,ywith,xwith); % (Robust) LM tests
%下面的一小段是我对照那个程序自己加上的
model=2;
[ywith,xwith,meanny,meannx,meanty,meantx]=demean(y,x,N,T,model);
results=wls(ywith,xwith);
vnames=strvcat('logc','intercept','log1','logp2','log3','log4','logp5','log6'); % should be changed if x is changed
prt_reg(results,vnames);
sige=results.sige*((nobs-K)/nobs);
loglikfe=-nobs/2*log(2*pi*sige)-1/(2*sige)*results.resid'*results.resid
LMsarsem_panel(results,W,ywith,xwith); % (Robust) LM tests
% ----------------------------------------------------------------------------------------
% spatial and time period fixed effects + (robust) LM tests for spatial lag and spatial error model
% fixed effects, within estimator
% demeaning of the y and x variables
model=3;
[ywith,xwith,meanny,meannx,meanty,meantx]=demean(y,x,N,T,model);
results=wls(ywith,xwith);
vnames=strvcat('logc','intercept','log1','logp2','log3','log4','logp5','log6'); % should be changed if x is changed
prt_reg(results,vnames);
sige=results.sige*((nobs-K)/nobs);
loglikfe=-nobs/2*log(2*pi*sige)-1/(2*sige)*results.resid'*results.resid
LMsarsem_panel(results,W,ywith,xwith); % (Robust) LM tests

我并没有发现有调用inv函数,还希望你可以继续指点我一下,感谢!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-1-16 15:52:43
LM检验(LMsarsem_panel)中应该含有inv(W),你type LMsarsem_panel看看
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-1-17 22:47:50
deng203 发表于 2013-1-16 15:52
LM检验(LMsarsem_panel)中应该含有inv(W),你type LMsarsem_panel看看
恩,我会按照你的提示尝试去做的,现在要期末考试了,软件的学习暂时先放下。感谢你对我的帮助!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-2-22 19:34:30
deng203 发表于 2013-1-16 15:52
LM检验(LMsarsem_panel)中应该含有inv(W),你type LMsarsem_panel看看
我又重新设置了一下权重矩阵,得出了这样的结果,想问下您这是什么原因
LM test no spatial lag, probability          =       NaN,      NaN
robust LM test no spatial lag, probability   =       NaN,      NaN
LM test no spatial error, probability        =       NaN,      NaN
robust LM test no spatial error, probability =       NaN,      NaN
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群