x=model.matrix(Apps~.,College)[,-1]
y=College$Apps
enet.model=enet(x, y, lambda=1)
mx_x <- data.frame(coef_enet = round(predict(enet.model, type="coef", s=2, mode="penalty", naive = F)$coefficients, 2))
mx_x$coef_enet.naive <- round(predict(enet.model, type="coef", s=2, mode="penalty", naive = T)$coefficients, 2)
glmnet.model=glmnet(x, y, alpha=0.50)
mx_x$coef_glmnet <- round(coef(glmnet.model, s=2, exact=TRUE), 2)[-1]
mx_x
La secuencia de comandos anterior nos da:
coef_enet coef_enet.naive coef_glmnet
PrivateYes -1125.13 -562.56 -493.72
Accept 0.85 0.43 1.57
Enroll 1.44 0.72 -0.80
Top10perc 26.13 13.06 48.90
Top25perc 17.20 8.60 -13.47
F.Undergrad 0.25 0.12 0.05
P.Undergrad 0.21 0.10 0.04
Outstate 0.02 0.01 -0.08
Room.Board 0.31 0.15 0.15
Books 0.70 0.35 0.02
Personal 0.12 0.06 0.03
PhD 11.77 5.88 -8.53
Terminal 10.30 5.15 -3.31
S.F.Ratio 23.87 11.93 14.97
perc.alumni -17.08 -8.54 -0.01
Expend 0.09 0.05 0.08
Grad.Rate 19.05 9.52 8.51
El uso de ingenuo=FALSE para ElasticNet es sólo la transformación de los coeficientes de ingenuo=TRUE , según la fórmula:
coef(ENet) = (1 + lambda) * coef(NaiveENet)
El glmnet da "listo para usar" de los coeficientes. Sin embargo son diferentes de los de ElasticNet. (tal vez los diferentes algoritmos)
Obtendrás ElasticNet y glmnet "alineados" si no normalizar los predictores.
enet.model=enet(x, y, lambda=1, normalize = F)
mx_x <- data.frame(coef_enet = round(predict(enet.model, type="coef", s=2, mode="penalty", naive = F)$coefficients, 2))
mx_x$coef_enet.naive <- round(predict(enet.model, type="coef", s=2, mode="penalty", naive = T)$coefficients, 2)
glmnet.model=glmnet(x, y, alpha=0.50, standardize = F)
mx_x$coef_glmnet <- round(coef(glmnet.model, s=2, exact=TRUE), 2)[-1]
mx_x
coef_enet coef_enet.naive coef_glmnet
PrivateYes -971.26 -485.63 -481.73
Accept 3.17 1.59 1.58
Enroll -1.76 -0.88 -0.87
Top10perc 99.84 49.92 49.57
Top25perc -28.47 -14.24 -14.03
F.Undergrad 0.12 0.06 0.06
P.Undergrad 0.09 0.04 0.04
Outstate -0.17 -0.09 -0.09
Room.Board 0.30 0.15 0.15
Books 0.04 0.02 0.02
Personal 0.06 0.03 0.03
PhD -17.29 -8.65 -8.54
Terminal -6.59 -3.30 -3.35
S.F.Ratio 31.04 15.52 15.48
perc.alumni 0.31 0.16 0.09
Expend 0.16 0.08 0.08
Grad.Rate 17.31 8.66 8.63
Cómo predecir las obras?
mx_x2 <- data.frame(pred_enet = predict(enet.model, newx=x, s=2, mode="penalty", naive=F)$fit,
pred_enet.naive = predict(enet.model, newx=x, s=2, mode="penalty", naive=T)$fit,
pred_glmnet = as.vector(predict(glmnet.model, newx=x, s=2, exact=T)))
head(mx_x2)
pred_enet pred_enet.naive pred_glmnet
Abilene Christian University -200.7408 1400.4488 1403.1934
Adelphi University 3757.6684 3379.6534 3376.7797
Adrian College -456.4379 1272.6002 1272.1445
Agnes Scott College 994.5582 1998.0983 1997.6611
Alaska Pacific University -3453.9529 -226.1573 -221.7383
Albertson College -1664.2285 668.7049 668.3047
enet predicción es lineal en la enet ingenuo de predicción (fórmula):
fit.lm <- lm(pred_enet~pred_enet.naive, data=mx_x2)
coef(fit.lm)
(Intercept) pred_enet.naive
-3001.638 2.000
summary(fit.lm)$r.squared
[1] 1
enet ingenuo es casi idéntica a la glmnet predicción:
fit.lm <- lm(pred_enet.naive~-1+pred_glmnet, data=mx_x2)
coef(fit.lm)
pred_glmnet
1.000126
summary(fit.lm)$r.squared
[1] 0.9999994