In [1]:
import numpy
import pandas as pd
import matplotlib.pyplot as plt
s = input("Input Excel data file (*.xlsx) = ")
xlsx_fl = pd.ExcelFile(s)
data_xlsx = pd.read_excel(xlsx_fl, header = None)
data = data_xlsx.values
s_out = input("Output text data file (*.txt) = ")
fout = open(s_out, "w")
fout.write("Input data file = " + s + "\n")
Out[1]:
33
In [2]:
data = data.T
n_vars = 0
var_names = []
X = []
for v in data:
if v[1] == 1:
var_names.append(v[0])
X.append(list(v[2:]))
n_vars += 1
n_data = len(X[0])
print('n_data =', n_data)
print('n_vars =', n_vars)
print('var_names = :', var_names)
fout.write('\nn_data ={}\n'.format(n_data))
fout.write('n_vars = {}\n'.format(n_vars))
fout.write('var_names: {}\n'.format(var_names))
print()
for i, nm in enumerate(var_names):
print(f'{nm:<20s} ==> X[{i+1}]')
fout.write('\n\n')
for i, nm in enumerate(var_names):
fout.write(f'{nm:<20s} ==> X[{i+1}]\n')
fout.write('\n')
n_data = 50 n_vars = 8 var_names = : ['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5', 'Item_7', 'Item_8', 'Item_10'] Item_1 ==> X[1] Item_2 ==> X[2] Item_3 ==> X[3] Item_4 ==> X[4] Item_5 ==> X[5] Item_7 ==> X[6] Item_8 ==> X[7] Item_10 ==> X[8]
Out[2]:
1
In [3]:
X = numpy.array(X).T
RawScore = numpy.sum(X, axis=1)
meanX = numpy.mean(X, axis=0)
X = X - meanX # Centering of X
U, Sv, Vh = numpy.linalg.svd(X, full_matrices=False)
In [4]:
plt.plot(numpy.arange(1, len(Sv)+1), Sv)
plt.title('Scree Plot', fontsize= 16)
plt.ylabel('Singular value', fontsize=14)
plt.tight_layout()
fnm_scree = s_out[:-4] + '_Scree.png'
plt.savefig(fnm_scree)
plt.show()
In [5]:
if numpy.sum(Vh[0]) < 0.0:
U *= -1.0
Vh *= -1.0
S = numpy.cov(X, rowvar=False)
A = Vh[0] * Sv[0] / (n_data**0.5) # Vector of Coefficient
for i in range(n_vars):
print('Lambda[{0}]**2 / Var(X[{0}]) = {1:.3f}'.format(
i+1, A[i]**2 / S[i][i]))
fout.write('Lambda[{0}]**2 / Var(X[{0}]) = {1:.3f}\n'.format(
i+1, A[i]**2 / S[i][i]))
print()
fout.write('\n')
for i in range(n_vars):
print('{0}: Lambda[{1}] = {2:.3f}'.format(
var_names[i], i+1, A[i]))
fout.write('{0}: Lambda[{1}] = {2:.3f}\n'.format(
var_names[i], i+1, A[i]))
#
# Calculation of GFI (Goodness of Fit Index)
#
sum_num = 0.0
sum_den = 0.0
for i in range(n_vars):
for j in range(n_vars):
if i == j:
sum_den += S[i][i] ** 2
else:
sum_den += S[i][j] ** 2
sum_num += (S[i][j] - A[i] * A[j]) ** 2
GFI = 1.0 - sum_num / sum_den
print('\nGFI = {0:.3f}'.format(GFI))
fout.write('\nGFI = {0:.3f}\n'.format(GFI))
#
# Calculation of omega
#
sx2 = numpy.var(X.sum(axis=1))
omega = (A.sum() ** 2) / sx2
print('\nρE= {0:.3f}'.format(omega))
fout.write('\nρE= {0:.3f}\n'.format(omega))
Lambda[1]**2 / Var(X[1]) = 0.648 Lambda[2]**2 / Var(X[2]) = 0.690 Lambda[3]**2 / Var(X[3]) = 0.595 Lambda[4]**2 / Var(X[4]) = 0.747 Lambda[5]**2 / Var(X[5]) = 0.682 Lambda[6]**2 / Var(X[6]) = 0.665 Lambda[7]**2 / Var(X[7]) = 0.766 Lambda[8]**2 / Var(X[8]) = 0.746 Item_1: Lambda[1] = 13.280 Item_2: Lambda[2] = 12.717 Item_3: Lambda[3] = 13.244 Item_4: Lambda[4] = 16.572 Item_5: Lambda[5] = 13.578 Item_7: Lambda[6] = 12.422 Item_8: Lambda[7] = 15.906 Item_10: Lambda[8] = 16.154 GFI = 0.997 ρE= 0.999
Out[5]:
11
In [6]:
f = U[:,0] * (n_data**0.5)
f = numpy.reshape(f, (n_data,1))
A = numpy.reshape(A, (1, len(A)))
ModelScore = numpy.sum(f @ A, axis=1) + numpy.sum(meanX)
plt.scatter(ModelScore, RawScore)
plt.xlabel('Model Score', fontsize=14)
plt.ylabel('Raw Score', fontsize=14)
plt.plot([numpy.min(ModelScore), numpy.max(ModelScore)],[numpy.min(ModelScore), numpy.max(ModelScore)],
label = '$Y=X$', color='k')
plt.title(f'$\omega={omega:.3f}$', fontsize=16)
plt.legend(fontsize=14)
plt.tight_layout()
fnm_scatter = s_out[:-4] + 'Scatter.png'
plt.savefig(fnm_scatter)
plt.show()
fout.close()
print()
print(fnm_scree + ' was saved.')
print(fnm_scatter + ' was saved.')
print(s_out + " was saved.\n")
Results_Scree.png was saved. ResultsScatter.png was saved. Results.txt was saved.
In [ ]: