Home > OS >  "Anova" function refuses to compile in R Knitr?
"Anova" function refuses to compile in R Knitr?

Time:02-15

The entirety of my code is running perfectly well. When I try to use Knitr to generate PDF/ HTML I get a error that says

Error in Anova(mod1, type = "III") : could not find function "Anova"

Any suggestions on what I can do differently?

Relevant code:
library(lsmeans)
library(lme4)
df <- read.csv(file = "file.csv", header = TRUE)

library(lmerTest)
mod1 <- lm(Yield ~ Treatment   Block, data = df)
Anova(mod1, type = "III")
lsm <- lsmeans (mod1, "Treatment")


Full Error:
Loading required package: emmeans
The 'lsmeans' package is now basically a front end for 'emmeans'.
Users are encouraged to switch the rest of the way.
See help('transition') for more information, including how to
convert old 'lsmeans' objects and scripts to work with 'emmeans'.
Loading required package: Matrix


Attaching package: 'lmerTest'

The following object is masked from 'package:lme4':

    lmer

The following object is masked from 'package:stats':

    step

Quitting from lines 12-38 (df_test.Rmd) 
Error in Anova(mod1, type = "III") : could not find function "Anova"
Calls: <Anonymous> ... handle -> withCallingHandlers -> withVisible -> eval -> eval
Execution halted

CodePudding user response:

You have to add library(car). Anova() function is from this package!

library(car)
library(lsmeans)
library(lme4)
df <- read.csv(file = "file.csv", header = TRUE)

library(lmerTest)
mod1 <- lm(Yield ~ Treatment   Block, data = df)
Anova(mod1, type = "III")
lsm <- lsmeans (mod1, "Treatment")

Example mtcars:

```{r mtcars}
library(lsmeans)
library(lme4)
library(car)

library(lmerTest)
mod1 <- lm(mpg ~ cyl, data = mtcars)
Anova(mod1, type = "III")
lsm <- lsmeans (mod1, "cyl")
```

gives:

> Anova(mod1, type = "III")
Anova Table (Type III tests)

Response: mpg
            Sum Sq Df F value  Pr(>F)    
(Intercept)   3430  1   333.7 < 2e-16 ***
cyl            818  1    79.6 6.1e-10 ***
Residuals      308 30                    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> lsm <- lsmeans (mod1, "cyl")
> lsm
  cyl lsmean    SE df lower.CL upper.CL
 6.19   20.1 0.567 30     18.9     21.2

Confidence level used: 0.95 
  • Related