Search code examples
rclassificationregressionglmmulticlass-classification

glmnet multinomial logistic regression prediction result


I'm building a penalized multinomial logistic regression, but I'm having trouble coming up with a easy way to get the prediction accuracy. Here's my code:

fit.ridge.cv <- cv.glmnet(train[,-1], train[,1], type.measure="mse", alpha=0,
                      family="multinomial")

fit.ridge.best <- glmnet(train[,-1], train[,1], family = "multinomial", alpha = 0,
                     lambda = fit.ridge.cv$lambda.min)

fit.ridge.pred <- predict(fit.ridge.best, test[,-1], type = "response")

The first column of my test data is the response variable and it has 4 categories. And if I look at the result(fit.ridge.pred) it looks like this:

1,2,3,4
0.8743061353, 0.0122328811, 0.004798154, 0.1086628297

From what I understand these are the class probabilities. I want to know if there's a easy way to compute the model accuracy on the test data. Now I'm taking the max for each row and comparing with the original label. Thanks


Solution

  • Something like:

    predicted <- colnames(fit.ridge.pred)[apply(fit.ridge.pred,1,which.max)]
    table(predicted, test[, 1]
    

    The first line takes the class for which the model outputs the highest probability per row, after which the second line constructs a confusion matrix.

    The accuracy is then basically the proportion of observations classified correct (sum of the diagonal / total)