Module: Darkext::Statistics::Regression
- Defined in:
- lib/darkext/statistics.rb
Overview
def self.p_val(r, n = 30, rho = 1, mu = r.mean)
probs = r.map do |x|
(x - mu) / (rho / n.sqrt)
end.map do |x|
Statistics::prob(x)
end
return 1 - (probs[1] - probs[0])
end
Class Method Summary collapse
-
.least_squares(xs, ys) ⇒ Object
Do a least squares linear regression on the two sets of x’s and y’s Returns a hash containing many relevant values * n (:n) * B_1 (:b_1) * B_0 (:b_0) * predicted values (:predicted) * residuals (:residuals) * SSE (:ss_e) * SST (:ss_t) * R^2 (:r_2) * R (:r) * unbiased estimator (:estimator) * the equation as a lambda (:equation) Raises an argument error if the arguments are not the same size or either is zero.
Class Method Details
.least_squares(xs, ys) ⇒ Object
Do a least squares linear regression on the two sets of x’s and y’s Returns a hash containing many relevant values
-
n (:n)
-
B_1 (:b_1)
-
B_0 (:b_0)
-
predicted values (:predicted)
-
residuals (:residuals)
-
SSE (:ss_e)
-
SST (:ss_t)
-
R^2 (:r_2)
-
R (:r)
-
unbiased estimator (:estimator)
-
the equation as a lambda (:equation)
Raises an argument error if the arguments are not the same size or either is zero
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/darkext/statistics.rb', line 166 def self.least_squares(xs,ys) raise ArgumentError.new('Arrays must have size > 0') if xs.size.zero? || ys.size.zero? raise ArgumentError.new('Arrays must be of equal size') if xs.size != ys.size n = xs.size b_1 = (xs.zip(ys).map(&:product).sum - ((ys.sum * xs.sum)/n))/(xs.map(&:square).sum - (xs.sum.square/n)) b_0 = ys.mean - b_1 * xs.mean equation = lambda { |x| b_0 + b_1 * x } predicted = xs.map(&equation) residuals = ys.zip(predicted).map { |y| y.shift - y.shift } ss_e = residuals.map(&:square).sum ss_t = ys.sum_of_squares estimator = ss_e/(n - 2) r_2 = 1 - (ss_e/ss_t) r = r_2.sqrt reg = { :n => n, :b_1 => b_1, :b_0 => b_0, :predicted => predicted, :residuals => residuals, :ss_e => ss_e, :ss_t => ss_t, :estimator => estimator, :equation => equation, :r_2 => r_2, :r => r } return reg end |