Error replicating r4ds book example

Anyone else have this error? From Model Building chapter of r4ds, section on A More Complicated Model.

grid <- diamonds2 %>% 
  data_grid(cut, .model = mod_diamond2) %>% 
  add_predictions(mod_diamond2)
Error in overscope_eval_next(overscope, expr) : object 'G' not found

Thanks!

1 Like

I'm able to reproduce what you're describing. I've added some notes re. the section(s) of R for Data Science, and then produced a little reprex (short for minimal reproducible example).

[n.b. I didn't really troubleshoot, but thought the reprex would be helpful for anyone else who reads this thread!]

suppressPackageStartupMessages(library(tidyverse))
library(modelr)

## R4DS: 24.2.1 Price and carat
diamonds2 <- diamonds %>% 
  filter(carat <= 2.5) %>% 
  mutate(lprice = log2(price), lcarat = log2(carat))

mod_diamond <- lm(lprice ~ lcarat, data = diamonds2)

grid <- diamonds2 %>% 
  data_grid(carat = seq_range(carat, 20)) %>% 
  mutate(lcarat = log2(carat)) %>% 
  add_predictions(mod_diamond, "lprice") %>% 
  mutate(price = 2 ^ lprice)

diamonds2 <- diamonds2 %>% 
  add_residuals(mod_diamond, "lresid")

## R4DS: 24.2.2 A more complicated model
mod_diamond2 <- lm(lprice ~ lcarat + color + cut + clarity, data = diamonds2)

grid <- diamonds2 %>% 
  data_grid(cut, .model = mod_diamond2) %>% 
  add_predictions(mod_diamond2)
#> Error in eval_tidy(xs[[i]], unique_output): object 'G' not found

Created on 2018-02-19 by the reprex package (v0.2.0).

There is an issue open in the R4DS GitHub repo that's related.

4 Likes