Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Figure 3.1 code does not work #46

Open
RaymondBalise opened this issue Mar 13, 2021 · 0 comments
Open

Figure 3.1 code does not work #46

RaymondBalise opened this issue Mar 13, 2021 · 0 comments

Comments

@RaymondBalise
Copy link

RaymondBalise commented Mar 13, 2021

The code for figure 3.1 does not render properly.

image

There is a known bug in boom::augment.lm() that causes this:
tidymodels/broom#937

This shows the guts of the problem:

library(broom)
aModel <- augment(lm(log(Sale_Price) ~ Year_Built, data = ames_train))
names(aModel)
aModel <- augment(lm(Sale_Price ~ Year_Built, data = ames_train))
names(aModel)

This code works:

library(tidyverse)
ames <- AmesHousing::make_ames() %>% 
  mutate(log_Sale_Price = log(Sale_Price))

# Load and split the Ames housing data using stratified sampling
set.seed(123)  # for reproducibility
split  <- rsample::initial_split(ames, prop = 0.7, strata = "Sale_Price")
ames_train  <- rsample::training(split)
ames_test   <- rsample::testing(split)

models <- c("Non-log transformed model residuals", 
            "Log transformed model residuals")
list(
  m1 = lm(Sale_Price ~ Year_Built, data = ames_train) ,
  m2 = lm(log_Sale_Price ~ Year_Built, data = ames_train)
) %>%
  map2_dfr(models, ~ broom::augment(.x) %>% mutate(model = .y)) %>%
  ggplot(aes(.resid)) +
  geom_histogram(bins = 75) +
  facet_wrap(~ model, scales = "free") +
  ylab(NULL) +
  xlab("Residuals")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant