<p>Could it be that you are getting different results because you are not setting a seed on your random number generator?</p>
<p>The recommended way to make sure you get the same results when using numpy to generate dummy data is:</p>
<pre class="lang-py prettyprint-override"><code>import numpy as np
# make random number generator with seed
rng = np.random.default_rng(seed=42)
rng.random([4,4])
#Out: 
#array([[0.77395605, 0.43887844, 0.85859792, 0.69736803],
#       [0.09417735, 0.97562235, 0.7611397 , 0.78606431],
#       [0.12811363, 0.45038594, 0.37079802, 0.92676499],
#       [0.64386512, 0.82276161, 0.4434142 , 0.22723872]])


<span class="math-container">```</span>
</code></pre>

CC BY-SA 4.0
2024-01-11T12:41:13.653Z
2024-01-11T12:41:13.653Z
mk638801
<p>I am struggling in conducting at test on a board of 1000 genes in 50 individuals divided into to groups (A and B). I have a dataset of 1000 genes of two groups of people (total individuals=50)
group A = 30 (healthy) individuals &amp; group B = 20 sick) individuals</p>
<p>I created a matrix where rows are genes and ind are columns.
now I need to perform a t-test for all the genes and find the p values of my alternative hypothesis (not the null one): i)A&lt;B, A&gt;B and A=!B(not equal). threshold is 0.05 after FDR (which I must include)</p>
<p>I am struggling with the code since t-test differ on sensitivity and each of my approach returns a different result. The dummy data is as follows:</p>
<pre class="lang-py prettyprint-override"><code>import pandas as pd
import numpy as np

# Generate data for group A (healthy individuals) &amp; group B (sick individuals)
A = np.random.rand(30, 1000)
B = np.random.rand(20, 1000)
# Create dataframe for group A &amp; B with genes as columns
Adf = pd.DataFrame(A, columns=[f'Gene_{i}' for i in range(1, num_genes + 1)])
Bdf = pd.DataFrame(B, columns=[f'Gene_{i}' for i in range(1, num_genes + 1)])

# Add a column indicating the group
Adf['Group'] = 'A'
Bdf['Group'] = 'B'

df = pd.concat([Adf, Bdf], ignore_index=True)
df.head()
</code></pre>
<p>Any ideas?</p>

CC BY-SA 4.0
2024-01-07T06:32:12.953Z
can you please post the code that you've tried so far, along with a minimal example of your data? what are the approaches you've tried and why is it unexpected that different approaches yield different answers?
Maximilian Press
CC BY-SA 4.0
2024-01-07T09:35:05.410Z
sure. thanks for the response. so basically i have a dataset that i turned into a python matrix (with pandas DataFrame) consisting of 50 individuals, 30 individuals in group A (the first 30 columns) and 20 individuals in group B. the individuals in group A are healthy and the individuals in group B are sick. i applied the t-test for all genes but i have to find the pvalues for Hypothesis 1 (alternative) if i) A < B, ii) A > B, iii) A != B (not equal); threshold is 0.05 after FDR
(code at my next comment)
biostefania
CC BY-SA 4.0
2024-01-07T09:41:30.250Z
group_A = dataset.iloc[:, :30]  # first 30 columns for gr A (up to 30, including 30)
group_B = dataset.iloc[:, 30:]  # next 20 columns for gr B (next 20, from 31 to 50)
p_values_less = []
p_values_greater = []
p_values_not_equal = []
# t-tests for each gene
for gene in range(dataset.shape[0]):  #access to all genes
    t_stat, p_val_not_equal = stats.ttest_ind(group_A.iloc[gene], group_B.iloc[gene])
    p_values_not_equal.append(p_val_not_equal)
    p_values_less.append(p_val_not_equal / 2 if t_stat < 0 else 1)
    p_values_greater.append(p_val_not_equal / 2 if t_stat > 0 else 1)
biostefania
CC BY-SA 4.0
2024-01-07T09:43:46.867Z
p_values_corrected_less = smm.multipletests(p_values_less, alpha=0.05, method='fdr_bh')[1]
p_values_corrected_greater = smm.multipletests(p_values_greater, alpha=0.05, method='fdr_bh')[1]
p_values_corrected_not_equal = smm.multipletests(p_values_not_equal, alpha=0.05, method='fdr_bh')[1]
results = pd.DataFrame({
    'Gene': dataset.index,
    'p_value_less': p_values_corrected_less,
    'p_value_greater': p_values_corrected_greater,
    'p_value_not_equal': p_values_corrected_not_equal
})
biostefania
CC BY-SA 4.0
2024-01-07T09:43:51.377Z
significant_statistic_results = results[(results['p_value_less'] < 0.05) | 
                              (results['p_value_greater'] < 0.05) | 
                              (results['p_value_not_equal'] < 0.05)]
print(significant_statistic_results)
biostefania
CC BY-SA 4.0
2024-01-07T09:44:14.187Z
it messed my lines and Tabs but thats my code
biostefania
CC BY-SA 4.0
2024-01-07T12:46:18.340Z
Please [edit] your question and add the code there. You can use the `{}` button to format it as code.
terdon
CC BY-SA 4.0
2024-01-07T16:30:36.710Z
`group_A = dataset.iloc[:, :30]  # first 30 columns for group A (up to 30, including 30)
group_B = dataset.iloc[:, 30:]  # next 20 columns for group B (next 20, from 31 to 50)
#hypothesis:i)Α < Β, ii) Α>Β, iii) A != B
p_values_less = []
p_values_greater = []
p_values_not_equal = []
for gene in range(dataset.shape[0]):
    t_stat, p_val_not_equal = stats.ttest_ind(group_A.iloc[gene], group_B.iloc[gene])
    p_values_not_equal.append(p_val_not_equal)
    p_values_less.append(p_val_not_equal / 2 if t_stat < 0 else 1)
    p_values_greater.append(p_val_not_equal / 2 if t_stat > 0 else 1)`
biostefania
CC BY-SA 4.0
2024-01-07T16:31:51.913Z
`# Apply FDR correction
# the p values for each hypothesis (H1) are: p_values_less, p_values_greater, p_values_not_equal 
#a = 0,05
#FDR=b/h
p_values_corrected_less = smm.multipletests(p_values_less, alpha=0.05, method='fdr_bh')[1]
p_values_corrected_greater = smm.multipletests(p_values_greater, alpha=0.05, method='fdr_bh')[1]
p_values_corrected_not_equal = smm.multipletests(p_values_not_equal, alpha=0.05, method='fdr_bh')[1]`
biostefania
CC BY-SA 4.0
2024-01-07T16:32:16.410Z
`results = pd.DataFrame({
    'Gene': dataset.index,
    'p_value_less': p_values_corrected_less,
    'p_value_greater': p_values_corrected_greater,
    'p_value_not_equal': p_values_corrected_not_equal
})

