多元线性回归

时间:2015-05-28 13:35:50

标签: r regression bioinformatics lm linear

我有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文件。

1 个答案:

答案 0 :(得分:1)

见下面的例子:

#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