Function 1: scoreq.m
function q = scoreq(qmin,qmax,y,x,east,north)
% PURPOSE: evaluates cross-validation score for optimal temporal q(with spatial tie) in gwr
% based on tricube weighting
% ---
% USAGE: score = scoreq(qmin,qmax,y,x,east,north);
% where: qmin = minimum # nearest neighbors to use in CV search
% qmax = maximum # nearest neighbors to use in CV search
% y = dependent variable
% x = matrix of explanatory variables
% east = longitude (x-direction) coordinates
% north = lattitude (y-direction) coordinates
% ---
% RETURNS: q = # of nearest temporal neighbors that minimum the score
% function
% written by: Sun Hua with reference on LeSage (1999) [n k] = size(x); res = zeros(n,1);
dx = east(1:iter-1,1) - east(iter,1);
dy = north(1:iter-1,1) - north(iter,1);
d = (dx.*dx + dy.*dy);
newdist=sort(d);
uniqdist=newdist(1,1);
dd=length(d);
Appendix B Matlab Supporting Functions for This Study
% computational trick to speed things up
% use wt non-zero to pull out y,x observations nzip = find(wt(:,j) > 0);
ys = y(nzip,1).*sqrt(wt(nzip,j));
xs = matmul(x(nzip,:),sqrt(wt(nzip,j)));
bi=xs\ys;
% compute predicted values yhat = x(iter,:)*bi;
% compute residuals res(iter,j) = y(iter,1) - yhat;
end; % end of for j loop over q-values end; % end of for iter loop
tmp = res.*res;
score = sum(tmp);
[smin sind] = min(score);
q = qgrid(sind);
Appendix B Matlab Supporting Functions for This Study
__________________________________________________________________________________ 134 Function 2: scoret.m
function t = scoret(qmin,qmax,y,x)
% PURPOSE: evaluates cross-validation score for optimal t
%
% ---
% USAGE: score = scoreq(qmin,qmax,y,x,east,north);
% where: qmin = minimum # nearest neighbors to use in CV search
% qmax = maximum # nearest neighbors to use in CV search
nzip = (iter-(qmin+10*(j-1))):iter-1;
wt(nzip,j) = 1;
wt(iter,j) = 0.0;
end; % end of j loop for j=1:nq;
% computational trick to speed things up
% use wt non-zero to pull out y,x observations nzip = find(wt(:,j) > 0);
ys = y(nzip,1).*sqrt(wt(nzip,j));
xs = matmul(x(nzip,:),sqrt(wt(nzip,j)));
bi=xs\ys;
% compute predicted values yhat = x(iter,:)*bi;
% compute residuals res(iter,j) = y(iter,1) - yhat;
end; % end of for j loop over q-values end; % end of for iter loop
tmp = res.*res;
score = sum(tmp);
[smin sind] = min(score);
q = qgrid(sind);
Appendix B Matlab Supporting Functions for This Study
__________________________________________________________________________________ 135 Function 3:triW.m
function W=triW(east,north,order,W)
% Purpose: to compute tricube weighting temporal-based spatial matrix (with spatial tie) for combine spatial weight matrix S and building effect matrix W1
% Note: each spatial lag only selects latest three transactions as reference.
%---
% east:latitude or x-coordiante
% north:longitude or y-coordinate
% order: the number of nearest observations
% W: (optional) user options: number by number zeros matrix
% written by Sun Hua
TT=sparse(zeros(5000,5000));
TTT=sparse(zeros(triv,triv));
for ii=2:main
Appendix B Matlab Supporting Functions for This Study
Appendix B Matlab Supporting Functions for This Study
__________________________________________________________________________________ 137 if newdist(jjj,1)~=newdist(jjj-1,1)
uniqdist(end+1,1)=newdist(jjj,1);
if length(uniqdist)==order+1 break;
end end end
ddmax=uniqdist(order+1,1);
for vv=1:order
j1=find(dist(:,1)==uniqdist(vv,1));
% when vv is larger than 4, we regard them all as 4 and divided them by 4.
if vv>=4 vvv=4;
else vvv=vv;
end
if length(j1)<=3
W(i,j1)=((1-(dist(j1(1,1))/ddmax)^3)^3)/vvv;
else
W(i,j1(end-2:end,1))=((1-(dist(j1(1,1))/ddmax)^3)^3)/vvv;
end end end
tt=sum(W(i,:));
if tt~=0
W(i,:)=W(i,:)./tt;
end i
end
Appendix B Matlab Supporting Functions for This Study
__________________________________________________________________________________ 138 Function 4: triW2.m
function W2=triW2(east,north,order,W)
% Purpose: to compute tricube weighting temporal-based spatial matrix for neighborhood effect matrix W2 (with spatial tie)
% Note: each spatial lag only selects latest three transactions as reference
%---
% east:latitude or x-coordiante
% north:longitude or y-coordinate
% order: the number of nearest observations
% W:(optional) user options: number by number zeros matrix
% written by Sun Hua
% see also for creatW,creatT number=length(east);
TT=sparse(zeros(5000,5000));
TTT=sparse(zeros(triv,triv));
for ii=2:main
Appendix B Matlab Supporting Functions for This Study
Appendix B Matlab Supporting Functions for This Study
__________________________________________________________________________________ 140 for jjj=2:i-1
if newdist(jjj,1)~=newdist(jjj-1,1) uniqdist(end+1,1)=newdist(jjj,1);
if length(uniqdist)==order+1 break;
end end end
ddmax=uniqdist(order+1,1);
for vv=2:order
j1=find(dist(:,1)==uniqdist(vv,1));
% when vv is larger than 4, we regard them all as 4 and divided them by 4.
if vv>=4 vvv=4;
else vvv=vv;
end
if length(j1)<=3
W(i,j1)=((1-(dist(j1(1,1))/ddmax)^3)^3)/vvv;
else
W(i,j1(end-2:end,1))=((1-(dist(j1(1,1))/ddmax)^3)^3)/vvv;
end end end
tt=sum(W(i,:));
if tt~=0
W(i,:)=W(i,:)./tt;
end end
Appendix B Matlab Supporting Functions for This Study
__________________________________________________________________________________ 141 Function 5: creatT.m
function result=creatt(number,order,W)
% Purpose: to compute temporal matrix
%function result=creatt(number,order,W)
%---
% number: number of observations
% order: temporal lag number
% W: user options: number by number zeros matrix
% written by Sun Hua
TT=sparse(zeros(5000,5000));
TTT=sparse(zeros(triv,triv));
for ii=2:main
Appendix B Matlab Supporting Functions for This Study
__________________________________________________________________________________ 142 for i=2:number
for j=1:order if j<i
T(i,i-j)=1;
end end end
for t=1:number tt=sum(T(t,:));
if tt~=0
T(t,:)=T(t,:)./tt;
end end
result.W=W;
result.T=T;
Appendix B Matlab Supporting Functions for This Study
__________________________________________________________________________________ 143 Function 6: ols_g.m (written by LeSage 1999)
function results = ols_g(y,x,ndraw,nomit,prior,start)
% PURPOSE: MCMC estimates for the Bayesian heteroscedastic linear model
% y = X B + E, E = N(0,sige*V),
% V = diag(v1,v2,...vn), r/vi = ID chi(r)/r, r = Gamma(m,k)
% B = N(c,T), sige = gamma(nu,d0)
%---
% USAGE: results = ols_g(y,x,ndraw,nomit,prior,start)
% where: y = dependent variable vector
% RETURNS: a structure:
% results.meth = 'ols_g'
Appendix B Matlab Supporting Functions for This Study
% NOTE: use either improper prior.rval
% or informative Gamma prior.m, prior.k, not both of them
%---
% SEE ALSO: coda, gmoment, prt_gibbs(results)
%---
% REFERENCES: Geweke (1993) 'Bayesian Treatment of the
% Independent Student-$t$ Linear Model', Journal of Applied
% Econometrics, 8, s19-s40.
% ---
% written by:
% James P. LeSage, Dept of Economics
% University of Toledo
% 2801 W. Bancroft St,
% Toledo, OH 43606
% [email protected] [n k] = size(x);
if nargin > 4
% error checking on input if ~isstruct(prior)
error('ols_g: must supply the prior as a structure variable');
end;
end;
if nargin == 6 % user-supplied starting values if ~isstruct(start)
b0 = (x'*x)\(x'*y); % Find ols values as initial starting values sige = (y-x*b0)'*(y-x*b0)/(n-k);
V = ones(n,1); in = ones(n,1); % initial value for V end;
if nargin == 4 % default values
b0 = (x'*x)\(x'*y); % Find ols values as initial starting values sige = (y-x*b0)'*(y-x*b0)/(n-k);
Appendix B Matlab Supporting Functions for This Study
error('Wrong # of arguments to ols_g');
end; elseif strcmp(fields{i},'nu')
nu = prior.nu;
elseif strcmp(fields{i},'d0') d0 = prior.d0;
elseif strcmp(fields{i},'beta');
c = prior.beta;
elseif strcmp(fields{i},'bcov');
T = prior.bcov;
error('ols_g: prior means are wrong');
elseif junk ~= 1
error('ols_g: prior means are wrong');
end;
[checkk junk] = size(T);
if checkk ~= k
error('ols_g: prior bcov is wrong');
elseif junk ~= k
error('ols_g: prior bcov is wrong');
Appendix B Matlab Supporting Functions for This Study
__________________________________________________________________________________ 146 end;
Q = inv(T); Qpc = Q*c;
bsave = zeros(ndraw-nomit,k); % allocate storage for results ssave = zeros(ndraw-nomit,1);
rsave = zeros(ndraw-nomit,1);
vmean = zeros(n,1);
yhat = zeros(n,1);
hwait = waitbar(0,'MCMC sampling ...');
t0 = clock;
for i=1:ndraw; % Start the sampling ystar = matmul(sqrt(V),y);
Appendix B Matlab Supporting Functions for This Study
__________________________________________________________________________________ 147 if mm~= 0
rsave(i-nomit,1) = rval;
end;
end;
waitbar(i/ndraw);
end; % End the sampling gtime = etime(clock,t0);
close(hwait);
vmean = vmean/(ndraw-nomit);
yhat = yhat/(ndraw-nomit);
% return results results.meth = 'ols_g';
results.bdraw = bsave;
results.pmean = c;
results.pstd = sqrt(diag(T));
results.vmean = vmean;
results.sdraw = ssave;
results.yhat = yhat;
if mm~= 0
results.rdraw = rsave;
results.m = mm;
results.k = kk;
else
results.r = rval;
results.rdraw = rsave;
end;
results.nobs = n;
results.nvar = k;
results.y = y;
results.x = x;
results.nu = nu;
results.d0 = d0;
results.time = gtime;
results.ndraw = ndraw;
results.nomit = nomit;
results.pflag = 'plevel';
Appendix C Table 6.1 to 6.6 for Chapter 6 148