computationally efficient nested for loop type solution for regression
0
0
Entering edit mode
3.1 years ago
pmw • 0

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?

library(lme4)
library(lm.beta)

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]]})
betas<- as.data.frame(as.matrix(betas))
Pvals<- lapply(metabsMODs3, function(x){
  x <- summary(x)                          
  y <- x$coefficients
  y[2,4]})

Pvals<- as.data.frame(as.matrix(Pvals))
Pvals$Pval <- as.numeric(Pvals$V1)
Pvals$V1 <- NULL
betas$P <- Pvals$Pval
betas$Q <- p.adjust(betas$P, method="fdr")
betas <- as.data.frame(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
lme4 forloop nested regression r • 482 views
ADD COMMENT

Login before adding your answer.

Traffic: 2331 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6