you can use the newdata statement in the predict function, as in n<-50 x<-sample(40:70,n,rep=T) y<-.7*x+rnorm(n,sd=5) plot(x,y,xlim=c(20,90),ylim=c(0,80)) mylm<-lm(y~x) abline(mylm,col="red") newx<-seq(20,90) prd<-predict(mylm,newdata=data.frame(x=newx),interval = c("confidence"), level = 0.90,type="response") lines(newx,prd[,2],col="red",lty=2) lines(newx,prd[,3],col="red",lty=2)
hth. Tim Holland schrieb: > Hello, > > I am trying to generate a confidence interval (90 or 95%) of a regression > line. This is primarily just for illustration on a scatter plot (i.e. I am > trying to make this > http://www.ast.cam.ac.uk/~rgm/scratch/statsbook/graphics/anima4.gif). > > I have been trying to use the predict.lm function, with interval set as > "confidence", but this still seems to be giving me a prediction interval (as > below). It is giving the interval based on the x value of a given point, as > opposed to the confidence interval of the regression line in general. Is > predict.lm the correct function to be using, or is there something else more > appropriate? > > >> predict.lm(test, interval = c("confidence"), level = 0.90) >> > fit lwr upr > 1 4.170807 -0.2171226 8.558738 > 2 5.807453 2.1283100 9.486597 > 3 6.625776 3.2397994 10.011753 > 4 8.262422 5.2795295 11.245315 > 5 9.080745 6.1821741 11.979317 > 6 9.899068 6.9954692 12.802667 > 7 7.444099 4.2947040 10.593495 > 8 13.172360 9.4577068 16.887014 > 9 20.537267 13.0143098 28.060224 > > I have also tried using package 'ggplot' as follows: > > >> install.packages("ggplot") >> library(ggplot) >> qplot(wt, mpg, data=mtcars, type=c("point","smooth"), method=lm) >> > (following Hadley Wickham on this help list) > > This generates a plot that looks roughly like what I want, but there is some > customizing of the plot that I would prefer to do in the standard 'plot' > function of the 'graphics' package. Also, ggplot does not give me a vector > of the values of the CI themselves, which would be useful to have. > > Any help would be very appreciated. > > Thanks, > Tim > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > 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. > -- Eik Vettorazzi Institut für Medizinische Biometrie und Epidemiologie Universitätsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/42803-8243 F ++49/40/42803-7790 ______________________________________________ R-help@r-project.org mailing list 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.