HW9數學規劃

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

HW9 數學規劃

# Data for the assignment:


# BiocManager::install("grndata")
library(grndata)
data(toy.data)
N0 <- nrow(toy.data)
p0 <- 10
set.seed(123)
I1 <- sample(1:N0, N0/2)
Z1 <- toy.data[I1, 1:p0] # Training data
I2 <- setdiff(1:N0, I1)
Z2 <- toy.data[I2, 1:p0] # Test data
T1 <- c("G9") # Target variable
P0 <- setdiff(colnames(Z1), T1)

# 1. Compute the training errors, Cps, test errors of regression models with
# sequentially added predictors by the method on p6-7

X1.p = Z1[, P0]


X1.p.c = scale(X1.p, center = TRUE, scale = FALSE)
y1 = Z1[, T1]
y1.c = scale(y1, center = TRUE, scale = FALSE)

B1 = solve(t(X1.p.c) %*% X1.p.c + diag(10^-12, p0-1)) %*% t(X1.p.c) %*% y1.c

py1.c = X1.p.c %*% B1


s2.p = mean((y1.c - py1.c )^2)

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)

py1.lm <- predict(lm.fit, D1)


err.tr <- mean((y1 - py1.lm)^2)
Err.tr <- c(Err.tr, err.tr) # Training error

N1 <- nrow(D1) # Number of observations


p1 <- ncol(D1) - 1 # Number of predictors
df.reg <- (1 + p1)
# df.res <- N1 - df.reg
# if (df.res > 0){
Cp = err.tr + 2 * df.reg / N1 * s2.p ######p2
# }else(Cp <- NaN)
Cp.tr <- c(Cp.tr, Cp)

py2.lm <- predict(lm.fit, D2)


Err.te <- c(Err.te, mean((y2 - py2.lm)^2)) # Test error
}
Err.tr
Err.te
Cp.tr

# 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

### Section of regression models


windows(h = 6, w = 16)
par(mfcol = c(1, 3))
plot(Err.tr, type = "b", ylim = c(0, 0.2), pch = 20, main = "Training error")
abline(v = which.min(Err.tr), col = "red")

plot(Cp.tr, type = "b", pch = 20, main = "Cps")


abline(v = which.min(Cp.tr), col = "red")
# The training error of regression decreases as the amount of predictors i

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

### A11 subsets of predictors with the size <= n1


n1 <- 3 #Maximum numbeer of predictors
PP <- list()
for(i1 in 1:n1){
PP <- append(PP, combn(P0, i1, simplify = FALSE))
}

PP
# 4. Compute the training errors, Cps, test errors of regression models
# with number of predictors <= 3 by the method on p10-11.

Err.te <- Cp.tr <- Err.tr <- NULL


for (l1 in 1:length(PP)){
u1 <- c(T1, PP[[l1]])
D1 <- data.frame(Z1[, u1])
D2 <- data.frame(Z2[, u1])
#
lm.fit <- lm(G9~., data = D1)
#
py1.lm <- predict(lm.fit, D1)
err.tr <- mean((y1 - py1.lm)^2)
Err.tr <- c(Err.tr, err.tr) # Training error

N1 <- nrow(D1) # Number of observations


p1 <- ncol(D1) - 1 # Number of predictors
df.reg <- (1 + p1)
# df.res <- N1 - df.reg
# if (df.res > 0){
Cp <- err.tr + 2 * df.reg / N1 * s2.p ######p2
# }else(Cp <- NaN)
Cp.tr <- c(Cp.tr, Cp)

py2.lm <- predict(lm.fit, D2)


err.te <- mean((y2 - py2.lm)^2)
Err.te <- c(Err.te, err.te) # Test error
}

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.

### Section of regression models


windows(h = 6, w = 16)
par(mfcol = c(1, 3))
plot(Err.tr, type = "b", ylim = c(0, 0.2), pch = 20, main = "Training error")
abline(v = which.min(Err.tr), col = "red")
plot(Cp.tr, type = "b", pch = 20, main = "Cps")
abline(v = which.min(Cp.tr), col = "red")
# The training error of regression decreases as the amount of predictors i

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)]]

You might also like