matlab 加权回归估计_matlab代码:地理加权回归(GWR)示例
【实例简介】地理加权回归(GWR)matlab代码,亲测可用,该代码利用matlab实现了地理加权回归的代码,内附实际算例。
【实例截图】
【核心代码】
function result = gwr(y,x,east,north,info);
% PURPOSE: compute geographically weighted regression
%----------------------------------------------------
% USAGE: results = gwr(y,x,east,north,info)
% where: y = dependent variable vector
% x = explanatory variable matrix
% east = x-coordinates in space
% north = y-coordinates in space
% info = a structure variable with fields:
% info.bwidth = scalar bandwidth to use or zero
% for cross-validation estimation (default)
% info.bmin = minimum bandwidth to use in CV search
% info.bmax = maximum bandwidth to use in CV search
% defaults: bmin = 0.1, bmax = 20
% info.dtype = 'gaussian' for Gaussian weighting (default)
% = 'exponential' for exponential weighting
% = 'tricube' for tri-cube weighting
% info.q = q-nearest neighbors to use for tri-cube weights
% (default: CV estimated)
% info.qmin = minimum # of neighbors to use in CV search
% info.qmax = maximum # of neighbors to use in CV search
% defaults: qmin = nvar 2, qmax = 4*nvar
% ---------------------------------------------------
% NOTE: res = gwr(y,x,east,north) does CV estimation of bandwidth
% ---------------------------------------------------
% RETURNS: a results structure
% results.meth = 'gwr'
% results.beta = bhat matrix (nobs x nvar)
% results.tstat = t-stats matrix (nobs x nvar)
% results.yhat = yhat
% results.resid = residuals
% results.sige = e'e/(n-dof) (nobs x 1)
% results.nobs = nobs
% results.nvar = nvars
% results.bwidth = bandwidth if gaussian or exponential
% results.q = q nearest neighbors if tri-cube
% results.dtype = input string for Gaussian, exponential weights
% results.iter = # of simplex iterations for cv
% results.north = north (y-coordinates)
% results.east = east (x-coordinates)
% results.y = y data vector
%---------------------------------------------------
% See also: prt,plt, prt_gwr, plt_gwr to print and plot results
%---------------------------------------------------
% References: Brunsdon, Fotheringham, Charlton (1996)
% Geographical Analysis, pp. 281-298
%---------------------------------------------------
% NOTES: uses auxiliary function scoref for cross-validation
%---------------------------------------------------
% written by: James P. LeSage 2/98
% University of Toledo
% Department of Economics
% Toledo, OH 43606
% jpl@jpl.econ.utoledo.edu
if nargin == 5 % user options
if ~isstruct(info)
error('gwr: must supply the option argument as a structure variable');
else
fields = fieldnames(info);
nf = length(fields);
% set defaults
[n k] = size(x);
bwidth = 0; dtype = 0; q = 0; qmin = k 2; qmax = 5*k;
bmin = 0.1; bmax = 20.0;
for i=1:nf
if strcmp(fields{i},'bwidth')
bwidth = info.bwidth;
elseif strcmp(fields{i},'dtype')
dstring = info.dtype;
if strcmp(dstring,'gaussian')
dtype = 0;
elseif strcmp(dstring,'exponential')
dtype = 1;
elseif strcmp(dstring,'tricube')
dtype = 2;
end;
elseif strcmp(fields{i},'q')
q = info.q;
elseif strcmp(fields{i},'qmax');
qmax = info.qmax;
elseif strcmp(fields{i},'qmin');
qmin = info.qmin;
elseif strcmp(fields{i},'bmin');
bmin = info.bmin;
elseif strcmp(fields{i},'bmax');
bmax = info.bmax;
end;
end; % end of for i
end; % end of if else
elseif nargin == 4
bwidth = 0; dtype = 0; dstring = 'gaussian';
bmin = 0.1; bmax = 20.0;
else
error('Wrong # of arguments to gwr');
end;
% error checking on inputs
[nobs nvar] = size(x);
[nobs2 junk] = size(y);
[nobs3 junk] = size(north);
[nobs4 junk] = size(east);
result.north = north;
result.east = east;
if nobs ~= nobs2
error('gwr: y and x must contain same # obs');
elseif nobs3 ~= nobs
error('gwr: north coordinates must equal # obs');
elseif nobs3 ~= nobs4
error('gwr: east coordinates must equal # in north');
end;
switch dtype
case{0,1} % bandwidth cross-validation
if bwidth == 0 % cross-validation
options = optimset('fminbnd');
optimset('MaxIter',500);
if dtype == 0 % Gaussian weights
[bdwt,junk,exitflag,output] = fminbnd('scoref',bmin,bmax,options,y,x,east,north,dtype);
elseif dtype == 1 % exponential weights
[bdwt,junk,exitflag,output] = fminbnd('scoref',bmin,bmax,options,y,x,east,north,dtype);
end;
if output.iterations == 500,
fprintf(1,'gwr: cv convergence not obtained in %4d iterations',output.iterations);
else
result.iter = output.iterations;
end;
else
bdwt = bwidth*bwidth; % user supplied bandwidth
end;
case{2} % q-nearest neigbhor cross-validation
if q == 0 % cross-validation
q = scoreq(qmin,qmax,y,x,east,north);
else
% use user-supplied q-value
end;
otherwise
end;
% do GWR using bdwt as bandwidth
[n k] = size(x);
bsave = zeros(n,k);
ssave = zeros(n,k);
sigv = zeros(n,1);
yhat = zeros(n,1);
resid = zeros(n,1);
wt = zeros(n,1);
d = zeros(n,1);
for iter=1:n;
dx = east - east(iter,1);
dy = north - north(iter,1);
d = (dx.*dx dy.*dy);
sd = std(sqrt(d));
% sort distance to find q nearest neighbors
ds = sort(d);
if dtype == 2, dmax = ds(q,1); end;
if dtype == 0, % Gausian weights
wt = stdn_pdf(sqrt(d)/(sd*bdwt));
elseif dtype == 1, % exponential weights
wt = exp(-d/bdwt);
elseif dtype == 2, % tricube weights
wt = zeros(n,1);
nzip = find(d <= dmax);
wt(nzip,1) = (1-(d(nzip,1)/dmax).^3).^3;
end; % end of if,else
wt = sqrt(wt);
% computational trick to speed things up
% use non-zero wt to pull out y,x observations
nzip = find(wt >= 0.01);
ys = y(nzip,1).*wt(nzip,1);
xs = matmul(x(nzip,:),wt(nzip,1));
xpxi = invpd(xs'*xs);
b = xpxi*xs'*ys;
% compute predicted values
yhatv = xs*b;
yhat(iter,1) = x(iter,:)*b;
resid(iter,1) = y(iter,1) - yhat(iter,1);
% compute residuals
e = ys - yhatv;
% find # of non-zero observations
nadj = length(nzip);
sige = (e'*e)/nadj;
% compute t-statistics
sdb = sqrt(sige*diag(xpxi));
% store coefficient estimates and std errors in matrices
% one set of beta,std for each observation
bsave(iter,:) = b';
ssave(iter,:) = sdb';
sigv(iter,1) = sige;
end;
% fill-in results structure
result.meth = 'gwr';
result.nobs = nobs;
result.nvar = nvar;
if (dtype == 0 | dtype == 1)
result.bwidth = sqrt(bdwt);
else
result.q = q;
end;
result.beta = bsave;
result.tstat = bsave./ssave;
result.sige = sigv;
result.dtype = dstring;
result.y = y;
result.yhat = yhat;
% compute residuals and conventional r-squared
result.resid = resid;
sigu = result.resid'*result.resid;
ym = y - mean(y);
rsqr1 = sigu;
rsqr2 = ym'*ym;
result.rsqr = 1.0 - rsqr1/rsqr2; % r-squared
rsqr1 = rsqr1/(nobs-nvar);
rsqr2 = rsqr2/(nobs-1.0);
result.rbar = 1 - (rsqr1/rsqr2); % rbar-squared
matlab 加权回归估计_matlab代码:地理加权回归(GWR)示例相关推荐
- 地理加权回归 | 模型如何应用于新数据的预测?
专注系列化.高质量的R语言教程 推文索引 | 联系小编 | 付费合集 有读者不知道如何用地理加权回归去预测新的数据.本篇以常用的两个工具包为例进行介绍. 本篇目录如下: 0 数据准备 1 spgwr工 ...
- ArcGIS与地理加权回归【三】
开 工 大 急 原址链接: ArcGIS与地理加权回归[三]https://mp.weixin.qq.com/s/x85EXKImSHio1IZovW9qdA 接着5个月之前..... ...
- 空间地理加权回归stata_xy妙妙屋丨地理加权回归和空间自相关
关于地理加权回归和空间自相关 菜鸡的我只是大神文章的搬运工orz,本意是想搞清楚双变量局部空间自相关和地理加权回归的区别,虽然依旧一知半解,但是,害.(下面网址我不会搞超链接,我发现有点麻烦,所以我懒 ...
- spgwr | R语言与地理加权回归(Ⅰ-2):广义线性地理加权回归
本篇来介绍基于广义线性模型的地理加权模型.广义线性模型包括Logistic模型.泊松模型等系列回归模型,具体内容请查看数学模型专辑的相关系列推文. 广义线性GWR的使用方法与线性GWR类似: ggwr ...
- spgwr | R语言与地理加权回归(Ⅰ-1):线性地理加权回归
地理加权回归(Geographically Weighted Regression, GWR)经过多年发展,已经具备了多种形式,在R语言中也对应着多个工具包,其中spgwr是一个开发较早.比较经典的工 ...
- R语言地理加权回归数据分析
在自然和社会科学领域有大量与地理或空间有关的数据,这一类数据一般具有严重的空间异质性,而通常的统计学方法并不能处理空间异质性,因而对此类型的数据无能为力.以地理加权回归为基础的一系列方法:经典地理加权 ...
- R语言GWR地理加权回归
最近需要用到GWR地理加权回归,数据量有5万条,使用了GIS.GWR4进行计算,但都没能成功.应该是数据量过大. 参考相关博客,还有一个方法是R语言的实现.因为没怎么接触过R语言,所有想请问一下各位, ...
- gis中的加权求和工具在哪里_干货分享 | 地理加权回归介绍及其arcgis软件操作
一.地理加权回归模型概述 橘生淮南则为橘,生于淮北则为枳,叶徒相似,其实味不同.所以然者何?水土异也.--<晏子春秋·内篇杂下>这段文字很好的描述了空间异质性.从地理空间的角度,经济发展尤 ...
- 白话空间统计二十四:地理加权回归(八)结果解读(一)
地理加权回归分析完成之后,与OLS不同的是会默认生成一张可视化图,像下面这张一样的: 这种图里面数值和颜色,主要是系数的标准误差.主要用来衡量每个系数估计值的可靠性.标准误差与实际系数值相比较小时,这 ...
- GWmodel | 地理加权模型(Ⅱ-1):地理加权主成分分析(GWPCA)
地理加权回归(GWR)相比于普通的回归模型能够考虑到回归系数的空间异质性.但实际上,地理加权并非GWR模型所独有,其他数据分析方法同样也能通过加入地理权重进行改进.本篇介绍的就是地理加权主成分分析(G ...
最新文章
- vim中taglist无法显示问题
- Xamarin效果第八篇之视频监控
- mysql递归查询 缓存_MySQL-递归查询方法解析
- 默认方法一种扩展旧代码的方法
- 树链剖分 讲解+模板+习题
- 标准正态分布_正态分布,正态分布如何变换为标准正态分布
- python没有这个xlwt模块_python xlwt模块简介
- python如何输入多个数据并增加到一个列表里_python 将表格多个列数据放到同一个单元格中...
- Leetcode c语言-Implement strStr()
- /bin、/sbin、/usr/bin、/usr/sbin目录的区别
- HDU 1317 XYZZY
- Egret入门学习日记 --- 第十三篇(书中 5.2~5.3节 内容)
- Shell命令学习笔记
- 480.滑动窗口中位数
- Mac 上使用windows软件--wineskin
- 七人表决器VHDL代码
- 笛卡尔积:(SQL语句中)
- Eclipse 安装 yml 编辑器插件
- 023_fireshot
- 简单利用路由黑洞解决DDOS流量攻击
热门文章
- 军用加固便携式计算机,PCI/PCIe
- Java数据类型的转换
- 使用js调用摄像头拍照
- 服务器imm装系统,通过IMM With Remote Console为服务器安装操作系统
- UE4开发笔记1——UE4(虚幻4)引擎下载与安装
- 万能打印之Delphi 2010实现(序言)
- [转载]Codejock Xtreme ToolkitPro MFC 使用
- 安装完 SQL Server 2008 后没有 SQL Server Management Studio
- 广东2022年上半年系统集成项目管理工程师下午真题及答案解析
- StackOverFlow处女问