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

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

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 ...

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 

And both models have xlevels list[0] Let me know if you need more information.

I have quadruple checked but the df is data.frame, so I have no clue why I keep getting the error message..
I would really appreciate it if you could help me out with this!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.