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

Class Method 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

Class Method Details

.from_observations(x, weights) ⇒ Stat

A new instance of Stat, from vector and weights.

Parameters:

  • x (Array)

    observations of dataset

  • weights (WeightsMatrix)

    to define relationships between observations

Returns:

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
# File 'lib/spatial_stats/global/stat.rb', line 25

def self.from_observations(x, weights)
  raise ArgumentError, 'Data size != weights.n' if x.size != weights.n

  instance = new(nil, nil, weights.standardize)
  instance.x = x
  instance
end

Instance Method Details

#expectationFloat

The expected value of #stat

Returns:

  • (Float)

Raises:

  • (NotImplementedError)


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

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

#mc(permutations, seed) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/spatial_stats/global/stat.rb', line 66

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/spatial_stats/global/stat.rb', line 91

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)


33
34
35
# File 'lib/spatial_stats/global/stat.rb', line 33

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

#varianceObject

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/spatial_stats/global/stat.rb', line 45

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

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



57
58
59
# File 'lib/spatial_stats/global/stat.rb', line 57

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

#y=(values) ⇒ Object



62
63
64
# File 'lib/spatial_stats/global/stat.rb', line 62

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

#z_scoreFloat

Z-score of the statistic.

Returns:

  • (Float)

    the number of deviations from the mean



53
54
55
# File 'lib/spatial_stats/global/stat.rb', line 53

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