简体   繁体   中英

How to get errors for coefficients from a linear regression?

I've been able to calculate the coefficients of a linear regression. But is there a way to get the associated errors of the coefficients? My code shown below.

from scipy.interpolate import *
from numpy import *
x = np.array([4, 12, 56, 58.6,67, 89])
y = np.array([5, 6, 7, 16,18, 19])

degrees = [0,1]       # list of degrees of x to use
matrix = np.stack([x**d for d in degrees], axis=-1)    
coeff = np.linalg.lstsq(matrix, y)[0]     
print("Coefficients", coeff)
fit = np.dot(matrix, coeff)
print("Linear regression", fit)
p1=polyfit(x,y,1)

Output:

Coefficients for y=a +bx [3.70720668 0.17012128]
Linear fit [ 4.38769182  5.74866209 13.23399857 13.67631391 15.10533269 18.84800093]

Errors are not shown? How to calculate the errors?

You can generate the "predicted" values for y, let's call it y_pred, and compare them to y to get the errors.

predicted_line = poly1d(coeff)
y_pred = predicted_line(x)
errors = y-y_pred

Althorugh I like the solution of David Moseler, if you want an error to evaluate the goodness of your regression, you could use the R2 score (which use the squared error), already implemented in sklearn :

from sklearn.linear_model import LinearRegression
import numpy as np

x = np.array([4, 12, 56, 58.6,67, 89]).reshape(-1, 1)
y = np.array([5, 6, 7, 16,18, 19])

reg = LinearRegression().fit(x, y)
reg.score(x, y) # R2 score
# 0.7481301984276703

If the R2 value is near 1, the model is a good one

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM