From 2a826ea8ebb8fcbd43913e87cac8ebc3a131b4f0 Mon Sep 17 00:00:00 2001 From: "Vera N. Karlbauer" Date: Sat, 4 Nov 2023 17:16:29 +0100 Subject: [PATCH] Function for calculating appropriate number of PCs to include for batch identification/correction --- data/Calculate_PC_Cutoff.R | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 data/Calculate_PC_Cutoff.R diff --git a/data/Calculate_PC_Cutoff.R b/data/Calculate_PC_Cutoff.R new file mode 100644 index 0000000..5a93226 --- /dev/null +++ b/data/Calculate_PC_Cutoff.R @@ -0,0 +1,14 @@ +### Function calculating number of PCs to be kept for batch identification and correction +# threshold for N of PCs: start at PC1 and keep including PCs until drop in explained variance from PCi to PCi+2 < 1% +# if threshold >2, include first 2 PCs (needed for plots) +# include max 10 PCs + +pc_cutoff <- function(data){ + for(i in 1:nrow(data)) { + if(i > 10) break + else if((data[i,]$var_explained - data[i + 2,]$var_explained) >=1) PC_number <- i + else if(PC_number < 2) PC_number = 2 + else break + } + return(PC_number) +}