Class: SpatialStats::Global::Stat
- Inherits:
-
Object
- Object
- SpatialStats::Global::Stat
- 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
Instance Attribute Summary collapse
-
#field ⇒ Object
Returns the value of attribute field.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#weights ⇒ Object
Returns the value of attribute weights.
Instance Method Summary collapse
-
#expectation ⇒ Float
The expected value of
#stat
. -
#initialize(scope, field, weights) ⇒ Stat
constructor
A new instance of Stat.
- #mc(permutations, seed) ⇒ Object
- #mc_bv(permutations, seed) ⇒ Object
- #stat ⇒ Object
- #variance ⇒ Object
- #x=(values) ⇒ Object (also: #z=)
- #y=(values) ⇒ Object
-
#z_score ⇒ Float
Z-score of the statistic.
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
#field ⇒ Object
Returns the value of attribute field.
16 17 18 |
# File 'lib/spatial_stats/global/stat.rb', line 16 def field @field end |
#scope ⇒ Object
Returns the value of attribute scope.
16 17 18 |
# File 'lib/spatial_stats/global/stat.rb', line 16 def scope @scope end |
#weights ⇒ Object
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
#expectation ⇒ Float
The expected value of #stat
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 |
#stat ⇒ Object
18 19 20 |
# File 'lib/spatial_stats/global/stat.rb', line 18 def stat raise NotImplementedError, 'method stat not defined' end |
#variance ⇒ Object
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_score ⇒ Float
Z-score of the statistic.
38 39 40 |
# File 'lib/spatial_stats/global/stat.rb', line 38 def z_score (stat - expectation) / Math.sqrt(variance) end |