I am using data (birthwt) from the library (MASS) and I'd like to calculate Mathews Correlation Coefficient (MCC) with R in a Lasso model. I am really confused. Thks in advance
birthwt=birthwt[,-10]
boot=sample(nrow(birthwt), 40)
train.data=birthwt[-boot, ]
test.data=birthwt[boot, ]
x =model.matrix(low~., train.data)[,-1]
y =train.data$low
Lasso Model:
library(glmnet)
set.seed(123)
cv.lasso = cv.glmnet(x, y, alpha = 1, family = "binomial")
model.lasso=glmnet(x,y,alpha=1,family="binomial",lambda= cv.lasso$lambda.min)
coef(model.lasso)
x.test = model.matrix(low ~., test.data)[,-1]
proba.lasso = predict(model.lasso,newx = x.test)
class.lasso = ifelse(proba.lasso > 0.5, 1, 0)
class.obs = test.data$low
If you do predict on a glmnet object, the default response is the logit link, but in your case, you need to do:
You can also use the probability this way:
To calculate mcc, you can do:
Or use something that calculates pearson's phi:
Or if you derive it from scratch using the formula from wiki: