Using R to do a regression with multiple dependent and multiple independent variables

前端 未结 1 602
感动是毒
感动是毒 2020-12-06 07:32

I am trying to do a regression with multiple dependent variables and multiple independent variables. Basically I have House Prices at a county level for the who

相关标签:
1条回答
  • 2020-12-06 07:41

    I am assuming you have dataframe as 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  
    

    Note: You can use the same process for the large number of variables.

    Alternative approach:

    Motivated by Hadley's answer here, I use function Map to solve above problem:

    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 讨论(0)
提交回复
热议问题