[R] extend lines of prediction interval ggplot2

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Wed Mar 15 02:58:40 CET 2017


Define your new_df before you calculate temp_var, and give it to predict so predict doesn't start from your original x values. Add temp_var to new_df.

Read ?predict.lm.

Don't create matrices with cbind before creating data frames... data frames can have a variety of data storage modes (types) for the columns, matrices cannot. 

fit <-lm(y~x) 
new_df <- data.frame(x,y) 
new_df$temp_var <- predict(fit, newdata=new_df, interval="prediction") 

-- 
Sent from my phone. Please excuse my brevity.

On March 14, 2017 4:39:51 AM PDT, Andras Farkas via R-help <r-help at r-project.org> wrote:
>Dear All, 
>
>would you have some thoughts on how to extend the prediction interval
>lines to beyond the "range of data"? 
>
>
>example: 
>
>y <-c(0.4316092,0.4156757,0.3517915,0.3669508,0.3899471,0.3964143, 
>0.4001074,0.3851003,0.4222451,0.375324,0.3652045,0.3376978,0.383012, 
>0.3763665,0.3550609,0.2958678,0.3726571,0.3442298 
>#,0.3403275,0.2973978 
>)*100 
>x <-seq(1,length(y),1) 
>
>z<-c("07/01/2015","08/01/2015","09/01/2015","10/01/2015","11/01/2015", 
>"12/01/2015","01/01/2016","02/01/2016","03/01/2016","04/01/2016","05/01/2016",
>
>"06/01/2016","07/01/2016","08/01/2016","09/01/2016","10/01/2016","11/01/2016",
>
>"12/01/2016","01/01/2017","02/01/2017") 
>
>fit <-lm(y~x) 
>
>temp_var <- predict(fit, interval="prediction") 
>
>new_df <- data.frame(cbind(x,y, temp_var)) 
>#new_df$x<-factor(new_df$x, ordered = T) 
>
>library(ggplot2) 
>ggplot(new_df, aes(x,y))+ 
>geom_point() + 
>theme(panel.background = element_rect(fill = 'white', colour =
>'black'))+ 
>geom_line(aes(y=lwr), color = "black", linetype = "dashed",size=0.75)+ 
>geom_line(aes(y=upr), color = "black", linetype = "dashed",size=0.75)+ 
>scale_x_discrete(limits=z)+ 
>theme(axis.text.x = element_text(angle = 45, hjust = 1))+ 
>theme(panel.grid.major=element_line(colour = "grey"))+ 
>lims(y=c(0,50))+ 
>geom_smooth(method=lm,
>se=TRUE,fullrange=TRUE,fill="darkgrey",col="black")+labs(title =
>paste("Adj R2 = ",signif(summary(fit)$adj.r.squared, 4), 
>"Intercept =",signif(fit$coef[[1]],4 ), 
>" Slope =",signif(fit$coef[[2]], 4) 
># " P =",signif(summary(fit)$coef[2,4], 3) 
>))+ 
>ggtitle("Consumption Over Time") + 
>theme(plot.title = element_text(hjust = 0.5))+ 
>labs(y="y",x="x")+ 
>geom_point(shape=15,aes(x=c(7),y=new_df[,2][7]), color="black",cex=4)+ 
>geom_point(shape=15,aes(x=c(8),y=new_df[,2][8]), color="black",cex=4)+ 
>geom_point(shape=17,aes(x=c(19),y=0.3403275*100), color="black",cex=4)+
>
>geom_point(shape=17,aes(x=c(20),y=0.2973978*100), color="black",cex=4) 
>
>
>as you  will see the regresssion line and confidence interval is
>extended, but would also want to extend the prediction interval lines
>to the "same length"... Wonder if you have any insights to this
>question... 
>
>
>appreciate the help, 
>
>
>Andras Farkas
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list