2013-08-05 37 views
5

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

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

我想为每个独立和每个因变量做到这一点。

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

我不认为我很好地解释了这个问题,我很抱歉。每个因变量都有2个独立的变量,它们是唯一的。所以如果我有500个因变量,我有500个独特的自变量1和500个独特的自变量2.

好吧,我会再试一次,如果我不能再次解释我自己,我可能会放弃(哈哈) 。我不知道你的意思是mtcars从R,但[这是参考指标的回答],所以让我试试这种方式。我将在每一个中有3个数据向量大约500行。我试图从每一行数据中建立一个回归。假设矢量1是我的因变量(我试图预测的那个),矢量2和3构成了我的自变量。因此,第一次回归将包含每个向量的第1行值,第2次将包含每个向量的第2行值,依此类推。再次感谢你们。

+2

通过“因变量”,你的意思是你要预测数,和“自变量”是你想要用来做预测的数字吗?请注意,在R的公式语法中,因变量在代字号的左侧进行,IVs在RHS('lm(DV〜IV)')上进行。 – gung

+0

[PLS回归](http://en.wikipedia.org/wiki/Partial_least_squares_regression)是一种选择。 – chl

+0

对不起,我确实是这样说的。我打开了我的IV和DV.I也标记了我的问题,让它移到了堆栈溢出,因为我主要是看如何在R中实现它,因为我理解它背后的概念。谢谢你。 – user2355903

回答

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 

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

替代做法:

由哈德利的答案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 
+0

@ user2355903:'mtcars'是一个内置于R的示例数据集。您应该可以自己运行此代码来试用它,看看它是否是您需要的。根据你所描述的(虽然SeñorO的问题确实需要澄清),将公式粘贴在一起并将它们一起运行(如Metrics所建议的)可能是一条路。 – Aaron

+0

我不明白在这些循环方法中,结果之间的相关性在哪里被考虑 –