0

It is possible to use anova on multible models stored in a tibble without listing them manually.

An example prediction of wage from age in Wage dataset from the ISLR2 library. I have a tibble a column for polynomial degrees in one column, GLM models in another and CV errors in the third column.

I can use anova through do.call but it does not show p-values without passing test = 'F' as an argument.

library(ISLR2) 
library(tidyverse)
library(boot)
GLM <- function(n) {
  result <- glm(wage ~ poly(age, n), data = Wage)
  return(result)
}
CV <- function(n) {
  glm_fit <- glm(wage ~ poly(age, n), data = Wage)
  result <- cv.glm(Wage, glm_fit, K = 10)$delta[1]
  return(result)
}

set.seed(1)
models <- tibble(polynom_degrees = 1:10) %>% 
  mutate(linear_model = map(polynom_degrees, GLM)) %>% 
  mutate(CV_error = map(polynom_degrees, CV)) %>% 
  mutate(CV_error = unlist(CV_error))

do.call(anova, models$linear_model)
  • Please make your post self-contained. It is not clear what you are asking. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 22 '22 at 20:57
  • Ok, removed the link and reworded as a new question. – Denis Kazakov Feb 22 '22 at 22:04
  • Where does `Wage` come from? Be sure the list all the `library()` calls that are necessary to run the code. If we can't copy/paste in the code into a fresh R session it's much more difficult to help you. – MrFlick Feb 22 '22 at 22:19
  • 1
    Maybe `do.call(anova, c(models$linear_model, test="F"))` will work? Had to say without testing. – MrFlick Feb 22 '22 at 22:20
  • The dataset came from the ISLR2 library. And yes, ```do.call(anova, c(models$linear_model, test="F"))``` did the trick! I tried to combine the models with the test argument using the list command, etc. but did not try the simple c() – Denis Kazakov Feb 22 '22 at 22:28

0 Answers0