Cracking down the spatial econometrics code

cell2mat

A = cell2mat(C)

Convert Cell Array to Numeric Array
Convert numeric arrays in four cells of a cell array into one numeric array.
C = {[1], [2 3 4];
[5; 9], [6 7 8; 10 11 12]}
C = 2×2 cell array
{[ 1]} {1×3 double}
{2×1 double} {2×3 double}

A = cell2mat(C)
A = 3×4

1 2 3 4
5 6 7 8
9 10 11 12

vnames=char(vnames,cell2mat(names(i)));

C = char(A1,...,An) converts the arrays A1,...,An into a single character array. After conversion to characters, the input arrays become rows in C. The char function pads rows with blank spaces as needed. If any input array is an empty character array, then the corresponding row in C is a row of blank spaces.

So the char function adds vnames with names(i) into a character array.

prt_reg(results,vnames,1);

To illustrate the use of the `results’ structure returned by our ols function,
consider the associated function plt_reg which plots actual versus predicted values along with the residuals.

So it’s a graphical function to draw graphs.

resultssar=sar_panel_FE(y,x,W,T,info);

sar_panel_FE is just a m file in jplv7 in folder \Matlab Version 7 zip file\spatial\panel.

And this subroutine explains what is an info which I cannot find reference through F2/F3 command in IntelliJ!

I change to bold font to show the commands used in the code.

function results = sar_panel_FE(y,x,W,T,info)
% PURPOSE: computes spatial lag model estimates for spatial panels
% (N regions*T time periods) with spatial fixed effects (u)
% and/or time period fixed effects (v)
% y = p*W*y + X*b + u (optional) + v(optional) + e, using sparse matrix algorithms
% Supply data sorted first by time and then by spatial units, so first region 1,
% region 2, et cetera, in the first year, then region 1, region 2, et
% cetera in the second year, and so on
% sar_panel_FE computes y and x in deviation of the spatial and/or time means
% —————————————————
% USAGE: results = sar_panel_FE(y,x,W,T,info)
% where: y = dependent variable vector
% x = independent variables matrix
% W = spatial weights matrix (standardized)
% T = number of points in time
% info = an (optional) structure variable with input options:
% info.model = 0 pooled model without fixed effects (default, x may contain an intercept)
% = 1 spatial fixed effects (x may not contain an intercept)
% = 2 time period fixed effects (x may not contain an intercept)
% = 3 spatial and time period fixed effects (x may not contain an intercept)
% info.fe = report fixed effects and their t-values in prt_sp (default=0=not reported; info.fe=1=report)
% info.Nhes = N =< Nhes asymptotic variance matrix is computed using analytical formulas,
% N > Nhes asymptotic variance matrix is computed using numerical formulas
% (Default NHes=500)
% info.rmin = (optional) minimum value of rho to use in search
% info.rmax = (optional) maximum value of rho to use in search
% info.convg = (optional) convergence criterion (default = 1e-8)
% info.maxit = (optional) maximum # of iterations (default = 500)
% info.lflag = 0 for full lndet computation (default = 1, fastest)
% = 1 for MC lndet approximation (fast for very large problems)
% = 2 for Spline lndet approximation (medium speed)
% info.order = order to use with info.lflag = 1 option (default = 50)
% info.iter = iterations to use with info.lflag = 1 option (default = 30)
% info.lndet = a matrix returned by sar containing log-determinant information to save time
% —————————————————
% RETURNS: a structure
% results.meth = ‘psar’ if infomodel=0
% = ‘sarsfe’ if info.model=1
% = ‘sartfe’ if info.model=2
% = ‘sarstfe’ if info.model=3
% results.beta = bhat
% results.rho = rho (p above)
% results.cov = asymptotic variance-covariance matrix of the parameters b(eta) and rho
% results.tstat = asymp t-stat (last entry is rho=spatial autoregressive coefficient)
% results.yhat = [inv(y-p*W)]*[x*b+fixed effects] (according to prediction formula)
% results.resid = y-p*W*y-x*b
% results.sige = (y-p*W*y-x*b)’*(y-p*W*y-x*b)/n
% results.rsqr = rsquared
% results.corr2 = goodness-of-fit between actual and fitted values
% results.sfe = spatial fixed effects (if info.model=1 or 3)
% results.tfe = time period fixed effects (if info.model=2 or 3)
% results.tsfe = t-values spatial fixed effects (if info.model=1 or 3)
% results.ttfe = t-values time period fixed effects (if info.model=2 or 3)
% results.con = intercept
% results.con = t-value intercept
% results.lik = log likelihood
% results.nobs = # of observations
% results.nvar = # of explanatory variables in x
% results.tnvar = nvar + W*y + # fixed effects
% results.iter = # of iterations taken
% results.rmax = 1/max eigenvalue of W (or rmax if input)
% results.rmin = 1/min eigenvalue of W (or rmin if input)
% results.lflag = lflag from input
% results.fe = fe from input
% results.liter = info.iter option from input
% results.order = info.order option from input
% results.limit = matrix of [rho lower95,logdet approx, upper95] intervals
% for the case of lflag = 1
% results.time1 = time for log determinant calcluation
% results.time2 = time for eigenvalue calculation
% results.time3 = time for hessian or information matrix calculation
% results.time4 = time for optimization
% results.time = total time taken
% results.lndet = a matrix containing log-determinant information
% (for use in later function calls to save time)
% ————————————————–
% NOTES: if you use lflag = 1 or 2, info.rmin will be set = -1
% info.rmax will be set = 1
% For number of spatial units < 500 you should use lflag = 0 to get
% exact results,
% Fixed effects and their t-values are calculated as the deviation
% from the mean intercept
% —————————————————
%
% Updated by: J.Paul Elhorst summer 2008
% University of Groningen
% Department of Economics
% 9700AV Groningen
% the Netherlands
% j.p.elhorst@rug.nl
%
% REFERENCES:
% Elhorst JP (2003) Specification and Estimation of Spatial Panel Data Models,
% International Regional Science Review 26: 244-268.
% 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.

prt_sp(resultssar,vnames,1);

what is the difference between prt_sp and prt_reg/plt_reg ?

There is no a prt_sp in the jplv7 folder. But I see the plt_reg/prt_reg appeared in Applied Econometrics using MATLAB James P. LeSage.pdf.

It seems that pl is short for plot.

function prt_reg(results,vnames,fid)
% PURPOSE: Prints output using regression results structures
%—————————————————
% USAGE: prt_reg(results,vnames,fid)
% Where: results = a structure returned by a regression
% vnames = an optional vector of variable names
% fid = optional file-id for printing results to a file
% (defaults to the MATLAB command window)
%—————————————————
% NOTES: e.g. vnames = strvcat(‘y’,’const’,’x1′,’x2′);
% e.g. fid = fopen(‘ols.out’,’wr’);
% use prt_reg(results,[],fid) to print to a file with no vnames
% ————————————————–
% RETURNS: nothing, just prints the regression results
% ————————————————–
% SEE ALSO: prt, plt
%—————————————————

function plt_reg(results,vnames);
% PURPOSE: plots regression actual vs predicted and residuals
%—————————————————
% USAGE: plt_reg(results);
% where: results is a structure returned by a regression function
%—————————————————
% RETURNS: nothing, just plots regression results
% ————————————————–
% NOTE: user must supply pause commands, none are in plt_reg function
% e.g. plt_reg(results);
% pause;
% plt_reg(results2);
% ————————————————–
% SEE ALSO: prt_reg(results), prt, plt
%—————————————————

Leave a Reply

Your email address will not be published. Required fields are marked *