Class: ViralSeq::Math::BinomCI

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

Overview

Use R to calculate binomial 95% confidence intervals. Require R function binom.test.

Examples:

mutation M184V found in 3 out of 923 sequences, calculate 95% confidence interval

freq = ViralSeq::Math::BinomCI.new(3,923)
freq.mean.round(5)
=> 0.00325
freq.lower.round(5)
=> 0.00067
freq.upper.round(5)
=> 0.00947

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n1, n2) ⇒ BinomCI

initialize with numerator @n1 and denominator @n2 as Integer



95
96
97
98
99
100
101
102
103
# File 'lib/viral_seq/math.rb', line 95

def initialize(n1, n2)
  @n1 = n1
  @n2 = n2
  @mean = n1/n2.to_f
  r_output = `Rscript -e 'binom.test(#{n1},#{n2})$conf.int[1];binom.test(#{n1},#{n2})$conf.int[2]'`
  lines = r_output.split "\n"
  @lower = lines[0].chomp[4..-1].to_f
  @upper = lines[1].chomp[4..-1].to_f
end

Instance Attribute Details

#lowerFloat (readonly)

Returns lower limit of 95% CI.

Returns:

  • (Float)

    lower limit of 95% CI



112
113
114
# File 'lib/viral_seq/math.rb', line 112

def lower
  @lower
end

#meanFloat (readonly)

Returns mean.

Returns:

  • (Float)

    mean



110
111
112
# File 'lib/viral_seq/math.rb', line 110

def mean
  @mean
end

#n1Integer

Returns number of observations.

Returns:

  • (Integer)

    number of observations



106
107
108
# File 'lib/viral_seq/math.rb', line 106

def n1
  @n1
end

#n2Integer

Returns total numbers.

Returns:



108
109
110
# File 'lib/viral_seq/math.rb', line 108

def n2
  @n2
end

#upperFloat (readonly)

Returns upper limit of 95% CI.

Returns:

  • (Float)

    upper limit of 95% CI



114
115
116
# File 'lib/viral_seq/math.rb', line 114

def upper
  @upper
end