简体   繁体   English

使用R中的anova()进行模型比较时出现错误消息

[英]Error message in model comparison using anova() in R

I am trying to compare the two models below 我想比较下面的两个模型

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

anova(H1, H2)

However, I get an error message: 但是,我收到一条错误消息:

Error: Argument 'data' must be a data frame 错误:参数'data'必须是数据框

And when I define the data, then I get another error message: 当我定义数据时,我收到另一条错误消息:

anova(H1, H2, data = df)

Error in .subset2(x, i) : recursive indexing failed at level 2 .subset2(x,i)出错:递归索引在级别2失败

I tried to look at the models and they show (not sure if I am looking at the correct one, but): 我试着看一下这些模型并展示(不确定我是否正在查看正确的模型,但是):

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 



both have xlevels list[0] Let me know if you need more information. 两个都有xlevels列表[0]如果您需要更多信息,请告诉我。

I would really appreciate it if you could help me out with this! 如果你能帮助我,我真的很感激!

Anova calls the data function from the lm objects. Anova从lm对象调用数据函数。 Are you sure they are right? 你确定他们是对的吗?

The term data in fit should be a data frame with columns x1, x2 and y. 拟合中的术语数据应该是具有列x1,x2和y的数据帧。

If x1, x2 and y are vectors you don't use the data argument. 如果x1,x2和y是向量,则不使用data参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM