Why can "fitlm" predict untrained categorical values? When I train a linear model on categorical variable "Var3" with values '1' & '2', I should not be able to predict another sample data when the categorical value is '3' (that the model was untrained for).
REPRODUCTION STEP:
% Create a table containing categorical variable
>> T = table([-82;-67;-82;-77;-113;-123;-116;-71;-106;-108], ...[8.45;8.8;9.1;8.8;8.8;9.05;8.61;8.3;8.28;7.6], ...categorical({'1';'1';'1';'2';'2';'2';'2';'3';'3';'3'}))% Split data to training & test sets
>> training_set = T(1:7,:) % train on cat var 1 & 2 only
>> test_set = T(8:end,:) % test on cat var = 3
>> model = fitlm(training_set,'Var1 ~ Var2 + Var3');>> prediction = predict(model,test_set); % this should not work
Best Answer