4
$\begingroup$

I tried discriminant analysis with lda() in R and in SPSS, but the scalings were different, why?

N, how to get (Constant) with R like SPSS result?

data:

head(data)
  ï..smoke age selfcon anxiety absence subtestb
1        1  36      42      17       3       30
2        1  45      45      21       0       29
3        1  43      36      13       8       23
4        2  25      25      23      14       20
5        2  36      32      25       9       16
6        2  25      19      27       5       20

lda() result

lda(x,cl)$scaling
                LD1
age      -0.0237009
selfcon  -0.0800297
anxiety   0.0999290
absence   0.0115092
subtestb -0.1341198

SPSS result:

age            .024
selfcon        .080
anxiety score −.100
absence       −.012
subtestb       .134
(Constant)   −4.543
$\endgroup$
2
  • 1
    $\begingroup$ Yopy, I've added a formula to here showing how those constants are being computed in SPSS. Hope that helps. $\endgroup$
    – ttnphns
    Aug 16, 2015 at 9:06
  • $\begingroup$ A commented step-by-step LDA example with iris data (with results identical to what spss will give) is here. $\endgroup$
    – ttnphns
    Nov 5, 2016 at 8:39

2 Answers 2

3
$\begingroup$

Except for the constant, the numbers in SPSS are just the rounded results of the numbers in R. There is no constant in R because by default, R function 'lda' from the MASS package, centers the data.

Because of questions in the comments I added:

If you look at the numbers in R and those in SPSS then (1) they have opposite signs but that doesn't make a difference it just means that the class that R takes as positive is chosen as the negative by SPSS (it is just a matter of coding the binary outcome) (2) in SPSS they are rounded to 3 digits and (3) in SPSS You have a constant while in R you don't. That is because R centers the data.

If one wants to obtain the constant in R, then you can apply the LDA-formulas as in this pdf (section 4.3). Formula (4.9) of this reference shows the constant on the first line (no $x$ there) and the coefficients on the second line. On the next page, with bullets, you see how you can estimate the parameters from your data.

$\endgroup$
9
  • $\begingroup$ sorry, i don't understand with your statement. plese give me more information $\endgroup$
    – yopy
    Aug 13, 2015 at 8:51
  • 2
    $\begingroup$ f coppens said the correct thing. Those two outputs are identical. Except sign reversion (which is arbitrary, as it is in PCA). And the constant term which SPSS added in case a user wants to de-center the results. $\endgroup$
    – ttnphns
    Aug 13, 2015 at 8:58
  • 2
    $\begingroup$ If you look at the numbers in R and those in SPSS then (1) they have opposite signs but that doesn't make a difference it just means that the class that R takes as positive is chosen as the negative by SPSS (it is just a matter of coding the binary outcome) (2) in SPSS they are rounded to 3 digits and (3) in SPSS You have a constant while in R you don't. That is because R centers the data $\endgroup$
    – user83346
    Aug 13, 2015 at 8:59
  • $\begingroup$ @fcoppens, SPSS centers the data too (as always is done in LDA). The constant term is computed afterwards, - for a user to be able to shift results in space onto the noncentered position. This constant is hardly ever needed. $\endgroup$
    – ttnphns
    Aug 13, 2015 at 9:03
  • $\begingroup$ in SPSS they are rounded to 3 digits @yopy, you can open the output table in SPSS and set whatever you like the number of digits. $\endgroup$
    – ttnphns
    Aug 13, 2015 at 9:06
0
$\begingroup$

For obtain constant term in Discriminant Analysis on R (with library MASS):

groupmean<-(model$prior%*%model$means)
constant<-(groupmean%*%model$scaling)
-constant # for result equal to SPSS

where model is the model discriminant. Example:

model<-lda(y~x1+x2+xn,data=mydata)
model
$\endgroup$
1
  • $\begingroup$ Although implementation is often mixed with substantive content in questions, we are supposed to be a site for providing information about statistics, machine learning, etc., not code. It can be good to provide code as well, but please elaborate your substantive answer in text for people who don't read this language well enough to recognize & extract the answer from the code. $\endgroup$ Sep 23, 2018 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.