Class: SpatialStats::Global::Stat

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

Overview

Stat is the abstract base class for global stats. It defines the methods that are common between all classes and will raise a NotImplementedError on those that are specific for each type of statistic.

Direct Known Subclasses

BivariateMoran, Moran

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, field, weights) ⇒ Stat

Returns a new instance of Stat.



11
12
13
14
15
# File 'lib/spatial_stats/global/stat.rb', line 11

def initialize(scope, field, weights)
  @scope = scope
  @field = field
  @weights = weights.standardize
end

Instance Attribute Details

#fieldObject

Returns the value of attribute field.



16
17
18
# File 'lib/spatial_stats/global/stat.rb', line 16

def field
  @field
end

#scopeObject

Returns the value of attribute scope.



16
17
18
# File 'lib/spatial_stats/global/stat.rb', line 16

def scope
  @scope
end

#weightsObject

Returns the value of attribute weights.



16
17
18
# File 'lib/spatial_stats/global/stat.rb', line 16

def weights
  @weights
end

Instance Method Details

#expectationFloat

The expected value of #stat

Returns:

  • (Float)

Raises:

  • (NotImplementedError)


26
27
28
# File 'lib/spatial_stats/global/stat.rb', line 26

def expectation
  raise NotImplementedError, 'method expectation not implemented'
end

#mc(permutations, seed) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/spatial_stats/global/stat.rb', line 51

def mc(permutations, seed)
  rng = gen_rng(seed)
  shuffles = []
  permutations.times do
    shuffles << x.shuffle(random: rng)
  end
  shuffles = Numo::DFloat.cast(shuffles)

  # r is the number of equal to or more extreme samples
  # one sided
  stat_orig = stat.round(5)
  # r = 0

  # compute new stat values
  stat_new = stat_mc(shuffles)

  r = if stat_orig.positive?
        (stat_new >= stat_orig).count
      else
        (stat_new <= stat_orig).count
      end

  (r + 1.0) / (permutations + 1.0)
end

#mc_bv(permutations, seed) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/spatial_stats/global/stat.rb', line 76

def mc_bv(permutations, seed)
  # in multivariate, hold x and shuffle y
  rng = gen_rng(seed)
  shuffles = []
  permutations.times do
    shuffles << y.shuffle(random: rng)
  end
  shuffles = Numo::DFloat.cast(shuffles)

  # r is the number of equal to or more extreme samples
  stat_orig = stat.round(5)
  stat_new = stat_mc(shuffles)

  r = if stat_orig.positive?
        (stat_new >= stat_orig).count
      else
        (stat_new <= stat_orig).count
      end

  (r + 1.0) / (permutations + 1.0)
end

#statObject

Raises:

  • (NotImplementedError)


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

def stat
  raise NotImplementedError, 'method stat not defined'
end

#varianceObject

Raises:

  • (NotImplementedError)


30
31
32
# File 'lib/spatial_stats/global/stat.rb', line 30

def variance
  raise NotImplementedError, 'method variance not implemented'
end

#x=(values) ⇒ Object Also known as: z=



42
43
44
# File 'lib/spatial_stats/global/stat.rb', line 42

def x=(values)
  @x = values.standardize
end

#y=(values) ⇒ Object



47
48
49
# File 'lib/spatial_stats/global/stat.rb', line 47

def y=(values)
  @y = values.standardize
end

#z_scoreFloat

Z-score of the statistic.

Returns:

  • (Float)

    the number of deviations from the mean



38
39
40
# File 'lib/spatial_stats/global/stat.rb', line 38

def z_score
  (stat - expectation) / Math.sqrt(variance)
end