# -*- coding: utf-8 -*-
"""
Created on Mon Nov 29 12:32:08 2021

@author: Sim
"""
import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt

iq = np.array([100 ,123 ,132,  98 ,105 ,110 ,117 , 95, 127 ,135])
vt = np.array([89 , 93 , 97,  80 , 83 , 88 , 90 , 70, 93 , 97])

#------------ statsmodels OLS ---------------------
vt1 = sm.add_constant(vt)
lm2 = sm.OLS(iq, vt1).fit()
print(lm2.summary())
resid2 = lm2.resid
yhat21 = lm2.predict()
lm2.fittedvalues
lm2.fvalue
x = np.array([np.min(vt), np.max(vt)])
x1 = sm.add_constant(x)
yhat22 = lm2.predict(x1)

plt.plot(vt, iq, '*')
plt.plot(x, yhat22, 'r-')
#또는 
#plt.axline((x[0],yhat22[0]),(x[1], yhat22[1]), color='r')
angle = np.arctan(1.5365) *180/np.pi 
angle = angle * \
        (np.max(x)-np.min(x))/(np.max(yhat22)-np.min(yhat22)*6/8)
angle = angle*8/6
plt.text(90, 110, 'y = -21.01 + 1.54 x', 
         rotation=angle)

