My goal is to create multiple models and then using a novel dataset, create prediction values for that new data set and the corresponding prediction intervals around each of those new fitted points.
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 a set of points with a new data set, lets call it data_2, for each model, and then build prediction intervals around each fitted point (the upper and lower bounds of the prediction interval for each point, as produced by the function predict() when the argument interval = "prediction" is included. I successfully generated fitted values like this:
data_2 <- map(model_dna$model_dna, ~ spread_predictions(data = data_2, models = .x)
But then struggle to add in the "upr" and "lwr" columns for these newly fitted values. Is there a way to perhaps simultaneously "spread_prediction_intervals" when fitting these new points? It would be very helpful to understand how to do this for multiple data sets, as well as if given the model which was used to generate predicted values and a set of those predicted values, be able to then produce the upr and lwr bounds of the prediction interval. Thank you very much for your help in advance.
Apparently you can construct the confidence intervals yourself using the results of predict
.
data_2 <- map(model_dna$model_dna, function(x) {
preds=predict(x, data_1, se.fit=TRUE)
mutate(data_1, fit=preds$fit, lwr=fit-preds$se.fit*1.96, upr=fit+preds$se.fit*1.96)
})
If you don't care about the confidence intervals, you can use map
with add_predictions
or use spread_predictions
to create one big data frame.
data_2 <- map(model_dna$model_dna, ~ add_predictions(data = data_1, model = .x))
data_2=spread_predictions(data_1, mods[[1]], mods[[2]], mods[[3]], mods[[4]], mods[[5]], mods[[6]],
mods[[7]], mods[[8]], mods[[9]], mods[[10]], mods[[11]])