LDpred-gibbs#
LDpred is a tool for calculating Polygenic Risk Scores (PRS). This notebook demonstrates how to use LDpred to perform these calculations.
Repository: LDpred GitHub Repository
If you encounter issues with installing the software, kindly visit their issue tracker on GitHub.
Issue Tracker: Issue #131
Installation#
LDpred requires the following Python packages:
h5py
scipy
libplinkio
(installable via pip)
To install libplinkio
, you can use:
pip install plinkio
Alternatively, for a local installation:
pip install --user plinkio
You can install LDpred using pip:
pip install ldpred
Or clone the repository using git:
git clone https://github.com/bvilhjal/ldpred.git
Basic calculation#
Step 1: Create the Coordinate File#
First, synchronize your data (GWAS and genotype) by running:
ldpred coord
Usage:
LDpred coord [-h] --gf GF --ssf SSF [--N N] --out OUT [--vbim VBIM] [--vgf VGF] [--only-hm3] [--ilist ILIST]
[--skip-coordination] [--eff_type {LOGOR,OR,LINREG,BLUP}] [--match-genomic-pos] [--maf MAF]
[--max-freq-discrep MAX_FREQ_DISCREP] [--ssf-format {STANDARD,CUSTOM,BASIC,PGC,LDPRED,GIANT}]
[--rs RS] [--A1 A1] [--A2 A2] [--pos POS] [--info INFO] [--chr CHR] [--reffreq REFFREQ]
[--pval PVAL] [--eff EFF] [--se SE] [--ncol NCOL] [--case-freq CASE_FREQ]
[--control-freq CONTROL_FREQ] [--case-n CASE_N] [--control-n CONTROL_N] [--z-from-se]
Arguments:
Option |
Description |
---|---|
|
Show help message and exit. |
|
LD Reference Genotype File. Full path filename prefix to a standard PLINK bed file (without .bed). |
|
Summary Statistic File. Filename for a text file with the GWAS summary statistics. |
|
Number of Individuals in Summary Statistic File. Required for most summary statistics formats. |
|
Output Prefix. |
|
Validation SNP file. A PLINK BIM file (.bim) used to filter SNPs. |
|
Validation genotype file. Filename prefix (without .bed) for filtering SNPs. |
|
Restrict analysis to 1.2M HapMap 3 SNPs. |
|
List of individuals to include in the analysis. |
|
Assumes alleles have already been coordinated between LD reference, validation samples, and summary stats. |
|
Type of effect estimates reported in the summary statistics. |
|
Exclude SNPs from summary stats if their genomic positions differ from validation data. |
|
MAF filtering threshold. Set to 0 to disable MAF filtering. |
|
Max frequency discrepancy allowed between reported sum stats frequency and frequency in the LD reference data. |
|
Format type of the summary statistics file. |
|
Column header of SNP ID. |
|
Column header containing the effective allele. |
|
Column header containing non-effective allele. |
|
Column header containing the coordinate of SNPs. |
|
Column header containing the INFO score. |
|
Column header containing the chromosome information. |
|
Column header containing the reference MAF. |
|
Column header containing the P-value information. |
|
Column header containing effect size information. |
|
Column header containing standard error. |
|
Column header containing sample size information. |
|
Column header containing case frequency information. |
|
Column header containing control frequency information. |
|
Column header containing case sample size information. |
|
Column header containing control sample size information. |
|
Derive effects using effect estimates and their standard errors. |
Step 2: Generate LDpred SNP Weights#
After generating the coordinated data file, apply LDpred by running:
ldpred gibbs
The ldpred gibbs
command is used to perform Gibbs sampling for LDpred. Below are the options available:
usage: LDpred gibbs [-h] --cf CF --ldr LDR --ldf LDF --out OUT [--f F [F ...]] [--N N] [--n-iter N_ITER]
[--n-burn-in N_BURN_IN] [--h2 H2] [--use-gw-h2] [--no-ld-compression] [--hickle-ld]
[--incl-long-range-ld]
Options:#
Option |
Description |
---|---|
|
Show this help message and exit. |
|
Coordinated file (generated using |
|
LD radius. An integer number which denotes the number of SNPs on each side of the focal SNP for which LD should be adjusted. A value corresponding to M/3000, where M is the number of SNPs in the genome is recommended. |
|
LD file (prefix). A path and filename prefix for the LD file. If it does not exist, it will be generated. This can take up to several hours, depending on LD radius used. |
|
Output Prefix for SNP weights. |
|
Fraction of causal variants used in the Gibbs sampler. |
|
Number of individuals on which the summary statistics are assumed to be based. |
|
The number of iterations used by the Gibbs sampler. The default is 60. |
|
The number of burn-in iterations used by the Gibbs sampler. The default is 5. |
|
The genome-wide heritability assumed by LDpred, which is then partitioned proportional to the number of SNPs on each chromosome. By default, it estimates the heritability for each chromosome from the GWAS summary statistics using LD score regression. |
|
Estimate heritability genome-wide and partition it proportional to the number of SNPs on each chromosome instead of estimating it for each chromosome separately. This is generally recommended if the summary statistics are based on small sample sizes (approx <50K), or if the trait is not very heritable. |
|
Do not compress LD information. Saves storing and loading time of LD information, but takes more space on disk. |
|
Use hickle instead of pickle for storing LD files. This saves memory, but generally takes more time to write and load. Requires hickle to be installed on your system. |
|
Includes SNPs that are located in long-range LD regions in the Gibbs sampler. The LD-pred inf effect estimates are used for these by default. |
References#
GWAS File Processing for LDpred for Binary Phenotypes#
LDpred can process both Odds Ratios (OR) and BETAs and generates new BETAs for both binary and continuous phenotypes. In our workflow, we first generate BETAs from ORs and then use these BETAs to create a model using the specified LDpred model.
import os
import pandas as pd
import numpy as np
import sys
#filedirec = sys.argv[1]
filedirec = "SampleData1"
#filedirec = "asthma_19"
#filedirec = "migraine_0"
def check_phenotype_is_binary_or_continous(filedirec):
# Read the processed quality controlled file for a phenotype
df = pd.read_csv(filedirec+os.sep+filedirec+'_QC.fam',sep="\s+",header=None)
column_values = df[5].unique()
if len(set(column_values)) == 2:
return "Binary"
else:
return "Continous"
# Read the GWAS file.
GWAS = filedirec + os.sep + filedirec+".gz"
df = pd.read_csv(GWAS,compression= "gzip",sep="\s+")
if check_phenotype_is_binary_or_continous(filedirec)=="Binary":
if "BETA" in df.columns.to_list():
# For Binary Phenotypes.
df["OR"] = np.exp(df["BETA"])
df["SE"] = df["BETA"] * df["SE"]
df = df[['CHR', 'BP', 'SNP', 'A1', 'A2', 'N', 'SE', 'P', 'OR', 'INFO', 'MAF']]
else:
# For Binary Phenotype.
df = df[['CHR', 'BP', 'SNP', 'A1', 'A2', 'N', 'SE', 'P', 'OR', 'INFO', 'MAF']]
df = df.rename(columns={
'CHR':'CHR',
'BP': 'POS', # Rename 'BP' to 'POS'
'SNP': 'SNP_ID', # Rename 'SNP' to 'SNP_ID'
'A1': 'REF', # Rename 'A1' to 'REF'
'A2': 'ALT', # Rename 'A2' to 'ALT'
'MAF': 'REF_FRQ',
'P': 'PVAL',
'OR':'OR',
})
df = df[['CHR', 'POS', 'SNP_ID', 'REF', 'ALT', 'REF_FRQ', 'PVAL', 'OR', 'SE', 'N']]
elif check_phenotype_is_binary_or_continous(filedirec)=="Continous":
if "BETA" in df.columns.to_list():
# For Continous Phenotype.
df = df[['CHR', 'BP', 'SNP', 'A1', 'A2', 'N', 'SE', 'P', 'BETA', 'INFO', 'MAF']]
else:
df["BETA"] = np.log(df["OR"])
df["SE"] = df["SE"]/df["OR"]
df = df[['CHR', 'BP', 'SNP', 'A1', 'A2', 'N', 'SE', 'P', 'BETA', 'INFO', 'MAF']]
df = df.rename(columns={
'CHR':'CHR',
'BP': 'POS', # Rename 'BP' to 'POS'
'SNP': 'SNP_ID', # Rename 'SNP' to 'SNP_ID'
'A1': 'REF', # Rename 'A1' to 'REF'
'A2': 'ALT', # Rename 'A2' to 'ALT'
'MAF': 'REF_FRQ',
'P': 'PVAL',
'BETA':'BETA',
})
df = df[['CHR', 'POS', 'SNP_ID', 'REF', 'ALT', 'REF_FRQ', 'PVAL', 'BETA', 'SE', 'N']]
N = df["N"].mean()
df.to_csv(filedirec + os.sep +filedirec+"_ldpredgibs.txt",sep="\t",index=False)
print(df.head().to_markdown())
print("Length of DataFrame!",len(df))
| | CHR | POS | SNP_ID | REF | ALT | REF_FRQ | PVAL | BETA | SE | N |
|---:|------:|-------:|:-----------|:------|:------|----------:|---------:|------------:|-----------:|-------:|
| 0 | 1 | 756604 | rs3131962 | A | G | 0.36939 | 0.483171 | -0.00211532 | 0.00302305 | 388028 |
| 1 | 1 | 768448 | rs12562034 | A | G | 0.336846 | 0.834808 | 0.00068708 | 0.00329246 | 388028 |
| 2 | 1 | 779322 | rs4040617 | G | A | 0.377368 | 0.42897 | -0.00239932 | 0.00304073 | 388028 |
| 3 | 1 | 801536 | rs79373928 | G | T | 0.483212 | 0.808999 | 0.00203363 | 0.00839615 | 388028 |
| 4 | 1 | 808631 | rs11240779 | G | A | 0.45041 | 0.590265 | 0.00130747 | 0.00242504 | 388028 |
Length of DataFrame! 499617
Define Hyperparameters#
Define hyperparameters to be optimized and set initial values.
Extract Valid SNPs from Clumped File#
For Windows, download gwak
, and for Linux, the awk
command is sufficient. For Windows, GWAK
is required. You can download it from here. Get it and place it in the same directory.
Execution Path#
At this stage, we have the genotype training data newtrainfilename = "train_data.QC"
and genotype test data newtestfilename = "test_data.QC"
.
We modified the following variables:
filedirec = "SampleData1"
orfiledirec = sys.argv[1]
foldnumber = "0"
orfoldnumber = sys.argv[2]
for HPC.
Only these two variables can be modified to execute the code for specific data and specific folds. Though the code can be executed separately for each fold on HPC and separately for each dataset, it is recommended to execute it for multiple diseases and one fold at a time. Here’s the corrected text in Markdown format:
P-values#
PRS calculation relies on P-values. SNPs with low P-values, indicating a high degree of association with a specific trait, are considered for calculation.
You can modify the code below to consider a specific set of P-values and save the file in the same format.
We considered the following parameters:
Minimum P-value:
1e-10
Maximum P-value:
1.0
Minimum exponent:
10
(Minimum P-value in exponent)Number of intervals:
100
(Number of intervals to be considered)
The code generates an array of logarithmically spaced P-values:
import numpy as np
import os
minimumpvalue = 10 # Minimum exponent for P-values
numberofintervals = 100 # Number of intervals to be considered
allpvalues = np.logspace(-minimumpvalue, 0, numberofintervals, endpoint=True) # Generating an array of logarithmically spaced P-values
print("Minimum P-value:", allpvalues[0])
print("Maximum P-value:", allpvalues[-1])
count = 1
with open(os.path.join(folddirec, 'range_list'), 'w') as file:
for value in allpvalues:
file.write(f'pv_{value} 0 {value}\n') # Writing range information to the 'range_list' file
count += 1
pvaluefile = os.path.join(folddirec, 'range_list')
In this code:
minimumpvalue
defines the minimum exponent for P-values.numberofintervals
specifies how many intervals to consider.allpvalues
generates an array of P-values spaced logarithmically.The script writes these P-values to a file named
range_list
in the specified directory.
from operator import index
import pandas as pd
import numpy as np
import os
import subprocess
import sys
import pandas as pd
import statsmodels.api as sm
import pandas as pd
from sklearn.metrics import roc_auc_score, confusion_matrix
from statsmodels.stats.contingency_tables import mcnemar
def create_directory(directory):
"""Function to create a directory if it doesn't exist."""
if not os.path.exists(directory): # Checking if the directory doesn't exist
os.makedirs(directory) # Creating the directory if it doesn't exist
return directory # Returning the created or existing directory
#foldnumber = sys.argv[1]
foldnumber = "0" # Setting 'foldnumber' to "0"
folddirec = filedirec + os.sep + "Fold_" + foldnumber # Creating a directory path for the specific fold
trainfilename = "train_data" # Setting the name of the training data file
newtrainfilename = "train_data.QC" # Setting the name of the new training data file
testfilename = "test_data" # Setting the name of the test data file
newtestfilename = "test_data.QC" # Setting the name of the new test data file
# Number of PCA to be included as a covariate.
numberofpca = ["6"] # Setting the number of PCA components to be included
# Clumping parameters.
clump_p1 = [1] # List containing clump parameter 'p1'
clump_r2 = [0.1] # List containing clump parameter 'r2'
clump_kb = [200] # List containing clump parameter 'kb'
# Pruning parameters.
p_window_size = [200] # List containing pruning parameter 'window_size'
p_slide_size = [50] # List containing pruning parameter 'slide_size'
p_LD_threshold = [0.25] # List containing pruning parameter 'LD_threshold'
# Kindly note that the number of p-values to be considered varies, and the actual p-value depends on the dataset as well.
# We will specify the range list here.
minimumpvalue = 10 # Minimum p-value in exponent
numberofintervals = 20 # Number of intervals to be considered
allpvalues = np.logspace(-minimumpvalue, 0, numberofintervals, endpoint=True) # Generating an array of logarithmically spaced p-values
count = 1
with open(folddirec + os.sep + 'range_list', 'w') as file:
for value in allpvalues:
file.write(f'pv_{value} 0 {value}\n') # Writing range information to the 'range_list' file
count = count + 1
pvaluefile = folddirec + os.sep + 'range_list'
# Initializing an empty DataFrame with specified column names
prs_result = pd.DataFrame(columns=["clump_p1", "clump_r2", "clump_kb", "p_window_size", "p_slide_size", "p_LD_threshold",
"pvalue", "numberofpca","numberofvariants","Train_pure_prs", "Train_null_model", "Train_best_model",
"Test_pure_prs", "Test_null_model", "Test_best_model"])
Define Helper Functions#
Perform Clumping and Pruning
Calculate PCA Using Plink
Fit Binary Phenotype and Save Results
Fit Continuous Phenotype and Save Results
import os
import subprocess
import pandas as pd
import statsmodels.api as sm
from sklearn.metrics import explained_variance_score
def perform_clumping_and_pruning_on_individual_data(traindirec, newtrainfilename,numberofpca, p1_val, p2_val, p3_val, c1_val, c2_val, c3_val,Name,pvaluefile):
command = [
"./plink",
"--bfile", traindirec+os.sep+newtrainfilename,
"--indep-pairwise", p1_val, p2_val, p3_val,
"--out", traindirec+os.sep+trainfilename
]
subprocess.run(command)
# First perform pruning and then clumping and the pruning.
command = [
"./plink",
"--bfile", traindirec+os.sep+newtrainfilename,
"--clump-p1", c1_val,
"--extract", traindirec+os.sep+trainfilename+".prune.in",
"--clump-r2", c2_val,
"--clump-kb", c3_val,
"--clump", filedirec+os.sep+filedirec+".txt",
"--clump-snp-field", "SNP",
"--clump-field", "P",
"--out", traindirec+os.sep+trainfilename
]
subprocess.run(command)
# Extract the valid SNPs from th clumped file.
# For windows download gwak for linux awk commmand is sufficient.
### For windows require GWAK.
### https://sourceforge.net/projects/gnuwin32/
##3 Get it and place it in the same direc.
#os.system("gawk "+"\""+"NR!=1{print $3}"+"\" "+ traindirec+os.sep+trainfilename+".clumped > "+traindirec+os.sep+trainfilename+".valid.snp")
#print("gawk "+"\""+"NR!=1{print $3}"+"\" "+ traindirec+os.sep+trainfilename+".clumped > "+traindirec+os.sep+trainfilename+".valid.snp")
#Linux:
command = f"awk 'NR!=1{{print $3}}' {traindirec}{os.sep}{trainfilename}.clumped > {traindirec}{os.sep}{trainfilename}.valid.snp"
os.system(command)
command = [
"./plink",
"--make-bed",
"--bfile", traindirec+os.sep+newtrainfilename,
"--indep-pairwise", p1_val, p2_val, p3_val,
"--extract", traindirec+os.sep+trainfilename+".valid.snp",
"--out", traindirec+os.sep+newtrainfilename+".clumped.pruned"
]
subprocess.run(command)
command = [
"./plink",
"--make-bed",
"--bfile", traindirec+os.sep+testfilename,
"--indep-pairwise", p1_val, p2_val, p3_val,
"--extract", traindirec+os.sep+trainfilename+".valid.snp",
"--out", traindirec+os.sep+testfilename+".clumped.pruned"
]
subprocess.run(command)
def calculate_pca_for_traindata_testdata_for_clumped_pruned_snps(traindirec, newtrainfilename,p):
# Calculate the PRS for the test data using the same set of SNPs and also calculate the PCA.
# Also extract the PCA at this point.
# PCA are calculated afer clumping and pruining.
command = [
"./plink",
"--bfile", folddirec+os.sep+testfilename+".clumped.pruned",
# Select the final variants after clumping and pruning.
"--extract", traindirec+os.sep+trainfilename+".valid.snp",
"--pca", p,
"--out", folddirec+os.sep+testfilename
]
subprocess.run(command)
command = [
"./plink",
"--bfile", traindirec+os.sep+newtrainfilename+".clumped.pruned",
# Select the final variants after clumping and pruning.
"--extract", traindirec+os.sep+trainfilename+".valid.snp",
"--pca", p,
"--out", traindirec+os.sep+trainfilename
]
subprocess.run(command)
# This function fit the binary model on the PRS.
def fit_binary_phenotype_on_PRS(traindirec, newtrainfilename,p,radius,betafile,fraction,burn,iterations, p1_val, p2_val, p3_val, c1_val, c2_val, c3_val,Name,pvaluefile):
threshold_values = allpvalues
# Merge the covariates, pca and phenotypes.
tempphenotype_train = pd.read_table(traindirec+os.sep+newtrainfilename+".clumped.pruned"+".fam", sep="\s+",header=None)
phenotype_train = pd.DataFrame()
phenotype_train["Phenotype"] = tempphenotype_train[5].values
pcs_train = pd.read_table(traindirec+os.sep+trainfilename+".eigenvec", sep="\s+",header=None, names=["FID", "IID"] + [f"PC{str(i)}" for i in range(1, int(p)+1)])
covariate_train = pd.read_table(traindirec+os.sep+trainfilename+".cov",sep="\s+")
covariate_train.fillna(0, inplace=True)
covariate_train = covariate_train[covariate_train["FID"].isin(pcs_train["FID"].values) & covariate_train["IID"].isin(pcs_train["IID"].values)]
covariate_train['FID'] = covariate_train['FID'].astype(str)
pcs_train['FID'] = pcs_train['FID'].astype(str)
covariate_train['IID'] = covariate_train['IID'].astype(str)
pcs_train['IID'] = pcs_train['IID'].astype(str)
covandpcs_train = pd.merge(covariate_train, pcs_train, on=["FID","IID"])
covandpcs_train.fillna(0, inplace=True)
## Scale the covariates!
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import explained_variance_score
scaler = MinMaxScaler()
normalized_values_train = scaler.fit_transform(covandpcs_train.iloc[:, 2:])
#covandpcs_train.iloc[:, 2:] = normalized_values_test
tempphenotype_test = pd.read_table(traindirec+os.sep+testfilename+".clumped.pruned"+".fam", sep="\s+",header=None)
phenotype_test= pd.DataFrame()
phenotype_test["Phenotype"] = tempphenotype_test[5].values
pcs_test = pd.read_table(traindirec+os.sep+testfilename+".eigenvec", sep="\s+",header=None, names=["FID", "IID"] + [f"PC{str(i)}" for i in range(1, int(p)+1)])
covariate_test = pd.read_table(traindirec+os.sep+testfilename+".cov",sep="\s+")
covariate_test.fillna(0, inplace=True)
covariate_test = covariate_test[covariate_test["FID"].isin(pcs_test["FID"].values) & covariate_test["IID"].isin(pcs_test["IID"].values)]
covariate_test['FID'] = covariate_test['FID'].astype(str)
pcs_test['FID'] = pcs_test['FID'].astype(str)
covariate_test['IID'] = covariate_test['IID'].astype(str)
pcs_test['IID'] = pcs_test['IID'].astype(str)
covandpcs_test = pd.merge(covariate_test, pcs_test, on=["FID","IID"])
covandpcs_test.fillna(0, inplace=True)
normalized_values_test = scaler.transform(covandpcs_test.iloc[:, 2:])
#covandpcs_test.iloc[:, 2:] = normalized_values_test
tempalphas = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
l1weights = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
tempalphas = [0.1]
l1weights = [0.1]
phenotype_train["Phenotype"] = phenotype_train["Phenotype"].replace({1: 0, 2: 1})
phenotype_test["Phenotype"] = phenotype_test["Phenotype"].replace({1: 0, 2: 1})
for tempalpha in tempalphas:
for l1weight in l1weights:
try:
null_model = sm.Logit(phenotype_train["Phenotype"], sm.add_constant(covandpcs_train.iloc[:, 2:])).fit_regularized(alpha=tempalpha, L1_wt=l1weight)
#null_model = sm.Logit(phenotype_train["Phenotype"], sm.add_constant(covandpcs_train.iloc[:, 2:])).fit()
except:
print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
continue
train_null_predicted = null_model.predict(sm.add_constant(covandpcs_train.iloc[:, 2:]))
from sklearn.metrics import roc_auc_score, confusion_matrix
from sklearn.metrics import r2_score
test_null_predicted = null_model.predict(sm.add_constant(covandpcs_test.iloc[:, 2:]))
global prs_result
for i in threshold_values:
try:
prs_train = pd.read_table(traindirec+os.sep+Name+os.sep+"train_data.pv_"+f"{i}.profile", sep="\s+", usecols=["FID", "IID", "SCORE"])
except:
continue
prs_train['FID'] = prs_train['FID'].astype(str)
prs_train['IID'] = prs_train['IID'].astype(str)
try:
prs_test = pd.read_table(traindirec+os.sep+Name+os.sep+"test_data.pv_"+f"{i}.profile", sep="\s+", usecols=["FID", "IID", "SCORE"])
except:
continue
prs_test['FID'] = prs_test['FID'].astype(str)
prs_test['IID'] = prs_test['IID'].astype(str)
pheno_prs_train = pd.merge(covandpcs_train, prs_train, on=["FID", "IID"])
pheno_prs_test = pd.merge(covandpcs_test, prs_test, on=["FID", "IID"])
try:
model = sm.Logit(phenotype_train["Phenotype"], sm.add_constant(pheno_prs_train.iloc[:, 2:])).fit_regularized(alpha=tempalpha, L1_wt=l1weight)
#model = sm.Logit(phenotype_train["Phenotype"], sm.add_constant(pheno_prs_train.iloc[:, 2:])).fit()
except:
continue
train_best_predicted = model.predict(sm.add_constant(pheno_prs_train.iloc[:, 2:]))
test_best_predicted = model.predict(sm.add_constant(pheno_prs_test.iloc[:, 2:]))
from sklearn.metrics import roc_auc_score, confusion_matrix
prs_result = prs_result._append({
"clump_p1": c1_val,
"clump_r2": c2_val,
"clump_kb": c3_val,
"p_window_size": p1_val,
"p_slide_size": p2_val,
"p_LD_threshold": p3_val,
"pvalue": i,
"numberofpca":p,
"tempalpha":str(tempalpha),
"l1weight":str(l1weight),
"ldradius":radius,
"ldfilename":betafile,
"gibsfraction":fraction,
"gibsburn":burn,
"gibsiterations":iterations,
"Train_pure_prs":roc_auc_score(phenotype_train["Phenotype"].values,prs_train['SCORE'].values),
"Train_null_model":roc_auc_score(phenotype_train["Phenotype"].values,train_null_predicted.values),
"Train_best_model":roc_auc_score(phenotype_train["Phenotype"].values,train_best_predicted.values),
"Test_pure_prs":roc_auc_score(phenotype_test["Phenotype"].values,prs_test['SCORE'].values),
"Test_null_model":roc_auc_score(phenotype_test["Phenotype"].values,test_null_predicted.values),
"Test_best_model":roc_auc_score(phenotype_test["Phenotype"].values,test_best_predicted.values),
}, ignore_index=True)
prs_result.to_csv(traindirec+os.sep+Name+os.sep+"Results.csv",index=False)
return
# This function fit the binary model on the PRS.
def fit_continous_phenotype_on_PRS(traindirec, newtrainfilename,p,radius,betafile,fraction,burn,iterations, p1_val, p2_val, p3_val, c1_val, c2_val, c3_val,Name,pvaluefile):
threshold_values = allpvalues
# Merge the covariates, pca and phenotypes.
tempphenotype_train = pd.read_table(traindirec+os.sep+newtrainfilename+".clumped.pruned"+".fam", sep="\s+",header=None)
phenotype_train = pd.DataFrame()
phenotype_train["Phenotype"] = tempphenotype_train[5].values
pcs_train = pd.read_table(traindirec+os.sep+trainfilename+".eigenvec", sep="\s+",header=None, names=["FID", "IID"] + [f"PC{str(i)}" for i in range(1, int(p)+1)])
covariate_train = pd.read_table(traindirec+os.sep+trainfilename+".cov",sep="\s+")
covariate_train.fillna(0, inplace=True)
covariate_train = covariate_train[covariate_train["FID"].isin(pcs_train["FID"].values) & covariate_train["IID"].isin(pcs_train["IID"].values)]
covariate_train['FID'] = covariate_train['FID'].astype(str)
pcs_train['FID'] = pcs_train['FID'].astype(str)
covariate_train['IID'] = covariate_train['IID'].astype(str)
pcs_train['IID'] = pcs_train['IID'].astype(str)
covandpcs_train = pd.merge(covariate_train, pcs_train, on=["FID","IID"])
covandpcs_train.fillna(0, inplace=True)
## Scale the covariates!
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import explained_variance_score
scaler = MinMaxScaler()
normalized_values_train = scaler.fit_transform(covandpcs_train.iloc[:, 2:])
#covandpcs_train.iloc[:, 2:] = normalized_values_test
tempphenotype_test = pd.read_table(traindirec+os.sep+testfilename+".clumped.pruned"+".fam", sep="\s+",header=None)
phenotype_test= pd.DataFrame()
phenotype_test["Phenotype"] = tempphenotype_test[5].values
pcs_test = pd.read_table(traindirec+os.sep+testfilename+".eigenvec", sep="\s+",header=None, names=["FID", "IID"] + [f"PC{str(i)}" for i in range(1, int(p)+1)])
covariate_test = pd.read_table(traindirec+os.sep+testfilename+".cov",sep="\s+")
covariate_test.fillna(0, inplace=True)
covariate_test = covariate_test[covariate_test["FID"].isin(pcs_test["FID"].values) & covariate_test["IID"].isin(pcs_test["IID"].values)]
covariate_test['FID'] = covariate_test['FID'].astype(str)
pcs_test['FID'] = pcs_test['FID'].astype(str)
covariate_test['IID'] = covariate_test['IID'].astype(str)
pcs_test['IID'] = pcs_test['IID'].astype(str)
covandpcs_test = pd.merge(covariate_test, pcs_test, on=["FID","IID"])
covandpcs_test.fillna(0, inplace=True)
normalized_values_test = scaler.transform(covandpcs_test.iloc[:, 2:])
#covandpcs_test.iloc[:, 2:] = normalized_values_test
tempalphas = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
l1weights = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
tempalphas = [0.1]
l1weights = [0.1]
#phenotype_train["Phenotype"] = phenotype_train["Phenotype"].replace({1: 0, 2: 1})
#phenotype_test["Phenotype"] = phenotype_test["Phenotype"].replace({1: 0, 2: 1})
for tempalpha in tempalphas:
for l1weight in l1weights:
try:
#null_model = sm.OLS(phenotype_train["Phenotype"], sm.add_constant(covandpcs_train.iloc[:, 2:])).fit_regularized(alpha=tempalpha, L1_wt=l1weight)
null_model = sm.OLS(phenotype_train["Phenotype"], sm.add_constant(covandpcs_train.iloc[:, 2:])).fit()
#null_model = sm.OLS(phenotype_train["Phenotype"], sm.add_constant(covandpcs_train.iloc[:, 2:])).fit()
except:
print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
continue
train_null_predicted = null_model.predict(sm.add_constant(covandpcs_train.iloc[:, 2:]))
from sklearn.metrics import roc_auc_score, confusion_matrix
from sklearn.metrics import r2_score
test_null_predicted = null_model.predict(sm.add_constant(covandpcs_test.iloc[:, 2:]))
global prs_result
for i in threshold_values:
try:
prs_train = pd.read_table(traindirec+os.sep+Name+os.sep+"train_data.pv_"+f"{i}.profile", sep="\s+", usecols=["FID", "IID", "SCORE"])
except:
continue
prs_train['FID'] = prs_train['FID'].astype(str)
prs_train['IID'] = prs_train['IID'].astype(str)
try:
prs_test = pd.read_table(traindirec+os.sep+Name+os.sep+"test_data.pv_"+f"{i}.profile", sep="\s+", usecols=["FID", "IID", "SCORE"])
except:
continue
prs_test['FID'] = prs_test['FID'].astype(str)
prs_test['IID'] = prs_test['IID'].astype(str)
pheno_prs_train = pd.merge(covandpcs_train, prs_train, on=["FID", "IID"])
pheno_prs_test = pd.merge(covandpcs_test, prs_test, on=["FID", "IID"])
try:
#model = sm.OLS(phenotype_train["Phenotype"], sm.add_constant(pheno_prs_train.iloc[:, 2:])).fit_regularized(alpha=tempalpha, L1_wt=l1weight)
model = sm.OLS(phenotype_train["Phenotype"], sm.add_constant(pheno_prs_train.iloc[:, 2:])).fit()
except:
continue
train_best_predicted = model.predict(sm.add_constant(pheno_prs_train.iloc[:, 2:]))
test_best_predicted = model.predict(sm.add_constant(pheno_prs_test.iloc[:, 2:]))
from sklearn.metrics import roc_auc_score, confusion_matrix
prs_result = prs_result._append({
"clump_p1": c1_val,
"clump_r2": c2_val,
"clump_kb": c3_val,
"p_window_size": p1_val,
"p_slide_size": p2_val,
"p_LD_threshold": p3_val,
"pvalue": i,
"numberofpca":p,
"tempalpha":str(tempalpha),
"l1weight":str(l1weight),
"ldradius":radius,
"ldfilename":betafile,
"gibsfraction":fraction,
"gibsburn":burn,
"gibsiterations":iterations,
"Train_pure_prs":explained_variance_score(phenotype_train["Phenotype"],prs_train['SCORE'].values),
"Train_null_model":explained_variance_score(phenotype_train["Phenotype"],train_null_predicted),
"Train_best_model":explained_variance_score(phenotype_train["Phenotype"],train_best_predicted),
"Test_pure_prs":explained_variance_score(phenotype_test["Phenotype"],prs_test['SCORE'].values),
"Test_null_model":explained_variance_score(phenotype_test["Phenotype"],test_null_predicted),
"Test_best_model":explained_variance_score(phenotype_test["Phenotype"],test_best_predicted),
}, ignore_index=True)
prs_result.to_csv(traindirec+os.sep+Name+os.sep+"Results.csv",index=False)
return
Execute LDpred-gibbs#
# Define a global variable to store results
prs_result = pd.DataFrame()
def transform_plink_data(traindirec, newtrainfilename,p,radius,ldpredmodel,fraction,burn,iterations, p1_val, p2_val, p3_val, c1_val, c2_val, c3_val,Name,pvaluefile):
### First perform clumping on the file and save the clumpled file.
#perform_clumping_and_pruning_on_individual_data(traindirec, newtrainfilename,p, p1_val, p2_val, p3_val, c1_val, c2_val, c3_val,Name,pvaluefile)
#newtrainfilename = newtrainfilename+".clumped.pruned"
#testfilename = testfilename+".clumped.pruned"
#clupmedfile = traindirec+os.sep+newtrainfilename+".clump"
#prunedfile = traindirec+os.sep+newtrainfilename+".clumped.pruned"
# Also extract the PCA at this point for both test and training data.
#calculate_pca_for_traindata_testdata_for_clumped_pruned_snps(traindirec, newtrainfilename,p)
#Extract p-values from the GWAS file.
# Command for Linux.
os.system("awk "+"\'"+"{print $3,$8}"+"\'"+" ./"+filedirec+os.sep+filedirec+".txt > ./"+traindirec+os.sep+"SNP.pvalue")
# Command for windows.
### For windows get GWAK.
### https://sourceforge.net/projects/gnuwin32/
##3 Get it and place it in the same direc.
#os.system("gawk "+"\""+"{print $3,$8}"+"\""+" ./"+filedirec+os.sep+filedirec+".txt > ./"+traindirec+os.sep+"SNP.pvalue")
#print("gawk "+"\""+"{print $3,$8}"+"\""+" ./"+filedirec+os.sep+filedirec+".txt > ./"+traindirec+os.sep+"SNP.pvalue")
#exit(0)
# Delete files generated in the previous iteration.
files_to_remove = [
traindirec+os.sep+"LDpred_gibbs_gwas",
]
# Loop through the files and directories and remove them if they exist
for file_path in files_to_remove:
if os.path.exists(file_path):
if os.path.isfile(file_path):
os.remove(file_path)
print(f"Removed file: {file_path}")
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
print(f"Removed directory: {file_path}")
else:
print(f"File or directory does not exist: {file_path}")
output_file = os.path.join(traindirec, "output_file.h5")
# Check if the file exists and remove it
if os.path.exists(output_file):
os.remove(output_file)
print(f"Removed existing file: {output_file}")
import glob
# Use glob to find all files starting with 'ld.h5_LDpred_' in the specified directory
file_pattern = os.path.join(traindirec, 'ld.h5_LDpred_*')
file_list = glob.glob(file_pattern)
file_list.append(traindirec+os.sep+'ld.h5_LDpred-inf.txt')
for file_path in file_list:
if os.path.exists(file_path):
os.remove(file_path)
print(f"Removed: {file_path}")
else:
print(f"File not found: {file_path}")
import glob
# Use glob to find all files starting with 'ld.h5_LDpred_' in the specified directory
file_pattern = os.path.join(traindirec, 'inf_*')
file_list = glob.glob(file_pattern)
for file_path in file_list:
if os.path.exists(file_path):
os.remove(file_path)
print(f"Removed: {file_path}")
else:
print(f"File not found: {file_path}")
if check_phenotype_is_binary_or_continous(filedirec)=="Binary":
eff_type = "OR"
eff = "OR"
else:
eff_type = "LOGOR"
eff = "BETA"
gwas_file = filedirec + os.sep +filedirec+"_ldpredgibs.txt"
bim_file = traindirec + os.sep + newtrainfilename+".clumped.pruned.bim"
# Read the files
df = pd.read_csv(gwas_file, sep="\s+" )
bim = pd.read_csv(bim_file, delim_whitespace=True, header=None)
print(len(df))
print(len(bim))
# Create a 'match' column to find common SNPs
bim['match'] = bim[0].astype(str) + "_" + bim[3].astype(str) + "_" + bim[4].astype(str) + "_" + bim[5].astype(str)
df['match'] = df['CHR'].astype(str) + "_" + df['POS'].astype(str) + "_" + df['REF'].astype(str) + "_" + df['ALT'].astype(str)
# Drop duplicates based on the 'match' column
df.drop_duplicates(subset='match', inplace=True)
bim.drop_duplicates(subset='match', inplace=True)
# Filter dataframes to keep only matching SNPs
df = df[df['match'].isin(bim['match'].values)]
bim = bim[bim['match'].isin(df['match'].values)]
print(len(bim))
print(len(df))
print(bim.head())
print(df.head())
del df["match"]
del bim["match"]
df.to_csv(traindirec+os.sep+filedirec+".ldpred",sep="\t",index=None)
bim.to_csv(traindirec + os.sep + "commonsnps.txt",sep="\t",index=None)
command = [
'./plink',
'--bfile', traindirec+os.sep+newtrainfilename,
'--extract', traindirec + os.sep + "commonsnps.txt",
'--make-bed',
'--chr','1-22',
'--out', traindirec+os.sep+newtrainfilename+".clumped.pruned"
]
subprocess.run(command)
command = [
"ldpred", "coord",
"--gf", traindirec+os.sep+newtrainfilename+".clumped.pruned",
"--ssf", traindirec+os.sep+filedirec+".ldpred",
"--out", traindirec+os.sep+"output_file.h5",
"--N", str(int(N)),
"--eff_type", eff_type,
"--maf", "0.01",
#"--ssf-format", "STANDARD",
"--rs", "SNP_ID",
"--A1", "REF",
"--A2", "ALT",
"--pos", "POS",
#"--info", "INFO",
"--chr", "CHR",
"--pval", "PVAL",
"--eff", eff,
#"--se", "SE"
#"--ncol", "5",
#"--case-freq", "0.2",
#"--control-freq", "0.3",
#"--case-n", "5000",
#"--control-n", "5000"
]
print(" ".join(command))
subprocess.run(command)
if ldpredmodel =="gibbs":
command = [
'ldpred', 'gibbs',
'--cf', traindirec+os.sep+"output_file.h5",
'--ldr', str(radius),
'--n-burn-in',str(burn),
'--n-iter',str(iterations),
'--f',str(fraction),
'--ldf', traindirec+os.sep+'inf_',
'--out', traindirec+os.sep+"ld.h5",
]
subprocess.run(command)
import glob
# Use glob to find all files starting with 'ld.h5_LDpred_' in the specified directory
file_pattern = os.path.join(traindirec, 'ld.h5_LDpred_*')
file_list = glob.glob(file_pattern)
#file_list.append(traindirec+os.sep+'ld.h5_LDpred-inf.txt')
# Initialize a list to store dataframes
dataframes = []
# Iterate over the list of files and read them
for betafile in file_list:
temp = pd.read_csv(betafile,sep="\s+" )
if check_phenotype_is_binary_or_continous(filedirec)=="Binary":
if len(temp)<2:
continue
try:
temp['ldpred_beta'] = np.exp(temp['ldpred_beta'])
except:
try:
temp['ldpred_inf_beta'] = np.exp(temp['ldpred_inf_beta'])
except:
print("CHECK OUTPUT FILE.!")
else:
pass
temp.iloc[:,[2,3,6]].to_csv(traindirec+os.sep+"LDpred_gibbs_gwas",sep="\t",index=False)
command = [
"./plink",
"--bfile", traindirec+os.sep+newtrainfilename+".clumped.pruned",
### SNP column = 3, Effect allele column 1 = 4, OR column=9
"--score", traindirec+os.sep+"LDpred_gibbs_gwas", "1", "2", "3", "header",
"--q-score-range", traindirec+os.sep+"range_list",traindirec+os.sep+"SNP.pvalue",
"--extract", traindirec+os.sep+trainfilename+".valid.snp",
"--out", traindirec+os.sep+Name+os.sep+trainfilename
]
#exit(0)
subprocess.run(command)
command = [
"./plink",
"--bfile", folddirec+os.sep+testfilename+".clumped.pruned",
### SNP column = 3, Effect allele column 1 = 4, Beta column=12
"--score", traindirec+os.sep+"LDpred_gibbs_gwas", "1", "2", "3", "header",
"--q-score-range", traindirec+os.sep+"range_list",traindirec+os.sep+"SNP.pvalue",
"--extract", traindirec+os.sep+trainfilename+".valid.snp",
"--out", folddirec+os.sep+Name+os.sep+testfilename
]
subprocess.run(command)
# At this stage the scores are finalizied.
# The next step is to fit the model and find the explained variance by each profile.
# Load the PCA and Load the Covariates for trainingdatafirst.
if check_phenotype_is_binary_or_continous(filedirec)=="Binary":
print("Binary Phenotype!")
fit_binary_phenotype_on_PRS(traindirec, newtrainfilename,p,radius,os.path.basename(betafile), fraction,burn,iterations, p1_val, p2_val, p3_val, c1_val, c2_val, c3_val,Name,pvaluefile)
else:
print("Continous Phenotype!")
fit_continous_phenotype_on_PRS(traindirec, newtrainfilename,p,radius,os.path.basename(betafile),fraction,burn,iterations, p1_val, p2_val, p3_val, c1_val, c2_val, c3_val,Name,pvaluefile)
gibssamplerfractions = [0.1]
gibssamplerburn = [5]
gibssampleriterations = [6]
ldradius = [4]
ldpredmodels = ['gibbs']
result_directory = "LDpred-gibbs"
# Nested loops to iterate over different parameter values
create_directory(folddirec+os.sep+result_directory)
for p1_val in p_window_size:
for p2_val in p_slide_size:
for p3_val in p_LD_threshold:
for c1_val in clump_p1:
for c2_val in clump_r2:
for c3_val in clump_kb:
for p in numberofpca:
for radius in ldradius:
for ldpredmodel in ldpredmodels:
for fraction in gibssamplerfractions:
for burn in gibssamplerburn:
for iterations in gibssampleriterations:
transform_plink_data(folddirec, newtrainfilename, p,radius,ldpredmodel,fraction,burn,iterations, str(p1_val), str(p2_val), str(p3_val), str(c1_val), str(c2_val), str(c3_val), result_directory, pvaluefile)
File or directory does not exist: SampleData1/Fold_0/LDpred_gibbs_gwas
Removed existing file: SampleData1/Fold_0/output_file.h5
Removed: SampleData1/Fold_0/ld.h5_LDpred-inf.txt
Removed: SampleData1/Fold_0/inf__ldradius4.pkl.gz
/tmp/ipykernel_675661/2717629761.py:94: FutureWarning: The 'delim_whitespace' keyword in pd.read_csv is deprecated and will be removed in a future version. Use ``sep='\s+'`` instead
bim = pd.read_csv(bim_file, delim_whitespace=True, header=None)
499617
171216
171216
171216
0 1 2 3 4 5 match
0 1 rs79373928 0.587220 801536 G T 1_801536_G_T
1 1 rs4970382 0.620827 840753 C T 1_840753_C_T
2 1 rs13303222 0.620827 849998 A G 1_849998_A_G
3 1 rs72631889 0.620827 851390 T G 1_851390_T_G
4 1 rs192998324 0.620827 862772 G A 1_862772_G_A
CHR POS SNP_ID REF ALT REF_FRQ PVAL BETA SE \
3 1 801536 rs79373928 G T 0.483212 0.808999 0.002034 0.008396
8 1 840753 rs4970382 C T 0.498936 0.199967 -0.002658 0.002079
9 1 849998 rs13303222 A G 0.435377 0.221234 0.003237 0.002638
11 1 851390 rs72631889 T G 0.432960 0.009708 0.013190 0.005034
14 1 862772 rs192998324 G A 0.417471 0.054822 -0.011314 0.005959
N match
3 388028 1_801536_G_T
8 388028 1_840753_C_T
9 388028 1_849998_A_G
11 388028 1_851390_T_G
14 388028 1_862772_G_A
PLINK v1.90b7.2 64-bit (11 Dec 2023) www.cog-genomics.org/plink/1.9/
(C) 2005-2023 Shaun Purcell, Christopher Chang GNU General Public License v3
Logging to SampleData1/Fold_0/train_data.QC.clumped.pruned.log.
Options in effect:
--bfile SampleData1/Fold_0/train_data.QC
--chr 1-22
--extract SampleData1/Fold_0/commonsnps.txt
--make-bed
--out SampleData1/Fold_0/train_data.QC.clumped.pruned
63761 MB RAM detected; reserving 31880 MB for main workspace.
491952 variants loaded from .bim file.
380 people (183 males, 197 females) loaded from .fam.
380 phenotype values loaded from .fam.
--extract: 171216 variants remaining.
Using 1 thread (no multithreaded calculations invoked).
Before main variant filters, 380 founders and 0 nonfounders present.
Calculating allele frequencies... 10111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989 done.
Total genotyping rate is 0.999891.
171216 variants and 380 people pass filters and QC.
Phenotype data is quantitative.
--make-bed to SampleData1/Fold_0/train_data.QC.clumped.pruned.bed +
SampleData1/Fold_0/train_data.QC.clumped.pruned.bim +
SampleData1/Fold_0/train_data.QC.clumped.pruned.fam ... 101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899done.
ldpred coord --gf SampleData1/Fold_0/train_data.QC.clumped.pruned --ssf SampleData1/Fold_0/SampleData1.ldpred --out SampleData1/Fold_0/output_file.h5 --N 388028 --eff_type LOGOR --maf 0.01 --rs SNP_ID --A1 REF --A2 ALT --pos POS --chr CHR --pval PVAL --eff BETA
=============================== LDpred v. 1.0.11 ===============================
Parsing summary statistics file: SampleData1/Fold_0/SampleData1.ldpred
100.00%
SS file loaded, now sorting and storing in HDF5 file.
Coordinating datasets (Summary statistics and LD reference genotypes).
100.00%
{'ldpred_action': 'coord', 'debug': False, 'gf': 'SampleData1/Fold_0/train_data.QC.clumped.pruned', 'ssf': 'SampleData1/Fold_0/SampleData1.ldpred', 'N': 388028, 'out': 'SampleData1/Fold_0/output_file.h5', 'vbim': None, 'vgf': None, 'only_hm3': False, 'ilist': None, 'skip_coordination': False, 'eff_type': 'LOGOR', 'match_genomic_pos': False, 'maf': 0.01, 'max_freq_discrep': 0.1, 'ssf_format': 'CUSTOM', 'rs': 'SNP_ID', 'A1': 'REF', 'A2': 'ALT', 'pos': 'POS', 'info': 'INFO', 'chr': 'CHR', 'reffreq': 'MAF', 'pval': 'PVAL', 'eff': 'BETA', 'se': 'SE', 'ncol': 'N', 'case_freq': None, 'control_freq': None, 'case_n': None, 'control_n': None, 'z_from_se': False}
========================= Summary of coordination step =========================
Summary statistics filename:
SampleData1/Fold_0/SampleData1.ldpred
LD reference genotypes filename:
SampleData1/Fold_0/train_data.QC.clumped.pruned
Coordinated data output filename:
SampleData1/Fold_0/output_file.h5
------------------------------ Summary statistics ------------------------------
Num SNPs parsed from sum stats file 171216
--------------------------------- Coordination ---------------------------------
Num individuals in LD Reference data: 380
SNPs in LD Reference data: 171216
Num chromosomes used: 22
SNPs common across datasets: 171216
SNPs retained after filtering: 171216
SNPs w MAF<0.010 filtered: 0
SNPs w allele freq discrepancy > 0.100 filtered: 0
-------------------------------- Running times ---------------------------------
Run time for parsing summary stats: 0 min and 48.67 sec
Run time for coordinating datasets: 0 min and 13.31 sec
================================================================================
=============================== LDpred v. 1.0.11 ===============================
Calculating LD information w. radius 4
Storing LD information to compressed pickle file
Applying LDpred with LD radius: 4
171216 SNP effects were found
Warning: LD radius seems small in comparison to the average LD score. Please consider a larger one, or a smaller number of SNPs used in the analysis.
Calculating LDpred-inf weights
Starting LDpred gibbs with f=0.1000
100.00%
=========================== Summary of LDpred Gibbs ============================
Coordinated data filename
SampleData1/Fold_0/output_file.h5
SNP weights output file (prefix) SampleData1/Fold_0/ld.h5
LD data filename (prefix) SampleData1/Fold_0/inf_
-------------------------------- LD information --------------------------------
LD radius used 4
Average LD score: 1.069989724346003
Genome-wide (LDscore) estimated heritability: 0.9620
Chi-square lambda (inflation statistic). 3.3327
Running time for calculating LD information: 0 min and 8.82 secs
SNPs in long-range LD regions 0
----------------------------- LDpred Gibbs sampler -----------------------------
Gibbs sampler fractions used [0.1]
Number of burn-iterations used 5
Number of iterations used 6
Convergence issues (for each fraction) ['No']
Running time for Gibbs sampler(s): 0 min and 27.82 secs
================================================================================
PLINK v1.90b7.2 64-bit (11 Dec 2023) www.cog-genomics.org/plink/1.9/
(C) 2005-2023 Shaun Purcell, Christopher Chang GNU General Public License v3
Logging to SampleData1/Fold_0/LDpred-gibbs/train_data.log.
Options in effect:
--bfile SampleData1/Fold_0/train_data.QC.clumped.pruned
--extract SampleData1/Fold_0/train_data.valid.snp
--out SampleData1/Fold_0/LDpred-gibbs/train_data
--q-score-range SampleData1/Fold_0/range_list SampleData1/Fold_0/SNP.pvalue
--score SampleData1/Fold_0/LDpred_gibbs_gwas 1 2 3 header
63761 MB RAM detected; reserving 31880 MB for main workspace.
171216 variants loaded from .bim file.
380 people (183 males, 197 females) loaded from .fam.
380 phenotype values loaded from .fam.
--extract: 171216 variants remaining.
Using 1 thread (no multithreaded calculations invoked).
Before main variant filters, 380 founders and 0 nonfounders present.
Calculating allele frequencies... 10111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989 done.
Total genotyping rate is 0.999891.
171216 variants and 380 people pass filters and QC.
Phenotype data is quantitative.
--score: 171216 valid predictors loaded.
Warning: 328402 lines skipped in --q-score-range data file.
--score: 20 ranges processed.
Results written to SampleData1/Fold_0/LDpred-gibbs/train_data.*.profile.
PLINK v1.90b7.2 64-bit (11 Dec 2023) www.cog-genomics.org/plink/1.9/
(C) 2005-2023 Shaun Purcell, Christopher Chang GNU General Public License v3
Logging to SampleData1/Fold_0/LDpred-gibbs/test_data.log.
Options in effect:
--bfile SampleData1/Fold_0/test_data.clumped.pruned
--extract SampleData1/Fold_0/train_data.valid.snp
--out SampleData1/Fold_0/LDpred-gibbs/test_data
--q-score-range SampleData1/Fold_0/range_list SampleData1/Fold_0/SNP.pvalue
--score SampleData1/Fold_0/LDpred_gibbs_gwas 1 2 3 header
63761 MB RAM detected; reserving 31880 MB for main workspace.
172878 variants loaded from .bim file.
95 people (44 males, 51 females) loaded from .fam.
95 phenotype values loaded from .fam.
--extract: 172878 variants remaining.
Using 1 thread (no multithreaded calculations invoked).
Before main variant filters, 95 founders and 0 nonfounders present.
Calculating allele frequencies... 0%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% done.
Total genotyping rate is 0.999891.
172878 variants and 95 people pass filters and QC.
Phenotype data is quantitative.
--score: 171216 valid predictors loaded.
Warning: 328402 lines skipped in --q-score-range data file.
--score: 20 ranges processed.
Results written to SampleData1/Fold_0/LDpred-gibbs/test_data.*.profile.
Continous Phenotype!
PLINK v1.90b7.2 64-bit (11 Dec 2023) www.cog-genomics.org/plink/1.9/
(C) 2005-2023 Shaun Purcell, Christopher Chang GNU General Public License v3
Logging to SampleData1/Fold_0/LDpred-gibbs/train_data.log.
Options in effect:
--bfile SampleData1/Fold_0/train_data.QC.clumped.pruned
--extract SampleData1/Fold_0/train_data.valid.snp
--out SampleData1/Fold_0/LDpred-gibbs/train_data
--q-score-range SampleData1/Fold_0/range_list SampleData1/Fold_0/SNP.pvalue
--score SampleData1/Fold_0/LDpred_gibbs_gwas 1 2 3 header
63761 MB RAM detected; reserving 31880 MB for main workspace.
171216 variants loaded from .bim file.
380 people (183 males, 197 females) loaded from .fam.
380 phenotype values loaded from .fam.
--extract: 171216 variants remaining.
Using 1 thread (no multithreaded calculations invoked).
Before main variant filters, 380 founders and 0 nonfounders present.
Calculating allele frequencies... 10111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989 done.
Total genotyping rate is 0.999891.
171216 variants and 380 people pass filters and QC.
Phenotype data is quantitative.
--score: 171216 valid predictors loaded.
Warning: 328402 lines skipped in --q-score-range data file.
--score: 20 ranges processed.
Results written to SampleData1/Fold_0/LDpred-gibbs/train_data.*.profile.
PLINK v1.90b7.2 64-bit (11 Dec 2023) www.cog-genomics.org/plink/1.9/
(C) 2005-2023 Shaun Purcell, Christopher Chang GNU General Public License v3
Logging to SampleData1/Fold_0/LDpred-gibbs/test_data.log.
Options in effect:
--bfile SampleData1/Fold_0/test_data.clumped.pruned
--extract SampleData1/Fold_0/train_data.valid.snp
--out SampleData1/Fold_0/LDpred-gibbs/test_data
--q-score-range SampleData1/Fold_0/range_list SampleData1/Fold_0/SNP.pvalue
--score SampleData1/Fold_0/LDpred_gibbs_gwas 1 2 3 header
63761 MB RAM detected; reserving 31880 MB for main workspace.
172878 variants loaded from .bim file.
95 people (44 males, 51 females) loaded from .fam.
95 phenotype values loaded from .fam.
--extract: 172878 variants remaining.
Using 1 thread (no multithreaded calculations invoked).
Before main variant filters, 95 founders and 0 nonfounders present.
Calculating allele frequencies... 0%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% done.
Total genotyping rate is 0.999891.
172878 variants and 95 people pass filters and QC.
Phenotype data is quantitative.
--score: 171216 valid predictors loaded.
Warning: 328402 lines skipped in --q-score-range data file.
--score: 20 ranges processed.
Results written to SampleData1/Fold_0/LDpred-gibbs/test_data.*.profile.
Continous Phenotype!
Repeat the process for each fold.#
Change the foldnumber
variable.
#foldnumber = sys.argv[1]
foldnumber = "0" # Setting 'foldnumber' to "0"
Or uncomment the following line:
# foldnumber = sys.argv[1]
python LDpred-gibbs.py 0
python LDpred-gibbs.py 1
python LDpred-gibbs.py 2
python LDpred-gibbs.py 3
python LDpred-gibbs.py 4
The following files should exist after the execution:
SampleData1/Fold_0/LDpred-gibbs/Results.csv
SampleData1/Fold_1/LDpred-gibbs/Results.csv
SampleData1/Fold_2/LDpred-gibbs/Results.csv
SampleData1/Fold_3/LDpred-gibbs/Results.csv
SampleData1/Fold_4/LDpred-gibbs/Results.csv
Check the results file for each fold.#
import os
# List of file names to check for existence
f = [
"./"+filedirec+"/Fold_0"+os.sep+result_directory+"Results.csv",
"./"+filedirec+"/Fold_1"+os.sep+result_directory+"Results.csv",
"./"+filedirec+"/Fold_2"+os.sep+result_directory+"Results.csv",
"./"+filedirec+"/Fold_3"+os.sep+result_directory+"Results.csv",
"./"+filedirec+"/Fold_4"+os.sep+result_directory+"Results.csv",
]
# Loop through each file name in the list
for loop in range(0,5):
# Check if the file exists in the specified directory for the given fold
if os.path.exists(filedirec+os.sep+"Fold_"+str(loop)+os.sep+result_directory+os.sep+"Results.csv"):
temp = pd.read_csv(filedirec+os.sep+"Fold_"+str(loop)+os.sep+result_directory+os.sep+"Results.csv")
print("Fold_",loop, "Yes, the file exists.")
#print(temp.head())
print("Number of P-values processed: ",len(temp))
# Print a message indicating that the file exists
else:
# Print a message indicating that the file does not exist
print("Fold_",loop, "No, the file does not exist.")
Fold_ 0 Yes, the file exists.
Number of P-values processed: 20
Fold_ 1 Yes, the file exists.
Number of P-values processed: 20
Fold_ 2 No, the file does not exist.
Fold_ 3 No, the file does not exist.
Fold_ 4 No, the file does not exist.
Sum the results for each fold.#
print("We have to ensure when we sum the entries across all Folds, the same rows are merged!")
def sum_and_average_columns(data_frames):
"""Sum and average numerical columns across multiple DataFrames, and keep non-numerical columns unchanged."""
# Initialize DataFrame to store the summed results for numerical columns
summed_df = pd.DataFrame()
non_numerical_df = pd.DataFrame()
for df in data_frames:
# Identify numerical and non-numerical columns
numerical_cols = df.select_dtypes(include=[np.number]).columns
non_numerical_cols = df.select_dtypes(exclude=[np.number]).columns
# Sum numerical columns
if summed_df.empty:
summed_df = pd.DataFrame(0, index=range(len(df)), columns=numerical_cols)
summed_df[numerical_cols] = summed_df[numerical_cols].add(df[numerical_cols], fill_value=0)
# Keep non-numerical columns (take the first non-numerical entry for each column)
if non_numerical_df.empty:
non_numerical_df = df[non_numerical_cols]
else:
non_numerical_df[non_numerical_cols] = non_numerical_df[non_numerical_cols].combine_first(df[non_numerical_cols])
# Divide the summed values by the number of dataframes to get the average
averaged_df = summed_df / len(data_frames)
# Combine numerical and non-numerical DataFrames
result_df = pd.concat([averaged_df, non_numerical_df], axis=1)
return result_df
from functools import reduce
import os
import pandas as pd
from functools import reduce
def find_common_rows(allfoldsframe):
# Define the performance columns that need to be excluded
performance_columns = [
'Train_null_model', 'Train_pure_prs', 'Train_best_model',
'Test_pure_prs', 'Test_null_model', 'Test_best_model'
]
important_columns = [
'clump_p1',
'clump_r2',
'clump_kb',
'p_window_size',
'p_slide_size',
'p_LD_threshold',
'pvalue',
"ldradius",
"ldfilename",
"gibsfraction",
"gibsburn",
"gibsiterations",
'numberofpca',
'tempalpha',
'l1weight',
]
# Function to remove performance columns from a DataFrame
def drop_performance_columns(df):
return df.drop(columns=performance_columns, errors='ignore')
def get_important_columns(df ):
existing_columns = [col for col in important_columns if col in df.columns]
if existing_columns:
return df[existing_columns].copy()
else:
return pd.DataFrame()
# Drop performance columns from all DataFrames in the list
allfoldsframe_dropped = [drop_performance_columns(df) for df in allfoldsframe]
# Get the important columns.
allfoldsframe_dropped = [get_important_columns(df) for df in allfoldsframe_dropped]
# Iteratively find common rows and track unique and common rows
common_rows = allfoldsframe_dropped[0]
for i in range(1, len(allfoldsframe_dropped)):
# Get the next DataFrame
next_df = allfoldsframe_dropped[i]
# Count unique rows in the current DataFrame and the next DataFrame
unique_in_common = common_rows.shape[0]
unique_in_next = next_df.shape[0]
# Find common rows between the current common_rows and the next DataFrame
common_rows = pd.merge(common_rows, next_df, how='inner')
# Count the common rows after merging
common_count = common_rows.shape[0]
# Print the unique and common row counts
print(f"Iteration {i}:")
print(f"Unique rows in current common DataFrame: {unique_in_common}")
print(f"Unique rows in next DataFrame: {unique_in_next}")
print(f"Common rows after merge: {common_count}\n")
# Now that we have the common rows, extract these from the original DataFrames
extracted_common_rows_frames = []
for original_df in allfoldsframe:
# Merge the common rows with the original DataFrame, keeping only the rows that match the common rows
extracted_common_rows = pd.merge(common_rows, original_df, how='inner', on=common_rows.columns.tolist())
# Add the DataFrame with the extracted common rows to the list
extracted_common_rows_frames.append(extracted_common_rows)
# Print the number of rows in the common DataFrames
for i, df in enumerate(extracted_common_rows_frames):
print(f"DataFrame {i + 1} with extracted common rows has {df.shape[0]} rows.")
# Return the list of DataFrames with extracted common rows
return extracted_common_rows_frames
# Example usage (assuming allfoldsframe is populated as shown earlier):
allfoldsframe = []
# Loop through each file name in the list
for loop in range(0, 5):
# Check if the file exists in the specified directory for the given fold
file_path = os.path.join(filedirec, "Fold_" + str(loop), result_directory, "Results.csv")
if os.path.exists(file_path):
allfoldsframe.append(pd.read_csv(file_path))
# Print a message indicating that the file exists
print("Fold_", loop, "Yes, the file exists.")
else:
# Print a message indicating that the file does not exist
print("Fold_", loop, "No, the file does not exist.")
# Find the common rows across all folds and return the list of extracted common rows
extracted_common_rows_list = find_common_rows(allfoldsframe)
# Sum the values column-wise
# For string values, do not sum it the values are going to be the same for each fold.
# Only sum the numeric values.
divided_result = sum_and_average_columns(extracted_common_rows_list)
print(divided_result)
We have to ensure when we sum the entries across all Folds, the same rows are merged!
Fold_ 0 Yes, the file exists.
Fold_ 1 Yes, the file exists.
Fold_ 2 No, the file does not exist.
Fold_ 3 No, the file does not exist.
Fold_ 4 No, the file does not exist.
Iteration 1:
Unique rows in current common DataFrame: 20
Unique rows in next DataFrame: 20
Common rows after merge: 20
DataFrame 1 with extracted common rows has 20 rows.
DataFrame 2 with extracted common rows has 20 rows.
clump_p1 clump_r2 clump_kb p_window_size p_slide_size p_LD_threshold \
0 1.0 0.1 200.0 200.0 50.0 0.25
1 1.0 0.1 200.0 200.0 50.0 0.25
2 1.0 0.1 200.0 200.0 50.0 0.25
3 1.0 0.1 200.0 200.0 50.0 0.25
4 1.0 0.1 200.0 200.0 50.0 0.25
5 1.0 0.1 200.0 200.0 50.0 0.25
6 1.0 0.1 200.0 200.0 50.0 0.25
7 1.0 0.1 200.0 200.0 50.0 0.25
8 1.0 0.1 200.0 200.0 50.0 0.25
9 1.0 0.1 200.0 200.0 50.0 0.25
10 1.0 0.1 200.0 200.0 50.0 0.25
11 1.0 0.1 200.0 200.0 50.0 0.25
12 1.0 0.1 200.0 200.0 50.0 0.25
13 1.0 0.1 200.0 200.0 50.0 0.25
14 1.0 0.1 200.0 200.0 50.0 0.25
15 1.0 0.1 200.0 200.0 50.0 0.25
16 1.0 0.1 200.0 200.0 50.0 0.25
17 1.0 0.1 200.0 200.0 50.0 0.25
18 1.0 0.1 200.0 200.0 50.0 0.25
19 1.0 0.1 200.0 200.0 50.0 0.25
pvalue ldradius gibsfraction gibsburn ... numberofpca tempalpha \
0 0.000100 4.0 0.1 5.0 ... 6.0 0.1
1 0.000278 4.0 0.1 5.0 ... 6.0 0.1
2 0.000774 4.0 0.1 5.0 ... 6.0 0.1
3 0.002154 4.0 0.1 5.0 ... 6.0 0.1
4 0.005995 4.0 0.1 5.0 ... 6.0 0.1
5 0.016681 4.0 0.1 5.0 ... 6.0 0.1
6 0.046416 4.0 0.1 5.0 ... 6.0 0.1
7 0.129155 4.0 0.1 5.0 ... 6.0 0.1
8 0.359381 4.0 0.1 5.0 ... 6.0 0.1
9 1.000000 4.0 0.1 5.0 ... 6.0 0.1
10 0.000100 4.0 0.1 5.0 ... 6.0 0.1
11 0.000278 4.0 0.1 5.0 ... 6.0 0.1
12 0.000774 4.0 0.1 5.0 ... 6.0 0.1
13 0.002154 4.0 0.1 5.0 ... 6.0 0.1
14 0.005995 4.0 0.1 5.0 ... 6.0 0.1
15 0.016681 4.0 0.1 5.0 ... 6.0 0.1
16 0.046416 4.0 0.1 5.0 ... 6.0 0.1
17 0.129155 4.0 0.1 5.0 ... 6.0 0.1
18 0.359381 4.0 0.1 5.0 ... 6.0 0.1
19 1.000000 4.0 0.1 5.0 ... 6.0 0.1
l1weight Train_pure_prs Train_null_model Train_best_model \
0 0.1 -7.450466e-08 0.242738 0.243085
1 0.1 2.851397e-07 0.242738 0.243968
2 0.1 1.177535e-07 0.242738 0.242757
3 0.1 -1.286063e-07 0.242738 0.245954
4 0.1 -2.794755e-07 0.242738 0.246167
5 0.1 -4.557383e-06 0.242738 0.278256
6 0.1 -5.100398e-06 0.242738 0.283107
7 0.1 -5.079328e-06 0.242738 0.282042
8 0.1 -5.079328e-06 0.242738 0.282042
9 0.1 -5.079328e-06 0.242738 0.282042
10 0.1 2.720242e-06 0.242738 0.243818
11 0.1 -3.903542e-07 0.242738 0.245352
12 0.1 -1.991665e-07 0.242738 0.243687
13 0.1 -1.055668e-06 0.242738 0.244894
14 0.1 -1.526119e-06 0.242738 0.250728
15 0.1 -5.273897e-06 0.242738 0.282494
16 0.1 -5.749931e-06 0.242738 0.286876
17 0.1 -5.722206e-06 0.242738 0.285966
18 0.1 -5.722206e-06 0.242738 0.285966
19 0.1 -5.722206e-06 0.242738 0.285966
Test_pure_prs Test_null_model Test_best_model \
0 -1.974469e-06 0.154698 0.157339
1 1.009131e-06 0.154698 0.154655
2 2.060301e-07 0.154698 0.153824
3 1.432619e-07 0.154698 0.145029
4 1.107627e-08 0.154698 0.155966
5 -6.513731e-06 0.154698 0.211963
6 -7.232676e-06 0.154698 0.223564
7 -7.715996e-06 0.154698 0.223270
8 -7.715996e-06 0.154698 0.223270
9 -7.715996e-06 0.154698 0.223270
10 -3.013002e-06 0.154698 0.151992
11 1.872009e-06 0.154698 0.137942
12 -4.819821e-07 0.154698 0.156432
13 -2.533759e-06 0.154698 0.156209
14 -1.379211e-06 0.154698 0.165421
15 -8.395581e-06 0.154698 0.225726
16 -8.959826e-06 0.154698 0.234608
17 -9.521418e-06 0.154698 0.234811
18 -9.521418e-06 0.154698 0.234811
19 -9.521418e-06 0.154698 0.234811
ldfilename
0 ld.h5_LDpred_p1.0000e-01.txt
1 ld.h5_LDpred_p1.0000e-01.txt
2 ld.h5_LDpred_p1.0000e-01.txt
3 ld.h5_LDpred_p1.0000e-01.txt
4 ld.h5_LDpred_p1.0000e-01.txt
5 ld.h5_LDpred_p1.0000e-01.txt
6 ld.h5_LDpred_p1.0000e-01.txt
7 ld.h5_LDpred_p1.0000e-01.txt
8 ld.h5_LDpred_p1.0000e-01.txt
9 ld.h5_LDpred_p1.0000e-01.txt
10 ld.h5_LDpred-inf.txt
11 ld.h5_LDpred-inf.txt
12 ld.h5_LDpred-inf.txt
13 ld.h5_LDpred-inf.txt
14 ld.h5_LDpred-inf.txt
15 ld.h5_LDpred-inf.txt
16 ld.h5_LDpred-inf.txt
17 ld.h5_LDpred-inf.txt
18 ld.h5_LDpred-inf.txt
19 ld.h5_LDpred-inf.txt
[20 rows x 21 columns]
/tmp/ipykernel_424586/2745022655.py:24: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
non_numerical_df[non_numerical_cols] = non_numerical_df[non_numerical_cols].combine_first(df[non_numerical_cols])
Results#
1. Reporting Based on Best Training Performance:#
One can report the results based on the best performance of the training data. For example, if for a specific combination of hyperparameters, the training performance is high, report the corresponding test performance.
Example code:
df = divided_result.sort_values(by='Train_best_model', ascending=False) print(df.iloc[0].to_markdown())
Binary Phenotypes Result Analysis#
You can find the performance quality for binary phenotype using the following template:
This figure shows the 8 different scenarios that can exist in the results, and the following table explains each scenario.
We classified performance based on the following table:
Performance Level |
Range |
---|---|
Low Performance |
0 to 0.5 |
Moderate Performance |
0.6 to 0.7 |
High Performance |
0.8 to 1 |
You can match the performance based on the following scenarios:
Scenario |
What’s Happening |
Implication |
---|---|---|
High Test, High Train |
The model performs well on both training and test datasets, effectively learning the underlying patterns. |
The model is well-tuned, generalizes well, and makes accurate predictions on both datasets. |
High Test, Moderate Train |
The model generalizes well but may not be fully optimized on training data, missing some underlying patterns. |
The model is fairly robust but may benefit from further tuning or more training to improve its learning. |
High Test, Low Train |
An unusual scenario, potentially indicating data leakage or overestimation of test performance. |
The model’s performance is likely unreliable; investigate potential data issues or random noise. |
Moderate Test, High Train |
The model fits the training data well but doesn’t generalize as effectively, capturing only some test patterns. |
The model is slightly overfitting; adjustments may be needed to improve generalization on unseen data. |
Moderate Test, Moderate Train |
The model shows balanced but moderate performance on both datasets, capturing some patterns but missing others. |
The model is moderately fitting; further improvements could be made in both training and generalization. |
Moderate Test, Low Train |
The model underperforms on training data and doesn’t generalize well, leading to moderate test performance. |
The model may need more complexity, additional features, or better training to improve on both datasets. |
Low Test, High Train |
The model overfits the training data, performing poorly on the test set. |
The model doesn’t generalize well; simplifying the model or using regularization may help reduce overfitting. |
Low Test, Low Train |
The model performs poorly on both training and test datasets, failing to learn the data patterns effectively. |
The model is underfitting; it may need more complexity, additional features, or more data to improve performance. |
Recommendations for Publishing Results#
When publishing results, scenarios with moderate train and moderate test performance can be used for complex phenotypes or diseases. However, results showing high train and moderate test, high train and high test, and moderate train and high test are recommended.
For most phenotypes, results typically fall in the moderate train and moderate test performance category.
Continuous Phenotypes Result Analysis#
You can find the performance quality for continuous phenotypes using the following template:
This figure shows the 8 different scenarios that can exist in the results, and the following table explains each scenario.
We classified performance based on the following table:
Performance Level |
Range |
---|---|
Low Performance |
0 to 0.2 |
Moderate Performance |
0.3 to 0.7 |
High Performance |
0.8 to 1 |
You can match the performance based on the following scenarios:
Scenario |
What’s Happening |
Implication |
---|---|---|
High Test, High Train |
The model performs well on both training and test datasets, effectively learning the underlying patterns. |
The model is well-tuned, generalizes well, and makes accurate predictions on both datasets. |
High Test, Moderate Train |
The model generalizes well but may not be fully optimized on training data, missing some underlying patterns. |
The model is fairly robust but may benefit from further tuning or more training to improve its learning. |
High Test, Low Train |
An unusual scenario, potentially indicating data leakage or overestimation of test performance. |
The model’s performance is likely unreliable; investigate potential data issues or random noise. |
Moderate Test, High Train |
The model fits the training data well but doesn’t generalize as effectively, capturing only some test patterns. |
The model is slightly overfitting; adjustments may be needed to improve generalization on unseen data. |
Moderate Test, Moderate Train |
The model shows balanced but moderate performance on both datasets, capturing some patterns but missing others. |
The model is moderately fitting; further improvements could be made in both training and generalization. |
Moderate Test, Low Train |
The model underperforms on training data and doesn’t generalize well, leading to moderate test performance. |
The model may need more complexity, additional features, or better training to improve on both datasets. |
Low Test, High Train |
The model overfits the training data, performing poorly on the test set. |
The model doesn’t generalize well; simplifying the model or using regularization may help reduce overfitting. |
Low Test, Low Train |
The model performs poorly on both training and test datasets, failing to learn the data patterns effectively. |
The model is underfitting; it may need more complexity, additional features, or more data to improve performance. |
Recommendations for Publishing Results#
When publishing results, scenarios with moderate train and moderate test performance can be used for complex phenotypes or diseases. However, results showing high train and moderate test, high train and high test, and moderate train and high test are recommended.
For most continuous phenotypes, results typically fall in the moderate train and moderate test performance category.
2. Reporting Generalized Performance:#
One can also report the generalized performance by calculating the difference between the training and test performance, and the sum of the test and training performance. Report the result or hyperparameter combination for which the sum is high and the difference is minimal.
Example code:
df = divided_result.copy() df['Difference'] = abs(df['Train_best_model'] - df['Test_best_model']) df['Sum'] = df['Train_best_model'] + df['Test_best_model'] sorted_df = df.sort_values(by=['Sum', 'Difference'], ascending=[False, True]) print(sorted_df.iloc[0].to_markdown())
3. Reporting Hyperparameters Affecting Test and Train Performance:#
Find the hyperparameters that have more than one unique value and calculate their correlation with the following columns to understand how they are affecting the performance of train and test sets:
Train_null_model
Train_pure_prs
Train_best_model
Test_pure_prs
Test_null_model
Test_best_model
4. Other Analysis#
Once you have the results, you can find how hyperparameters affect the model performance.
Analysis, like overfitting and underfitting, can be performed as well.
The way you are going to report the results can vary.
Results can be visualized, and other patterns in the data can be explored.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib notebook
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
df = divided_result.sort_values(by='Train_best_model', ascending=False)
print("1. Reporting Based on Best Training Performance:\n")
print(df.iloc[0].to_markdown())
df = divided_result.copy()
# Plot Train and Test best models against p-values
plt.figure(figsize=(10, 6))
plt.plot(df['pvalue'], df['Train_best_model'], label='Train_best_model', marker='o', color='royalblue')
plt.plot(df['pvalue'], df['Test_best_model'], label='Test_best_model', marker='o', color='darkorange')
# Highlight the p-value where both train and test are high
best_index = df[['Train_best_model']].sum(axis=1).idxmax()
best_pvalue = df.loc[best_index, 'pvalue']
best_train = df.loc[best_index, 'Train_best_model']
best_test = df.loc[best_index, 'Test_best_model']
# Use dark colors for the circles
plt.scatter(best_pvalue, best_train, color='darkred', s=100, label=f'Best Performance (Train)', edgecolor='black', zorder=5)
plt.scatter(best_pvalue, best_test, color='darkblue', s=100, label=f'Best Performance (Test)', edgecolor='black', zorder=5)
# Annotate the best performance with p-value, train, and test values
plt.text(best_pvalue, best_train, f'p={best_pvalue:.4g}\nTrain={best_train:.4g}', ha='right', va='bottom', fontsize=9, color='darkred')
plt.text(best_pvalue, best_test, f'p={best_pvalue:.4g}\nTest={best_test:.4g}', ha='right', va='top', fontsize=9, color='darkblue')
# Calculate Difference and Sum
df['Difference'] = abs(df['Train_best_model'] - df['Test_best_model'])
df['Sum'] = df['Train_best_model'] + df['Test_best_model']
# Sort the DataFrame
sorted_df = df.sort_values(by=['Sum', 'Difference'], ascending=[False, True])
#sorted_df = df.sort_values(by=[ 'Difference','Sum'], ascending=[ True,False])
# Highlight the general performance
general_index = sorted_df.index[0]
general_pvalue = sorted_df.loc[general_index, 'pvalue']
general_train = sorted_df.loc[general_index, 'Train_best_model']
general_test = sorted_df.loc[general_index, 'Test_best_model']
plt.scatter(general_pvalue, general_train, color='darkgreen', s=150, label='General Performance (Train)', edgecolor='black', zorder=6)
plt.scatter(general_pvalue, general_test, color='darkorange', s=150, label='General Performance (Test)', edgecolor='black', zorder=6)
# Annotate the general performance with p-value, train, and test values
plt.text(general_pvalue, general_train, f'p={general_pvalue:.4g}\nTrain={general_train:.4g}', ha='left', va='bottom', fontsize=9, color='darkgreen')
plt.text(general_pvalue, general_test, f'p={general_pvalue:.4g}\nTest={general_test:.4g}', ha='left', va='top', fontsize=9, color='darkorange')
# Add labels and legend
plt.xlabel('p-value')
plt.ylabel('Model Performance')
plt.title('Train vs Test Best Models')
plt.legend()
plt.show()
print("2. Reporting Generalized Performance:\n")
df = divided_result.copy()
df['Difference'] = abs(df['Train_best_model'] - df['Test_best_model'])
df['Sum'] = df['Train_best_model'] + df['Test_best_model']
sorted_df = df.sort_values(by=['Sum', 'Difference'], ascending=[False, True])
print(sorted_df.iloc[0].to_markdown())
print("3. Reporting the correlation of hyperparameters and the performance of 'Train_null_model', 'Train_pure_prs', 'Train_best_model', 'Test_pure_prs', 'Test_null_model', and 'Test_best_model':\n")
print("3. For string hyperparameters, we used one-hot encoding to find the correlation between string hyperparameters and 'Train_null_model', 'Train_pure_prs', 'Train_best_model', 'Test_pure_prs', 'Test_null_model', and 'Test_best_model'.")
print("3. We performed this analysis for those hyperparameters that have more than one unique value.")
correlation_columns = [
'Train_null_model', 'Train_pure_prs', 'Train_best_model',
'Test_pure_prs', 'Test_null_model', 'Test_best_model'
]
hyperparams = [col for col in divided_result.columns if len(divided_result[col].unique()) > 1]
hyperparams = list(set(hyperparams+correlation_columns))
# Separate numeric and string columns
numeric_hyperparams = [col for col in hyperparams if pd.api.types.is_numeric_dtype(divided_result[col])]
string_hyperparams = [col for col in hyperparams if pd.api.types.is_string_dtype(divided_result[col])]
# Encode string columns using one-hot encoding
divided_result_encoded = pd.get_dummies(divided_result, columns=string_hyperparams)
# Combine numeric hyperparams with the new one-hot encoded columns
encoded_columns = [col for col in divided_result_encoded.columns if col.startswith(tuple(string_hyperparams))]
hyperparams = numeric_hyperparams + encoded_columns
# Calculate correlations
correlations = divided_result_encoded[hyperparams].corr()
# Display correlation of hyperparameters with train/test performance columns
hyperparam_correlations = correlations.loc[hyperparams, correlation_columns]
hyperparam_correlations = hyperparam_correlations.fillna(0)
# Plotting the correlation heatmap
plt.figure(figsize=(12, 8))
ax = sns.heatmap(hyperparam_correlations, annot=True, cmap='viridis', fmt='.2f', cbar=True)
ax.set_xticklabels(ax.get_xticklabels(), rotation=90, ha='right')
# Rotate y-axis labels to horizontal
#ax.set_yticklabels(ax.get_yticklabels(), rotation=0, va='center')
plt.title('Correlation of Hyperparameters with Train/Test Performance')
plt.show()
sns.set_theme(style="whitegrid") # Choose your preferred style
pairplot = sns.pairplot(divided_result_encoded[hyperparams],hue = 'Test_best_model', palette='viridis')
# Adjust the figure size
pairplot.fig.set_size_inches(15, 15) # You can adjust the size as needed
for ax in pairplot.axes.flatten():
ax.set_xlabel(ax.get_xlabel(), rotation=90, ha='right') # X-axis labels vertical
#ax.set_ylabel(ax.get_ylabel(), rotation=0, va='bottom') # Y-axis labels horizontal
# Show the plot
plt.show()
1. Reporting Based on Best Training Performance:
| | 94 |
|:-----------------|--------------:|
| clump_p1 | 1 |
| clump_r2 | 0.1 |
| clump_kb | 200 |
| p_window_size | 200 |
| p_slide_size | 50 |
| p_LD_threshold | 0.25 |
| pvalue | 0.312572 |
| numberofpca | 6 |
| tempalpha | 0.1 |
| l1weight | 0.1 |
| Train_pure_prs | 6.53073e-06 |
| Train_null_model | 0.23001 |
| Train_best_model | 0.393524 |
| Test_pure_prs | 6.75312e-06 |
| Test_null_model | 0.118692 |
| Test_best_model | 0.340104 |
2. Reporting Generalized Performance:
| | 94 |
|:-----------------|--------------:|
| clump_p1 | 1 |
| clump_r2 | 0.1 |
| clump_kb | 200 |
| p_window_size | 200 |
| p_slide_size | 50 |
| p_LD_threshold | 0.25 |
| pvalue | 0.312572 |
| numberofpca | 6 |
| tempalpha | 0.1 |
| l1weight | 0.1 |
| Train_pure_prs | 6.53073e-06 |
| Train_null_model | 0.23001 |
| Train_best_model | 0.393524 |
| Test_pure_prs | 6.75312e-06 |
| Test_null_model | 0.118692 |
| Test_best_model | 0.340104 |
| Difference | 0.0534201 |
| Sum | 0.733628 |
3. Reporting the correlation of hyperparameters and the performance of 'Train_null_model', 'Train_pure_prs', 'Train_best_model', 'Test_pure_prs', 'Test_null_model', and 'Test_best_model':
3. For string hyperparameters, we used one-hot encoding to find the correlation between string hyperparameters and 'Train_null_model', 'Train_pure_prs', 'Train_best_model', 'Test_pure_prs', 'Test_null_model', and 'Test_best_model'.
3. We performed this analysis for those hyperparameters that have more than one unique value.