summaryrefslogtreecommitdiff
path: root/lib/feature_selection.rb
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.ch>2016-10-05 13:22:12 +0200
committerChristoph Helma <helma@in-silico.ch>2016-10-05 13:22:12 +0200
commit5d4e5e463c2b87241bbb56e4658e1e26c0ed084f (patch)
treebbae8f77dbb2ac85053f1253ab518c3076e0d176 /lib/feature_selection.rb
parentadefea0e78a4f05a2c9537e643873ad61fc22a0a (diff)
substance and nanoparticle model creation and predictions
Diffstat (limited to 'lib/feature_selection.rb')
-rw-r--r--lib/feature_selection.rb46
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/feature_selection.rb b/lib/feature_selection.rb
new file mode 100644
index 0000000..43e3bea
--- /dev/null
+++ b/lib/feature_selection.rb
@@ -0,0 +1,46 @@
+module OpenTox
+ module Algorithm
+
+ class FeatureSelection
+
+ def self.correlation_filter dataset:, prediction_feature:, types:nil
+ # TODO: speedup, single assignment of all features to R+ parallel computation of significance?
+ relevant_features = {}
+ measurements = []
+ substances = []
+ dataset.substances.each do |s|
+ dataset.values(s,prediction_feature).each do |act|
+ measurements << act
+ substances << s
+ end
+ end
+ R.assign "tox", measurements
+ feature_ids = dataset.substances.collect{ |s| s["properties"].keys}.flatten.uniq
+ feature_ids.select!{|fid| types.include? Feature.find(fid).category} if types
+ feature_ids.each do |feature_id|
+ feature_values = substances.collect{|s| s["properties"][feature_id].first if s["properties"][feature_id]}
+ unless feature_values.uniq.size == 1
+ R.assign "feature", feature_values
+ begin
+ R.eval "cor <- cor.test(tox,feature,method = 'pearson',use='pairwise')"
+ pvalue = R.eval("cor$p.value").to_ruby
+ if pvalue <= 0.05
+ r = R.eval("cor$estimate").to_ruby
+ relevant_features[feature_id] = {}
+ relevant_features[feature_id]["pvalue"] = pvalue
+ relevant_features[feature_id]["r"] = r
+ relevant_features[feature_id]["mean"] = R.eval("mean(feature, na.rm=TRUE)").to_ruby
+ relevant_features[feature_id]["sd"] = R.eval("sd(feature, na.rm=TRUE)").to_ruby
+ end
+ rescue
+ warn "Correlation of '#{Feature.find(feature_id).name}' (#{feature_values}) with '#{Feature.find(prediction_feature_id).name}' (#{measurements}) failed."
+ end
+ end
+ end
+ relevant_features.sort{|a,b| a[1]["pvalue"] <=> b[1]["pvalue"]}.to_h
+ end
+
+ end
+
+ end
+end