0

I want to use LDA only with each predictor separately. Therefore I want to use a for loop and for that I need to perform LDA without using the name of the predictor and using its index instead.

When I use the name of the predictor (for example, Lag2), it runs perfectly. But when I type Weekly[,3] (which is Lag2), the prediction LDA.pred has more length than it should.

My code:

library(ISLR2)
attach(Weekly)
library(MASS)

#Separation of training and test data, 1089 total observations, 104 test observations
train <- (Year<2009)
Weekly.Test=Weekly[!train,]
Direction.Test=Direction[!train]

#If I substitute "Weekly[,3]" by Lag2 it works
LDA=lda(Direction~Weekly[,3], data=Weekly, subset=train)
LDA.pred=predict(LDA, Weekly.Test)
LDA.Direction=LDA.pred$class

I recieved this warning message (LDA.pred has 1089 predictions but it should have only 104)

Warning: 'newdata' had 104 rows but variables found have 1089 rows

So: How can I perform LDA using the predictor index instead of the name?

Thanks a lot!

IreneGS
  • 1
  • 1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 16 '22 at 10:32

0 Answers0