Trying to figure out how to combine 2 graphs into 1 plot.
My data set is something like:
x,y,label
I want to plot a regression line (using lm) plus the prediction interval (using predict) of two subsets of my data.
Say I want label==A and label==B. These subsets (A and B) do not have the same number of rows.
So the plot show show two regression lines each with their own prediction interval.
Doing this for the confidence interval is easy (se=TRUE within geom_smooth), but the prediction interval needs to be calculated separately.
Something like:
subsetmydata <- subset(mydata, label=="A" | label=="B")
fit <- lm(y ~ x, data=subsetmydata)
prediction_fit <- predict(fit, interval="prediction", level=0.95)
combined_analysis <- data.frame(subsetmydata, prediction_fit)
ggplot(combined_analysis, aes(x=x, y=y, linetype=label)+.....
This however, only calculates the prediction interval of the whole subset and not the two subsets.
Is there a way to do the and calculations separately and then combining them into one dataset and use that in ggplot?