Skip to content

Instantly share code, notes, and snippets.

@explodecomputer
Created March 27, 2017 12:13
Show Gist options
  • Save explodecomputer/57fef8c05f40a0a26d6d400652db0031 to your computer and use it in GitHub Desktop.
Save explodecomputer/57fef8c05f40a0a26d6d400652db0031 to your computer and use it in GitHub Desktop.
make phenotype residuals from covariates
library(data.table)
arguments <- commandArgs(T)
phenfile <- arguments[1]
covfile <- arguments[2]
outfile <- arguments[3]
lmodel <- arguments[4]
# expect lmodel to be "logistic" or "linear"
stopifnot(lmodel %in% c("logistic", "linear"))
phen <- fread(phenfile)
names(phen)[1:2] <- c("FID", "IID")
cov <- fread(covfile)
names(cov)[1:2] <- c("FID", "IID")
phen <- subset(phen, FID %in% cov$FID)
cov <- subset(cov, FID %in% phen$FID)
index <- match(cov$FID, phen$FID)
cov <- cov[index, ]
stopifnot(all(phen$FID == cov$FID))
dat <- data.frame(pxxxxxx=phen[,3], cov[,-c(1:2)])
dat$pxxxxxx[1] <- NA
f <- paste0("pxxxxxx ~ ", paste(names(cov)[-c(1:2)], collapse=" + "))
if(lmodel == "linear")
{
dat$resid <- residuals(lm(f, data=dat, na.action="na.exclude"))
} else {
dat$resid <- residuals(glm(f, data=dat, na.action="na.exclude", family="binomial"))
}
write.table(data.frame(FID=phen$FID, IID=phen$IID, y=dat$resid), file=outfile, row=FALSE, col=FALSE, qu=FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment