0

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!

1 Answer 1

1

It is difficult to tell what you are doing wrong without some proper idea of your data. But I am guessing you are trying to implement the Normal Equation to get weight. If that is the case, this seems way too complicated for that, here is a simpler approch.

def LinearRegression(X, y):
    X_t = X.T
    theta = np.dot(np.linalg.inv(np.dot(X.T, X)), np.dot(y,X))
    mean_sqr_error = np.mean((y - np.dot(theta,X.T))**2)
    return theta, mean_sqr_error
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help. I'll try what you have written, and I guess I need to do some readings on econometrics with matrix algebra. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.