## Can someone tell me if there is an error in fitcdiscr function?

Asked by Jose Sanchez

### Jose Sanchez (view profile)

on 20 Oct 2018
Latest activity Commented on by Tom Lane

### Tom Lane (view profile)

on 9 Nov 2018
Accepted Answer by Bernhard Suhm

### Bernhard Suhm (view profile)

I think the fitcdiscr function has error(s). I tested both the quadratic discrimination option:
rng('default'); X = rand(40,2); GroupLabels = randsample({'HC','MCI'}, 40, true)'; Cost0 = [0 1; 1 0];
DAC = fitcdiscr(X, GroupLabels, 'DiscrimType', 'quadratic', 'Cost', Cost0);
K = DAC.Coeffs(1,2).Const;
L = DAC.Coeffs(1,2).Linear;
and the linear augmented option that reproduce the same quadratic fit:
X1 = X(:,1); X2 = X(:,2); X3 = X1.*X1; X4 = X1.*X2; X5 = X2.*X2;
tbl = table(GroupLabels, X1, X2, X3, X4, X5);
DAC2 = fitcdiscr(tbl, 'GroupLabels ~ X1 + X2 + X3 + X4 + X5', 'Cost', Cost0);
K2 = DAC2.Coeffs(1,2).Const;
L2 = DAC2.Coeffs(1,2).Linear;
Please check the values of K,L,Q and K2,L2. They don't match, not even close and fitting is very differenct between both classifiers.
Also, I think that fitcdiscr is not reading or using properly the specified formula. Previously, I had used the declaration:
DAC2 = fitcdiscr(tbl, 'GroupLabels ~ X1 + X2 + X3 + X5 + X4', 'Cost', Cost0);
That is, switching the 4th and 5th predictors, and the coefficient values were the same in the same order.
But that is wrong (in my opinion) cause coefficients 4th and 5th are now inverted!
Thanks!

R2017a

### Bernhard Suhm (view profile)

Answer by Bernhard Suhm

### Bernhard Suhm (view profile)

on 8 Nov 2018

LDA fits normal distributions with the same covariance. QDA allows different covariances. Fitting normal distributions including squared and product terms usually won't give the same results as QDA on just the linear terms.

Jose Sanchez

### Jose Sanchez (view profile)

on 8 Nov 2018
Please, can you provide a reference paper to check this? With respect to the table issue regarding the covariates order that I also mentioned above, is there any logical explanation? Thanks!
Bernhard Suhm

### Bernhard Suhm (view profile)

on 8 Nov 2018
You could review your favorite reference explaining linear versus quadratic discriminant analysis, e.g. on Wikipedia. - Regarding the order, you defined X4 and X5 as something specific, not to reference specific columns in your matrix, that's why the order in your formula doesn't matter.
Tom Lane

### Tom Lane (view profile)

on 9 Nov 2018
To elaborate on Bernhard's response, the formulas generally define the types of terms to be used rather than the order, and this is easier to see in functions that produce coefficients that are displayed directly:
>> fitlm(t,'x1~x3+x2')
ans =
Linear regression model:
x1 ~ 1 + x2 + x3
Estimated Coefficients:
Estimate SE tStat pValue
________ ________ ______ __________
(Intercept) 2.2491 0.24797 9.0702 7.0385e-16
x2 0.59552 0.069328 8.5899 1.1633e-14
x3 0.47192 0.017118 27.569 5.8479e-60