Professional Documents
Culture Documents
HW9數學規劃
HW9數學規劃
HW9數學規劃
# 1. Compute the training errors, Cps, test errors of regression models with
# sequentially added predictors by the method on p6-7
y1 = Z1[, T1]
y2 = Z2[, T1]
### Select a best regression model of data from the set of models with
# sequentially added predictors that induces a minimum test error using
# the ols estimation
Err.te <- Cp.tr <- Err.tr <- NULL
for (k1 in 1:length(P0)){
u1 <- c(T1, P0[1:k1])
D1 <- data.frame(Z1[, u1])
D2 <- data.frame(Z2[, u1])
#
lm.fit <- lm(G9~., data = D1)
# 2. Plot training errors, Cps, test errors of regression models and select
# the predictors of the target variable by training errors, Cps, test errors
# respectively by the mthod on p8
plot(Err.te, type = "b", ylim = c(0, 1.5), pch = 20, main = "Test errors")
abline(v = which.min(Err.te), col = "red")
# 3. Find all subsets of predictors with the size <= 3 using the method on p9
PP
# 4. Compute the training errors, Cps, test errors of regression models
# with number of predictors <= 3 by the method on p10-11.
Err.tr
Err.te
Cp.tr
# 5. Plot training errors, Cps, test errors of regression models calculated in
# question 4 amd select the predictors by different error critera, respectively
# by the mthod on p12.
plot(Err.te, type = "b", ylim = c(0, 1.5), pch = 20, main = "Test error")
abline(v = which.min(Err.te), col = "red")
PP[[which.min(Err.tr)]]
PP[[which.min(Cp.tr)]]
PP[[which.min(Err.te)]]