Class: SpatialStats::Local::MultivariateGeary
- Defined in:
- lib/spatial_stats/local/multivariate_geary.rb
Overview
MultivariateGeary works like univariate Geary, except that it takes an array of data fields, rather than one data field. It measures the extent to which the average distance in attribute space between values and its neighbors compared to what they would be under spatial randomness.
Functionally, C is computed by averaging the C values for each attribute at a certain location, under a univariate context.
Instance Attribute Summary collapse
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#weights ⇒ Object
Returns the value of attribute weights.
Attributes inherited from Stat
Instance Method Summary collapse
- #groups ⇒ Object
-
#initialize(scope, fields, weights) ⇒ MultivariateGeary
constructor
A new instance of Moran.
-
#mc(permutations = 99, seed = nil) ⇒ Array
Permutation test to determine a pseudo p-values of the
#stat
method. -
#stat ⇒ Array
(also: #c)
Computes the stat for MultivariateGeary.
Methods inherited from Stat
#crand, #expectation, #mc_bv, #quads, #summary, #variance, #x=, #y=, #z_score
Constructor Details
#initialize(scope, fields, weights) ⇒ MultivariateGeary
A new instance of Moran
23 24 25 26 27 |
# File 'lib/spatial_stats/local/multivariate_geary.rb', line 23 def initialize(scope, fields, weights) @scope = scope @fields = fields @weights = weights.standardize end |
Instance Attribute Details
#fields ⇒ Object
Returns the value of attribute fields.
28 29 30 |
# File 'lib/spatial_stats/local/multivariate_geary.rb', line 28 def fields @fields end |
#scope ⇒ Object
Returns the value of attribute scope.
28 29 30 |
# File 'lib/spatial_stats/local/multivariate_geary.rb', line 28 def scope @scope end |
#weights ⇒ Object
Returns the value of attribute weights.
28 29 30 |
# File 'lib/spatial_stats/local/multivariate_geary.rb', line 28 def weights @weights end |
Instance Method Details
#groups ⇒ Object
100 101 102 |
# File 'lib/spatial_stats/local/multivariate_geary.rb', line 100 def groups raise NotImplementedError, 'groups not implemented' end |
#mc(permutations = 99, seed = nil) ⇒ Array
Permutation test to determine a pseudo p-values of the #stat
method. Shuffles all tuples, recomputes #stat
for each variation, then compares to the computed one. The ratio of more extreme values to permutations is returned for each observation.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/spatial_stats/local/multivariate_geary.rb', line 57 def mc(permutations = 99, seed = nil) # in this case, one tuple of vals is held constant, then # the rest are shuffled, so for crand we will pass in an arr # of indices, which will return a list of new orders for the fields. # They will then be shuffled corresponding to the new indices. rng = gen_rng(seed) rids = crand(permutations, rng) n_1 = weights.n - 1 sparse = weights.sparse row_index = sparse.row_index ws = sparse.values wc = weights.wc stat_orig = stat ids = (0..n_1).to_a observations = Array.new(weights.n) (0..n_1).each do |idx| idsi = ids.dup idsi.delete_at(idx) idsi.shuffle!(random: rng) idsi = Numo::Int32.cast(idsi) sample = rids[idsi[rids[true, 0..wc[idx] - 1]]] # account for case where there are no neighbors row_range = row_index[idx]..(row_index[idx + 1] - 1) if row_range.size.zero? observations[idx] = permutations next end wi = Numo::DFloat.cast(ws[row_range]) stat_i_new = mc_i(wi, sample, idx) stat_i_orig = stat_orig[idx] observations[idx] = mc_observation_calc(stat_i_orig, stat_i_new, permutations) end observations.map do |ri| (ri + 1.0) / (permutations + 1.0) end end |
#stat ⇒ Array Also known as: c
Computes the stat for MultivariateGeary.
36 37 38 39 40 41 42 |
# File 'lib/spatial_stats/local/multivariate_geary.rb', line 36 def stat m = fields.size gearys = fields.map do |field| Geary.new(scope, field, weights).stat end gearys.transpose.map { |x| x.reduce(:+) / m } end |