Перепиши код на R, изменив его стиль. Поменяй комбинацию предикторов (Lag1, Lag2, Lag3, Volume и т. д.). Можешь комментариев добавить (на англ):

library(ISLR)
data("Weekly")

train_data <- subset(Weekly, Year < 2009)
test_data <- subset(Weekly, Year >= 2009)

# 1
logit_model1 <- glm(Direction ~ Lag2 + Volume, family = binomial, data = train_data)
logit_pred1 <- predict(logit_model1, newdata = test_data, type = "response")
logit_class1 <- ifelse(logit_pred1 > 0.5, "Up", "Down")
confusion_matrix_logit1 <- table(Predicted = logit_class1, Actual = test_data$Direction)
accuracy_logit1 <- sum(diag(confusion_matrix_logit1)) / sum(confusion_matrix_logit1)
print(1)
print(confusion_matrix_logit1)
print(accuracy_logit1)

# 2
logit_model2 <- glm(Direction ~ Lag2 * Volume, family = binomial, data = train_data)
logit_pred2 <- predict(logit_model2, newdata = test_data, type = "response")
logit_class2 <- ifelse(logit_pred2 > 0.5, "Up", "Down")
confusion_matrix_logit2 <- table(Predicted = logit_class2, Actual = test_data$Direction)
accuracy_logit2 <- sum(diag(confusion_matrix_logit2)) / sum(confusion_matrix_logit2)
print(2)
print(confusion_matrix_logit2)
print(accuracy_logit2)

# 3
logit_model3 <- glm(Direction ~ poly(Lag2, 2), family = binomial, data = train_data)
logit_pred3 <- predict(logit_model3, newdata = test_data, type = "response")
logit_class3 <- ifelse(logit_pred3 > 0.5, "Up", "Down")
confusion_matrix_logit3 <- table(Predicted = logit_class3, Actual = test_data$Direction)
accuracy_logit3 <- sum(diag(confusion_matrix_logit3)) / sum(confusion_matrix_logit3)
print(3)
print(confusion_matrix_logit3)
print(accuracy_logit3)

# 4
lda_model1 <- lda(Direction ~ Lag2, data = train_data)
lda_pred1 <- predict(lda_model1, newdata = test_data)
confusion_matrix_lda1 <- table(Predicted = lda_pred1$class, Actual = test_data$Direction)
accuracy_lda1 <- sum(diag(confusion_matrix_lda1)) / sum(confusion_matrix_lda1)
print(4)
print(confusion_matrix_lda1)
print(accuracy_lda1)

# 5
lda_model2 <- lda(Direction ~ Lag2 * Volume, data = train_data)
lda_pred2 <- predict(lda_model2, newdata = test_data)
confusion_matrix_lda2 <- table(Predicted = lda_pred2$class, Actual = test_data$Direction)
accuracy_lda2 <- sum(diag(confusion_matrix_lda2)) / sum(confusion_matrix_lda2)
print(5)
print(confusion_matrix_lda2)
print(accuracy_lda2)

# 6
lda_model2 <- lda(Direction ~ Lag2 * Volume + Today, data = train_data)
lda_pred2 <- predict(lda_model2, newdata = test_data)
confusion_matrix_lda2 <- table(Predicted = lda_pred2$class, Actual = test_data$Direction)
accuracy_lda2 <- sum(diag(confusion_matrix_lda2)) / sum(confusion_matrix_lda2)
print(6)
print(confusion_matrix_lda2)
print(accuracy_lda2)

# 7
qda_model1 <- qda(Direction ~ Lag2, data = train_data)
qda_pred1 <- predict(qda_model1, newdata = test_data)
confusion_matrix_qda1 <- table(Predicted = qda_pred1$class, Actual = test_data$Direction)
accuracy_qda1 <- sum(diag(confusion_matrix_qda1)) / sum(confusion_matrix_qda1)
print(7)
print(confusion_matrix_qda1)
print(accuracy_qda1)

# 8
qda_model2 <- qda(Direction ~ Lag2 * Volume, data = train_data)
qda_pred2 <- predict(qda_model2, newdata = test_data)
confusion_matrix_qda2 <- table(Predicted = qda_pred2$class, Actual = test_data$Direction)
accuracy_qda2 <- sum(diag(confusion_matrix_qda2)) / sum(confusion_matrix_qda2)
print(8)
print(confusion_matrix_qda2)
print(accuracy_qda2)

# 9
qda_model3 <- qda(Direction ~ poly(Lag2, 2), data = train_data)
qda_pred3 <- predict(qda_model3, newdata = test_data)
confusion_matrix_qda3 <- table(Predicted = qda_pred3$class, Actual = test_data$Direction)
accuracy_qda3 <- sum(diag(confusion_matrix_qda3)) / sum(confusion_matrix_qda3)
print(9)
print(confusion_matrix_qda3)
print(accuracy_qda3)