class Statsample::GLM::Base

Public Class Methods

new(ds, y, opts={}) click to toggle source
# File lib/statsample-glm/glm/base.rb, line 11
def initialize ds, y, opts={}
  @opts   = opts
    
  set_default_opts_if_any

  @data_set  = ds.dup(ds.vectors.to_a - [y])
  @dependent = ds[y]

  add_constant_vector if @opts[:constant]
  add_constant_vector(1) if self.is_a? Statsample::GLM::Normal

  algorithm = @opts[:algorithm].upcase
  method    = @opts[:method].capitalize

  # TODO: Remove this const_get jugaad after 1.9.3 support is removed.

  @regression = Kernel.const_get("Statsample").const_get("GLM")
                      .const_get("#{algorithm}").const_get("#{method}")
                      .new(@data_set, @dependent, @opts)
end

Public Instance Methods

coefficients(as_a=:array) click to toggle source
# File lib/statsample-glm/glm/base.rb, line 32
def coefficients as_a=:array
  if as_a == :hash
    c = {}
    @data_set.vectors.to_a.each_with_index do |f,i|
      c[f.to_sym] = @regression.coefficients[i]
    end
    return c
  end
  create_vector @regression.coefficients
end
degree_of_freedom() click to toggle source
# File lib/statsample-glm/glm/base.rb, line 67
def degree_of_freedom
  @regression.degree_of_freedom
end
fitted_mean_values() click to toggle source
# File lib/statsample-glm/glm/base.rb, line 59
def fitted_mean_values
  @regression.fitted_mean_values
end
iterations() click to toggle source
# File lib/statsample-glm/glm/base.rb, line 55
def iterations
  @regression.iterations
end
log_likelihood() click to toggle source
# File lib/statsample-glm/glm/base.rb, line 71
def log_likelihood
  @regression.log_likelihood if @opts[:algorithm] == :mle
end
residuals() click to toggle source
# File lib/statsample-glm/glm/base.rb, line 63
def residuals
  @regression.residuals
end
standard_error(as_a=:array) click to toggle source
# File lib/statsample-glm/glm/base.rb, line 43
def standard_error as_a=:array  
  if as_a == :hash
    se = {}
    @data_set.vectors.to_a.each_with_index do |f,i|
      se[f.to_sym] = @regression.standard_error[i]
    end
    return se
  end

  create_vector @regression.standard_error
end

Private Instance Methods

add_constant_vector(x=nil) click to toggle source
# File lib/statsample-glm/glm/base.rb, line 88
def add_constant_vector x=nil
  @data_set.add_vector :constant, [@opts[:constant]]*@data_set.nrows

  unless x.nil?
    @data_set.add_vector :constant, [1]*@data_set.nrows
  end
end
create_vector(arr) click to toggle source
# File lib/statsample-glm/glm/base.rb, line 84
def create_vector arr
  Daru::Vector.new(arr)
end
set_default_opts_if_any() click to toggle source
# File lib/statsample-glm/glm/base.rb, line 77
def set_default_opts_if_any
  @opts[:algorithm]  ||= :irls 
  @opts[:iterations] ||= 100   
  @opts[:epsilon]    ||= 1e-7  
  @opts[:link]       ||= :log  
end