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.
-
#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, options = {}) ⇒ Model
constructor
A new instance of Model.
- #model_file ⇒ Object
- #predict(tsv, field = "Prediction") ⇒ Object
- #r_options(tsv) ⇒ Object
- #update(tsv, field = "Prediction") ⇒ Object
Constructor Details
#initialize(name, formula, options = {}) ⇒ Model
Returns a new instance of Model.
22 23 24 25 26 |
# File 'lib/rbbt/util/R/model.rb', line 22 def initialize(name, formula, = {}) @name = name @formula = formula @options = || {} end |
Instance Attribute Details
#formula ⇒ Object
Returns the value of attribute formula.
21 22 23 |
# File 'lib/rbbt/util/R/model.rb', line 21 def formula @formula end |
#name ⇒ Object
Returns the value of attribute name.
21 22 23 |
# File 'lib/rbbt/util/R/model.rb', line 21 def name @name end |
Class Method Details
.groom(tsv, formula) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rbbt/util/R/model.rb', line 53 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
28 29 30 31 32 |
# File 'lib/rbbt/util/R/model.rb', line 28 def colClasses(tsv) "c('character', " << (tsv.fields.collect{|f| R.ruby2R(@options[f] ? @options[f].to_s : ":NA") } * ", ") << ")" end |
#exists? ⇒ Boolean
74 75 76 |
# File 'lib/rbbt/util/R/model.rb', line 74 def exists? File.exists? model_file end |
#fit(tsv, method = 'lm', args = {}) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rbbt/util/R/model.rb', line 78 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.R <<-EOF, (tsv) model = rbbt.model.fit(data, #{formula}, method=#{method}#{args_str}) save(model, file='#{model_file}') data = NULL EOF end |
#model_file ⇒ Object
40 41 42 |
# File 'lib/rbbt/util/R/model.rb', line 40 def model_file @model_file ||= R.model_dir[Misc.name2basename([name, Misc.name2basename(formula)] * ": ")].find end |
#predict(tsv, field = "Prediction") ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/rbbt/util/R/model.rb', line 65 def predict(tsv, field = "Prediction") 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 end |
#r_options(tsv) ⇒ Object
34 35 36 37 38 |
# File 'lib/rbbt/util/R/model.rb', line 34 def (tsv) {:R_open => "colClasses=#{colClasses(tsv)}", :R_method => (@options[:R_method] || R_METHOD), :source => @options[:source]} end |
#update(tsv, field = "Prediction") ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/rbbt/util/R/model.rb', line 44 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 |