Class: SpatialStats::Global::BivariateMoran

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

#field, #scope, #weights

Instance Method Summary collapse

Methods inherited from Stat

#mc_bv, #x=, #y=, #z_score

Constructor Details

#initialize(scope, x_field, y_field, weights) ⇒ BivariateMoran

A new instance of BivariateMoran

Parameters:

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

    to query from scope

  • y_field (Symbol, String)

    to query from scope

  • weights (WeightsMatrix)

    to define relationship between observations in scope



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

#expectationFloat

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.

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:

  • (Float)

See Also:



86
87
88
# File 'lib/spatial_stats/global/bivariate_moran.rb', line 86

def mc(permutations = 99, seed = nil)
  mc_bv(permutations, seed)
end

#statFloat Also known as: i

Computes the global spatial correlation of x against spatially lagged y.

Returns:

  • (Float)


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.

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:

  • (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

#varianceFloat

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

#xArray

Standardized variables queried from x_field.

Returns:

  • (Array)


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

#yArray

Standardized variables queried from y_field.

Returns:

  • (Array)


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