title: Tidymodels package: Model predictions to find the best model fit using the juice() and bake() functions in R
body: <p><strong>Overview</strong></p>
  <p>I have produced four models using the <strong>tidymodels package</strong> with the <strong>data frame FID</strong> (see below):</p>
  <ol>
  <li><strong>General Linear Model</strong></li>
  <li><strong>Bagged Tree</strong></li>
  <li><strong>Random Forest</strong></li>
  <li><strong>Boosted Trees</strong></li>
  </ol>
  <p>The data frame contains <strong>three predictors</strong>:</p>
  <ol>
  <li>Year (numeric)</li>
  <li>Month (Factor)</li>
  <li>Days (numeric)</li>
  </ol>
  <p><strong>The dependent variable is Frequency (numeric)</strong></p>
  <p><strong>Aim</strong></p>
  <p>My aim is to undertake model predictions to extract the <strong>class</strong> and <strong>probability</strong> values for all <strong>fitted models</strong>, which have all undergone <strong>10 fold cross-validation</strong>.</p>
  <p>I am attempting to use the functions <strong>prep(), juice(), and bake()</strong> in order to generate the correct data objects for model predictions objects by following this tutorial below.</p>
  <p><strong>Tutorial (see screenshots below)</strong></p>
  <p><a href="https://meghan.rbind.io/post/tidymodels-intro/" rel="nofollow noreferrer">https://meghan.rbind.io/post/tidymodels-intro/</a></p>
  <p>After producing the model prediction values (i.e class and probability) for all four models, the ultimate aim is to produce <strong>confusion matrices</strong> and <strong>receiver operating curves (ROC)</strong> to evaluate all models. Therefore, I need to bind the <strong>true values</strong> from the testing data, with the <strong>class</strong> and <strong>probability</strong> columns extracted from these model predictions.</p>
  <p><strong>Issue</strong></p>
  <p>I am trying to run the predict() function to produce the <strong>class and probability values</strong> from the tutorial (see screenshots below and the link above), but I am experiencing this error message below.</p>
  <p><strong>Error Messages</strong></p>
  <pre><code>##Class prediction object
  Error in UseMethod(&quot;predict&quot;) : 
    no applicable method for 'predict' applied to an object of class &quot;c('tbl_df', 'tbl', 'data.frame')&quot;
  
  ##Prob
  Error in UseMethod(&quot;predict&quot;) : 
    no applicable method for 'predict' applied to an object of class &quot;c('resample_results', 'tune_results', 'tbl_df', 'tbl', 'data.frame')&quot;
  </code></pre>
  <p>If anyone is able to help, I would be deeply appreciative</p>
  <p>Many thanks in advance.</p>
  <p><strong>Screen-shots from the tutorial</strong></p>
  <p><a href="https://i.stack.imgur.com/koEB9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/koEB9.png" alt="enter image description here" /></a>
  <a href="https://i.stack.imgur.com/zud5y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zud5y.png" alt="enter image description here" /></a></p>
  <p><strong>R-code</strong></p>
  <pre><code>    ##################################################
      ##Model Prediction
      ###################################################
      ##Open the tidymodels package
      library(tidymodels)
      library(tidyverse)
      library(glmnet)
      library(parsnip)
      library(rpart)
      library(tidyverse) # manipulating data
      library(skimr) # data visualization
      library(baguette) # bagged trees
      library(future) # parallel processing &amp; decrease computation time
      library(xgboost) # boosted trees
      library(ranger)
      library(yardstick)
      library(purrr)
      library(forcats)    
  
  ###########################################################
  #split this single dataset into two: a training set and a testing set
  data_split &lt;- initial_split(FID)
  # Create data frames for the two sets:
  train_data &lt;- training(data_split)
  test_data  &lt;- testing(data_split)
  
  # resample the data with 10-fold cross-validation (10-fold by default)
  cv &lt;- vfold_cv(train_data, v=10)
  
  ###########################################################
  ##Produce the recipe
  
  rec &lt;- recipe(Frequency ~ ., data = FID) %&gt;% 
            step_nzv(all_predictors(), freq_cut = 0, unique_cut = 0) %&gt;% # remove variables with zero variances
            step_novel(all_nominal()) %&gt;% # prepares test data to handle previously unseen factor levels 
            step_medianimpute(all_numeric(), -all_outcomes(), -has_role(&quot;id vars&quot;))  %&gt;% # replaces missing numeric observations with the median
            step_dummy(all_nominal(), -has_role(&quot;id vars&quot;)) # dummy codes categorical variables
  
  ###########################################################
  ##Create Models
  ###########################################################
  
  ##########################################################
  ##General Linear Models
  #########################################################
  
  ##glm
  mod_glm&lt;-linear_reg(mode=&quot;regression&quot;,
                         penalty = 0.1, 
                         mixture = 1) %&gt;% 
                              set_engine(&quot;glmnet&quot;)
  
  ##Create workflow
  wflow_glm &lt;- workflow() %&gt;% 
                  add_recipe(rec) %&gt;%
                        add_model(mod_glm)
  
  ##Fit the model
  plan(multisession)
  
  fit_glm &lt;- fit_resamples(
                          wflow_glm,
                          cv,
                          metrics = metric_set(rmse, rsq),
                          control = control_resamples(save_pred = TRUE,
                                extract = function(x) extract_model(x)))
  
  ##########################################################
  ##Bagged Trees
  ##########################################################
  
  #####Bagged Trees
  mod_bag &lt;- bag_tree() %&gt;%
              set_mode(&quot;regression&quot;) %&gt;%
                set_engine(&quot;rpart&quot;, times = 10) #10 bootstrap resamples
                  
  
  ##Create workflow
  wflow_bag &lt;- workflow() %&gt;% 
                     add_recipe(rec) %&gt;%
                         add_model(mod_bag)
  
  ##Fit the model
  plan(multisession)
  
  fit_bag &lt;- fit_resamples(
                        wflow_bag,
                        cv,
                        metrics = metric_set(rmse, rsq),
                        control = control_resamples(save_pred = TRUE,
                                extract = function(x) extract_model(x)))
  ###################################################
  ##Random forests
  ###################################################
  
  mod_rf &lt;-rand_forest(trees = 1e3) %&gt;%
                                set_engine(&quot;ranger&quot;,
                                num.threads = parallel::detectCores(), 
                                importance = &quot;permutation&quot;, 
                                verbose = TRUE) %&gt;% 
                                set_mode(&quot;regression&quot;) 
                                
  ##Create Workflow
  
  wflow_rf &lt;- workflow() %&gt;% 
                 add_model(mod_rf) %&gt;% 
                       add_recipe(rec)
  
  ##Fit the model
  
  plan(multisession)
  
  fit_rf&lt;-fit_resamples(
               wflow_rf,
               cv,
               metrics = metric_set(rmse, rsq),
               control = control_resamples(save_pred = TRUE,
                                           extract = function(x) extract_model(x)))
  
  ############################################################
  ##Boosted Trees
  ############################################################
  
  mod_boost &lt;- boost_tree() %&gt;% 
                   set_engine(&quot;xgboost&quot;, nthreads = parallel::detectCores()) %&gt;% 
                        set_mode(&quot;regression&quot;)
  
  ##Create Workflow
  
  wflow_boost &lt;- workflow() %&gt;% 
                    add_recipe(rec) %&gt;% 
                      add_model(mod_boost)
  
  ##Fit model
  
  plan(multisession)
  
  fit_boost &lt;-fit_resamples(
                         wflow_boost,
                         cv,
                         metrics = metric_set(rmse, rsq),
                         control = control_resamples(save_pred = TRUE,
                                           extract = function(x) extract_model(x)))
  
  ##################################################
  ##Prep the models for model prediction
  ##################################################
  
  # Extract our prepped training data 
  # and &quot;bake&quot; our testing data
  
  prep&lt;-prep(rec)
  
  training_baked&lt;-juice(prep)
  
  testing_baked &lt;- prep %&gt;% bake(test_data) 
  
  # Run the model with our training data
  # Find the class predictions from our testing data
  # And add back in the true values from testing data
  
  predictions_class &lt;-  %&gt;% fit_glm %&gt;%
                                predict(new_data = testing_baked) %&gt;%
                                    bind_cols(testing_baked %&gt;% dplyr::select(Frequency))
  
  ##Error message
  
    Error in UseMethod(&quot;predict&quot;) : 
    no applicable method for 'predict' applied to an object of class &quot;c('tbl_df', 'tbl', 'data.frame')&quot;
      
  # Find the probability predictions
  # And add all together
  
  predictions_Prob &lt;- fit_glm %&gt;%
                          predict(testing_baked, type = &quot;prob&quot;) %&gt;%
                                bind_cols(predictions_class)
  
       ##Error message
       Error in UseMethod(&quot;predict&quot;) : 
    no applicable method for 'predict' applied to an object of class &quot;c('resample_results', 'tune_results', 'tbl_df', 'tbl', 'data.frame')&quot;
  </code></pre>
  <p><strong>Data frame - FID</strong></p>
  <pre><code>structure(list(Year = c(2015, 2015, 2015, 2015, 2015, 2015, 2015, 
  2015, 2015, 2015, 2015, 2015, 2016, 2016, 2016, 2016, 2016, 2016, 
  2016, 2016, 2016, 2016, 2016, 2016, 2017, 2017, 2017, 2017, 2017, 
  2017, 2017, 2017, 2017, 2017, 2017, 2017), Month = structure(c(1L, 
  2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 
  5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
  8L, 9L, 10L, 11L, 12L), .Label = c(&quot;January&quot;, &quot;February&quot;, &quot;March&quot;, 
  &quot;April&quot;, &quot;May&quot;, &quot;June&quot;, &quot;July&quot;, &quot;August&quot;, &quot;September&quot;, &quot;October&quot;, 
  &quot;November&quot;, &quot;December&quot;), class = &quot;factor&quot;), Frequency = c(36, 
  28, 39, 46, 5, 0, 0, 22, 10, 15, 8, 33, 33, 29, 31, 23, 8, 9, 
  7, 40, 41, 41, 30, 30, 44, 37, 41, 42, 20, 0, 7, 27, 35, 27, 
  43, 38), Days = c(31, 28, 31, 30, 6, 0, 0, 29, 15, 
  29, 29, 31, 31, 29, 30, 30, 7, 0, 7, 30, 30, 31, 30, 27, 31, 
  28, 30, 30, 21, 0, 7, 26, 29, 27, 29, 29)), row.names = c(NA, 
  -36L), class = &quot;data.frame&quot;)
  </code></pre>