InĀ [1]:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as scst
f_in_nm = input('Input data file name (*.csv) = ')
r_data = pd.read_csv(f_in_nm, header=0)
f_out_nm = input('Output file name (*.txt) = ')
f_out = open(f_out_nm, 'w')
f_out.write(f'Input data file = {f_in_nm}\n')
print('r_data =\n', r_data)
#print('r_data.values =\n', r_data.values)
f_out.write(f'\n{r_data}\n')
ID = r_data.values[:,0]
print('ID =\n', ID)
r_data = ID Q1-0 Q1-1 Q2-0 Q2-1 Q3-0 Q3-1 Q4-0 \ 0 39D 0.79057 1.27475 -1.48663 0.83062 1.22508 1.35371 1.69367 1 40D 0.79057 1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 2 41D 0.79057 1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 3 42D -1.26491 0.00000 -1.48663 0.83062 -0.87731 0.05524 -0.65002 4 43D -1.26491 0.00000 0.77081 0.17566 -0.87731 0.05524 -0.65002 5 44D 0.79057 -1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 6 45D 0.79057 1.27475 -1.48663 0.83062 -0.87731 0.05524 -0.65002 7 46D -1.26491 0.00000 0.77081 0.17566 -0.87731 0.05524 1.69367 8 47D 0.79057 1.27475 -1.48663 0.83062 1.22508 1.35371 1.69367 9 48D 0.79057 -1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 10 49D -1.26491 0.00000 0.77081 0.17566 1.03440 -1.67482 1.69367 11 50D -1.26491 0.00000 0.77081 0.17566 -0.87731 0.05524 -0.65002 12 51D -1.26491 0.00000 -0.72662 -2.50440 1.22508 1.35371 -0.65002 13 52D 0.79057 1.27475 -1.48663 0.83062 -0.87731 0.05524 -0.65002 14 53D 0.79057 1.27475 0.77081 0.17566 1.03440 -1.67482 -0.65002 15 54D -1.26491 0.00000 0.77081 0.17566 -0.87731 0.05524 -0.65002 16 55D 0.79057 1.27475 -1.48663 0.83062 1.03440 -1.67482 -0.65002 17 56D 0.79057 1.27475 -0.72662 -2.50440 1.03440 -1.67482 0.76920 18 57D 0.79057 -1.27475 -1.48663 0.83062 1.03440 -1.67482 -0.65002 19 58D -1.26491 0.00000 0.77081 0.17566 -0.87731 0.05524 -0.65002 20 59A -1.26491 0.00000 -1.48663 0.83062 -0.87731 0.05524 -0.65002 21 60A 0.79057 -1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 22 61A -1.26491 0.00000 0.77081 0.17566 1.03440 -1.67482 -0.65002 23 62A 0.79057 -1.27475 0.77081 0.17566 -0.87731 0.05524 0.76920 24 63A 0.79057 -1.27475 -0.72662 -2.50440 1.22508 1.35371 1.69367 25 64A 0.79057 1.27475 -1.48663 0.83062 1.03440 -1.67482 0.76920 26 65A 0.79057 -1.27475 0.77081 0.17566 1.22508 1.35371 1.69367 27 66A -1.26491 0.00000 0.77081 0.17566 1.22508 1.35371 1.69367 28 67A 0.79057 -1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 29 68A 0.79057 -1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 30 69A 0.79057 -1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 31 70A -1.26491 0.00000 0.77081 0.17566 -0.87731 0.05524 1.69367 32 71A -1.26491 0.00000 -1.48663 0.83062 1.03440 -1.67482 -0.65002 33 72A -1.26491 0.00000 -0.72662 -2.50440 1.22508 1.35371 1.69367 34 73A -1.26491 0.00000 0.77081 0.17566 1.22508 1.35371 -0.65002 35 74A 0.79057 -1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 36 75A 0.79057 1.27475 0.77081 0.17566 1.22508 1.35371 -0.65002 37 76A 0.79057 1.27475 -0.72662 -2.50440 -0.87731 0.05524 -0.65002 38 77A 0.79057 -1.27475 0.77081 0.17566 -0.87731 0.05524 -0.65002 Q4-1 Q5-0 Q5-1 Q6 Q7 0 0.68177 -0.58248 -0.07445 10 0 1 0.14803 -0.58248 -0.07445 40 0 2 0.14803 -0.58248 -0.07445 0 90 3 0.14803 -0.58248 -0.07445 60 80 4 0.14803 -0.58248 -0.07445 80 0 5 0.14803 -0.58248 -0.07445 80 80 6 0.14803 -0.58248 -0.07445 20 10 7 0.68177 -0.58248 -0.07445 20 0 8 0.68177 -0.58248 -0.07445 20 50 9 0.14803 -0.58248 -0.07445 60 45 10 0.68177 -0.58248 -0.07445 50 50 11 0.14803 1.89362 -1.38354 80 30 12 0.14803 1.89362 -1.38354 80 80 13 0.14803 -0.58248 -0.07445 80 75 14 0.14803 -0.58248 -0.07445 90 60 15 0.14803 1.38256 2.61506 50 50 16 0.14803 -0.58248 -0.07445 50 50 17 -3.37762 -0.58248 -0.07445 -20 0 18 0.14803 1.89362 -1.38354 70 50 19 0.14803 -0.58248 -0.07445 0 0 20 0.14803 -0.58248 -0.07445 80 80 21 0.14803 1.38256 2.61506 80 40 22 0.14803 -0.58248 -0.07445 70 50 23 -3.37762 -0.58248 -0.07445 90 50 24 0.68177 -0.58248 -0.07445 0 0 25 -3.37762 -0.58248 -0.07445 100 90 26 0.68177 -0.58248 -0.07445 90 80 27 0.68177 1.89362 -1.38354 -100 -100 28 0.14803 -0.58248 -0.07445 80 50 29 0.14803 1.89362 -1.38354 100 80 30 0.14803 1.38256 2.61506 65 25 31 0.68177 -0.58248 -0.07445 60 60 32 0.14803 -0.58248 -0.07445 50 50 33 0.68177 1.89362 -1.38354 -90 0 34 0.14803 -0.58248 -0.07445 50 70 35 0.14803 -0.58248 -0.07445 60 70 36 0.14803 -0.58248 -0.07445 80 100 37 0.14803 1.38256 2.61506 0 -10 38 0.14803 -0.58248 -0.07445 50 0 ID = ['39D' '40D' '41D' '42D' '43D' '44D' '45D' '46D' '47D' '48D' '49D' '50D' '51D' '52D' '53D' '54D' '55D' '56D' '57D' '58D' '59A' '60A' '61A' '62A' '63A' '64A' '65A' '66A' '67A' '68A' '69A' '70A' '71A' '72A' '73A' '74A' '75A' '76A' '77A']
InĀ [2]:
y_name = input('Name of variable y = ')
X_name = input('Name of variable X = ')
f_out.write(f'\nVariable y = {y_name}\n')
f_out.write(f'\nVariable x = {X_name}\n')
Out[2]:
19
$$y=\begin{pmatrix}
1 & x \\
\end{pmatrix}
\begin{pmatrix}
a \\
b
\end{pmatrix}
+E$$
$$y=X\beta+E$$
$$\hat{\beta}={(X^{T}X)}^{-1}X^{T}y$$
InĀ [3]:
y = np.array(r_data[y_name], dtype=float)
y = np.reshape(y, (len(y), 1))
print(f'y({y_name}) =\n', y)
X = np.array(r_data[X_name], dtype=float)
X = np.concatenate((np.ones((len(X), 1)), np.reshape(X, (len(X), 1))), axis=1)
print(f'X({X_name}) =\n', X)
b = np.linalg.inv(X.T @ X) @ X.T @ y
print('b = ', b)
#f_out.write(f'\nb =\n {b}\n')
#
# Calculation of the Correlation Coefficient
#
X1 = X.transpose()[1]
vy = []
for v in y:
vy.append(v[0])
y = vy
r, p = scst.pearsonr(X1, y)
print('r = ', r)
R2 = r ** 2
print('R2 = ', R2)
y(Q6) = [[ 10.] [ 40.] [ 0.] [ 60.] [ 80.] [ 80.] [ 20.] [ 20.] [ 20.] [ 60.] [ 50.] [ 80.] [ 80.] [ 80.] [ 90.] [ 50.] [ 50.] [ -20.] [ 70.] [ 0.] [ 80.] [ 80.] [ 70.] [ 90.] [ 0.] [ 100.] [ 90.] [-100.] [ 80.] [ 100.] [ 65.] [ 60.] [ 50.] [ -90.] [ 50.] [ 60.] [ 80.] [ 0.] [ 50.]] X(Q2-1) = [[ 1. 0.83062] [ 1. 0.17566] [ 1. 0.17566] [ 1. 0.83062] [ 1. 0.17566] [ 1. 0.17566] [ 1. 0.83062] [ 1. 0.17566] [ 1. 0.83062] [ 1. 0.17566] [ 1. 0.17566] [ 1. 0.17566] [ 1. -2.5044 ] [ 1. 0.83062] [ 1. 0.17566] [ 1. 0.17566] [ 1. 0.83062] [ 1. -2.5044 ] [ 1. 0.83062] [ 1. 0.17566] [ 1. 0.83062] [ 1. 0.17566] [ 1. 0.17566] [ 1. 0.17566] [ 1. -2.5044 ] [ 1. 0.83062] [ 1. 0.17566] [ 1. 0.17566] [ 1. 0.17566] [ 1. 0.17566] [ 1. 0.17566] [ 1. 0.17566] [ 1. 0.83062] [ 1. -2.5044 ] [ 1. 0.17566] [ 1. 0.17566] [ 1. 0.17566] [ 1. -2.5044 ] [ 1. 0.17566]] b = [[47.05126216] [19.39526779]] r = 0.42720650502314783 R2 = 0.18250539793409284
InĀ [4]:
#
# Drawing the regression line
#
min_x = np.amin(X1)
max_x = np.amax(X1)
y_left = b[0][0] + b[1][0] * min_x
y_right = b[0][0] + b[1][0] * max_x
plt.plot([min_x, max_x],[y_left, y_right], 'b-')
dis_x = (max_x - min_x) * 0.01
dis_y = (np.amax(y) - np.amin(y)) * 0.01
#
# Plotting the data points
#
plt.plot(X1, y, 'bo')
#
# Displaying labels and figures
#
for vx, vy, name in zip(X1, y, ID):
plt.text(vx + dis_x, vy + dis_y, name)
plt.xlabel(X_name)
plt.ylabel(y_name)
plt.title('Simple Linear Regression\n' +
'r = {0:.3f} $R^2$ = {1:.3f}'.format(r, R2))
plt.tight_layout()
plt.savefig('FigResults.png')
plt.show()
print('FigResults.png was saved.')
f_out.write('\nb0<Constant> = {0:.5}\n'.format(b[0][0]))
f_out.write('b1<{0}> = {1:.5}\n'.format(y_name, b[1][0]))
f_out.write('\nr = {0:.3} R^2 = {1:.3}\n'.format(r, R2))
f_out.close()
print('{0} was saved.'.format(f_out_nm))
FigResults.png was saved. temp.txt was saved.