0

I'm trying to double check that the Linear Discriminant Analysis function is doing what I think it is, and that the associated model lines up.

I create my data frame, run it through lda() and predict() and it changes the order in some way I don't understand, so I can't manually check that things work using the generated coefficients

 My_Data <-as.data.frame( cbind(
   c("CBT","CBT","CBT","CBT","CBT","CBT","CBT","CBT","CBT","CBT","BT","BT","BT","BT","BT","BT","BT","BT","BT","BT","No_Treatment_Control","No_Treatment_Control","No_Treatment_Control","No_Treatment_Control","No_Treatment_Control","No_Treatment_Control","No_Treatment_Control","No_Treatment_Control","No_Treatment_Control","No_Treatment_Control"),
      as.numeric(c(5,5,4,4,5,3,7,6,6,4,4,4,1,1,4,6,5,5,2,5,4,5,5,4,6,4,7,4,6,5)),
      as.numeric(c(14,11,16,13,12,14,12,15,16,11,14,15,13,14,15,19,13,18,14,17,13,15,14,14,13,20,13,16,14,18))))
colnames(My_Data) <-  c("Group","Thoughts","Actions")


 ocdDFA<-lda(Group ~ Actions + Thoughts, data = My_Data)
 predict(ocdDFA)
 plot(ocdDFA, fill =  "Group")

This produces: LDA_Output

I should be able to get the predict(ocdDFA)$x values by taking each row of my data and going...

LD1 = Coefficient_ActionAction_Value + Coefficient_ThoughtsAction_Thoughts.

But the output from predict() changes the order of my data points, so I can't actually test this. ( I can tell because the order of Group changed from my original My_Data ) Predict_LDA_Output

So I'm wondering.

  1. What is actually happening to the order
  2. Why isn't the sum of coefficient*values lining up with the output from predict()?
  • I believe the order is correct. The order of groups in `predict(ocdDFA)$class` is different because it is a prediction and the model is not perfect (about 77% of predictions match your original grouping). As for question 2., I am not sure but there is probably some scaling and rotation going on. – Robert Hacken Sep 12 '22 at 14:54
  • Thanks Robert, any idea how to track what kind of scaling/rotation is going on? I've tried naming rows, but they disappear after being processed. – JeffArrrr Sep 13 '22 at 08:49
  • I don't know how LDA works, you would probably have to go to the references mentioned in the documentation to find out what exactly is going on. Or look into the source code: `MASS:::lda.default`. Row names only disappear in `predict(...)$class` but are retained in both `predict(...)$posterior` and `predict(...)$x` (if this is what you meant). – Robert Hacken Sep 13 '22 at 09:04

0 Answers0