Created
January 27, 2016 13:48
-
-
Save k5trismegistus/afbc61433ac80c3f843e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def regression_analysis(datas): | |
# x, yの平均値を求める | |
x_ave = sum([d[0] for d in datas]) / len(datas) | |
y_ave = sum([d[1] for d in datas]) / len(datas) | |
# x, yの分散、xyの共分散を求める | |
c_xy = sum([(d[0] - x_ave) * (d[1] - y_ave) for d in datas]) / len(datas) | |
d_x = sum([(d[0] - x_ave) ** 2 for d in datas]) / len(datas) | |
d_y = sum([(d[1] - y_ave) ** 2 for d in datas]) / len(datas) | |
# 回帰方程式のパラメータa, bを求める | |
a = (c_xy / d_x) | |
b = (y_ave - (c_xy / d_x) * x_ave) | |
print('y = {a}x + {b}'.format(a=a, b=b)) | |
# yの予測値hat_yの分散を求めたら決定係数を求める | |
hat_y = [a * d[0] + b for d in datas] | |
d_hat_y = sum([(hy - y_ave) ** 2 for hy in hat_y]) / len(datas) | |
r2 = d_hat_y / d_y | |
print('R^2 = {r2}'.format(r2=r2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment