How to succinctly write a formula with many variables from a data frame?

后端 未结 6 2136
一整个雨季
一整个雨季 2020-11-22 17:01

Suppose I have a response variable and a data containing three covariates (as a toy example):

y = c(1,4,6)
d = data.frame(x1 = c(4,-1,3), x2 = c(3,9,8), x3 =         


        
6条回答
  •  余生分开走
    2020-11-22 17:42

    Yes of course, just add the response y as first column in the dataframe and call lm() on it:

    d2<-data.frame(y,d)
    > d2
      y x1 x2 x3
    1 1  4  3  4
    2 4 -1  9 -4
    3 6  3  8 -2
    > lm(d2)
    
    Call:
    lm(formula = d2)
    
    Coefficients:
    (Intercept)           x1           x2           x3  
        -5.6316       0.7895       1.1579           NA  
    

    Also, my information about R points out that assignment with <- is recommended over =.

提交回复
热议问题