library(leaps)
regfit.fwd <- regsubsets(y ~ x + I(x^2) + I(x^3) + I(x^4) + I(x^5) + I(x^6) + I(x^7) + I(x^8) + I(x^9) + I(x^10), data = data.full, nvmax = 10, method = "forward")
reg.summary.fwd <- summary(regfit.fwd)
par(mfrow = c(2, 2))
plot(reg.summary.fwd$cp, xlab = "Numero de variables", ylab = "C_p", type = "l")
points(which.min(reg.summary.fwd$cp), reg.summary.fwd$cp[which.min(reg.summary.fwd$cp)],
col = "red", cex = 2, pch = 20)
plot(reg.summary.fwd$bic, xlab = "Numero de variables", ylab = "BIC", type = "l")
points(which.min(reg.summary.fwd$bic), reg.summary.fwd$bic[which.min(reg.summary.fwd$bic)],
col = "red", cex = 2, pch = 20)
plot(reg.summary.fwd$adjr2, xlab = "Numero de variables", ylab = "Ajuste R^2", type = "l")
points(which.max(reg.summary.fwd$adjr2), reg.summary.fwd$adjr2[which.max(reg.summary.fwd$adjr2)],
col = "red", cex = 2, pch = 20)
mtext("Graficas de C_p, BIC y ajuste de R^2 Para forward stepwise selection", side = 3, line = -2, outer = TRUE)
coef(regfit.fwd, which.max(reg.summary.fwd$adjr2))
(Intercept) x I(x^2) I(x^5)
2.07219472 3.44514720 -1.15676236 0.09022577
De lo anterior podemos decir que selecionaremos el modelo de tres variables
coef(regfit.fwd, which.max(reg.summary.fwd$adjr2))
(Intercept) x I(x^2) I(x^5)
2.07219472 3.44514720 -1.15676236 0.09022577
library(leaps)
b0 <- 2
b1 <- 3
b2 <- -1
b3 <- 0.5
y <- b0 + b1 * x + b2 * x^2 + b3 * x^3 + eps
regfit.bwd <- regsubsets(y ~ x + I(x^2) + I(x^3) + I(x^4) + I(x^5) + I(x^6) + I(x^7) + I(x^8) + I(x^9) + I(x^10), data = data.full, nvmax = 10, method = "backward")
reg.summary.bwd <- summary(regfit.bwd)
par(mfrow = c(2, 2))
plot(reg.summary.bwd$cp, xlab = "Number of variables", ylab = "C_p", type = "l")
points(which.min(reg.summary.bwd$cp), reg.summary.bwd$cp[which.min(reg.summary.bwd$cp)], col = "red", cex = 2, pch = 20)
plot(reg.summary.bwd$bic, xlab = "Number of variables", ylab = "BIC", type = "l")
points(which.min(reg.summary.bwd$bic), reg.summary.bwd$bic[which.min(reg.summary.bwd$bic)], col = "red", cex = 2, pch = 20)
plot(reg.summary.bwd$adjr2, xlab = "Number of variables", ylab = "Adjusted R^2", type = "l")
points(which.max(reg.summary.bwd$adjr2), reg.summary.bwd$adjr2[which.max(reg.summary.bwd$adjr2)], col = "red", cex = 2, pch = 20)
mtext("Plots of C_p, BIC and adjusted R^2 for backward stepwise selection", side = 3, line = -2, outer = TRUE)
En base a lo anterior, selecionamos el modelo de tres variables
coef(regfit.bwd, which.max(reg.summary.bwd$adjr2))
(Intercept) x I(x^2) I(x^5)
2.07219472 3.44514720 -1.15676236 0.09022577
Selecionamos el modelo de tres variables con x, x^2 y x^5