def __init__(self, regressand_vector_matrix, regressor_matrix): self.regressand_vector_matrix = regressand_vector_matrix self.regressor_matrix = regressor_matrix f = linregression.add_to_matrix_for_intercept # Add intercept for improved performance of the model self.regressor_matrix_for_intecept = f(regressor_matrix) self.coeficents = linregression.coeficents_estimation( regressand_vector_matrix, regressor_matrix) self.mean = statchar.aritmetic_mean(regressand_vector_matrix.matrix[0]) self.SStot = statchar.SS_with_mean(regressand_vector_matrix.matrix[0], self.mean) fi = [] for element in regressor_matrix.matrix: fi.append(LinearRegression.predict(self, matrices.Matrix([element]))) self.SSreg = statchar.SS_with_mean(fi, self.mean) self.SSres = statchar.SS_abstract(fi, regressand_vector_matrix.matrix[0]) self.R_squared = self.SSreg/self.SStot n = len(regressand_vector_matrix.matrix[0]) p = len(regressor_matrix.matrix[0]) A = n - 1 B = n - p - 1 self.adjusted_R_squared = 1-(1-self.R_squared)*(A/B)
def test_results(self): matrix_1 = matrices.Matrix([[1], [2], [3], [4], [5]]) matrix_2 = matrices.Matrix([[1, 2, 1.3, 3.75, 2.25]]) result = linregression.coeficents_estimation(matrix_2, matrix_1) self.assertTrue(0.78 < result[0][0] and result[0][0] < 0.79) self.assertTrue(0.42 < result[0][1] and result[0][1] < 0.43)