library(tidyverse)
library(caret)
library(here)
library(inspectdf)
library(glmnet)
library(ROSE)

rm(seqs)
load(here("notebooks/data/nback_seqs.Rd"))
seqs.raw <- seqs


seqs <- seqs.raw %>% 
  mutate(
    rt_cat=cut(rt,breaks=c(-Inf,-1000,+500,+Inf), labels=c("low","mid","high")),
  ) %>%
  filter(rt_cat!="low") %>%
  mutate(
    rt_cat = droplevels(rt_cat)
  ) %>%
  drop_na(rt, rt_cat, correct, tl,sl)

f <- rt_cat ~ n + t + v 
#f <- rt_cat ~ n + t + v + s + l + vl + sl + tl + ul + ll
#f <- correct ~ n + participant

train.indices <- createDataPartition(seqs[[toString(f[[2]])]], p = .8, list =FALSE)

seqs.train.imbalanced <- seqs[train.indices,]
seqs.train <- seqs.train.imbalanced
seqs.train <- ROSE(f, data = seqs.train.balanced)$data

seqs.train.x <- model.matrix(f, seqs.train)[,-1]
seqs.train.y <- seqs.train[[toString(f[[2]])]]

seqs.test  <- seqs[-train.indices,]
seqs.test.x <-  model.matrix(f, seqs.test)[,-1]
seqs.test.observed_y <- seqs.test[[toString(f[[2]])]]
  

# ROC for each var
#filterVarImp(as.data.frame(seqs.train.x), seqs.train.y)

# model <- cv.glmnet(seqs.train.x,
#                    seqs.train.y,
#                    alpha = 1,
#                    nfolds = 5,
#                    family = "binomial",
#                    type.measure = "auc")
# 
# model$lambda.min

ctrl <- trainControl(method="cv",
                     number=1, 
                     classProbs=T,
                     verbose = T,
                     sampling = "up",
                     savePredictions = T,
                     summaryFunction=twoClassSummary)

max_components <- n_distinct(attr(terms(f),"term.labels")) -1
# pls tune
tune <- expand.grid(ncomp=2:max_components)

model <- train(seqs.train.x,
               seqs.train.y, 
               method = "pls",
               family = "binomial",
               metric = "ROC",
               preProc = c("center", "scale"),
               verboseIter = TRUE,
               tuneLength = 2,
               tuneGrid = tune,
               trControl = ctrl)

model$bestTune
plot(model)

seqs.test.y <- model %>% predict(seqs.test.x)
seqs.test.y_prob <- model %>% predict(seqs.test.x, type="prob")

confusionMatrix(seqs.test.y, seqs.test.observed_y)

plot(varImp(model, scale = F, useModel = F))

roc(seqs.test.observed_y,
    seqs.test.y_prob$mid,
    legacy.axes=T,
    plot = T,
    lwd=2,
    col="black",
    print.auc=T,
    percent = T,
    print.auc.y = 40,
    print.auc.x = 55,
    lty = 1,
    of = "auc",
    boot.n = 100,
    ci = T)



