I have written the code below to define a function for regression analysis, however, it does not work.
import numpy as np
import pandas as pd
def linear_regress(y, X):
X_trp = np.transpose(X)
b = np.linalg.inv(X_trp@X)@X_trp@y
e = y - X@b
e_trp = np.transpose(e)
sigma_sqr = (e_trp@e) / (X.shape[0] - X.shape[1])
var_b = sigma_sqr@(np.linalg.inv(X_trp@X))
SE = np.sqrt(var_b)
z_score = 1.96
upper = b+SE*z_score
lower = b-SE*z_score
CI = [lower, upper]
results = {"Regression Coefficients": b,
"Standard Error (SE)": SE,
"95% Confidence Interval": CI}
return results
It returns when I define array and run the function:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-17-8307755b9bbc> in <module>
----> 1 linear_regress(indt, dept)
<ipython-input-16-a6336ec1997c> in linear_regress(y, X)
8 e_trp = np.transpose(e)
9 sigma_sqr = (e_trp@e) / (X.shape[0] - X.shape[1])
---> 10 var_b = sigma_sqr@(np.linalg.inv(X_trp@X))
11 SE = np.sqrt(var_b)
12 z_score = 1.96
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 2 is different from 1)
Can you please help me to figure out what the problem is?
Thanks for your help!