| 1 |
linesHyperb.lm <- |
| 2 |
function(object, c.prob = .95, confidence = FALSE, |
| 3 |
k = if(confidence) Inf else 1, |
| 4 |
col = 2, lty = 2, do.abline = TRUE) |
| 5 |
{ |
| 6 |
n <- length(Res <- residuals(object)) |
| 7 |
df <- object $ df.resid |
| 8 |
s2 <- sum(Res^2)/df |
| 9 |
s <- sqrt(s2) |
| 10 |
if(is.null(R <- object $ R)) ## in R |
| 11 |
R <- qr.R(object $ qr) |
| 12 |
Xm <- R[1,2]/R[1,1] # = mean(x_i) : (X'X)[1,] = (R'R)[1,] = [n sum_{x_i}] |
| 13 |
##-- S_{xx} = sum_i{(x_i - mean(x_i))^2} : you can prove this: (R'R) = ... |
| 14 |
S.xx <- R[2,2]^2 |
| 15 |
|
| 16 |
ux <- par("usr")[1:2] |
| 17 |
d.xs <- data.frame(x = xs <- seq(ux[1],ux[2], length = 100)) |
| 18 |
names(d.xs) <- attr(object$terms,"term.labels") #-- proper x-variable name |
| 19 |
ys <- predict(object, new = d.xs) |
| 20 |
pred.err <- qt(1-(1-c.prob)/2, df) * s * sqrt(1/k + 1/n + (xs-Xm)^2/S.xx) |
| 21 |
o.p <- par(err=-1) |
| 22 |
on.exit(par(o.p)) |
| 23 |
if(do.abline) |
| 24 |
abline(object) |
| 25 |
lines(xs, ys - pred.err, col=col, lty=lty) |
| 26 |
lines(xs, ys + pred.err, col=col, lty=lty) |
| 27 |
} |
| 28 |
|