Search code examples
rdataframedplyrcompiler-warningstibble

Warning message: corrupt data frame


I am trying to create a data frame but the below code gives a warning message. Anyone know what the problem is?

library(tibble); library(magrittr); library(tidymodels); library(dplyr)

c(28L, 13L, 3L, 2L, 0L, 3L) -> points
c(100L, 91L, 8L, 5L, 13L, 12L) -> hours

tibble(points, hours) -> monthly

linear_reg() %>%
  set_engine("lm") %>%
  fit(points ~ hours, data = monthly) ->
  lm_fit

expand.grid(hours = seq(5, 30, 5)) %>%
  mutate(predicted = predict(lm_fit, .))

#  hours                                    predicted
# 1     5      \033[38;5;246m# A tibble: 6 x 1\033[39m
# 2    10                                        .pred
# 3    15   \033[3m\033[38;5;246m<dbl>\033[39m\033[23m
# 4    20                \033[38;5;250m1\033[39m 0.885
# 5    25                \033[38;5;250m2\033[39m 1.98 
# 6    30                \033[38;5;250m3\033[39m 3.08 

Solution

  • The predict() method called on lm_fit returns a tibble/data frame. I think you just want the predictions column of that data frame, .pred. You can get that through standard subsetting:

    expand.grid(hours = seq(5, 30, 5)) %>%
        mutate(predicted = predict(lm_fit, .)$.pred)
    
      hours predicted
    1     5  0.885311
    2    10  1.983003
    3    15  3.080695
    4    20  4.178386
    5    25  5.276078
    6    30  6.373770