【问题标题】:Error message in model comparison using anova() in R在 R 中使用 anova() 进行模型比较时出现错误消息
【发布时间】:2019-10-09 04:24:34
【问题描述】:

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

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 列表[0] 如果您需要更多信息,请与我们联系。

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

【问题讨论】:

  • 在 anova 中尝试不使用 data,只需:anova(H1, H2)
  • 是的,我一开始就是这样做的,但那不起作用,所以我用 data = 定义了数据,不过谢谢
  • 感谢您的评论;我确实知道链接中的帖子是关于什么的,但我似乎无法将其应用于我的问题。所有的 x 和 y 都是 data.frame 的列 & 是向量 - 我应该如何处理这些......?

标签: r model-comparison


【解决方案1】:

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

fit 中的术语 data 应该是一个包含 x1、x2 和 y 列的数据框。

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

【讨论】:

  • 感谢您的评论!是的,x1、x2 和 y 都是数据 df 的列,但我确实看到,当我执行 is.vector(df$x1) 时,我得到了 TRUE。然后我是否需要使用 as.data.frame() 函数将其中的每一个更改为 data.frame?
  • 你能运行 is.data.frame(df) 吗?要知道 df 是否真的是一个 data.frame。或 df
  • 是的,我刚刚检查过了,df 是一个 data.frame
  • 也做了 df
  • 改用 as.data.frame。但最好在尝试与数据框进行任何对话之前发布str(df) 的输出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-14
  • 1970-01-01
  • 1970-01-01
  • 2017-12-10
  • 2021-06-12
  • 1970-01-01
相关资源
最近更新 更多