K.S
K.S

Reputation: 113

Two sample dependent T test in python and rank sum test

I have a data set for which has two labels, label 1 = 0(case), label 2 =1(control). I have already calculated the mean for the two different labels. Furthermore, I need to calculate two sample t test(dependent) and two sample rank sum test. My data set looks like :

SRA ID  ERR169499           ERR169500           ERR169501           mean_ctrl   mean_case
Label   1                   0                   1
TaxID   PRJEB3251_ERR169499 PRJEB3251_ERR169500 PRJEB3251_ERR169501
333046  0.05                0                   0.4
1049    0.03                0.9                 0
337090  0.01                0.6                 0.7

I am new to statistics.The code I have so far is this:

label = []
data = {}

x = open('final_out_transposed.csv','rt')
for r in x:
datas = r.split(',')
if datas[0] == ' Label':
    label.append(r.split(",")[1:])
label = label[0]
label[-1] = label[-1].replace('\n','')
counter = len(label)

for row in file1:
content = row.split(',')
if content[0]=='SRA ID' or content[0]== 'TaxID' or content[0]==' Label':
    pass
else:
    dt = row.split(',')
    dt[-1] = dt[-1].replace('\n','')
    data[dt[0]]=dt[1:]
keys = list(data)

sum_file = open('sum.csv','w')
for key in keys:
sum_case = 0
sum_ctrl = 0
count_case = 0
count_ctrl = 0
mean_case = 0
mean_ctrl = 0
print(len(label))
for i in range(counter):
    print(i)
    if label[i] == '0' or label[i] == 0:
        sum_case=np.float64(sum_case)+np.float64(data[key][i])
        count_case = count_case+1
        mean_case = sum_case/count_case
    else:
        sum_ctrl = np.float64(sum_ctrl)+np.float64(data[key][i])
        count_ctrl = count_ctrl+1
        mean_ctrl = sum_ctrl/count_ctrl

Any help will be highly appreciated.

Upvotes: 1

Views: 847

Answers (1)

David Rinck
David Rinck

Reputation: 6986

Instead of using open to read your csv file, I would use Pandas. That will place it in a dataframe that will be easier to use

import pandas as pd
data_frame = pd.read_csv('final_out_transposed.csv')

For a Two Sample dependent T-test you want to use ttest_rel

notice ttest_ind is for independent groups. Since you specifically asked for dependent groups, use ttest_rel.

It's hard from your example above to see where your two columns of sample data are, but imagine I had the following made up data of 'case' and 'control'. I could calculate a dependent Two Sample t-test using pandas as shown below:

import pandas as pd
from scipy.stats import ttest_rel
data_frame = pd.DataFrame({
'case':[55, 43, 51, 62, 35, 48, 58, 45, 48, 54, 56, 32],
'control':[48, 38, 53, 58, 36, 42, 55, 40, 49, 50, 58, 25]})
(t_stat, p) = ttest_rel(data_frame['control'], data_frame['case'])
print (t_stat)
print (p)

p would be the p-value, t_stat would be the t-statistic. You can read more about this in the documentation

In a similar manner, once you have your sample .csv data in a dataframe, you can perform a rank sum test:

from scipy.stats import ranksums
(t_stat, p) = ranksums(data_frame['control'], data_frame['case'])

documentation for ranksums

Upvotes: 1

Related Questions