Class: CTable

Inherits:
Object
  • Object
show all
Defined in:
datavyu_api.rb

Overview

Class for keeping track of the agreement table for one code. !@attr table

@return [Matrix] contingency table of values

!@attr codes

@return [Array<String>] list of code valus; indices serve as keys for table

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*values) ⇒ CTable

Returns a new instance of CTable



572
573
574
575
576
# File 'datavyu_api.rb', line 572

def initialize(*values)
  raise "CTable must have at least 2 valid values. Got : #{values}" if values.size<2
  @codes = values
  @table = Matrix.zero(values.size)
end

Instance Attribute Details

#codesObject

Returns the value of attribute codes



570
571
572
# File 'datavyu_api.rb', line 570

def codes
  @codes
end

#tableObject

Returns the value of attribute table



570
571
572
# File 'datavyu_api.rb', line 570

def table
  @table
end

Instance Method Details

#add(pri_value, rel_value) ⇒ Object

Add a code pair. Order always pri,rel. Increments the appropriate index of the table by 1.

Parameters:

  • pri_value

    primary coder's value

  • rel_value

    reliability coder's value

Returns:

  • nil



582
583
584
585
586
587
588
589
# File 'datavyu_api.rb', line 582

def add(pri_value, rel_value)
  pri_idx = @codes.index(pri_value)
  raise "Invalid primary value: #{pri_value}" if pri_idx.nil?
  rel_idx = @codes.index(rel_value)
  raise "Invalid reliability value: #{rel_value}" if rel_idx.nil?

  @table[pri_idx, rel_idx] += 1
end

#ef(idx) ⇒ Fixnum

Return the expected frequency of agreement by chance for the given index

Parameters:

  • idx (Integer)

    index in codes

Returns:

  • (Fixnum)

    agreement by chance



604
605
606
607
608
609
610
611
612
# File 'datavyu_api.rb', line 604

def ef(idx)
  raise "Index out of bounds: requested #{idx}, have #{@codes.size}." if idx >= @codes.size

  # The expected frequency is (row_total * column_total)/matrix_total
  row_total = @table.row(idx).to_a.reduce(:+)
  col_total = @table.column(idx).to_a.reduce(:+)
  ret = (row_total * col_total)/self.total.to_f
  return ret
end

#efsFixnum

Return the sum of the expected frequency of agreement by chance for all indices in table

Returns:

  • (Fixnum)

    sum of a agreement by chance



616
617
618
619
620
621
622
# File 'datavyu_api.rb', line 616

def efs
  sum = 0
  for idx in 0..@codes.size-1
    sum += self.ef(idx).to_f
  end
  return sum
end

#kappaObject

Compute kappa

Returns:

  • simple kappa score



593
594
595
596
597
598
599
# File 'datavyu_api.rb', line 593

def kappa
  agree = @table.trace
  total = self.total
  efs = self.efs
  k = (agree-efs)/(total-efs)
  return k
end

#to_sString

Table to String Return formatted string to display the table

Returns:

  • (String)

    tab-delimited string showing values in contingency table



636
637
638
639
640
641
642
643
644
645
646
# File 'datavyu_api.rb', line 636

def to_s
  str = "\t" + codes.join("\t") + "\n"
  for i in 0..@codes.size-1
    str << @codes[i] + "\t"
    for j in 0..@codes.size-1
      str << @table[i,j].to_s + "\t"
    end
    str << "\n"
  end
  return str
end

#totalInteger

Return the sum of all elements in matrix table

Returns:

  • (Integer)

    sum of matrix elements



626
627
628
629
630
631
# File 'datavyu_api.rb', line 626

def total
  v = Matrix.row_vector([1] * @codes.size) # row vector of 1s
  vt = v.t  # column vector of 1s
  ret = (v * @table * vt)
  return ret[0,0]
end