s-news
[Top] [All Lists]

Re: regression line confidence interval

To: <ken.pierce@oregonstate.edu>, <s-news@lists.biostat.wustl.edu>
Subject: Re: regression line confidence interval
From: <Bill.Venables@csiro.au>
Date: Wed, 27 Apr 2005 09:01:52 +1000
Thread-index: AcVKqziNONrOiWuyTcyZT8LJmBTWfQABTuMw
Thread-topic: [S] regression line confidence interval
S is a programming language, with excellent graphical facilities, so why not write your own function?  Not only are you allowed to do that, but with S that's what you are supposed to do.  It can be quicker than google....
 
Here is a very simple first cut that treats the single predictor case in an obvious way:
 
showHourglass <- function(x, y) {
  dat <- data.frame(x = x, y = y)
  fm <- lm(y ~ x, dat)
  newdata = data.frame(x = seq(min(x), max(x), length = 500))
  pfm <- predict(fm, newdata = newdata, se = T)
  pfm$upper <- pfm$fit + 2*pfm$se
  pfm$lower <- pfm$fit - 2*pfm$se
  rg <- range(y, pfm$upper, pfm$lower)
  plot(x, y, ylim = rg, type = "n")
  points(x, y, pch = 1, col = 2)
  lines(newdata$x, pfm$upper, lty = 4, col = 3)
  lines(newdata$x, pfm$lower, lty = 4, col = 3)
  abline(coef(fm))
  invisible(fm)
}
 
Test drive:
 
x <- runif(50, 0, 10)
y <- x + rnorm(x)
showHourglass(x, y)
 
This gives the pointwise confidence interval for the mean (using '2' as the percentage point of the t-distribution).  If you want tolerance intervals (or prediction intervals for a single new observation) you need to be a bit more clever, but not much. 
 
Extend, generalize, tidy up, submit for others to use...  Easy.
 
Bill Venables.
-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu [mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of Pierce, Ken
Sent: Wednesday, 27 April 2005 7:59 AM
To: s-news@lists.biostat.wustl.edu
Subject: [S] regression line confidence interval

I've been searching for some time now how to add the hourglass shape confidence intervals to a simple (or not so simple) regression line. I've tried to search the archive and google but it has been in vain. A simple case might be just a simple linear model object with a single predictor.
 
Thanks,
 
Ken 
 
~~~~~~~~~~~~~~~~~~~~~~~
Kenneth B. Pierce Jr.
Research Ecologist
Forestry Sciences Laboratory
3200 SW Jefferson Way
Corvallis, OR 97331
ken.pierce@oregonstate.edu
541 750-7393
~~~~~~~~~~~~~~~~~~~~~~~~
 
<Prev in Thread] Current Thread [Next in Thread>