title: Getting persistent error when trying to predict new data from a GLM in R
body: <p>I'm getting a persistent error &quot;Error: variables ‘x1’, ‘x2’, ‘x3’ were specified with different types from the fit&quot; while trying to predict the outcome of new data using predict in R. I've run this code with success on other models, but for some reason I can't figure out what is wrong with this one. I've replicated the issue with the following code:</p>
  <pre><code># make data
  set.seed(19870630)
  n &lt;- 1000
  df &lt;- data.frame(y = rgamma(n, shape = .5, rate = 1),
                   #runif(n, 0, 1), # trows same error
                   x1 = runif(n, 0, 100),
                   x2 = runif(n, 0, 100),
                   x3 = runif(n, -1, 1))
  
  df$x2 &lt;-  df$x1*df$x1
  
  # refine data by scaling
  df$x1 &lt;- scale(df$x1, center = TRUE)
  df$x2 &lt;- scale(df$x2, center = TRUE)
  df$x3 &lt;- scale(df$x3, center = TRUE)
  
  # double check
  head(df); plot(df)
  
  # fit model
  mod &lt;- glm(y ~ x1 + x2 + x3, data = df, family=Gamma(link=&quot;log&quot;))
  
  # confirm, success
  summary(mod)
  
  
  # make data to retain predictions
  ## first get realistic ranges of variables of interest, other vars will be held at mean
  (x1_span &lt;- c(rep(seq(min(df$x1), max(df$x1)), length = 50)))
  (x2_span &lt;- c(rep(seq(min(df$x2), max(df$x2)), length = 50)))
  
  
  df_pred_x1_x2 &lt;- data.frame(x1 = x1_span,
                              x2 = x2_span,
                              x3 = mean(df$x3))
  
  # generate function for prediction ml predicted values
  predict_fun &lt;- function(my_glm) {
    predict(my_glm, newdata = df_pred_x1_x2)   # this is predict.glm
  }
  
  df_pred_x1_x2$y_value_pred &lt;- predict_fun(mod) # error
  
  # &quot;Error: variables ‘x1’, ‘x2’, ‘x3’ were specified with different types from the fit&quot;
                                                        
  # End March 8, 2021
  </code></pre>
  <p>Any help would be appreciated, thank you.</p>