My goal is to create multiple models from a dataframe and then generate confidence intervals around the fitted values that correspond to those different models.
Pulling in libraries:
library(purrr)
library(dplyr)
library(modelr)
Assigning data_1 as the DNase data set from R:
data_1 <- DNase
Creating one unique model for each run:
model_dna <- data_1 %>% group_by(Run) %>%
do(model_dna = lm(conc ~ density, data = .)) %>% ungroup()
I then want to predict the fit and 95% confidence interval for that same set of data, but do so for each model individually. When including the interval = "confidence", the resulting table should produce a "fit" column of fitted values, as well as an "upr" and "lwr" column, representing the range of confidence around the fitted value. I tried this, as spread_predictions has worked before in helping to spread fitted values across multiple sets of data:
data_2 <- map(model_dna$model_dna, ~ spread_predictions(data = data_1, models = .x, interval = "confidence"))
However, the following error is generated:
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "character"
Does anyone know what the best way to produce these numbers are? Do I have to change the way this function is processing my data? Or is there a better function to use, i.e. somehow using predict() directly, which certainly takes interval as an argument (http://www.sthda.com/english/articles/40-regression-analysis/166-predict-in-r-model-predictions-and-confidence-intervals/)?