Linear regression after select() method in R

Viewed 49

I am trying to create a linear regression model from openintro::babies that predicts a baby's birthweight from all other variables in the data except case.

I have to following code:

library(tidyverse)
library(tidymodels)
babies <- openintro::babies %>%
  drop_na() %>% 
  mutate(bwt = 28.3495 * bwt) %>% 
  mutate(weight = 0.453592 * weight) 

linear_reg() %>%
  set_engine("lm") %>%
  fit(formula = bwt ~ ., data = babies %>% select(-case)) %>%
  pluck("fit") %>%
  augment(babies)

but in my output, I obtain the case variable as well

# A tibble: 1,174 x 14
    case   bwt gestation parity   age height weight smoke .fitted .resid    .hat .sigma    .cooksd .std.resid
   <int> <dbl>     <int>  <int> <int>  <int>  <dbl> <int>   <dbl>  <dbl>   <dbl>  <dbl>      <dbl>      <dbl>
 1     1 3402.       284      0    27     62   45.4     0   3459.  -56.8 0.00374   449. 0.00000863     -0.127
 2     2 3203.       282      0    33     64   61.2     0   3547. -344.  0.00227   449. 0.000191       -0.767
 3     3 3629.       279      0    28     64   52.2     1   3244.  385.  0.00291   449. 0.000307        0.858
 4     5 3062.       282      0    23     67   56.7     1   3396. -334.  0.00475   449. 0.000379       -0.746
 5     6 3856.       286      0    25     62   42.2     0   3474.  381.  0.00495   449. 0.000515        0.851
 6     7 3912.       244      0    33     62   80.7     0   3065.  848.  0.0137    448. 0.00715         1.90 
 7     8 3742.       245      0    23     65   63.5     0   3124.  618.  0.00716   449. 0.00197         1.38 
 8     9 3402.       289      0    25     62   56.7     0   3558. -156.  0.00301   449. 0.0000521      -0.348
 9    10 4054.       299      0    30     66   61.7     1   3591.  463.  0.00462   449. 0.000710        1.03 
10    11 3969.       351      0    27     68   54.4     0   4527. -558.  0.0221    449. 0.00510        -1.26 
# ... with 1,164 more rows

I'm not sure is it the correct way or it is inherent with the output.

1 Answers

Your code is correct. You're getting the case column because of the augment(babies) call, but if you replace it with augment(babies %>% select(-case)) you wont get that column. In other words, the regression model you're fitting does not take into acount the case column].

Related