Class: Statsample::Regression::GLM::Poisson

Inherits:
Object
  • Object
show all
Defined in:
lib/bio-statsample-glm/regression/poisson.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ds, y) ⇒ Poisson

Returns a new instance of Poisson.



19
20
21
22
23
24
# File 'lib/bio-statsample-glm/regression/poisson.rb', line 19

def initialize(ds, y)
  @ds=ds
  @fields=@ds.fields
  @x = ds.to_matrix
  @y = y
end

Instance Attribute Details

#convergedObject (readonly)

Boolean. Tells whether the IRWLS for the given model converged or not



17
18
19
# File 'lib/bio-statsample-glm/regression/poisson.rb', line 17

def converged
  @converged
end

#dfObject (readonly)

The residuals degree of freedom



13
14
15
# File 'lib/bio-statsample-glm/regression/poisson.rb', line 13

def df
  @df
end

#fitObject (readonly)

The fitted mean values



9
10
11
# File 'lib/bio-statsample-glm/regression/poisson.rb', line 9

def fit
  @fit
end

#iterObject (readonly)

Number of iterations used for convergence



15
16
17
# File 'lib/bio-statsample-glm/regression/poisson.rb', line 15

def iter
  @iter
end

#residualsObject (readonly)

the working residuals; that is the residuals in the final iteration of the IRWLS fit.



11
12
13
# File 'lib/bio-statsample-glm/regression/poisson.rb', line 11

def residuals
  @residuals
end

#seObject (readonly)

Returns the value of attribute se.



7
8
9
# File 'lib/bio-statsample-glm/regression/poisson.rb', line 7

def se
  @se
end

Class Method Details

.h(x, b, y) ⇒ Object



60
61
62
63
64
65
# File 'lib/bio-statsample-glm/regression/poisson.rb', line 60

def self.h(x, b, y)
  x_t = x.transpose
  mu_flat = mu(x,b).column_vectors.map(&:to_a).flatten
  column_data = y.zip(mu_flat).collect { |x| x.inject(:-) }
  x_t * Matrix.columns([column_data])
end

.j(x, b) ⇒ Object



67
68
69
70
71
# File 'lib/bio-statsample-glm/regression/poisson.rb', line 67

def self.j(x, b)
  w_matrix = w(x, b)
  jacobian_matrix = x.transpose * w_matrix * x
  jacobian_matrix.map { |x| -x }
end

.mu(x, b, link = :log) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/bio-statsample-glm/regression/poisson.rb', line 41

def self.mu(x, b, link=:log)
  if link.downcase.to_sym == :log
    (x * b).map { |y| Math.exp(y) }
  elsif link.downcase.to_sym == :sqrt
    (x * b).collect { |y| y**2 }
  end
end

.w(x, b) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/bio-statsample-glm/regression/poisson.rb', line 49

def self.w(x, b)
  poisson_mu = mu(x,b)
  mu_flat = poisson_mu.column_vectors.map(&:to_a).flatten

  w_mat = Matrix.I(mu_flat.size)
  mu_enum = mu_flat.to_enum
  return w_mat.map do |x|
    x.eql?(1) ? mu_enum.next : x
  end
end

Instance Method Details

#coefficients(type = :array) ⇒ Object

named vector/hash of coefficients

Parameter

  • type: symbol; (:array, default). Options = [:array, :hash]



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bio-statsample-glm/regression/poisson.rb', line 29

def coefficients(type=:array)
  if type==:array
    @coefficients
  elsif type==:hash
    h={}
    @fields.size.times {|i|
      h[@fields[i]]=@coefficients[i]
    }
    h
  end
end

#irwlsObject



77
78
79
80
81
82
83
84
85
# File 'lib/bio-statsample-glm/regression/poisson.rb', line 77

def irwls
  x,y = @x,@y
  #calling irwls on Regression and passing equivalent methods in lambdas.
  #Ruby_level+=awesome!
  @coefficients, @se, @fit, @residuals, @df, @iter, @converged = Statsample::Regression.irwls(
      x,y, ->l,m{self.class.mu(l,m)}, ->l,m{self.class.w(l,m)},
      ->l,m{self.class.j(l,m)}, ->k,l,m{self.class.h(k,l,m)}
  )
end

#to_sObject



73
74
75
# File 'lib/bio-statsample-glm/regression/poisson.rb', line 73

def to_s
  sprintf("Logistic Regression (Statsample::Regression::GLM;:Logistic)")
end