Class: SpatialStats::Global::Moran

Inherits:
Stat
  • Object
show all
Defined in:
lib/spatial_stats/global/moran.rb

Overview

Moran's I statistic computes the spatial autocorrelation of variable x. It does this by computing a spatially lagged version of itself and comparing that with each observation based on the weights matrix.

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, field, weights) ⇒ Moran

A new instance of Moran

Parameters:

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

    to query from scope

  • weights (WeightsMatrix)

    to define relationship between observations in scope



18
19
20
# File 'lib/spatial_stats/global/moran.rb', line 18

def initialize(scope, field, weights)
  super(scope, field, weights)
end

Instance Method Details

#expectationFloat

The expected value of #stat.



48
49
50
51
# File 'lib/spatial_stats/global/moran.rb', line 48

def expectation
  # -1/(n-1)
  -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 x values recomputes 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:



89
90
91
# File 'lib/spatial_stats/global/moran.rb', line 89

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

#statFloat Also known as: i

Computes the global spatial autocorrelation of x against a spatially lagged x.

Returns:

  • (Float)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/spatial_stats/global/moran.rb', line 27

def stat
  # compute's Moran's I. numerator is sum of zi * spatial lag of zi
  # denominator is sum of zi**2.
  # have to use row-standardized weights
  z_lag = SpatialStats::Utils::Lag.neighbor_sum(weights, z)
  numerator = 0
  z.each_with_index do |zi, j|
    row_sum = zi * z_lag[j]
    numerator += row_sum
  end

  denominator = z.sum { |zi| zi**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)


101
102
103
104
# File 'lib/spatial_stats/global/moran.rb', line 101

def summary(permutations = 99, seed = nil)
  p_val = mc(permutations, seed)
  { stat: stat, p: p_val }
end

#varianceFloat

The variance of the spatial correlation.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/spatial_stats/global/moran.rb', line 58

def variance
  n = weights.n
  w_sum = n # standardized weights
  e = expectation

  wij = weights.sparse.coordinates

  s1 = s1_calc(wij)
  s2 = s2_calc(n, wij, weights.sparse.row_index)
  s3 = s3_calc(n, z)

  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 Also known as: z

Values of the field queried from the scope

Returns:

  • (Array)


110
111
112
113
# File 'lib/spatial_stats/global/moran.rb', line 110

def x
  @x ||= SpatialStats::Queries::Variables.query_field(@scope, @field)
                                         .standardize
end