In [1]:
import numpy
import pandas as pd
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
In [2]:
s_out = input("Output text data file (*.txt) = ")
fout = open(s_out, "w")
fout.write("Input data file = " + s + "\n")
Out[2]:
33
In [3]:
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))
X = numpy.array(X)
sx2 = numpy.cov(X.sum(axis = 0))
S = numpy.cov(X)
n_data = 50 n_vars = 8 var_names = : ['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5', 'Item_7', 'Item_8', 'Item_10']
In [4]:
gc_alpha = (1.0 - (numpy.diagonal(S).sum() / sx2)) * n_vars / (n_vars - 1)
print('\nα = {0:.3f}'.format(gc_alpha))
fout.write('\nα = {0:.3f}\n'.format(gc_alpha))
α = 0.940
Out[4]:
11
In [5]:
print('\nVar_names...')
for i, vnm in enumerate(var_names):
print(f'{vnm} ==> X[{i+1}]')
fout.write('\n\nVar_names...\n')
for i, vnm in enumerate(var_names):
fout.write(f'{vnm} ==> X[{i+1}]\n')
psi2 = numpy.zeros(n_vars)
A = numpy.zeros(n_vars)
while True:
A0 = A.copy()
C = S.copy()
for i in range(n_vars):
C[i][i] -= psi2[i]
w, vctr = numpy.linalg.eigh(C)
A = vctr[:, -1] * (w[-1] ** 0.5)
for i in range(n_vars):
psi2[i] = S[i][i] - (A[i] ** 2)
diff = ((A0 - A) ** 2).sum()
if diff < 1.0e-7:
break
print()
fout.write('\n')
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]))
Var_names... 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] Lambda[1]**2 / Var(X[1]) = 0.618 Lambda[2]**2 / Var(X[2]) = 0.683 Lambda[3]**2 / Var(X[3]) = 0.546 Lambda[4]**2 / Var(X[4]) = 0.709 Lambda[5]**2 / Var(X[5]) = 0.659 Lambda[6]**2 / Var(X[6]) = 0.653 Lambda[7]**2 / Var(X[7]) = 0.749 Lambda[8]**2 / Var(X[8]) = 0.715 Item_1: Lambda[1] = 12.968 Item_2: Lambda[2] = 12.648 Item_3: Lambda[3] = 12.685 Item_4: Lambda[4] = 16.145 Item_5: Lambda[5] = 13.350 Item_7: Lambda[6] = 12.306 Item_8: Lambda[7] = 15.736 Item_10: Lambda[8] = 15.817
In [6]:
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))
GFI = 0.999
Out[6]:
13
In [7]:
omega = (A.sum() ** 2) / sx2
print('\nρE= {0:.3f}'.format(omega))
fout.write('\nρE= {0:.3f}\n'.format(omega))
fout.close()
print()
print(s_out + " was saved.\n")
ρE= 0.942 temp69.txt was saved.
Check the website
http://y-okamoto-psy1949.la.coocan.jp/Python/misc/EstOmega/
In [ ]: