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
Class Method Summary collapse
-
.from_observations(x, y, weights) ⇒ BivariateMoran
A new instance of BivariateMoran, from vector and weights.
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 |
Class Method Details
.from_observations(x, y, weights) ⇒ BivariateMoran
A new instance of BivariateMoran, from vector and weights.
32 33 34 35 36 37 38 39 40 |
# File 'lib/spatial_stats/global/bivariate_moran.rb', line 32 def self.from_observations(x, y, weights) n = weights.n raise ArgumentError, 'Data size != weights.n' if x.size != n || y.size != n instance = new(nil, nil, nil, weights.standardize) instance.x = x instance.y = y instance end |
Instance Method Details
#expectation ⇒ Float
The expected value of #stat
.
63 64 65 |
# File 'lib/spatial_stats/global/bivariate_moran.rb', line 63 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.
103 104 105 |
# File 'lib/spatial_stats/global/bivariate_moran.rb', line 103 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.
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/spatial_stats/global/bivariate_moran.rb', line 47 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.
115 116 117 118 |
# File 'lib/spatial_stats/global/bivariate_moran.rb', line 115 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.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/spatial_stats/global/bivariate_moran.rb', line 72 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
.
124 125 126 127 |
# File 'lib/spatial_stats/global/bivariate_moran.rb', line 124 def x @x ||= SpatialStats::Queries::Variables.query_field(@scope, @x_field) .standardize end |
#y ⇒ Array
Standardized variables queried from y_field
.
133 134 135 136 |
# File 'lib/spatial_stats/global/bivariate_moran.rb', line 133 def y @y ||= SpatialStats::Queries::Variables.query_field(@scope, @y_field) .standardize end |