我有40个SNP,想看看每个SNP对更年期的影响。要做到这一点,我需要做一个多元线性回归,每个单独的SNP。我希望避免键入相同的命令40次不同的时间(在未来,我将使用更多的SNP)。
我想要做的是在一个csv文件中列出SNP并调用这个x
x <- read.csv("snps.csv")然后我想在这个命令中使用这个列表;
fit <- lm(a_menopause ~ "snps" + country, data=mydata)其中,snps是我需要分析的SNP列表,但每次需要执行一个SNP。理想情况下,我希望将结果打印到csv文件中。
发布于 2015-06-05 18:42:09
见下面的例子:
#dummy data
set.seed(123)
#phenotype
phenotype <- data.frame(
a_menopause=sample(c(0,1),10,replace=TRUE),
country=sample(letters[1:3],10,replace=TRUE))
#genotype
genotype <-
read.table(text="SNP1 SNP2 SNP3 SNP4
1 0 1 1
2 0 2 1
0 0 0 1
0 0 0 1
0 1 0 1
1 1 0 1
1 2 0 1
1 2 1 2
0 0 0 1
0 1 0 1
",header=TRUE)
#data for lm
fitDat <- cbind(phenotype,genotype)
#get fit for all SNPs
fitAllSNPs <-
lapply(colnames(fitDat)[3:6], function(SNP){
fit <- lm(paste("a_menopause ~ country + ", SNP),
data=fitDat)
})
#extract coef for each SNP
lapply(fitAllSNPs,coef)
#output
# [[1]]
# (Intercept) countryb countryc SNP1
# 1.000000e+00 -2.633125e-16 -1.000000e+00 9.462903e-17
#
# [[2]]
# (Intercept) countryb countryc SNP2
# 1.000000e+00 -1.755417e-16 -1.000000e+00 2.780094e-19
#
# [[3]]
# (Intercept) countryb countryc SNP3
# 1.000000e+00 -2.633125e-16 -1.000000e+00 1.079985e-16
#
# [[4]]
# (Intercept) countryb countryc SNP4
# 1.000000e+00 -1.755417e-16 -1.000000e+00 4.269835e-31 https://stackoverflow.com/questions/30508156
复制相似问题