significant_statistic_results = results[(results['p_value_less'] < 0.05) | 
                              (results['p_value_greater'] < 0.05) | 
                              (results['p_value_not_equal'] < 0.05)]
print(significant_statistic_results)`
biostefania
<python><gene><p-values><matrix><t-test>
conducting a ttest on a board of 1000 genes in 50 peope divided into to groups(A and B). pvalues are different each time
# conducting a ttest on a board of 1000 genes in 50 peope divided into to groups(A and B). pvalues are different each time

(Asked by: username_0 on 2024-01-06)

I am struggling in conducting at test on a board of 1000 genes in 50 individuals divided into to groups (A and B). I have a dataset of 1000 genes of two groups of people (total individuals=50)
group A = 30 (healthy) individuals & group B = 20 sick) individuals
I created a matrix where rows are genes and ind are columns.
now I need to perform a t-test for all the genes and find the p values of my alternative hypothesis (not the null one): i)AB and A=!B(not equal). threshold is 0.05 after FDR (which I must include)
I am struggling with the code since t-test differ on sensitivity and each of my approach returns a different result. The dummy data is as follows:
import pandas as pd
import numpy as np

# Generate data for group A (healthy individuals) & group B (sick individuals)
A = np.random.rand(30, 1000)
B = np.random.rand(20, 1000)
# Create dataframe for group A & B with genes as columns
Adf = pd.DataFrame(A, columns=[f'Gene_{i}' for i in range(1, num_genes + 1)])
Bdf = pd.DataFrame(B, columns=[f'Gene_{i}' for i in range(1, num_genes + 1)])

# Add a column indicating the group
Adf['Group'] = 'A'
Bdf['Group'] = 'B'

df = pd.concat([Adf, Bdf], ignore_index=True)
df.head()

Any ideas?

**Tags:** python, gene, p-values, matrix, t-test
> **Comments:**
> **username_1**: can you please post the code that you've tried so far, along with a minimal example of your data? what are the approaches you've tried and why is it unexpected that different approaches yield different answers?  
> **username_0**: sure. thanks for the response. so basically i have a dataset that i turned into a python matrix (with pandas DataFrame) consisting of 50 individuals, 30 individuals in group A (the first 30 columns) and 20 individuals in group B. the individuals in group A are healthy and the individuals in group B are sick. i applied the t-test for all genes but i have to find the pvalues for Hypothesis 1 (alternative) if i) A < B, ii) A > B, iii) A != B (not equal); threshold is 0.05 after FDR (code at my next comment)  
> **username_0**: group_A = dataset.iloc[:, :30]  # first 30 columns for gr A (up to 30, including 30) group_B = dataset.iloc[:, 30:]  # next 20 columns for gr B (next 20, from 31 to 50) p_values_less = [] p_values_greater = [] p_values_not_equal = [] # t-tests for each gene for gene in range(dataset.shape[0]):  #access to all genes     t_stat, p_val_not_equal = stats.ttest_ind(group_A.iloc[gene], group_B.iloc[gene])     p_values_not_equal.append(p_val_not_equal)     p_values_less.append(p_val_not_equal / 2 if t_stat < 0 else 1)     p_values_greater.append(p_val_not_equal / 2 if t_stat > 0 else 1)  
> **username_0**: p_values_corrected_less = smm.multipletests(p_values_less, alpha=0.05, method='fdr_bh')[1] p_values_corrected_greater = smm.multipletests(p_values_greater, alpha=0.05, method='fdr_bh')[1] p_values_corrected_not_equal = smm.multipletests(p_values_not_equal, alpha=0.05, method='fdr_bh')[1] results = pd.DataFrame({     'Gene': dataset.index,     'p_value_less': p_values_corrected_less,     'p_value_greater': p_values_corrected_greater,     'p_value_not_equal': p_values_corrected_not_equal })  
> **username_0**: significant_statistic_results = results[(results['p_value_less'] < 0.05) |                                (results['p_value_greater'] < 0.05) |                                (results['p_value_not_equal'] < 0.05)] print(significant_statistic_results)  
> **username_0**: it messed my lines and Tabs but thats my code  
> **username_2**: Please [edit] your question and add the code there. You can use the `{}` button to format it as code.  
> **username_0**: `group_A = dataset.iloc[:, :30]  # first 30 columns for group A (up to 30, including 30) group_B = dataset.iloc[:, 30:]  # next 20 columns for group B (next 20, from 31 to 50) #hypothesis:i)Α < Β, ii) Α>Β, iii) A != B p_values_less = [] p_values_greater = [] p_values_not_equal = [] for gene in range(dataset.shape[0]):     t_stat, p_val_not_equal = stats.ttest_ind(group_A.iloc[gene], group_B.iloc[gene])     p_values_not_equal.append(p_val_not_equal)     p_values_less.append(p_val_not_equal / 2 if t_stat < 0 else 1)     p_values_greater.append(p_val_not_equal / 2 if t_stat > 0 else 1)`  
> **username_0**: `# Apply FDR correction # the p values for each hypothesis (H1) are: p_values_less, p_values_greater, p_values_not_equal  #a = 0,05 #FDR=b/h p_values_corrected_less = smm.multipletests(p_values_less, alpha=0.05, method='fdr_bh')[1] p_values_corrected_greater = smm.multipletests(p_values_greater, alpha=0.05, method='fdr_bh')[1] p_values_corrected_not_equal = smm.multipletests(p_values_not_equal, alpha=0.05, method='fdr_bh')[1]`  
> **username_0**: `results = pd.DataFrame({     'Gene': dataset.index,     'p_value_less': p_values_corrected_less,     'p_value_greater': p_values_corrected_greater,     'p_value_not_equal': p_values_corrected_not_equal })  significant_statistic_results = results[(results['p_value_less'] < 0.05) |                                (results['p_value_greater'] < 0.05) |                                (results['p_value_not_equal'] < 0.05)] print(significant_statistic_results)`  

---

## 1 answers

### Answer by username_3 on 2024-01-11. Score: 1

Could it be that you are getting different results because you are not setting a seed on your random number generator?
The recommended way to make sure you get the same results when using numpy to generate dummy data is:
import numpy as np
# make random number generator with seed
rng = np.random.default_rng(seed=42)
rng.random([4,4])
#Out: 
#array([[0.77395605, 0.43887844, 0.85859792, 0.69736803],
#       [0.09417735, 0.97562235, 0.7611397 , 0.78606431],
#       [0.12811363, 0.45038594, 0.37079802, 0.92676499],
#       [0.64386512, 0.82276161, 0.4434142 , 0.22723872]])


```


---

