Class: SpatialStats::Local::MultivariateGeary

Inherits:
Stat
  • Object
show all
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

Attributes inherited from Stat

#field

Instance Method Summary collapse

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

Parameters:

  • scope (ActiveRecord::Relation)
  • fields (Symbol, String)

    to query from scope

  • weights (WeightsMatrix)

    to define relationship between observations in scope



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

#fieldsObject

Returns the value of attribute fields.



28
29
30
# File 'lib/spatial_stats/local/multivariate_geary.rb', line 28

def fields
  @fields
end

#scopeObject

Returns the value of attribute scope.



28
29
30
# File 'lib/spatial_stats/local/multivariate_geary.rb', line 28

def scope
  @scope
end

#weightsObject

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

#groupsObject

Raises:

  • (NotImplementedError)


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.

Parameters:

  • permutations (Integer) (defaults to: 99)

    to run. Last digit should be 9 to produce round numbers.

  • seed (Integer) (defaults to: nil)

    used in random number generator for shuffles.

Returns:

  • (Array)

    of p-values

See Also:



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

#statArray Also known as: c

Computes the stat for MultivariateGeary.

Returns:

  • (Array)

    of C values for each observation.

See Also:



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