-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20180828_Predicted values based on linear model object.R
92 lines (63 loc) · 2.03 KB
/
20180828_Predicted values based on linear model object.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
###'######################################################################
###'
###' Predicted values based on linear model object.
###'
###' 20180828 JoonHo Lee
###'
###'
###'######################################################################
###'
###' Basic settings
###'
###'
### Start with a clean slate
gc() # force R to release memory it is no longer using
rm(list=ls()) # delete all the objects in the workspace
### Set working directory
work_dir <- c("~/data-manipulation-and-visualization")
setwd(work_dir)
### Call libraries
library(graphics)
###'######################################################################
###'
###' Predictions
###'
###'
### Model fitting
x <- rnorm(15)
y <- x + rnorm(15)
predict(lm(y ~ x))
### Generate new data
new <- data.frame(x = seq(-3, 3, 0.5))
### Predict y based on new x
predict(lm(y ~ x), new, se.fit = TRUE)
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
###'
###' (1) Weighted regression
###' (2) ANOVA
###'
###'
### Fit weighted regression
w <- 1 + x^2
fit <- lm(y ~ x)
wfit <- lm(y ~ x, weights = w)
###' Predict intervals
###' The first three of these throw warnings
predict(fit, interval = "prediction")
predict(wfit, interval = "prediction")
predict(wfit, new, interval = "prediction")
predict(wfit, new, interval = "prediction", weights = (new$x)^2)
predict(wfit, new, interval = "prediction", weights = ~x^2)
### From aov(.) example: predict(.. terms)
npk.aov <- aov(yield ~ block + N*P*K, npk)
(termL <- attr(terms(npk.aov), "term.labels"))
(pt <- predict(npk.aov, type = "terms"))
pt. <- predict(npk.aov, type = "terms", terms = termL[1:4])
stopifnot(all.equal(pt[,1:4], pt.,
tolerance = 1e-12, check.attributes = FALSE))