[R] plotting confidence intervals

Daniel Malter daniel at umd.edu
Tue Jul 14 19:46:18 CEST 2009


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.




More information about the R-help mailing list