Class: CTable
- Inherits:
-
Object
- Object
- CTable
- 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
-
#codes ⇒ Object
Returns the value of attribute codes.
-
#table ⇒ Object
Returns the value of attribute table.
Instance Method Summary collapse
-
#add(pri_value, rel_value) ⇒ Object
Add a code pair.
-
#ef(idx) ⇒ Fixnum
Return the expected frequency of agreement by chance for the given index.
-
#efs ⇒ Fixnum
Return the sum of the expected frequency of agreement by chance for all indices in table.
-
#initialize(*values) ⇒ CTable
constructor
A new instance of CTable.
-
#kappa ⇒ Object
Compute kappa.
-
#to_s ⇒ String
Table to String Return formatted string to display the table.
-
#total ⇒ Integer
Return the sum of all elements in matrix table.
Constructor Details
Instance Attribute Details
#codes ⇒ Object
Returns the value of attribute codes
570 571 572 |
# File 'datavyu_api.rb', line 570 def codes @codes end |
#table ⇒ Object
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.
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
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 |
#efs ⇒ Fixnum
Return the sum of the expected frequency of agreement by chance for all indices in table
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 |
#kappa ⇒ Object
Compute kappa
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_s ⇒ String
Table to String Return formatted string to display the 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 |
#total ⇒ Integer
Return the sum of all elements in matrix table
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 |