首页 > 解决方案 > 在 R 中使用 anova() 进行模型比较时出现错误消息

问题描述

我正在尝试比较下面的两个模型

H1 <- lm(y ~ x1 + x2, data = df) 
H2 <- lm(y ~ x1 + x2 + x3, data = df)

anova(H1, H2)

但是,我收到一条错误消息:

错误:参数“数据”必须是数据框

当我定义数据时,我会收到另一条错误消息:

anova(H1, H2, data = df)

.subset2(x, i) 中的错误:递归索引在级别 2 失败

我试图查看模型并显示它们(不确定我是否正在查看正确的模型,但是):

H1
model   list[89 x 3] (S3: data.frame) A data.frame with 89 rows and 3 columns
y       double[89]                   3.00   3.50  4.25 5.11  1.00 ...
x1       double[89]                   19    24   31    35   20   21 ...
x2       double[89]                   1 1 1 1 2 1 1 ...

str(H1)
List of 12
 $ coefficients : Named num [1:3] 5.42739 0.000294 -0.950346
  ..- attr(*, "names")= chr [1:3] "(Intercept)" "x1" "x2"
 $ residuals    : Named num [1:89] -1.4844 -0.9835 -0.2326 -2.5338 0.0177 ...
  ..- attr(*, "names")= chr [1:89] "1" "2" "3" "4" ...
 $ effects      : Named num [1:89] -40.783 0.796 -3.258 -2.349 0.068 ...
  ..- attr(*, "names")= chr [1:89] "(Intercept)" "x1" "x2" "" ...
 $ rank         : int 3
 $ fitted.values: Named num [1:89] 4.48 4.48 4.48 3.53 4.48 ...
  ..- attr(*, "names")= chr [1:89] "1" "2" "3" "4" ...
 $ assign       : int [1:3] 0 1 2
 $ qr           :List of 5
  ..$ qr   : num [1:89, 1:3] -9.434 0.106 0.106 0.106 0.106 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:89] "1" "2" "3" "4" ...
  .. .. ..$ : chr [1:3] "(Intercept) "x1" "x2"
  .. ..- attr(*, "assign")= int [1:3] 0 1 2
  ..$ qraux: num [1:3] 1.11 1.02 1.03
  ..$ pivot: int [1:3] 1 2 3
  ..$ tol  : num 1e-07
  ..$ rank : int 3
  ..- attr(*, "class")= chr "qr"
 $ df.residual  : int 86
 $ xlevels      : Named list()
 $ call         : language lm(formula = y ~ x1 + x2, data = df)
 $ terms        :Classes 'terms', 'formula'  language y ~ x1 + x2
  .. ..- attr(*, "variables")= language list(y, x1, x2)
  .. ..- attr(*, "factors")= int [1:3, 1:2] 0 1 0 0 0 1
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:3] "y" "x1" "x2"
  .. .. .. ..$ : chr [1:2] "x1" "x2"
  .. ..- attr(*, "term.labels")= chr [1:2] "x1" "x2"
  .. ..- attr(*, "order")= int [1:2] 1 1
  .. ..- attr(*, "intercept")= int 1
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. ..- attr(*, "predvars")= language list(y, x1, x2)
  .. ..- attr(*, "dataClasses")= Named chr [1:3] "numeric" "numeric" "numeric"
  .. .. ..- attr(*, "names")= chr [1:3] "y" "x1" "x2"
 $ model        :'data.frame':  89 obs. of  3 variables:
  ..$ y : num [1:89] 3 3.5 4.25 1 4.5 5.25 4.75 3.75 3.5 5 ...
  ..$ x1    : num [1:89] 25 22 19 24 18 24 18 18 21 19 ...
  ..$ x2 : num [1:89] 1 1 1 2 1 1 1 1 1 1 ...
  ..- attr(*, "terms")=Classes 'terms', 'formula'  language y ~ x1 + x2
  .. .. ..- attr(*, "variables")= language list(y, x1, x2)
  .. .. ..- attr(*, "factors")= int [1:3, 1:2] 0 1 0 0 0 1
  .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. ..$ : chr [1:3] "y" "x1" "x2"
  .. .. .. .. ..$ : chr [1:2] "x1" "x2"
  .. .. ..- attr(*, "term.labels")= chr [1:2] "x1" "x2"
  .. .. ..- attr(*, "order")= int [1:2] 1 1
  .. .. ..- attr(*, "intercept")= int 1
  .. .. ..- attr(*, "response")= int 1
  .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. .. ..- attr(*, "predvars")= language list(y x1 x2)
  .. .. ..- attr(*, "dataClasses")= Named chr [1:3] "numeric" "numeric" "numeric"
  .. .. .. ..- attr(*, "names")= chr [1:3] "y" "x1" "x2"
 - attr(*, "class")= chr "lm"


H2
model   list[89 x 3] (S3: data.frame) A data.frame with 89 rows and 4 columns
y       double[89]                   3.00   3.50  4.25 5.11  1.00 ...
x1       double[89]                   19    24   31    35   20   21 ...
x2       double[89]                   1 1 1 1 2 1 1 ...
x3       double[89]                   0 0 0 0 1 0 0 



两者都有 xlevels list[0] 如果您需要更多信息,请告诉我。

如果您能帮我解决这个问题,我将不胜感激!

标签: rmodel-comparison

解决方案


Anova 从 lm 对象调用数据函数。你确定他们是对的吗?

拟合中的术语数据应该是具有 x1、x2 和 y 列的数据框。

如果 x1、x2 和 y 是向量,则不要使用 data 参数。


推荐阅读