Interpreting coefficient names in glmnet in R

I use glmnet to predict probabilities based on a set of 5 functions using the following code. I need the actual formula because I need to use it in another (not R) program.

deg = 3 glmnet.fit <- cv.glmnet(poly(train.matrix,degree=deg),train.result,alpha=0.05,family='binomial') 

The names of the obtained coefficients have five positions (I assume that this is one of each function), and each of them represents a number from 0 to 3 (I assume that this is the degree of the polynomial). But I'm still confused about how exactly to restore the formula.

Take them, for example:

 > coef(glmnet.fit,s= best.lambda) (Intercept) -2.25e-01 ... 0.1.0.0.1 3.72e+02 1.1.0.0.1 9.22e+04 0.2.0.0.1 6.17e+02 ... 

Let me name the functions A, B, C, D, E. This is how to interpret the formula?

 Y = -2.25e-01 + ... (3.72e+02 * (B * E) + (9.22e+04 * (A * B * E) + (6.17e+02 * (B^2 + E) ... 

If this is not the case, how should I interpret it?

I saw the following question and answer , but did not name these types of coefficient names.

Thanks in advance for your help.

+6
source share
1 answer

Usually we use the prediction function. In your case, you need coefficients for use in another program. We can check the agreement between the use of the forecast and the result of multiplying the data by coefficients.

 # example data library(ElemStatLearn) library(glmnet) data(prostate) # training data data.train <- prostate[prostate$train,] y <- data.train$lpsa # isolate predictors data.train <- as.matrix(data.train[,-c(9,10)]) # test data data.test <- prostate[!prostate$train,] data.test <- as.matrix(data.test[,-c(9,10)]) # fit training model myglmnet =cv.glmnet(data.train,y) # predictions by using predict function yhat_enet <- predict(myglmnet,newx=data.test, s="lambda.min") # get predictions by using coefficients beta <- as.vector( t(coef(myglmnet,s="lambda.min"))) # Coefficients are returned on the scale of the original data. # note we need to add column of 1s for intercept testX <- cbind(1,data.test) yhat2 <- testX %*% beta # check by plotting predictions plot(yhat2,yhat_enet) 

Thus, each coefficient corresponds to a column in your training data. The first corresponds to interception. In general, you can extract the coefficients and multiply by the test data to get the results you are interested in.

+6
source

Source: https://habr.com/ru/post/918676/


All Articles