Method: Cass::Analysis.permutation_test

Defined in:
lib/cass/analysis.rb

.permutation_test(doc1, doc2, contrasts, output_file, n_perm, opts = {}) ⇒ Object

Run a permutation test comparing two Documents.

  • doc1, doc2: The two Documents to compare

  • contrasts: an array of Contrasts used to compare the documents

  • output_file: name of output file

  • n_perm: number of permutations to run

  • opts: an optional hash of additional settings. Currently, only

‘verbose’ and ‘normalize_weights’ apply here.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cass/analysis.rb', line 96

def self.permutation_test(doc1, doc2, contrasts, output_file, n_perm, opts={})

  # Merge options with defaults
  opts = {'verbose'=>true, 'normalize_weights'=>false }.merge(opts)
  
  # Merge contexts. Could change this later to allow different contexts for each
  # document, but that would make processing substantially slower.
  context = doc1.context
  context.words = context.words & doc2.context.words
  context.index_words
  doc1.context, doc2.context = context, context

  # Generate cooccurrence matrices and get observed difference.
  doc1.cooccurrence(opts['normalize_weights'])
  doc2.cooccurrence(opts['normalize_weights'])

  outf = File.new(output_file,'w')
  outf.puts "contrast\titeration\t#{doc1.name}\t#{doc2.name}\tdifference"
  outf.sync = true
  # Save observed values
  contrasts.each { |c|
    res1, res2, diff = compare_docs(c, doc1, doc2)
    outf.puts "#{c.words.join(".")}\tobserved\t#{res1}\t#{res2}\t#{diff}"
  }
  # Run permutations and save results
  d1, d2 = doc1.clone, doc2.clone
  n_perm.times { |i|
    puts "Running permutation #{i+1}..." if opts['verbose']
    d1.clines, d2.clines = permute_labels(doc1.clines, doc2.clines)
    d1.cooccurrence(opts['normalize_weights'])
    d2.cooccurrence(opts['normalize_weights'])
    contrasts.each { |c|
      res1, res2, diff = compare_docs(c, d1, d2)
      outf.puts "#{c.words.join(".")}\tperm_#{i+1}\t#{res1}\t#{res2}\t#{diff}"
    }
  }
end