點選檢視程式碼
import numpy as np
import statsmodels.api as sm
import pylab as plt
def check(d):
x0 = d[0]; y0 = d[1]; d ={'x':x0, 'y':y0}
re = sm.formula.ols('y~x', d).fit() #擬合線性迴歸模型
print(re.summary())
print(re.outlier_test()) #輸出已知資料的野值檢驗
print('殘差的方差', re.mse_resid)
pre=re.get_prediction(d)
df = pre.summary_frame(alpha=0.05)
dfv = df.values; low, upp = dfv[:,4:].T #置信下限上限
r = (upp-low)/2 #置信半徑
num = np.arange(1, len(x0)+1)
plt.errorbar(num, re.resid, r, fmt='o')
plt.show()
a = np.loadtxt('F:\python數學建模與演算法\源程式\《Python數學建模演算法與應用》程式和資料/10第10章 迴歸分析/data10_1.txt')
plt.rc('font', size=15); plt.plot(a[0], a[1], 'o')
plt.figure(); check(a)
a2 = a; a2 = np.delete(a2, 8, axis=1) #刪除第9列
check(a2); a3 = a2
a3 = np.delete(a3, 4, axis=1); check(a3)