Trying to work on an Statistical test and below is the code with error.
if (data[V1].dtypes == 'float64') or (data[V1].dtypes == 'int64'):
if (data[V2].dtypes == 'float64') or (data[V2].dtypes == 'int64'):
corre=data[V1].corr(data[V2])
print ('Correlation between', V1, 'and',V2,'is',round(corre,2))
else:
if (data[V2].dtypes == 'object'):
#issue with V2, V1
t = V1+' ~ '+V2
model = ols(t , data=data).fit()
anovres = sm.stats.anova_lm(model, typ=2)
print(anovres)
else:
print('invalid type')
else:
if (data[V1].dtypes == 'object'):
if (data[V2].dtypes == 'float64') or (data[V2].dtypes == 'int64'):
#issue with V2, V1
t = V1+' ~ '+V2
model = ols(t , data=data).fit()
anovres = sm.stats.anova_lm(model, typ=2)
print(anovres)
else:
if (data[V2].dtypes == 'object'):
data_table=pd.crosstab(data[V1],data[V2])
Observed_Values = data_table.values
val=stats.chi2_contingency(data_table)
Expected_Values=val[3]
no_of_rows=len(data_table.iloc[0:4,0])
no_of_columns=len(data_table.iloc[0,0:2])
ddof=(no_of_rows-1)*(no_of_columns-1)
alpha=0.05
from scipy.stats import chi2
chi_square=sum([(o-e)**2./e for o,e in zip(Observed_Values,Expected_Values)])
chi_square_statistic=chi_square[0]+chi_square[1]
p_value=1-chi2.cdf(x=chi_square_statistic,df=ddof)
print('p-value:',p_value)
print('significance level:',alpha)
print('degree of freedom:',ddof)
if p_value<=alpha:
print ('reject H0,There is a relationship between',V1,'and',V2)
else:
print ('reject H0,There is no relationship between', V1, 'and',V2)
else:
print('invalid type')
else:
print('invalid type')
The Above code is having error in line 8 if I replace it with another data. I am getting the right output Error in One data