Home > custom > grid_tools > localmoran.m

localmoran

PURPOSE ^

PURPOSE: calculate local Moran's I for a local grid using a weight matrix.

SYNOPSIS ^

function m = localmoran(raster, W, s)

DESCRIPTION ^

 PURPOSE: calculate local Moran's I for a local grid using a weight matrix.
 -------------------------------------------------------------------
 USAGE: m = localmoran(raster, W, s);
 where: [raster] is the grid to analyse
        [W] is the normalized weight matrix of same size as [raster]
        [s] is an optional flag to use zscores of input values for
        calculation. Set to 'true' if zscores of local grid should be
        calculated. Leave blank if not desired or input values are already
        standardized. 
 -------------------------------------------------------------------------
 OUTPUTS:
        [m] Moran's I of input raster weighted using [W]
 -------------------------------------------------------------------
 NOTES: Weight matrix needs to be 'moving window' style, not contiguity
        matrix: Moran's I is calculated and weighted for neighbours to center cell.
        Matrix needs to be normalized (weights sum to 1) and center cell weight 
        will be set to 0 if not already. 
        Use euklid_W() or euklid_invW() to construct weight matrices with
        euklidean distances.

 See Anselin (1995, 'LISA.', Geogr. Analysis 27(2),p.93f) for details on standardized variables in 
 calculation of local Moran's I. 

 Felix Hebeler, Geography Dept., University Zurich, March 2006.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function m = localmoran(raster, W, s)
0002 % PURPOSE: calculate local Moran's I for a local grid using a weight matrix.
0003 % -------------------------------------------------------------------
0004 % USAGE: m = localmoran(raster, W, s);
0005 % where: [raster] is the grid to analyse
0006 %        [W] is the normalized weight matrix of same size as [raster]
0007 %        [s] is an optional flag to use zscores of input values for
0008 %        calculation. Set to 'true' if zscores of local grid should be
0009 %        calculated. Leave blank if not desired or input values are already
0010 %        standardized.
0011 % -------------------------------------------------------------------------
0012 % OUTPUTS:
0013 %        [m] Moran's I of input raster weighted using [W]
0014 % -------------------------------------------------------------------
0015 % NOTES: Weight matrix needs to be 'moving window' style, not contiguity
0016 %        matrix: Moran's I is calculated and weighted for neighbours to center cell.
0017 %        Matrix needs to be normalized (weights sum to 1) and center cell weight
0018 %        will be set to 0 if not already.
0019 %        Use euklid_W() or euklid_invW() to construct weight matrices with
0020 %        euklidean distances.
0021 %
0022 % See Anselin (1995, 'LISA.', Geogr. Analysis 27(2),p.93f) for details on standardized variables in
0023 % calculation of local Moran's I.
0024 %
0025 % Felix Hebeler, Geography Dept., University Zurich, March 2006.
0026 if nargin < 2
0027     error('You need at least 2 input arguments for this function');
0028 end
0029 
0030 ncols= size(raster,2);
0031 nrows= size(raster,1);
0032 
0033 %% Standardize values in window if needed
0034 if exist('s','var')
0035     if strcmp(s,'true');
0036         raster=zscore(raster);
0037     elseif strcmp(s,'false')
0038         %do nothing
0039     else
0040         error('Invalid option for s: set [true] to calculated zscores to determine local Moran or leave blank if values are already standardized.');
0041     end
0042 end
0043 
0044 %% calculate local Moran's I
0045 zi = raster(ceil(nrows/2),ceil(ncols/2));%  value of center cell (note: no weight applied!)
0046 if (isnan(zi));
0047     m=NaN; 
0048     return; 
0049 end;
0050 raster=raster.* W; % Weight values in window
0051 raster(ceil(nrows/2),ceil(ncols/2))=0; %set center cell to zero to exclude zi from sum
0052 zj = nansum(raster(:)); % sum of weighted values excluding zi
0053 m = zi * zj; % calculate local Moran's I and return
0054

Generated on Tue 24-Feb-2009 19:14:50 by m2html © 2003