1

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
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
lomha
  • 11
  • 1

1 Answers1

0

If you do predict on a glmnet object, the default response is the logit link, but in your case, you need to do:

class.lasso = predict(model.lasso,newx = x.test,type="class")
class.lasso = as.numeric(class.lasso)
class.obs = test.data$low

You can also use the probability this way:

class.lasso = ifelse(predict(model.lasso,newx = x.test,type="response") > 0.5,1,0)

To calculate mcc, you can do:

library(mltools)
mcc(pred = class.lasso, actual = class.obs)
[1] 0.2581989

Or use something that calculates pearson's phi:

library(psych)
phi(table(class.lasso,class.obs),digits=7)

[1] 0.2581989

Or if you derive it from scratch using the formula from wiki:

cm = table(class.lasso,class.obs)
TP = cm[2,2]
FP = cm[2,1]
TN = cm[1,1]
FN = cm[1,2]

(TP * TN - FP*FN)/sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN))
[1] 0.2581989
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • Very clear and quick. Thank you. Any way to calculate the Accuracy, sensitivity and sensibility for the model? – lomha Feb 13 '21 at 19:50
  • you can use confusionMatrix from the package caret. Do ```confusionMatrix(table(class.lasso,class.obs))``` – StupidWolf Feb 13 '21 at 19:54
  • it doesn't work: Error in confusionMatrix_(actual, predicted, cutoff) : argument "predicted" is missing, with no default Otherwise, for the first code: class.lasso = predict(model.lasso,newx = x.test,type="class"), is it porba.lasso (as I wrote it) or class.lasso? – lomha Feb 13 '21 at 20:08
  • you are missing the ```table()``` . it is class lasso. Hey I have to say this, if you have additional questions, please post another question. If you repeat my code above, for sure you can get MCC. can you please try that and don't detour – StupidWolf Feb 13 '21 at 20:12
  • see for example https://stackoverflow.com/questions/40451926/how-to-send-a-confusion-matrix-to-carets-confusionmatrix, https://stackoverflow.com/questions/33431694/caret-package-defining-positive-result/33432399#33432399 and such for specificity and sensitivity – StupidWolf Feb 13 '21 at 20:17