I am trying to do a two sample t test to check if there is significant difference in mean between two datasets.
I have two datasets and each dataset has 5 trials and each Trial has 3 features. Every Trial has different unique label but the 3 features(X1,X2,X3 are same across all). On every individual Trial we are measuring the 3 features and the measurement values are displayed below. I am trying to calculate the mean difference for each feature across both the datasets.
This is how my data looks after when i get it from SQL.
Data Set 1:
T1 X1 0.93
T1 X2 0.3
T1 X3 -2.9
T2 X1 1.3
T2 X2 0.8
T2 X3 1.9
T3 X1 2.3
T3 X2 -1.8
T3 X3 0.9
T4 X1 0.3
T4 X2 0.8
T4 X3 0.9
T5 X1 0.3
T5 X2 0.8
T5 X3 0.9
Data Set 2:
T10 X1 1.3
T10 X2 -2.8
T10 X3 0.09
T11 X1 3.3
T11 X2 0.8
T11 X3 1.9
T12 X1 0.3
T12 X2 -4.8
T12 X3 2.9
T13 X1 1.3
T13 X2 2.8
T13 X3 0.19
T14 X1 2.3
T14 X2 0.08
T14 X3 -0.9
This is how i want my output to look, where i want the ttest to be applied to each Feature, so I can get the p value for each feature
Feature Mean-DataSET1 Mean-DataSET2 P-value
X1
X2
X3
when i do stats.ttest_ind(set1['value'], set2['value']).pvalue, I am getting one single pvalue
Thanks!
If I understand you question correctly you can get the mean for each feature using Groupby, and then get the p-value for each feature all in one dataset. so first I would create the dataset,
a = {'Feature': ['X1','X2','X3','X4','X5']}
Results = pd.DataFrame(data = a)
Results.set_index('Feature')
Then to get the mean of your features, you can use group by and send the result to this new dataset,
Results['Mean-DataSET1'] = df1.groupby('feature')['value'].transform('mean')
Results['Mean-DataSET2'] = df2.groupby('feature')['value'].transform('mean')
Now as far as I know p-tests returns the value of the whole column so I would get both my value columns in one place and then split my data into temporary datasets and get the p-value of those,
df['value2'] = df2['value']
xone = df[(df['col2'] == 'X1')]
xtwo = df[(df['col2'] == 'X2')]
xthree = df[(df['col2'] == 'X3')]
xfour = df[(df['col2'] == 'X4')]
xfive = df[(df['col2'] == 'X5')]
This way you can preform the same function as before and get all the values like so,
p_vals = ttest_ind(xone['value'], xone['value2']).pvalue, ttest_ind(xtwo['value'], xtwo['value2']).pvalue, ttest_ind(xthree['value'], xthree['value2']).pvalue, ttest_ind(xfour['value'], xfour['value2']).pvalue, ttest_ind(xfive['value'], xfive['value2']).pvalue
Results['P_value'] = p_vals
So a sample output on a subset of your data would like like:
Feature mean1 mean2 P_value
X1 1.510000 1.633333 0.905175
X2 -0.233333 -2.266667 0.326891
X3 -0.033333 1.630000 0.377542
It's not the most graceful answer but it should be ok for now as you only have small datasets!
I written your output above to two tab delimited files, and I read it in below, and add a column to indicate the dataframe or table it is from:
import pandas as pd
from scipy.stats import ttest_ind
t1 = pd.read_csv("../t1.csv",names=['V1','V2','V3'],sep="\t")
t1['data'] = 'data1'
t2 = pd.read_csv("../t2.csv",names=['V1','V2','V3'],sep="\t")
t2['data'] = 'data2'
V1 V2 V3 data
0 T1 X1 0.93 data1
1 T1 X2 0.30 data1
2 T1 X3 -2.90 data1
3 T2 X1 1.30 data1
Then we concatenate them and calculating the mean is straight forward:
df = pd.concat([t1,t2])
res = df.groupby("V2").apply(lambda x:x['V3'].groupby(x['data']).mean())
data data1 data2
V2
X1 1.026 1.700
X2 0.180 -0.784
X3 0.340 0.836
p.value requires a bit more coding within the apply:
res['pvalue'] = df.groupby("V2").apply(lambda x:
ttest_ind(x[x['data']=="data1"]["V3"],x[x['data']=="data2"]["V3"])[1])
data data1 data2 pvalue
V2
X1 1.026 1.700 0.316575
X2 0.180 -0.784 0.521615
X3 0.340 0.836 0.657752
You can always choose to do res.reset_index()
to get a table..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.