Class: R::Model
- Inherits:
-
Object
- Object
- R::Model
- Defined in:
- lib/rbbt/util/R/model.rb
Constant Summary collapse
- R_METHOD =
:eval
Instance Attribute Summary collapse
-
#formula ⇒ Object
Returns the value of attribute formula.
-
#model_file ⇒ Object
Returns the value of attribute model_file.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #colClasses(tsv) ⇒ Object
- #exists? ⇒ Boolean
- #fit(tsv, method = 'lm', args = {}) ⇒ Object
-
#initialize(name, formula, data = nil, options = {}) ⇒ Model
constructor
A new instance of Model.
- #predict(tsv, field = "Prediction") ⇒ Object
- #predict_interval(value, interval = 'confidence') ⇒ Object
- #r_options(tsv) ⇒ Object
- #update(tsv, field = "Prediction") ⇒ Object
Constructor Details
#initialize(name, formula, data = nil, options = {}) ⇒ Model
Returns a new instance of Model.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rbbt/util/R/model.rb', line 21 def initialize(name, formula, data = nil, = {}) @name = name @formula = formula @options = || {} @model_file = [:model_file] if [:model_file] @model_file ||= Misc.sanitize_filename(File.join([:model_dir], name)) if [:model_dir] if data and not model_file.exists? method = Misc. , :fit fit(data, method || "lm", ) end end |
Instance Attribute Details
#formula ⇒ Object
Returns the value of attribute formula.
20 21 22 |
# File 'lib/rbbt/util/R/model.rb', line 20 def formula @formula end |
#model_file ⇒ Object
Returns the value of attribute model_file.
20 21 22 |
# File 'lib/rbbt/util/R/model.rb', line 20 def model_file @model_file end |
#name ⇒ Object
Returns the value of attribute name.
20 21 22 |
# File 'lib/rbbt/util/R/model.rb', line 20 def name @name end |
Class Method Details
.groom(tsv, formula) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rbbt/util/R/model.rb', line 60 def self.groom(tsv, formula) tsv = tsv.to_list if tsv.type == :single if formula.include? tsv.key_field and not tsv.fields.include? tsv.key_field tsv = tsv.add_field tsv.key_field do |k,v| k end end tsv end |
Instance Method Details
#colClasses(tsv) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/rbbt/util/R/model.rb', line 34 def colClasses(tsv) return nil unless TSV === tsv "c('character', " << (tsv.fields.collect{|f| R.ruby2R(@options[f] ? @options[f].to_s : ":NA") } * ", ") << ")" end |
#exists? ⇒ Boolean
116 117 118 |
# File 'lib/rbbt/util/R/model.rb', line 116 def exists? File.exists? model_file end |
#fit(tsv, method = 'lm', args = {}) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/rbbt/util/R/model.rb', line 120 def fit(tsv, method='lm', args = {}) args_str = "" args_str = args.collect{|name,value| [name,R.ruby2R(value)] * "=" } * ", " args_str = ", " << args_str unless args_str.empty? tsv = Model.groom(tsv, formula) FileUtils.mkdir_p File.dirname(model_file) unless File.exists?(File.dirname(model_file)) = (tsv) tsv.R <<-EOF, model = rbbt.model.fit(data, #{formula}, method=#{method}#{args_str}) save(model, file='#{model_file}') data = NULL EOF end |
#predict(tsv, field = "Prediction") ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rbbt/util/R/model.rb', line 85 def predict(tsv, field = "Prediction") case tsv when TSV tsv = Model.groom tsv, formula tsv.R <<-EOF, (tsv) model = rbbt.model.load('#{model_file}'); data.groomed = rbbt.model.groom(data,formula=#{formula}) data$#{field} = predict(model, data.groomed); EOF when Hash res = R.eval_a <<-EOF model = rbbt.model.load('#{model_file}'); predict(model, data.frame(#{R.ruby2R tsv})); EOF Array === tsv.values.first ? res : res.first when Fixnum, Array, Float, String field = formula.split("~").last.strip field.gsub!(/log\((.*)\)/,'\1') script = <<-EOF model = rbbt.model.load('#{model_file}'); predict(model, data.frame(#{field} = #{R.ruby2R tsv})); EOF res = R.eval_a script Array === tsv ? res : res.first else raise "Unknown object for predict: #{Misc.fingerprint tsv}" end end |
#predict_interval(value, interval = 'confidence') ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/rbbt/util/R/model.rb', line 72 def predict_interval(value, interval='confidence') field = formula.split("~").last.strip field.gsub!(/log\((.*)\)/,'\1') script = <<-EOF model = rbbt.model.load('#{model_file}'); predict(model, data.frame(#{field} = #{R.ruby2R value}), interval=#{R.ruby2R interval}); EOF res = R.eval_a script Hash[*%w(fit lower upper).zip(res).flatten] end |
#r_options(tsv) ⇒ Object
41 42 43 44 45 |
# File 'lib/rbbt/util/R/model.rb', line 41 def (tsv) {:R_open => "colClasses=#{colClasses(tsv)}", :R_method => (@options[:R_method] || R_METHOD), :source => @options[:source]} end |
#update(tsv, field = "Prediction") ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/rbbt/util/R/model.rb', line 51 def update(tsv, field = "Prediction") tsv.R <<-EOF, (tsv) model = rbbt.model.load('#{model_file}'); model = update(model, data); save(model, file='#{model_file}'); data = NULL EOF end |