1 | regularized_linear_regression(X,Y, alpha, cvfolds)
|
X |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | ##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function(X,Y, alpha, cvfolds){
#require(glmnet)
sol = matrix(0, nrow = nrow(Y), ncol = nrow(X))
# X <- X[-1,] # Get rid of intercept , it is automatically placed by glmnet
mse = 0
for(i in 1:nrow(Y)){
cv <- glmnet::cv.glmnet(t(X)[,-1],Y[i,], alpha=alpha ,nfolds = cvfolds,standardize=T)
small.lambda.index <- which(cv$lambda == cv$lambda.1se)
small.lambda.betas <- cv$glmnet.fit$beta[,small.lambda.index]
small.lambda.betas <- c(cv$glmnet.fit$a0[small.lambda.index],small.lambda.betas)
sol[i,] = small.lambda.betas
mse[i] = cv$cvm[small.lambda.index]
#print(cv$lambda.1se)
}
mse = mean(mse)
return(list(sol,mse))
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.