I am using this function to calculate MCC: https://www.rdocumentation.org/packages/mltools/versions/0.3.5/topics/mcc.
It generates this error:
Error in mcc (actuals = data.test $ Y, preds = pred_rf, TP = tab_rf [1, 1],:
Either {'preds' and 'actuals'} or {'TP', 'FP', 'TN', 'FN'} or {'confusionM'} should be provided.
My code is the following (I am using Random Forest):
library("mltools")
library("caret")
library("MASS")
library("MLmetrics")
data=data.frame()
for (i in 0:4){
mu1=c(0 i,4)
Sigma= matrix(c(1,0.5,0.5,1),ncol=2)
X1_distribution=mvrnorm(n=100,mu=mu1,Sigma=Sigma)
mu2=c(5-i,1)
Sigma= matrix(c(1,0.5,0.5,1),ncol=2)
X2_distribution=mvrnorm(n=100,mu=mu2,Sigma=Sigma)
X=rbind(X1_distribution,X2_distribution)
Y=c(rep(1,100),
rep(0,100))
data.aux=data.frame(Y=Y,
X1=X[,1],
X2=X[,2])
data=rbind(data,data.aux)
}
data.train=data[1:800,]
data.test=data[801:1000,]
method_rf= train(form= formula('factor(Y) ~ .'),data=data.train, method="rf") #randomForest
pred_rf=predict(method_rf,data.test[,2:3]) #Predict randomForest
tab_rf=table(actual=data.test$Y, #ConfusionMatrix for RF
predicted=pred_rf)
metrics_rf=data.frame(MCC_rf=mcc(actuals=data.test$Y,
preds=pred_rf,
TP=tab_rf[1,1],
FP=tab_rf[1,2],
TN=tab_rf[2,2],
FN=tab_rf[2,1],
confusionM=tab_rf))
metrics_rf
CodePudding user response:
As the error message implies, you should use either {'preds' and 'actuals'}
or {'TP', 'FP', 'TN', 'FN'}
or {'confusionM'}
, but certainly not all of them togetner, as you are trying to do here.
Changing the MCC calculation in your code to
MCC_rf=mcc(TP=tab_rf[1,1],
FP=tab_rf[1,2],
TN=tab_rf[2,2],
FN=tab_rf[2,1])
works like a charm:
> MCC_rf
[1] 0.60012