m<-model.matrix(x1~.,-1,data=datos)
n<-x1
fit.ridge=glmnet(m,n,alpha=0)
coef(fit.ridge)
## 4 x 100 sparse Matrix of class "dgCMatrix"
##
## (Intercept) 3.610625e+01 3.589889e+01 3.587870e+01 3.585654e+01 3.583224e+01
## (Intercept) . . . . .
## x2 1.408832e-37 1.617712e-04 1.775218e-04 1.948037e-04 2.137652e-04
## x3 7.854836e-37 9.014370e-04 9.891502e-04 1.085380e-03 1.190948e-03
##
## (Intercept) 3.580557e+01 3.577632e+01 3.574422e+01 3.570901e+01 3.567039e+01
## (Intercept) . . . . .
## x2 2.345689e-04 2.573932e-04 2.824334e-04 3.099038e-04 3.400389e-04
## x3 1.306757e-03 1.433795e-03 1.573144e-03 1.725988e-03 1.893625e-03
##
## (Intercept) 3.562802e+01 3.558155e+01 3.553057e+01 3.547466e+01 3.541335e+01
## (Intercept) . . . . .
## x2 3.730959e-04 4.093562e-04 4.491282e-04 4.927494e-04 5.405894e-04
## x3 2.077476e-03 2.279094e-03 2.500179e-03 2.742591e-03 3.008363e-03
##
## (Intercept) 3.534611e+01 3.527239e+01 3.519155e+01 3.510292e+01 3.500577e+01
## (Intercept) . . . . .
## x2 5.930525e-04 6.505812e-04 7.136592e-04 7.828156e-04 8.586284e-04
## x3 3.299716e-03 3.619078e-03 3.969099e-03 4.352672e-03 4.772951e-03
##
## (Intercept) 3.489928e+01 34.782572445 34.654681884 34.514558243 34.361056320
## (Intercept) . . . . .
## x2 9.417291e-04 0.001032808 0.001132616 0.001241976 0.001361782
## x3 5.233376e-03 0.005737694 0.006289984 0.006894681 0.007556607
##
## (Intercept) 34.192928727 34.008817784 33.807246987 33.586612056 33.345171614
## (Intercept) . . . . .
## x2 0.001493010 0.001636720 0.001794068 0.001966309 0.002154804
## x3 0.008280995 0.009073522 0.009940335 0.010888084 0.011923950
##
## (Intercept) 33.081037540 32.792165082 32.476342805 32.131182521 31.75410934
## (Intercept) . . . . .
## x2 0.002361032 0.002586593 0.002833219 0.003102782 0.00339730
## x3 0.013055679 0.014291605 0.015640683 0.017112508 0.01871734
##
## (Intercept) 31.342352033 30.892933992 30.402664975 29.868134088 29.285704323
## (Intercept) . . . . .
## x2 0.003718947 0.004070057 0.004453135 0.004870863 0.005326098
## x3 0.020466112 0.022370448 0.024442655 0.026695714 0.029143255
##
## (Intercept) 28.651509169 27.96145179 27.211207439 26.396229674 25.511761287
## (Intercept) . . . . .
## x2 0.005821885 0.00636145 0.006948203 0.007585733 0.008277802
## x3 0.031799519 0.03467930 0.037797850 0.041170801 0.044814004
##
## (Intercept) 24.552850590 23.514374024 22.39106592 21.17755629 19.86841758
## (Intercept) . . . . .
## x2 0.009028329 0.009841379 0.01072114 0.01167188 0.01269794
## x3 0.048743369 0.052974667 0.05752328 0.06240392 0.06763032
##
## (Intercept) 18.45822095 16.94160295 15.31334281 13.56845050 11.70226540
## (Intercept) . . . . .
## x2 0.01380368 0.01499340 0.01627133 0.01764152 0.01910780
## x3 0.07321481 0.07916798 0.08549817 0.09221104 0.09930901
##
## (Intercept) 9.71056482 7.58968107 5.33662534 2.94921590 0.42620729 -2.23258323
## (Intercept) . . . . . .
## x2 0.02067366 0.02234217 0.02411592 0.02599685 0.02798621 0.03008443
## x3 0.10679079 0.11465086 0.12287897 0.13145975 0.14037228 0.14958991
##
## (Intercept) -5.02615636 -7.95222369 -11.00711787 -14.18572543 -17.48144585
## (Intercept) . . . . .
## x2 0.03229101 0.03460447 0.03702222 0.03954056 0.04215459
## x3 0.15908008 0.16880434 0.17871861 0.18877353 0.19891508
##
## (Intercept) -20.88618895 -24.39039626 -27.98311123 -31.65208312 -35.3839097
## (Intercept) . . . . .
## x2 0.04485825 0.04764427 0.05050425 0.05342873 0.0564073
## x3 0.20908542 0.21922388 0.22926814 0.23915556 0.2488245
##
## (Intercept) -39.16421509 -42.9778589 -46.80916919 -50.64219402 -54.46096104
## (Intercept) . . . . .
## x2 0.05942869 0.0624810 0.06555181 0.06862841 0.07169801
## x3 0.25821590 0.2672744 0.27595004 0.28419881 0.29198409
##
## (Intercept) -58.24973797 -61.9932845 -65.6770873 -69.28757110 -72.81227969
## (Intercept) . . . . .
## x2 0.07474793 0.0777658 0.0807398 0.08365877 0.08651238
## x3 0.29927701 0.3060570 0.3123116 0.31803663 0.32323560
##
## (Intercept) -76.24002232 -79.56101174 -82.76682100 -85.85058730 -88.80689811
## (Intercept) . . . . .
## x2 0.08929132 0.09198734 0.09459326 0.09710315 0.09951225
## x3 0.32791896 0.33210333 0.33581076 0.33906734 0.34190245
##
## (Intercept) -91.6317852 -94.3226632 -96.8782451 -99.2984402 -101.5842385
## (Intercept) . . . . .
## x2 0.1018170 0.1040148 0.1061044 0.1080853 0.1099580
## x3 0.3443477 0.3464358 0.3482001 0.3496736 0.3508881
##
## (Intercept) -103.7375878 -105.7612667 -107.6587579 -109.4341254 -111.0918966
## (Intercept) . . . . .
## x2 0.1117239 0.1133848 0.1149435 0.1164029 0.1177666
## x3 0.3518744 0.3526611 0.3532750 0.3537407 0.3540804
##
## (Intercept) -112.6369536 -114.0744332 -115.4096374 -116.6479536
## (Intercept) . . . .
## x2 0.1190385 0.1202226 0.1213230 0.1223442
## x3 0.3543140 0.3544594 0.3545321 0.3545458
plot(fit.ridge,"lambda",label=TRUE)

cv.ridge=cv.glmnet(m,n,alpha=0)
plot(cv.ridge)
abline(v=log(fit.ridge$lambda[100]),col="blue",lwd=4,lty=3)

mejorlambda<-cv.ridge$lambda.min
log(mejorlambda)
## [1] 0.08504987
coef(fit.ridge)[,which(fit.ridge$lambda==mejorlambda)]
## (Intercept) (Intercept) x2 x3
## -116.6479536 0.0000000 0.1223442 0.3545458
############ DATOS DE ENTRENAMIENTO
split = sample.split(datos, SplitRatio = 0.7)
test1 = subset(datos, split == FALSE)
x.test1=model.matrix(x1~.,-1,data=test1)
pred=predict(fit.ridge, s=mejorlambda,newx = x.test1)
data.frame(RMSE=RMSE(pred,test1$x1),Rsquare = R2(pred,test1$x1))
## RMSE s1
## 1 2.828943 0.968323