Class: SpatialStats::Global::Moran
- Defined in:
- lib/spatial_stats/global/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.
Instance Attribute Summary
Attributes inherited from Stat
Instance Method Summary collapse
-
#expectation ⇒ Float
The expected value of
#stat
. -
#initialize(scope, field, weights) ⇒ Moran
constructor
A new instance of Moran.
-
#mc(permutations = 99, seed = nil) ⇒ Float
Permutation test to determine a pseudo p-value of the computed I stat.
-
#stat ⇒ Float
(also: #i)
Computes the global spatial autocorrelation of x against a spatially lagged x.
-
#summary(permutations = 99, seed = nil) ⇒ Hash
Summary of the statistic.
-
#variance ⇒ Float
The variance of the spatial correlation.
-
#x ⇒ Array
(also: #z)
Values of the
field
queried from thescope
.
Methods inherited from Stat
Constructor Details
#initialize(scope, field, weights) ⇒ Moran
A new instance of Moran
18 19 20 |
# File 'lib/spatial_stats/global/moran.rb', line 18 def initialize(scope, field, weights) super(scope, field, weights) end |
Instance Method Details
#expectation ⇒ Float
The expected value of #stat
.
48 49 50 51 |
# File 'lib/spatial_stats/global/moran.rb', line 48 def expectation # -1/(n-1) -1.0 / (weights.n - 1) end |
#mc(permutations = 99, seed = nil) ⇒ Float
Permutation test to determine a pseudo p-value of the computed I stat. Shuffles x values recomputes I for each variation, then compares that I value to the computed one. The ratio of more extreme values to permutations is returned.
89 90 91 |
# File 'lib/spatial_stats/global/moran.rb', line 89 def mc(permutations = 99, seed = nil) super(permutations, seed) end |
#stat ⇒ Float Also known as: i
Computes the global spatial autocorrelation of x against a spatially lagged x.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/spatial_stats/global/moran.rb', line 27 def stat # compute's Moran's I. numerator is sum of zi * spatial lag of zi # denominator is sum of zi**2. # have to use row-standardized weights z_lag = SpatialStats::Utils::Lag.neighbor_sum(weights, z) numerator = 0 z.each_with_index do |zi, j| row_sum = zi * z_lag[j] numerator += row_sum end denominator = z.sum { |zi| zi**2 } numerator / denominator end |
#summary(permutations = 99, seed = nil) ⇒ Hash
Summary of the statistic. Computes stat
and mc
and returns the values in a hash.
101 102 103 104 |
# File 'lib/spatial_stats/global/moran.rb', line 101 def summary(permutations = 99, seed = nil) p_val = mc(permutations, seed) { stat: stat, p: p_val } end |
#variance ⇒ Float
The variance of the spatial correlation.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/spatial_stats/global/moran.rb', line 58 def variance n = weights.n w_sum = n # standardized weights e = expectation wij = weights.sparse.coordinates s1 = s1_calc(wij) s2 = s2_calc(n, wij, weights.sparse.row_index) s3 = s3_calc(n, z) s4 = (n**2 - 3 * n + 3) * s1 - n * s2 + 3 * (w_sum**2) s5 = (n**2 - n) * s1 - 2 * n * s2 + 6 * (w_sum**2) var_left = (n * s4 - s3 * s5) / ((n - 1) * (n - 2) * (n - 3) * w_sum**2) var_right = e**2 var_left - var_right end |
#x ⇒ Array Also known as: z
Values of the field
queried from the scope
110 111 112 113 |
# File 'lib/spatial_stats/global/moran.rb', line 110 def x @x ||= SpatialStats::Queries::Variables.query_field(@scope, @field) .standardize end |