2016-07-16 3 views

답변

4

피팅 모델의 측면에서 실제 차이가 없습니다 :

> data(cars) 
> m1 <- lm(cars$dist ~ cars$speed) 
> m2 <- lm(dist ~ speed, data = cars) 
> all.equal(m1, m2) 
[1] "Component “coefficients”: Names: 1 string mismatch"             
[2] "Component “effects”: Names: 1 string mismatch"              
[3] "Component “qr”: Component “qr”: Attributes: < Component “dimnames”: Component 2: 1 string mismatch >" 
[4] "Component “call”: target, current do not match when deparsed"           
[5] "Component “terms”: formulas differ in contents"              
[6] "Component “model”: Names: 2 string mismatches"              
[7] "Component “model”: Attributes: < Component “terms”: formulas differ in contents >" 

그 차이 모든 인해 모델에서 변수의 파생 된 이름에 있습니다.

그러나 두 번째 형식이 더 유용합니다. 예를 들어, 첫 번째 모델로부터 예측하는 엉덩이에 통증 전적으로 :

df <- with(cars, data.frame(speed = c(30, 40))) 
predict(m1, newdata = df) 
predict(m2, newdata = df) 

> predict(m1, newdata = df) 
     1   2   3   4   5   6   7   8 
-1.849460 -1.849460 9.947766 9.947766 13.880175 17.812584 21.744993 21.744993 
     9  10  11  12  13  14  15  16 
21.744993 25.677401 25.677401 29.609810 29.609810 29.609810 29.609810 33.542219 
     17  18  19  20  21  22  23  24 
33.542219 33.542219 33.542219 37.474628 37.474628 37.474628 37.474628 41.407036 
     25  26  27  28  29  30  31  32 
41.407036 41.407036 45.339445 45.339445 49.271854 49.271854 49.271854 53.204263 
     33  34  35  36  37  38  39  40 
53.204263 53.204263 53.204263 57.136672 57.136672 57.136672 61.069080 61.069080 
     41  42  43  44  45  46  47  48 
61.069080 61.069080 61.069080 68.933898 72.866307 76.798715 76.798715 76.798715 
     49  50 
76.798715 80.731124 
Warning message: 
'newdata' had 2 rows but variables found have 50 rows 
> predict(m2, newdata = df) 
     1  2 
100.3932 139.7173 

번째 버전은 올바른되고, 그것을 따라 장착 된 모델로부터 예측하는 적절한 데이터 구조를 얻을 사소한 아니다 m1.

호감을 갖고 data 인수를 사용하여 두 번째 양식을 사용하십시오.

관련 문제