lib/eps/linear_regression.rb in eps-0.2.0 vs lib/eps/linear_regression.rb in eps-0.2.1
- old
+ new
@@ -221,30 +221,29 @@
arr.inject(0, &:+) / arr.size.to_f
end
# https://people.richland.edu/james/ictcm/2004/multiple.html
def summary(extended: false)
- @summary_str ||= begin
- str = String.new("")
- len = [coefficients.keys.map(&:size).max, 15].max
+ coefficients = @coefficients
+ str = String.new("")
+ len = [coefficients.keys.map(&:size).max, 15].max
+ if extended
+ str += "%-#{len}s %12s %12s %12s %12s\n" % ["", "coef", "stderr", "t", "p"]
+ else
+ str += "%-#{len}s %12s %12s\n" % ["", "coef", "p"]
+ end
+ coefficients.each do |k, v|
if extended
- str += "%-#{len}s %12s %12s %12s %12s\n" % ["", "coef", "stderr", "t", "p"]
+ str += "%-#{len}s %12.2f %12.2f %12.2f %12.3f\n" % [display_field(k), v, std_err[k], t_value[k], p_value[k]]
else
- str += "%-#{len}s %12s %12s\n" % ["", "coef", "p"]
+ str += "%-#{len}s %12.2f %12.3f\n" % [display_field(k), v, p_value[k]]
end
- coefficients.each do |k, v|
- if extended
- str += "%-#{len}s %12.2f %12.2f %12.2f %12.3f\n" % [display_field(k), v, std_err[k], t_value[k], p_value[k]]
- else
- str += "%-#{len}s %12.2f %12.3f\n" % [display_field(k), v, p_value[k]]
- end
- end
- str += "\n"
- str += "r2: %.3f\n" % [r2] if extended
- str += "adjusted r2: %.3f\n" % [adjusted_r2]
- str
end
+ str += "\n"
+ str += "r2: %.3f\n" % [r2] if extended
+ str += "adjusted r2: %.3f\n" % [adjusted_r2]
+ str
end
def r2
@r2 ||= (sst - sse) / sst
end
@@ -278,16 +277,16 @@
end
# add epsilon for perfect fits
# consistent with GSL
def t_value
- @t_value ||= Hash[coefficients.map { |k, v| [k, v / (std_err[k] + Float::EPSILON)] }]
+ @t_value ||= Hash[@coefficients.map { |k, v| [k, v / (std_err[k] + Float::EPSILON)] }]
end
def p_value
@p_value ||= begin
- Hash[coefficients.map do |k, _|
+ Hash[@coefficients.map do |k, _|
tp =
if @gsl
GSL::Cdf.tdist_P(t_value[k].abs, degrees_of_freedom)
else
tdist_p(t_value[k].abs, degrees_of_freedom)
@@ -348,10 +347,10 @@
def mse
@mse ||= sse / degrees_of_freedom
end
def degrees_of_freedom
- @y.size - coefficients.size
+ @y.size - @coefficients.size
end
def mean(arr)
arr.sum / arr.size.to_f
end