jthaman / ciTools

An R Package for Quick Uncertainty Intervals
GNU General Public License v3.0
106 stars 9 forks source link

Failure due to update with add_ci(type="boot") #50

Closed billdenney closed 3 years ago

billdenney commented 3 years ago

Thank you for the wonderful package!

I am a new user, and I'm having an issue with a model that I fit in one environment and need to use ciTools in another environment. My general use case is illustrated by the reprex below:

set.seed(5)
library(ciTools)
#> ciTools version 0.6.1 (C) Institute for Defense Analyses
library(tidyverse)

my_data <-
  data.frame(
    counts=c(18,17,15,20,10,20,25,13,12),
    outcome=rep(1:3, 3),
    treatment=rep(1:3, each=3)
  )

fit_my_data <- function(my_data_in_the_function) {
  link <- "log"
  glm.D93 <- glm(counts ~ outcome + treatment, data=my_data_in_the_function, family=gaussian(link=link))
  glm.D93
}

my_model <- fit_my_data(my_data)

# Works because it is not using `update()`
my_new_data <-
  my_data %>%
  add_ci(fit=my_model)

# Fails because `link` is not set in the current environment (it is only set in
# the function environment)
my_new_data <-
  my_data %>%
  add_ci(fit=my_model, type="boot")
#> Error in gaussian(link = link): object 'link' not found

# Works because now the call doesn't rely on the link from the current
# environment; the link comes from the model family.
my_model_update <- my_model
my_model_update$call$family <- my_model$family
my_new_data <-
  my_data %>%
  add_ci(fit=my_model_update, type="boot")
#> Warning in predict.lm(object, newdata, se.fit, scale = 1, type = if (type == :
#> prediction from a rank-deficient fit may be misleading

Created on 2021-05-17 by the reprex package (v2.0.0)