topepo / FES

Code and Resources for "Feature Engineering and Selection: A Practical Approach for Predictive Models" by Kuhn and Johnson
https://bookdown.org/max/FES
GNU General Public License v2.0
724 stars 237 forks source link

Error in ames_pairwise.R #109

Open obgeneralao opened 2 years ago

obgeneralao commented 2 years ago

In the section 7_04_The_Brute-Force_Approach_to_Identifying_Predictive_Interactions, the code ames_pairwise.R the particular chunk of codes as shown below are throwing some errors,

for (i in 1:nrow(interactions)) {
  tmp_vars <- c("Class", interactions$var1[i], interactions$var2[i])

  int <-
    as.formula(paste0(
      "~ starts_with('",
      interactions$var1[i],
      "'):starts_with('",
      interactions$var2[i],
      "')"
    ))

  int_rec <- 
    ames_rec %>% 
    step_interact(int) %>% 
    step_zv(all_predictors())

  set.seed(2691)
  main_int <- train(int_rec, 
                    data = ames_train, 
                    method = "lm", 
                    metric = "RMSE",
                    trControl = int_ctrl)  

  tmp_diff <- compare_models_1way(main_int, main_eff, alternative = "less")
  interactions$RMSE[i] <- getTrainPerf(main_eff)[1, "TrainRMSE"]
  interactions$Reduction[i] <- -tmp_diff$estimate
  interactions$Pvalue[i] <- tmp_diff$p.value

  a1 <- 
    anova(main_eff$finalModel, main_int$finalModel) %>% 
    tidy() %>% 
    slice(2) %>% 
    pull(p.value)

  interactions$anova_p[i] <- a1
}
Error in data.frame(..., check.names = FALSE): arguments imply differing number of rows: 19, 2
Traceback:

1. anova(main_eff$finalModel, main_int$finalModel) %>% tidy() %>% 
 .     slice(2) %>% pull(p.value)
2. pull(., p.value)
3. slice(., 2)
4. tidy(.)
5. tidy.anova(.)
6. cbind(term = mods, ret)
7. cbind(deparse.level, ...)
8. data.frame(..., check.names = FALSE)
9. stop(gettextf("arguments imply differing number of rows: %s", 
 .     paste(unique(nrows), collapse = ", ")), domain = NA)