使用R对多个依赖变量和多个独立变量进行回归

时间:2013-08-05 20:11:58

标签: r

我正在尝试使用多个因变量和多个自变量进行回归。基本上我在整个美国的县级House Prices,这是我的IV。然后我在县级(GDPconstruction employment)有其他几个变量,这些变量构成我的因变量。我想知道是否有一种有效的方法可以同时完成所有这些回归。我想要:

lm(IV1 ~ DV11 + DV21)
lm(IV2 ~ DV12 + DV22)

我想为每个独立变量和每个因变量执行此操作。

编辑: OP添加了此信息以回复我的回答,现已删除,误解了这个问题。

我认为我没有很好地解释这个问题,我道歉。每个因变量都有2个与之关联的独立变量,这是唯一的。因此,如果我有500个因变量,我有500个唯一的自变量1和500个唯一的自变量2。

好的,我会再试一次,如果我再也不能解释自己,我可能会放弃(哈哈)。我不知道你对来自R的mtcars是什么意思 [这是对Metrics的回答] ,所以让我这样试试吧。我将有3个数据向量,每个数据大约500行。我正在尝试从每行数据中构建回归。假设向量1是我的因变量(我试图预测的那个),向量2和3组成我的自变量。因此,第一个回归将包含每个向量的第1行值,第2个将包含每个向量的第2行值,依此类推。再次感谢大家。

1 个答案:

答案 0 :(得分:2)

我假设你有数据帧作为mydata。

mydata<-mtcars #mtcars is the data in R

dep<-c("mpg~","cyl~","disp~") # list of unique dependent variables with ~ 
indep1<-c("hp","drat","wt")  # list of first unique independent variables 
indep2<-c("qsec","vs","am") # list of second unique independent variables 
> myvar<-cbind(dep,indep1,indep2) # matrix of variables
> myvar
     dep     indep1 indep2
[1,] "mpg~"  "hp"   "qsec"
[2,] "cyl~"  "drat" "vs"  
[3,] "disp~" "wt"   "am" 



for (i in 1:dim(myvar)[1]){
print(paste("This is", i, "regression", "with dependent var",gsub("~","",myvar[i,1])))
k[[i]]<-lm(as.formula(paste(myvar[i,1],paste(myvar[i,2:3],collapse="+"))),mydata)
print(k[[i]]
}



 [1] "This is 1 regression with dependent var mpg"

Call:
lm(formula = as.formula(paste(myvar[i, 1], paste(myvar[i, 2:3], 
    collapse = "+"))), data = mydata)

Coefficients:
(Intercept)           hp         qsec  
   48.32371     -0.08459     -0.88658  

[1] "This is 2 regression with dependent var cyl"

Call:
lm(formula = as.formula(paste(myvar[i, 1], paste(myvar[i, 2:3], 
    collapse = "+"))), data = mydata)

Coefficients:
(Intercept)         drat           vs  
     12.265       -1.421       -2.209  

[1] "This is 3 regression with dependent var disp"

Call:
lm(formula = as.formula(paste(myvar[i, 1], paste(myvar[i, 2:3], 
    collapse = "+"))), data = mydata)

Coefficients:
(Intercept)           wt           am  
    -148.59       116.47        11.31  

注意:您可以对大量变量使用相同的过程。

替代方法:

在Hadley的回答here的推动下,我使用函数Map来解决上述问题:

dep<-list("mpg~","cyl~","disp~") # list of unique dependent variables with ~ 
indep1<-list("hp","drat","wt")  # list of first unique independent variables 
indep2<-list("qsec","vs","am") # list of second unique independent variables
Map(function(x,y,z) lm(as.formula(paste(x,paste(list(y,z),collapse="+"))),data=mtcars),dep,indep1,indep2)
[[1]]

Call:
lm(formula = as.formula(paste(x, paste(list(y, z), collapse = "+"))), 
    data = mtcars)

Coefficients:
(Intercept)           hp         qsec  
   48.32371     -0.08459     -0.88658  


[[2]]

Call:
lm(formula = as.formula(paste(x, paste(list(y, z), collapse = "+"))), 
    data = mtcars)

Coefficients:
(Intercept)         drat           vs  
     12.265       -1.421       -2.209  


[[3]]

Call:
lm(formula = as.formula(paste(x, paste(list(y, z), collapse = "+"))), 
    data = mtcars)

Coefficients:
(Intercept)           wt           am  
    -148.59       116.47        11.31