K fold Cross Validation Accuracy (mnrfit)

Hello,
I am trying to calculate cross validation accuracy of an ordinal model (using mnrfit and mnrval) as follows:
K=10;
indices=crossvalind('Kfold',DS,K);
for i_fold=1:K
test = (indices == i_fold);
train = ~test;
train_x=variable(train,:);
train_y=DS(train,:);
test_x=variable(test,:);
test_y=DS(test,:);
[b,dev,stats] = mnrfit(train_x,train_y,'interactions','off','model','ordinal','link','logit');
y_hat = mnrval(b,test_x,stats,'interactions','off','type','cumulative','model','ordinal','link','logit');
ccdf_yfit=1-y_hat;
class1 = ccdf_yfit > 0.5;
for i=1:length(test_x)
index_pred(i)=max(find(class1(i,:)))+1;
index_pred(i)=index_pred(i)';
Diff=abs(test_y-index_pred);
Pen_Acc=zeros(K,1);
if Diff == 0
Pen_Acc(i_fold)=1;
elseif Diff == 1
Pen_Acc(i_fold) = 1-0.2;
elseif Diff == 2
Pen_Acc(i_fold) = 1-0.4;
elseif Diff == 3
Pen_Acc(i_fold) = 1-0.6;
elseif Diff == 4
Pen_Acc(i_fold) = 1-0.8;
else
Pen_Acc(i_fold) = 0
end
end
end
DS are my response labels, and variable is my predictor.
My problem is, as I try to calculate 'Diff', I get an error telling me that test_y and index_pred are not the same size. For each value of i_fold, I want a vector of predicted classes index_pred that I can compare to the real labels in the 'test' subset. Any idea on what I am doing wrong / how I can achieve this?
Many thanks, IC

Answers (0)

Categories

Asked:

on 16 Dec 2013

Commented:

on 10 Nov 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!