[R] Drawing the regression line and the 95% confidence intervals

Jim Lemon drjimlemon at gmail.com
Thu May 7 00:01:51 CEST 2015


Hi Sacha,
As Brian noted, you will have to expand the vertical axis. Here is one
way to do it.

plot(GDP.per.head, fitted(LinearModel.1),ylim=c(-60,1200))
devlm1<-lm(fitted(LinearModel.1)~GDP.per.head)
abline(devlm1)
conflm1<-confint(devlm1)
abline(coef=conflm1[,1],lty=2)
abline(coef=conflm1[,2],lty=2)
predict(LinearModel.1, interval = "prediction")
library(plotrix)
predmat<-predict(LinearModel.1, interval = "prediction")
dispersion(GDP.per.head,fitted(LinearModel.1),
 predmat[,3],predmat[,2],interval=FALSE)

You may want to do something about the overlapping points.

Jim


On Thu, May 7, 2015 at 5:52 AM, David L Carlson <dcarlson at tamu.edu> wrote:
> Something like this?
>
> # Compute the prediction limits and get their range to set ylim=
> plim <- predict(LinearModel.1, interval = "prediction")
> rnge <- c(min(plim[ , 2]), max(plim[ , 3]))
> plot(GDP.per.head, fitted(LinearModel.1),ylim=rnge)
>
> # As before
> devlm1<-lm(fitted(LinearModel.1)~GDP.per.head)
> abline(devlm1)
> conflm1<-confint(devlm1)
> abline(coef=conflm1[,1],lty=2)
> abline(coef=conflm1[,2],lty=2)
>
> # Plot the prediction limits
> segments(GDP.per.head, plim[ , 2], GDP.per.head, plim[ , 3], col="gray")
>
> -------------------------------------
> David L Carlson
> Department of Anthropology
> Texas A&M University
> College Station, TX 77840-4352
>
>
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Cade, Brian
> Sent: Wednesday, May 6, 2015 2:22 PM
> To: varin sacha
> Cc: R-help Mailing List
> Subject: Re: [R] Drawing the regression line and the 95% confidence intervals
>
> The prediction intervals are likely to be much wider than the confidence
> intervals so you will need to be sure you scale the yaxis limits large
> enough to see them.
>
> Brian
>
> Brian S. Cade, PhD
>
> U. S. Geological Survey
> Fort Collins Science Center
> 2150 Centre Ave., Bldg. C
> Fort Collins, CO  80526-8818
>
> email:  cadeb at usgs.gov <brian_cade at usgs.gov>
> tel:  970 226-9326
>
>
> On Wed, May 6, 2015 at 12:59 PM, varin sacha <varinsacha at yahoo.fr> wrote:
>
>> Dear Jim,
>>
>> I really thank you lots, it perfectly works !
>> The reproducible example is below.
>>
>> Last thing : I can easily get the predictions intervals but I don't get to
>> draw them.
>> If I want to draw on the same graph (the one I already have the confidence
>> intervals) the prediction intervals. I have tried the abline function
>> without success. How can I do ?
>>
>> GDP.per.head=c(600,560,340,560,580,300,570,900,680,290,590,340)
>> Quality.score=c(4.5,6.5,6,4.5,7,3,9,10,12.5,6.5,7,9)
>> Competitivness.score=c(1000,1200,1400,700,680,1010,340,560,690,500,690,460)
>> LinearModel.1=lm(GDP.per.head ~ Quality.score + Competitivness.score)
>>
>> plot(GDP.per.head, fitted(LinearModel.1),ylim=c(200,800))
>> devlm1<-lm(fitted(LinearModel.1)~GDP.per.head)
>> abline(devlm1)
>> conflm1<-confint(devlm1)
>> abline(coef=conflm1[,1],lty=2)
>> abline(coef=conflm1[,2],lty=2)
>> predict(LinearModel.1, interval = "prediction")
>>
>> Many thanks once more for your help.
>> Sacha
>>
>>
>> ----- Mail original -----
>> De : Jim Lemon <drjimlemon at gmail.com>
>> À : varin sacha <varinsacha at yahoo.fr>
>> Cc : R-help Mailing List <r-help at r-project.org>
>> Envoyé le : Mercredi 6 mai 2015 1h02
>> Objet : Re: [R] Drawing the regression line and the 95% confidence
>> intervals
>>
>> Hi Sacha,
>> The line you have requested is off the plot. The following will
>> produce what I think you are asking, but I cannot speak for whether it
>> means anything sensible.
>>
>> plot(GDP.per.head, fitted(LinearModel.1),ylim=c(200,800))
>> devlm1<-lm(fitted(LinearModel.1)~GDP.per.head)
>> abline(devlm1)
>> conflm1<-confint(devlm1)
>> abline(coef=conflm1[,1],lty=2)
>> abline(coef=conflm1[,2],lty=2)
>>
>> Jim
>>
>>
>>
>> On Wed, May 6, 2015 at 8:01 AM, varin sacha <varinsacha at yahoo.fr> wrote:
>> > Hi, Dear R-helpers,
>> >
>> > Here below you will find a reproducible fictitious example working
>> except the "abline" function.
>> >
>> > First thing : I try to draw the regression line (multiple linear
>> regression). I try the "abline" function but it does not work. I don't get
>> any error message but the straight line does not appear on the scatterplot.
>> >
>> > Second thing : I try to draw the 95% confidence intervals on the
>> regression line. How could I do ?
>> >
>> > Using abline(0,1), I can of course add a line 45 degrees angle passing
>> through the origin (intercept=0 and slope=1), but it is not what I am
>> looking for.
>> >
>> > GDP.per.head=c(600,560,340,560,580,300,570,900,680,290,590,340)
>> > Quality.score=c(4.5,6.5,6,4.5,7,3,9,10,12.5,6.5,7,9)
>> >
>> Competitivness.score=c(1000,1200,1400,700,680,1010,340,560,690,500,690,460)
>> > LinearModel.1=lm(GDP.per.head ~ Quality.score + Competitivness.score)
>> > plot(GDP.per.head, fitted(LinearModel.1))
>> > abline(GDP.per.head, fitted(LinearModel.1))
>> >
>> > Thanks for your help. Looking forward to reading you.
>> >
>> > Sacha
>> >
>> > ______________________________________________
>> > 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.
>>
>> ______________________________________________
>> 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.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
> ______________________________________________
> 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