class R::Model
Constants
- R_METHOD
Attributes
Public Class Methods
Source
# File lib/rbbt/util/R/model.rb, line 68 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
Source
# File lib/rbbt/util/R/model.rb, line 34 def self.load(model_file) model = Model.new nil, nil, nil, :model_file => model_file formula = Open.read(model_file + '.formula') model.formula = formula model end
Source
# File lib/rbbt/util/R/model.rb, line 21 def initialize(name, formula, data = nil, options = {}) @name = name @formula = formula @options = options || {} @model_file = options[:model_file] if options[:model_file] @model_file ||= Misc.sanitize_filename(File.join(options[:model_dir], name)) if options[:model_dir] if data and not model_file.exists? method = Misc.process_options options, :fit fit(data, method || "lm", options) end end
Public Instance Methods
Source
# File lib/rbbt/util/R/model.rb, line 42 def colClasses(tsv) return nil unless TSV === tsv "c('character', " << (tsv.fields.collect{|f| R.ruby2R(@options[f] ? @options[f].to_s : "NA") } * ", ") << ")" end
Source
# File lib/rbbt/util/R/model.rb, line 128 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.exist?(File.dirname(model_file)) roptions = r_options(tsv) tsv.R <<-EOF, roptions model = rbbt.model.fit(data, #{formula}, method=#{method}#{args_str}) save(model, file='#{model_file}') data = NULL EOF Open.write(model_file + '.formula', formula) end
Source
# File lib/rbbt/util/R/model.rb, line 93 def predict(tsv, field = "Prediction") case tsv when TSV tsv = Model.groom tsv, formula tsv.R <<-EOF, r_options(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 Numeric, Array, 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
Source
# File lib/rbbt/util/R/model.rb, line 80 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}, level=0.90); EOF res = R.eval_a script Hash[*%w(fit lower upper).zip(res).flatten] end
Source
# File lib/rbbt/util/R/model.rb, line 49 def r_options(tsv) {:R_open => "colClasses=#{colClasses(tsv)}", :R_method => (@options[:R_method] || R_METHOD), :source => @options[:source]} end
Source
# File lib/rbbt/util/R/model.rb, line 59 def update(tsv, field = "Prediction") tsv.R <<-EOF, r_options(tsv) model = rbbt.model.load('#{model_file}'); model = update(model, data); save(model, file='#{model_file}'); data = NULL EOF end