Class: SpatialStats::Local::GetisOrd

Inherits:
Stat
  • Object
show all
Defined in:
lib/spatial_stats/local/getis_ord.rb

Overview

GetisOrd's G and G* statistics compute the spatial autocorrelation of a variable, x. G computes the ratio of spatially lagged x to the sum of all other x's except xi for every entry. G* does the same calculation but includes xi in the spatial lag and denominator.

Instance Attribute Summary collapse

Attributes inherited from Stat

#field, #scope, #weights

Instance Method Summary collapse

Methods inherited from Stat

#crand, #expectation, #mc, #mc_bv, #quads, #summary, #variance, #y=, #z_score

Constructor Details

#initialize(scope, field, weights, star = nil) ⇒ GetisOrd

A new instance of GetisOrd

Parameters:

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

    to query from scope

  • weights (WeightsMatrix)

    to define relationship between observations in scope

  • star (Boolean) (defaults to: nil)

    to preset if star will be true or false. Will be calculated otherwise.



20
21
22
23
24
25
26
# File 'lib/spatial_stats/local/getis_ord.rb', line 20

def initialize(scope, field, weights, star = nil)
  @scope = scope
  @field = field
  @weights = weights
  @star = star
  calc_weights
end

Instance Attribute Details

#starObject

Returns the value of attribute star.



27
28
29
# File 'lib/spatial_stats/local/getis_ord.rb', line 27

def star
  @star
end

#xArray Also known as: z

Values of the field queried from the scope

Returns:

  • (Array)


64
65
66
# File 'lib/spatial_stats/local/getis_ord.rb', line 64

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

Instance Method Details

#groupsArray

Computes the groups each observation belongs to. Potential groups for G are:

H

High

L

Low

Group is high when standardized z is positive, low otherwise.

Returns:

  • (Array)

    groups for each observation



50
51
52
53
54
55
56
57
58
# File 'lib/spatial_stats/local/getis_ord.rb', line 50

def groups
  z.standardize.map do |val|
    if val.positive?
      'H'
    else
      'L'
    end
  end
end

#star?Boolean

True if G* is being used, false if G is being used. If no value is passed in the constructor, it will be determined based off of the trace of the weights.

Returns:

  • (Boolean)

    of star



75
76
77
78
79
80
81
# File 'lib/spatial_stats/local/getis_ord.rb', line 75

def star?
  if @star.nil?
    @star = weights.dense.trace.positive?
  else
    @star
  end
end

#statArray Also known as: g

Computes the G or G* statistic for every observation in x.

Returns:

  • (Array)

    of autocorrelations for each observation.



34
35
36
37
38
# File 'lib/spatial_stats/local/getis_ord.rb', line 34

def stat
  x.each_with_index.map do |_x_val, idx|
    stat_i(idx)
  end
end