Sha256: da11a3af1f4b0e7023f44dc94e1760ef4af07fd20a9e1a6306e93ae147f5a329

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

module OpenTox
  module Algorithm
    
    class FeatureSelection

      def self.correlation_filter model
        relevant_features = {}
        R.assign "dependent", model.dependent_variables.collect{|v| to_r(v)}
        model.descriptor_weights = []
        selected_variables = []
        selected_descriptor_ids = []
        model.independent_variables.each_with_index do |v,i|
          v.collect!{|n| to_r(n)}
          R.assign "independent", v
          begin
            R.eval "cor <- cor.test(dependent,independent,method = 'pearson',use='pairwise')"
            pvalue = R.eval("cor$p.value").to_ruby
            if pvalue <= 0.05
              model.descriptor_weights << R.eval("cor$estimate").to_ruby**2
              selected_variables << v
              selected_descriptor_ids << model.descriptor_ids[i]
            end
          rescue
            warn "Correlation of '#{model.prediction_feature.name}' (#{model.dependent_variables}) with (#{v}) failed."
          end
        end

        model.independent_variables = selected_variables
        model.descriptor_ids = selected_descriptor_ids
        model
      end

      def self.to_r v
        return 0 if v == false
        return 1 if v == true
        v
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lazar-1.0.0 lib/feature_selection.rb