[R] confidence interval or error of x intercept of a linear

Kevin J Emerson kemerson at uoregon.edu
Wed Mar 25 03:15:24 CET 2009


I wanted to send out a quick thanks to all that replied to my query about estimating the confidence interval around the x-intercept of a linear regression.  The method I was able to implement in the most straightforward way was taken from Section 3.2 of Draper and Smith (1998). Applied Regression Analysis. The code follows - its not the most fancy code, but it gets the job done.  I will see if I can work out the other suggestions that were made and see how they all turn out.  

xInterceptCI <- function(x, alpha = 0.05, ...) {
  intercept <- coef(x)[1]              
  slope <- coef(x)[2]                  
  meanX <- mean(x$model[,2])           
  n <- length(x$model[,2])             
  tstar <- qt(alpha/2, n-2)            
  sxx <- sum(x$model[,2]^2) - sum(x$model[,2])^2 / n
  SSresidual <- (1-cor(x$model[,1], x$model[,2])^2) * 
                (sum(x$model[,1]^2)-sum(x$model[,1])^2/n)
  S <- sqrt(SSresidual/(n-2))
  SEslope <- S / sqrt(sxx)
  Xintercept <- - intercept / slope
  y0 <- 0
  g <- (tstar / (slope/SEslope))^2
  left <- (Xintercept - meanX) * g
  bottom <- 1 - g
  Right <- (tstar * S / slope) * sqrt( ((Xintercept - meanX)^2/sxx) + bottom/n)
  lower <- Xintercept + (left + Right) / bottom
  upper <- Xintercept + (left - Right) / bottom
  return(c(lower,upper))
}

On Tue, 24 Mar 2009 17:16:41 +0100, Peter Dalgaard <P.Dalgaard at biostat.ku.dk> wrote:
> (Ted Harding) wrote:
> > On 24-Mar-09 03:31:32, Kevin J Emerson wrote:
> ...
> > When I have time for it (not today) I'll see if I can implement
> > this neatly in R. It's basically a question of solving
> > 
> >   (N-2)*(1 - R(X0))/R(X0) = qf(P,1,(N-1))
> > 
> > for X0 (two solutions, maybe one, if any exist). 
> <etc.>
> 
> A quick and probably not-too-dirty way is to rewrite it as a nonlinear
> model:
> 
> x <- 1:10
> Y <- 2*x - 3 + rnorm(10, sd=.1)
> cf <- coef(lm(Y~x))
> confint(nls(Y~beta*(x-x0), start=c(beta=cf[[2]],x0=-cf[[1]]/cf[[2]])))
> 
> 
> 
> 
> -- 
>    O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
>   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
>  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
> ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907
> 


--
==========
==========
Kevin J Emerson
Bradshaw-Holzapfel Lab
Center for Ecology and Evolutionary Biology
1210 University of Oregon
Eugene, Oregon 97403
kemerson at uoregon.edu



More information about the R-help mailing list