Class: SpatialStats::Local::Moran
- Defined in:
- lib/spatial_stats/local/moran.rb
Overview
Moran's I statistic computes the spatial autocorrelation of variable x. It does this by computing a spatially lagged version of itself and comparing that with each observation based on the weights matrix. The local version returns the spatial autocorrelation for each observation in the dataset.
Instance Attribute Summary
Attributes inherited from Stat
Instance Method Summary collapse
-
#expectation ⇒ Float
Expected value of I for each observation.
-
#groups ⇒ Array
Computes the groups each observation belongs to.
-
#initialize(scope, field, weights) ⇒ Moran
constructor
A new instance of Moran.
-
#stat ⇒ Array
(also: #i)
Computes the local indicator of spatial autocorrelation (lisa) for x against lagged x.
-
#variance ⇒ Array
Variance of I for each observation.
-
#x ⇒ Array
(also: #z)
Values of the
field
queried from thescope
. -
#z_lag ⇒ Array
Spatially lagged x variable at each observation.
Methods inherited from Stat
#crand, #mc, #mc_bv, #quads, #summary, #x=, #y=, #z_score
Constructor Details
#initialize(scope, field, weights) ⇒ Moran
A new instance of Moran
20 21 22 |
# File 'lib/spatial_stats/local/moran.rb', line 20 def initialize(scope, field, weights) super(scope, field, weights) end |
Instance Method Details
#expectation ⇒ Float
Expected value of I for each observation. Since the weights matrix is standardized during the calculation, the expectation is the same for each observation.
42 43 44 45 46 47 48 |
# File 'lib/spatial_stats/local/moran.rb', line 42 def expectation # since we are using row standardized weights, the expectation # will just be -1/(n-1) for all items. Otherwise, it would be # a vector where the sum of the weights for each row is the numerator # in the equation. -1.0 / (@weights.n - 1) end |
#groups ⇒ Array
Computes the groups each observation belongs to. Potential groups for Moran's I are:
- HH
-
High-High
- HL
-
High-Low
- LH
-
Low-High
- LL
-
Low-Low
This is the same as the #quads
method in the Stat
class.
81 82 83 |
# File 'lib/spatial_stats/local/moran.rb', line 81 def groups quads end |
#stat ⇒ Array Also known as: i
Computes the local indicator of spatial autocorrelation (lisa) for x against lagged x.
29 30 31 32 33 |
# File 'lib/spatial_stats/local/moran.rb', line 29 def stat z.each_with_index.map do |_z_val, idx| stat_i(idx) end end |
#variance ⇒ Array
Variance of I for each observation.
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/spatial_stats/local/moran.rb', line 56 def variance # formula is A - B - (E[I])**2 exp = expectation vars = [] a_terms = a_calc b_terms = b_calc a_terms.each_with_index do |a_term, idx| vars << (a_term - b_terms[idx] - (exp**2)) end vars end |
#x ⇒ Array Also known as: z
Values of the field
queried from the scope
89 90 91 92 |
# File 'lib/spatial_stats/local/moran.rb', line 89 def x @x ||= SpatialStats::Queries::Variables.query_field(@scope, @field) .standardize end |
#z_lag ⇒ Array
Spatially lagged x variable at each observation.
99 100 101 102 103 |
# File 'lib/spatial_stats/local/moran.rb', line 99 def z_lag # w is already row_standardized, so we are using # neighbor sum instead of neighbor_average to save cost @z_lag ||= SpatialStats::Utils::Lag.neighbor_sum(weights, z) end |