[R] confidence band

From: Soare Marcian-Alin <soarealin_at_gmail.com>
Date: Thu, 31 May 2007 17:05:11 +0200


I made a function, which calculates the confidence interval and the prediction interval, but if I want to plot it, then it plots only the regressionline.
Maybe somebody can help me:

conf.band <- function(x,y) {
  res.lsfit <- lsfit(x,y)
  xi <- seq(from=40, to=160, length=200)   n <- length(x)
  MSE <- sqrt(sum(res.lsfit$residuals^2)/(n-2))

# confidence interval

  band <- sqrt(2*qf(0.95,2,n-2)) * MSE * sqrt(1/n+(xi-length(x))^2/(var(x)*(n-1)))   uiv <- res.lsfit$coef[1] + res.lsfit$coef[2]*xi - band   oiv <- res.lsfit$coef[1] + res.lsfit$coef[2]*xi + band

# prediction interval

  band <- sqrt(2*qf(0.95,2,n-2)) * MSE * sqrt(1+1/n +(xi-mean(x))^2/(var(x)*(n-1)))
  uip <- res.lsfit$coef[1] + res.lsfit$coef[2]*xi - band   oip <- res.lsfit$coef[1] + res.lsfit$coef[2]*xi + band

# creating the graphik

  plot(x, y, xlab=colnames(x), ylab=colnames(y), pch=19)   abline(res.lsfit, col=1)
  matlines(xi, cbind(uiv,oiv), col=3, lty=2, lwd=2)   matlines(xi, cbind(uiv,oip), col=2, lty=3, lwd=2) }

Mit freundlichen Grüssen / Best Regards

Soare Marcian-Alin

	[[alternative HTML version deleted]]

______________________________________________ R-help_at_stat.math.ethz.ch 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.

Received on Thu 31 May 2007 - 15:40:18 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 31 May 2007 - 16:31:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.