Hi R People: If I have a fitted values from a model, how do I plot the (1-alpha)100% confidence intervals along with the fitted values, please? Also, if the intervals are "shaded" gray, that would be nice too, please? I check confint, but that doesn't seem to do what I want. Thanks in advance, Sincerely, Erin -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodgess at gmail.com
On Jul 14, 2009, at 10:40 AM, Erin Hodgess wrote:> Hi R People: > > If I have a fitted values from a model, how do I plot the > (1-alpha)100% confidence intervals along with the fitted values, > please? > > Also, if the intervals are "shaded" gray, that would be nice too, > please? > > I check confint, but that doesn't seem to do what I want. > > Thanks in advance, > Sincerely, > ErinErin, confint() will get you the CI's for the model coefficients. Presuming that you are referring to a lm() model, take a look at ? predict.lm and note the 'interval' argument and the examples therein. If you are looking for CIs for the fitted line ('narrow'), you will want to use 'interval = "confidence"'. If you are looking for CI's for future observations ('wide'), you will want to use 'interval = "prediction". For shading, using the example in ?predict.lm and presuming that you want "confidence": x <- rnorm(15) y <- x + rnorm(15) new <- data.frame(x = seq(-3, 3, 0.5)) pred.w.clim <- predict(lm(y ~ x), new, interval="confidence") # Just create a blank plot region with axes first. We'll add to this plot(range(new$x), range(pred.w.clim), type = "n", ann = FALSE) # For convenience CI.U <- pred.w.clim[, "upr"] CI.L <- pred.w.clim[, "lwr"] # Create a 'loop' around the x values. Add values to 'close' the loop X.Vec <- c(new$x, tail(new$x, 1), rev(new$x), new$x[1]) # Same for y values Y.Vec <- c(CI.L, tail(CI.U, 1), rev(CI.U), CI.L[1]) # Use polygon() to create the enclosed shading area # We are 'tracing' around the perimeter as created above polygon(X.Vec, Y.Vec, col = "grey", border = NA) # Use matlines() to plot the fitted line and CI's # Add after the polygon above so the lines are visible matlines(new$x, pred.w.clim, lty = c(1, 2, 2), type = "l", col = c("black", "red", "red")) See ?polygon and ?matlines HTH, Marc Schwartz
Hi Erin, have a look at the following example: #Simulate data n=1000 x1=rnorm(n,0,0.05) x2=rnorm(n,0,0.1) x3=rnorm(n,0,0.02) e=rnorm(n,0,1) y=x1+2*x2-0.5*x3+e #Run regression reg=lm(y~x1+x2+x3) #Regression output summary(reg)$coef #Create dataset with confidence intervals and an index CI=data.frame(summary(reg)$coef[,1]-2*summary(reg)$coef[,2],summary(reg)$coe f[,1],summary(reg)$coef[,1]+2*summary(reg)$coef[,2],1:4) names(CI)=c("lower","estimate","upper","index") #inspect CI #now plot #first a dotchart with coefficients "CI$estimate" dotchart(CI$estimate,labels=row.names(summary(reg)$coef),xlim=c(min(CI[,1:3] ),max(CI[,1:3])),ylim=c(min(CI$index-1),max(CI$index+1)),pch=16,lcolor=NA) #use lables from the regression output for the dotchart #xlim assures there is enough space for the conf intervals #ylime assures the plot looks nicely #pch=16 puts a filled dot #lcolor=NA removes the dotted lines on the chart #now plot the confidence intervals for(i in 1:max(CI$index)){ lines(c(CI$lower[i],CI$upper[i]),c(i,i)) } #for each index (i.e., estimated coefficient) #plot x-coordinates CI$upper and CI$lower #at y-coordinate = index HTH, Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Urspr?ngliche Nachricht----- Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im Auftrag von Erin Hodgess Gesendet: Tuesday, July 14, 2009 11:40 AM An: R help Betreff: [R] plotting confidence intervals Hi R People: If I have a fitted values from a model, how do I plot the (1-alpha)100% confidence intervals along with the fitted values, please? Also, if the intervals are "shaded" gray, that would be nice too, please? I check confint, but that doesn't seem to do what I want. Thanks in advance, Sincerely, Erin -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodgess at gmail.com ______________________________________________ R-help at 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.
Erin and all, I lavishly used the factor 2 for the confidence intervals. Of course this should be approximately 1.96 for the 95% CI under the assumption of normality. So just adjust the factor accordingly or according to your desired alpha level when you create the CI data frame. Otherwise, my example should be fine. Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Urspr?ngliche Nachricht----- Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im Auftrag von Erin Hodgess Gesendet: Tuesday, July 14, 2009 11:40 AM An: R help Betreff: [R] plotting confidence intervals Hi R People: If I have a fitted values from a model, how do I plot the (1-alpha)100% confidence intervals along with the fitted values, please? Also, if the intervals are "shaded" gray, that would be nice too, please? I check confint, but that doesn't seem to do what I want. Thanks in advance, Sincerely, Erin -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodgess at gmail.com ______________________________________________ R-help at 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.
Erin Hodgess wrote:> Hi R People: > > If I have a fitted values from a model, how do I plot the > (1-alpha)100% confidence intervals along with the fitted values, > please? > > Also, if the intervals are "shaded" gray, that would be nice too, please? > > I check confint, but that doesn't seem to do what I want. > >Hi Erin, Probably the simplest way is to plot points with "error bars" as in the following example: coefficients<-rnorm(5)+4 plot(coefficients,main="Coefficient plot",xaxt="n",ylim=c(0,6),xlab="Coefficients") axis(1,at=1:5,labels=paste("Coef",1:5,sep="")) dispersion(1:5,coefficients,abs(rnorm(5)),col="lightgray") The dispersion function in the plotrix package is just one way to illustrate confidence intervals. Jim
I believe there is a set of options in the ggplot2 package that will create a plot and add the confidence region to it, you will need to look at the documentation for ggplot2, I don't know the details (have not made it that far on my to do list, not anything against the package). -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Erin Hodgess > Sent: Tuesday, July 14, 2009 9:40 AM > To: R help > Subject: [R] plotting confidence intervals > > Hi R People: > > If I have a fitted values from a model, how do I plot the > (1-alpha)100% confidence intervals along with the fitted values, > please? > > Also, if the intervals are "shaded" gray, that would be nice too, > please? > > I check confint, but that doesn't seem to do what I want. > > Thanks in advance, > Sincerely, > Erin > > > -- > Erin Hodgess > Associate Professor > Department of Computer and Mathematical Sciences > University of Houston - Downtown > mailto: erinm.hodgess at gmail.com > > ______________________________________________ > R-help at 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.