Class: SpatialStats::Global::BivariateMoran
- Defined in:
- lib/spatial_stats/global/bivariate_moran.rb
Overview
BivariateMoran computes the correlation between a variable x and a spatially lagged variable y.
Instance Attribute Summary
Attributes inherited from Stat
Instance Method Summary collapse
-
#expectation ⇒ Float
The expected value of
#stat
. -
#initialize(scope, x_field, y_field, weights) ⇒ BivariateMoran
constructor
A new instance of BivariateMoran.
-
#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 correlation of x against spatially lagged y.
-
#summary(permutations = 99, seed = nil) ⇒ Hash
Summary of the statistic.
-
#variance ⇒ Float
The variance of the bivariate spatial correlation.
-
#x ⇒ Array
Standardized variables queried from
x_field
. -
#y ⇒ Array
Standardized variables queried from
y_field
.
Methods inherited from Stat
Constructor Details
#initialize(scope, x_field, y_field, weights) ⇒ BivariateMoran
A new instance of BivariateMoran
18 19 20 21 22 23 |
# File 'lib/spatial_stats/global/bivariate_moran.rb', line 18 def initialize(scope, x_field, y_field, weights) @scope = scope @x_field = x_field @y_field = y_field @weights = weights.standardize end |
Instance Method Details
#expectation ⇒ Float
The expected value of #stat
.
46 47 48 |
# File 'lib/spatial_stats/global/bivariate_moran.rb', line 46 def expectation -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 y values while holding x constant and recomputing I for each variation, then compares that I value to the computed one. The ratio of more extreme values to permutations is returned.
86 87 88 |
# File 'lib/spatial_stats/global/bivariate_moran.rb', line 86 def mc(permutations = 99, seed = nil) mc_bv(permutations, seed) end |
#stat ⇒ Float Also known as: i
Computes the global spatial correlation of x against spatially lagged y.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/spatial_stats/global/bivariate_moran.rb', line 30 def stat y_lag = SpatialStats::Utils::Lag.neighbor_sum(weights, y) numerator = 0 x.each_with_index do |xi, idx| numerator += xi * y_lag[idx] end denominator = x.sum { |xi| xi**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.
98 99 100 101 |
# File 'lib/spatial_stats/global/bivariate_moran.rb', line 98 def summary(permutations = 99, seed = nil) p_val = mc(permutations, seed) { stat: stat, p: p_val } end |
#variance ⇒ Float
The variance of the bivariate spatial correlation.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/spatial_stats/global/bivariate_moran.rb', line 55 def variance n = weights.n w_sum = n.to_f e = expectation wij = weights.sparse.coordinates s1 = s1_calc(wij) s2 = s2_calc(n, wij, weights.sparse.row_index) s3 = s3_calc(n, x) 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
Standardized variables queried from x_field
.
107 108 109 110 |
# File 'lib/spatial_stats/global/bivariate_moran.rb', line 107 def x @x ||= SpatialStats::Queries::Variables.query_field(@scope, @x_field) .standardize end |
#y ⇒ Array
Standardized variables queried from y_field
.
116 117 118 119 |
# File 'lib/spatial_stats/global/bivariate_moran.rb', line 116 def y @y ||= SpatialStats::Queries::Variables.query_field(@scope, @y_field) .standardize end |