Class: Statsample::GLM::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/statsample-glm/glm/base.rb

Direct Known Subclasses

Logistic, Normal, Poisson, Probit

Instance Method Summary collapse

Constructor Details

#initialize(ds, y, opts = {}) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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

Instance Method Details

#coefficients(as_a = :array) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# 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_freedomObject



67
68
69
# File 'lib/statsample-glm/glm/base.rb', line 67

def degree_of_freedom
  @regression.degree_of_freedom
end

#fitted_mean_valuesObject



59
60
61
# File 'lib/statsample-glm/glm/base.rb', line 59

def fitted_mean_values
  @regression.fitted_mean_values
end

#iterationsObject



55
56
57
# File 'lib/statsample-glm/glm/base.rb', line 55

def iterations
  @regression.iterations
end

#log_likelihoodObject



71
72
73
# File 'lib/statsample-glm/glm/base.rb', line 71

def log_likelihood
  @regression.log_likelihood if @opts[:algorithm] == :mle
end

#residualsObject



63
64
65
# File 'lib/statsample-glm/glm/base.rb', line 63

def residuals
  @regression.residuals
end

#standard_error(as_a = :array) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# 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