Class: Byar

Inherits:
Object
  • Object
show all
Defined in:
lib/byar.rb

Overview

Byar is a Byar’s approximation confidence interval tool with SMR calculation.

Can be used with class-level methods or as instantiated object.

Class-level example with observed value 100 and expected value 90:

Byar.upper(100, 90) # => 1.3514260807978444, equals to SMR of 135.1

Object instance example with observed value 100 and expected value 90:

b = Byar.new(100)
b.upper_bound # => 121.628347271806
b.lower(90) # => 0.9040199067337297, equals to SMR of 90.4

Constant Summary collapse

Z_VALUE =

Z_VALUE defaults to 1.96 for 95% confidence interval

1.96

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obs) ⇒ Byar

Create new Byar approximation calculator for obs observed cases



24
25
26
# File 'lib/byar.rb', line 24

def initialize(obs)
  @obs = obs
end

Class Method Details

.lower(obs, exp) ⇒ Object

Calculate SMR-like lower boundary (1 equals SMR of 100)

Give obs observed and exp expected values to return a lower SMR-like boundary value (0 - Infinite)



33
34
35
36
# File 'lib/byar.rb', line 33

def self.lower(obs, exp)
  return nil if exp == 0
  lower_bound(obs).quo(exp)
end

.lower_bound(obs, z_value = Z_VALUE) ⇒ Object

Calculate lower boundary for observed cases



50
51
52
53
# File 'lib/byar.rb', line 50

def self.lower_bound(obs, z_value = Z_VALUE)
  return 0 if obs == 0
  obs * (1 - 1.quo(9 * obs) - z_value.quo(3 * Math.sqrt(obs))) ** 3
end

.upper(obs, exp) ⇒ Object

Calculate SMR-like upper boundary (1 equals SMR of 100)

Give obs observed and exp expected values to return an upper SMR-like boundary value (0 - Infinite)



43
44
45
46
# File 'lib/byar.rb', line 43

def self.upper(obs, exp)
  return nil if exp == 0
  upper_bound(obs).quo(exp)
end

.upper_bound(obs, z_value = Z_VALUE) ⇒ Object

Calculate upper boundary for observed cases



57
58
59
60
# File 'lib/byar.rb', line 57

def self.upper_bound(obs, z_value = Z_VALUE)
  obs = obs + 1
  obs * (1 - 1.quo(9 * obs) + z_value.quo(3 * Math.sqrt(obs))) ** 3
end

.z_valueObject



66
67
68
# File 'lib/byar.rb', line 66

def self.z_value
  @z_value ||= Z_VALUE
end

.z_value=(value) ⇒ Object



62
63
64
# File 'lib/byar.rb', line 62

def self.z_value=(value)
  @z_value = value
end

Instance Method Details

#lower(expected) ⇒ Object

Give lower SMR-like boundary for expected cases



72
73
74
# File 'lib/byar.rb', line 72

def lower(expected)
  self.class.lower(@obs, expected)
end

#lower_boundObject

Give lower boundary for observed value



84
85
86
# File 'lib/byar.rb', line 84

def lower_bound
  self.class.lower_bound(@obs, z_value)
end

#upper(expected) ⇒ Object

Give upper SMR-like boundary for expected cases



78
79
80
# File 'lib/byar.rb', line 78

def upper(expected)
  self.class.upper(@obs, expected)
end

#upper_boundObject

Give upper boundary for observed value



90
91
92
# File 'lib/byar.rb', line 90

def upper_bound
  self.class.upper_bound(@obs, z_value)
end

#z_valueObject

Give object’s Z value, with fallback to Class variable or Z_VALUE



96
97
98
# File 'lib/byar.rb', line 96

def z_value
  @z_value ||= self.class.z_value
end

#z_value=(value) ⇒ Object

Set custom Z value for this Byar instance



102
103
104
# File 'lib/byar.rb', line 102

def z_value=(value)
  @z_value = value
end