-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat.py
268 lines (202 loc) · 7.68 KB
/
stat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
'''
Biological hypothesis testing
Input : csv filles and via dataframe
Output : relevant plots, normality tests and other statistic tests results
Author: Hansi Thewarapperuma
Date: 31/01/2023
'''
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
from scipy import stats
from statsmodels.graphics.gofplots import qqplot
temperature = pd.read_csv('Temperature.csv')
# print(temperature)
print(temperature.describe())
# Testing the normality assumption for the temperature variable
# plt.hist(temperature)
# plt.show()
# plt.savefig('temperature histogram')
# sns.histplot(temperature, kde= True)
# plt.show()
# plt.savefig('temperature histogram using seaborn.jpg')
# plt.title('Histogram of temperature')
# from statsmodels.graphics.gofplots import qqplot
# qqplot(temperature, line= 's')
# plt.show()
# plt.savefig('QQplot for temperature.jpg')
# plt.title('QQplot for temperature')
# shapiro test
# from scipy import stats
stat,p = stats.shapiro(temperature)
print('shapiro test results: ','stat=%.3f p-value=%.3f'%(stat,p))
# one sample t test
known_temperature = 98.6
t,p = stats.ttest_1samp(temperature, known_temperature)
print('Q1 - one sample t test results: ')
print('t stat: ', t)
print('p value: ',p)
# Question 2
horn_length = pd.read_csv('HornedLizards.csv')
# print(horn_length)
# get rid of NA values in csv file
new_data = horn_length.dropna(axis = 0, how ='any')
# print(len(horn_length))
# print(len(new_data))
print(horn_length.describe())
# ******filter pandas data values by column value using df.loc*****
# extract horn lengths of survived
# step1- extract both values and annotation columns from mixed csv and assign it to 'survived'
survived = new_data.loc[new_data['Survive']=='survived']
# print(survived)
# step2 - extract the values from 'survived'
survived_val = survived.loc[:,"Squamosal horn length"]
# print(survived_val)
# print(len(survived_val))
# check = survived['Squamosal horn length']
# print(len(check))
# extract horn lengths of dead
# step1- extract both values and annotation columns from mixed csv and assign it to 'dead'
dead = new_data.loc[new_data['Survive']=='dead']
# step2 - extract the values from 'dead'
dead_val = dead.loc[:,"Squamosal horn length"]
# print(dead_values)
# histogram for the survived sample
# fig, axs = plt.subplots(1,2,figsize=(12,4))
# # ******mistake- replace survived by x
# sns.histplot(survived_val, kde= True, ax=axs[0])
# axs[0].set_title('Histogram of survived lizards')
# histogram for the dead sample
# sns.histplot(dead_val, kde=True, ax=axs[1])
# axs[1].set_title('Histogram of dead lizards')
# plt.show()
# plt.savefig('Histogram of 2 samples.jpg')
# QQplot for survived sample
# from statsmodels.graphics.gofplots import qqplot
# qqplot(survived_val, line= 's')
# plt.show()
# plt.title('QQplot for survived sample')
# plt.savefig('QQplot for survived sample.jpg')
# QQplot for dead sample
# qqplot(dead_val, line= 's')
# plt.show()
# plt.title('QQplot for dead sample')
# plt.savefig('QQplot for dead sample.jpg')
# shapiro tests for both groups
# from scipy import stats
stat,p = stats.shapiro(survived_val)
print('shapiro test for survived lizards: ','stat=%.3f p-value=%.3f'%(stat,p))
stat,p = stats.shapiro(dead_val)
print('shapiro test for dead lizards: ','stat=%.3f p-value=%.3f'%(stat,p))
# comparison of means using boxplot and violin plot
# boxplot for survived sample
# fig, axs = plt.subplots(1,2,figsize=(12,4))
# sns.boxplot(survived, ax=axs[0])
# axs[0].set_title('Boxplot of survived lizards')
# boxplot for the dead sample
# sns.boxplot(dead, ax=axs[1])
# axs[1].set_title('Boxplot of dead lizards')
# plt.show()
# plt.savefig('Histogram of 2 samples.jpg')
# violinplot for survived sample
# fig, axs = plt.subplots(1,2,figsize=(12,4))
# sns.violinplot(survived, ax=axs[0])
# axs[0].set_title('Violinplot of survived lizards')
# violinplot for the dead sample
# sns.violinplot(dead, ax=axs[1])
# axs[1].set_title('Violinplot of dead lizards')
# plt.show()
# plt.savefig('Violinplots of 2 samples.jpg')
# paired sample t test
# t,p = stats.ttest_ind(survived_val, dead_val, equal_var=True, alternative= 'greater')
# print('t stat: ',t)
# print('p value: ', p)
# check for equal variances
# print(np.var(survived_val),np.var(dead_val))
# Perform the two sample t-test with equal variances
# t,p = stats.ttest_ind(survived_val, dead_val, equal_var=True, alternative= 'greater')
# print('t stat: ',t)
# print('p value: ', p)
# perform MannWhitney test
t,p = stats.mannwhitneyu(survived_val, dead_val, alternative= 'greater')
print('Q2 - Mannwhitney test')
print('U statistics: ',t)
print('p value: ', p)
# question 3
# create dataframe
antibody_production = pd.read_csv('BlackbirdTestosterone.csv')
# select a subset of a dataframe
log_before = antibody_production['log before']
log_after = antibody_production['log after']
log_difference = antibody_production['dif in logs']
before_sample = antibody_production['Before']
after_sample = antibody_production['After']
# log_before.head()
print(log_before.describe())
print(log_after.describe())
print(log_difference.describe())
# plotting histogram for log difference using seaborn
# sns.histplot(log_difference, kde= True)
# plt.show()
# plt.savefig('histogram for log difference using seaborn.jpg')
# plt.title('Histogram of log difference')
# plotting qqplot for the log difference using statmodels
# qqplot(log_difference, line= 's')
# plt.show()
# plt.title('QQplot for log difference ')
# plt.savefig('QQplot for log difference.jpg')
stat,p = stats.shapiro(log_difference)
print('shapiro test for log difference','stat=%.3f p-value=%.3f'%(stat,p))
# comparison of means using boxplot and violin plot
# boxplot for before sample
# fig, axs = plt.subplots(1,2,figsize=(12,4))
# sns.boxplot(before_sample, ax=axs[0])
# axs[0].set_title('Boxplot of before sample')
# boxplot for the after sample
# sns.boxplot(after_sample, ax=axs[1])
# axs[1].set_title('Boxplot of after sample')
# plt.show()
# plt.savefig('Histogram of 2 before and after samples.jpg')
# violinplot for before sample
# fig, axs = plt.subplots(1,2,figsize=(12,4))
# sns.violinplot(before_sample, ax=axs[0])
# axs[0].set_title('Violinplot of before sample')
# violinplot for the after sample
# sns.violinplot(after_sample, ax=axs[1])
# axs[1].set_title('Violinplot of after sample')
# plt.show()
# plt.savefig('Violinplots of 2 samples.jpg')
# paired sample t test
# ********order of log_after anf log_before
t,p = stats.ttest_rel(log_before, log_after, alternative='less')
print('Q3 - Paired sample t test results')
print('t stat: ',t)
print('p value: ', p)
# t,p = stats.ttest_rel(after_sample, before_sample, alternative='less')
# print('t stat: ',t)
# print('p value: ', p)
# question 3
# create manual dataframe
df = pd.DataFrame([[1,10,37],[49,35,9]],index=['eaten','not_eaten'],columns=['uninfected','lightly_infected','highly_infected'])
print(df)
# myCrosstable = pd.crosstab(df['uninfected'], df['lightly_infected'], df['highly_infected'])
# df = pd.DataFrame([['Eaten by birds',1,10,37],['not eaten by birds',49,35,9]],columns=['','uninfected','lightly_infected','highly_infected'])
df2 = df.stack()
print(df2)
df3 = df2.to_dict()
print(df3)
# plotting the mosaic plot
# from statsmodels.graphics.mosaicplot import mosaic
# mosaic(df3, gap= 0.05)
# plt.show()
# plt.title('mosaic plot')
# plt.savefig('mosaic plot.jpg')
chi = stats.chi2_contingency(df)
print('chi square statistic: ',chi)
# output the expected values
# expected_values = stats.contingency.expected_freq(df)
# print(expected_values)
# create a dataframe using the above expected values
expected_values_table = pd.DataFrame(chi.expected_freq, index=['eaten','not_eaten'], columns=['uninfected','lightly_infected','highly_infected'])
print(expected_values_table)