Aliases: predict.lm
Keywords: regression
### ** Examples require(graphics) ## Predictions x <- rnorm(15) y <- x + rnorm(15) predict(lm(y ~ x))
1 2 3 4 5 6 0.391850289 0.330517736 0.006600849 -0.067498698 -0.412145353 0.066908576 7 8 9 10 11 12 0.368062400 -0.352972864 0.401085413 -0.145300170 -0.015942504 0.087323310 13 14 15 -0.092998071 0.057719733 0.168878031
new <- data.frame(x = seq(-3, 3, 0.5)) predict(lm(y ~ x), new, se.fit = TRUE)
$fit 1 2 3 4 5 6 -0.89733084 -0.73568034 -0.57402984 -0.41237934 -0.25072884 -0.08907834 7 8 9 10 11 12 0.07257216 0.23422266 0.39587316 0.55752366 0.71917415 0.88082465 13 1.04247515 $se.fit 1 2 3 4 5 6 7 8 0.8213672 0.6910641 0.5631533 0.4397273 0.3259217 0.2360854 0.2046806 0.2543588 9 10 11 12 13 0.3523480 0.4693041 0.5941296 0.7227586 0.8534732 $df [1] 13 $residual.scale [1] 0.7901284
pred.w.plim <- predict(lm(y ~ x), new, interval = "prediction") pred.w.clim <- predict(lm(y ~ x), new, interval = "confidence") matplot(new$x, cbind(pred.w.clim, pred.w.plim[,-1]), lty = c(1,2,2,3,3), type = "l", ylab = "predicted y")
## Prediction intervals, special cases ## The first three of these throw warnings w <- 1 + x^2 fit <- lm(y ~ x) wfit <- lm(y ~ x, weights = w) predict(fit, interval = "prediction")
Warning in predict.lm(fit, interval = "prediction"): predictions on current data refer to _future_ responses
fit lwr upr 1 0.391850289 -1.474748 2.258449 2 0.330517736 -1.502620 2.163655 3 0.006600849 -1.758329 1.771531 4 -0.067498698 -1.843831 1.708834 5 -0.412145353 -2.365470 1.541180 6 0.066908576 -1.696226 1.830043 7 0.368062400 -1.484841 2.220966 8 -0.352972864 -2.262648 1.556702 9 0.401085413 -1.471069 2.273240 10 -0.145300170 -1.944308 1.653708 11 -0.015942504 -1.783274 1.751389 12 0.087323310 -1.676732 1.851378 13 -0.092998071 -1.875571 1.689574 14 0.057719733 -1.705252 1.820692 15 0.168878031 -1.606533 1.944289
predict(wfit, interval = "prediction")
Warning in predict.lm(wfit, interval = "prediction"): predictions on current data refer to _future_ responses
Warning in predict.lm(wfit, interval = "prediction"): assuming prediction variance inversely proportional to weights used for fitting
fit lwr upr 1 0.450632327 -1.240682 2.141947 2 0.389896850 -1.413239 2.193033 3 0.069133312 -2.087593 2.225860 4 -0.004244872 -2.034231 2.025741 5 -0.345536373 -1.778035 1.086962 6 0.128853939 -2.071153 2.328860 7 0.427076015 -1.305141 2.159293 8 -0.286939932 -1.774451 1.200571 9 0.459777547 -1.216601 2.136157 10 -0.081288942 -1.938729 1.776151 11 0.046809420 -2.078274 2.171893 12 0.149069935 -2.050271 2.348411 13 -0.029496007 -2.004791 1.945799 14 0.119754551 -2.077954 2.317463 15 0.229830716 -1.893536 2.353198
predict(wfit, new, interval = "prediction")
Warning in predict.lm(wfit, new, interval = "prediction"): Assuming constant prediction variance even though model fit is weighted
fit lwr upr 1 -0.82599855 -3.425526 1.773529 2 -0.66592173 -3.143897 1.812054 3 -0.50584491 -2.881218 1.869529 4 -0.34576808 -2.640034 1.948498 5 -0.18569126 -2.422682 2.051300 6 -0.02561444 -2.231021 2.179792 7 0.13446239 -2.066158 2.335082 8 0.29453921 -1.928264 2.517343 9 0.45461603 -1.816551 2.725783 10 0.61469285 -1.729398 2.958784 11 0.77476968 -1.664604 3.214143 12 0.93484650 -1.619667 3.489360 13 1.09492332 -1.592037 3.781884
predict(wfit, new, interval = "prediction", weights = (new$x)^2)
fit lwr upr 1 -0.82599855 -2.4487653 0.7967682 2 -0.66592173 -2.1635936 0.8317501 3 -0.50584491 -1.9764335 0.9647436 4 -0.34576808 -1.9846849 1.2931487 5 -0.18569126 -2.4226821 2.0512996 6 -0.02561444 -4.3595381 4.3083092 7 0.13446239 -Inf Inf 8 0.29453921 -4.0482629 4.6373413 9 0.45461603 -1.8165510 2.7257830 10 0.61469285 -1.0932754 2.3226611 11 0.77476968 -0.7970998 2.3466391 12 0.93484650 -0.6863258 2.5560188 13 1.09492332 -0.6645045 2.8543511
predict(wfit, new, interval = "prediction", weights = ~x^2)
fit lwr upr 1 -0.82599855 -2.4487653 0.7967682 2 -0.66592173 -2.1635936 0.8317501 3 -0.50584491 -1.9764335 0.9647436 4 -0.34576808 -1.9846849 1.2931487 5 -0.18569126 -2.4226821 2.0512996 6 -0.02561444 -4.3595381 4.3083092 7 0.13446239 -Inf Inf 8 0.29453921 -4.0482629 4.6373413 9 0.45461603 -1.8165510 2.7257830 10 0.61469285 -1.0932754 2.3226611 11 0.77476968 -0.7970998 2.3466391 12 0.93484650 -0.6863258 2.5560188 13 1.09492332 -0.6645045 2.8543511
##-- From aov(.) example ---- predict(.. terms) npk.aov <- aov(yield ~ block + N*P*K, npk) (termL <- attr(terms(npk.aov), "term.labels"))
[1] "block" "N" "P" "K" "N:P" "N:K" "P:K" "N:P:K"
(pt <- predict(npk.aov, type = "terms"))
block N P K N:P N:K P:K N:P:K 1 -0.850 -4.925 0.2083333 -0.9583333 0.9416667 1.175 0.4250000 0 2 -0.850 4.925 0.2083333 0.9583333 -2.8250000 1.175 -0.1416667 0 3 -0.850 -4.925 -0.2083333 0.9583333 0.9416667 1.175 -0.1416667 0 4 -0.850 4.925 -0.2083333 -0.9583333 0.9416667 -3.525 -0.1416667 0 5 2.575 4.925 -0.2083333 0.9583333 0.9416667 1.175 -0.1416667 0 6 2.575 4.925 0.2083333 -0.9583333 -2.8250000 -3.525 0.4250000 0 7 2.575 -4.925 -0.2083333 -0.9583333 0.9416667 1.175 -0.1416667 0 8 2.575 -4.925 0.2083333 0.9583333 0.9416667 1.175 -0.1416667 0 9 5.900 -4.925 0.2083333 0.9583333 0.9416667 1.175 -0.1416667 0 10 5.900 4.925 0.2083333 -0.9583333 -2.8250000 -3.525 0.4250000 0 11 5.900 4.925 -0.2083333 0.9583333 0.9416667 1.175 -0.1416667 0 12 5.900 -4.925 -0.2083333 -0.9583333 0.9416667 1.175 -0.1416667 0 13 -4.750 4.925 -0.2083333 0.9583333 0.9416667 1.175 -0.1416667 0 14 -4.750 4.925 0.2083333 -0.9583333 -2.8250000 -3.525 0.4250000 0 15 -4.750 -4.925 -0.2083333 -0.9583333 0.9416667 1.175 -0.1416667 0 16 -4.750 -4.925 0.2083333 0.9583333 0.9416667 1.175 -0.1416667 0 17 -4.350 4.925 0.2083333 0.9583333 -2.8250000 1.175 -0.1416667 0 18 -4.350 -4.925 -0.2083333 0.9583333 0.9416667 1.175 -0.1416667 0 19 -4.350 4.925 -0.2083333 -0.9583333 0.9416667 -3.525 -0.1416667 0 20 -4.350 -4.925 0.2083333 -0.9583333 0.9416667 1.175 0.4250000 0 21 1.475 4.925 -0.2083333 -0.9583333 0.9416667 -3.525 -0.1416667 0 22 1.475 4.925 0.2083333 0.9583333 -2.8250000 1.175 -0.1416667 0 23 1.475 -4.925 0.2083333 -0.9583333 0.9416667 1.175 0.4250000 0 24 1.475 -4.925 -0.2083333 0.9583333 0.9416667 1.175 -0.1416667 0 attr(,"constant") [1] 54.875
pt. <- predict(npk.aov, type = "terms", terms = termL[1:4]) stopifnot(all.equal(pt[,1:4], pt., tolerance = 1e-12, check.attributes = FALSE))