computationally efficient nested for loop type solution for regression

I’m trying to adapt the code below where I apply a loop type regression of 1 SNP (coded 0,1,2) against a dataframe of 150 outcome variables (metabs), giving 150 models, and extract the results of the models into a dataframe.

I need to scale this up to include around 70 SNPs applied to a model in the same way (70 times 150 models), i.e a double loop/nested loop/parallel solution might be appropriate, what would be a computationally efficient way to do this?


metabsMODs3 <- apply(metabs, 2, function(x) {mod1 <- lm(x ~ GP3$rs10498633_G +
age + sex + batch

                                                    + PC1 + PC2, data = GP3)

stdcoef <-lapply(metabsMODs3, lm.beta)
betas<- lapply(stdcoef, function(x){x$coefficients[[2]]})
Pvals<- lapply(metabsMODs3, function(x){
x <- summary(x)
y <- x$coefficients

Pvals$Pval <- as.numeric(Pvals$V1)
Pvals$V1 <- NULL
betas$P <- Pvals$Pval
betas$Q <- p.adjust(betas$P, method=”fdr”)
betas <-
betas$beta <- betas$V1
betas$V1 <- NULL
BETAS3 <- BETAS3[-3]
SigSNP3 <- BETAS3[which(BETAS3$Q <0.05),]

SigSNP3$beta <- as.numeric(SigSNP3$beta)
BETAS3$beta <- as.numeric(BETAS3$beta)
BETAS3$SNP <- “rs10498633”
write.table(BETAS3, “rs10498633.txt”, quote = FALSE, row.names = FALSE, col.names = TRUE)
a30 <- BETAS3

Read more here: Source link