Class: SpatialStats::Local::Moran

Inherits:
Stat
  • Object
show all
Defined in:
lib/spatial_stats/local/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. The local version returns the spatial autocorrelation for each observation in the dataset.

Instance Attribute Summary

Attributes inherited from Stat

#field, #scope, #weights

Instance Method Summary collapse

Methods inherited from Stat

#crand, #mc, #mc_bv, #quads, #summary, #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



20
21
22
# File 'lib/spatial_stats/local/moran.rb', line 20

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

Instance Method Details

#expectationFloat

Expected value of I for each observation. Since the weights matrix is standardized during the calculation, the expectation is the same for each observation.

Returns:

  • (Float)


42
43
44
45
46
47
48
# File 'lib/spatial_stats/local/moran.rb', line 42

def expectation
  # since we are using row standardized weights, the expectation
  # will just be -1/(n-1) for all items. Otherwise, it would be
  # a vector where the sum of the weights for each row is the numerator
  # in the equation.
  -1.0 / (@weights.n - 1)
end

#groupsArray

Computes the groups each observation belongs to. Potential groups for Moran's I are:

HH

High-High

HL

High-Low

LH

Low-High

LL

Low-Low

This is the same as the #quads method in the Stat class.

Returns:

  • (Array)

    groups for each observation



81
82
83
# File 'lib/spatial_stats/local/moran.rb', line 81

def groups
  quads
end

#statArray Also known as: i

Computes the local indicator of spatial autocorrelation (lisa) for x against lagged x.

Returns:

  • (Array)

    of autocorrelations for each observation.



29
30
31
32
33
# File 'lib/spatial_stats/local/moran.rb', line 29

def stat
  z.each_with_index.map do |_z_val, idx|
    stat_i(idx)
  end
end

#varianceArray

Variance of I for each observation.



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/spatial_stats/local/moran.rb', line 56

def variance
  # formula is A - B - (E[I])**2
  exp = expectation

  vars = []
  a_terms = a_calc
  b_terms = b_calc

  a_terms.each_with_index do |a_term, idx|
    vars << (a_term - b_terms[idx] - (exp**2))
  end
  vars
end

#xArray Also known as: z

Values of the field queried from the scope

Returns:

  • (Array)


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

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

#z_lagArray

Spatially lagged x variable at each observation.

Returns:

  • (Array)


99
100
101
102
103
# File 'lib/spatial_stats/local/moran.rb', line 99

def z_lag
  # w is already row_standardized, so we are using
  # neighbor sum instead of neighbor_average to save cost
  @z_lag ||= SpatialStats::Utils::Lag.neighbor_sum(weights, z)
end