[R] understanding predict.lm

Spencer Graves @pencer@gr@ve@ @end|ng |rom e||ect|vede|en@e@org
Mon Nov 6 22:16:33 CET 2023


Hello, All:


	  I am unable to manually replicate predict.lm, specifically comparing 
se.fit with (fit[,3]-fit[,2]): I think their ratio should be 
2*qnorm((1-level)/2), and that's not what I'm getting.


	  Consider the following slight modification of the first example in 
help('predict.lm'):


set.seed(1)
x <- rnorm(15)
y <- x + rnorm(15)
predict(lm(y ~ x))
new <- data.frame(x = seq(-3, 3, 0.5))
predict(lm(y ~ x), new, se.fit = TRUE)
pred.w.plim <- predict(lm(y ~ x), new, interval = "prediction",
                        se.fit = TRUE)
pred.w.clim <- predict(lm(y ~ x), new, interval = "confidence",
                        se.fit = TRUE)

(z.confInt <- with(pred.w.clim, (fit[,3]-fit[,2])/se.fit))
pnorm(-z.confInt/2)

s.pred <- sqrt(with(pred.w.plim,
                     se.fit^2+residual.scale^2))
(z.predInt <- with(pred.w.plim, (fit[,3]-fit[,2])/s.pred))
pnorm(-z.predInt/2)


	  ** This gives me 0.01537207. I do not understand why it's not 0.025 
with level = 0.95.


	  Can someone help me understand this?
	  Thanks,
	  Spencer Graves



More information about the R-help mailing list