#load libraries and functions this could be a helper file, but hope to see this first in the rshiny

#get data, select participant

get the data and select a subject the select subject part will be a dropdown menu. so “Individual” will be selected by user

##note readit is more general and will pick up multiple data file types

data<-readit("/Users/jociarrochi/Dropbox/shinyr/ibh/current instructions/AndrewNa.csv")
## File guessed to be CSV ("/Users/jociarrochi/Dropbox/shinyr/ibh/current instructions/AndrewNa.csv")
## Rows: 5922 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (9): ID, Time, NAffect, FocusImportantMoments, AllowFeelings, ObserveTho...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Individual<-7168   ## reactive drop down menu

FocusData  <- data  %>% filter(ID == Individual)


ind_vars <- c('FocusImportantMoments', 'AllowFeelings', 'SelfPole', 
              'ChoseValue', 'CommitAction','ObserveThoughtsDistance')
dep_var <- 'NAffect'


IDs <- unique(FocusData$ID)


datatable(FocusData)

#smooth data and display as line chart

choseIV1 and 2 will be from a reactive option from dropdown menu

i.e., user can choose what is selected. time is always in data file and always on x

#i-boruta

Will need to make this select automatically. So we will have datafiles with DV, ivs, and the variable Time

# Implement boruta
set.seed(8675309)

boruta_results <- run_boruta(FocusData, dep_var, ind_vars)

temp<-datatable(boruta_results$stat_results)


temp

i-arima

scaled_data_by_id <- FocusData%>%
  dplyr::group_by(ID) %>%
  dplyr::select(dplyr::all_of(c(dep_var, ind_vars))) %>%
  dplyr::mutate(across(.cols=everything(), scale)) %>%
  as.vector() %>%
  as.data.frame() %>%
  ungroup()




results = run_all_participants(scaled_data_by_id, model_order=NULL,automate = T, lagged = F)



##person centered, long format
arima_summary <- tidyr::pivot_longer(results$se_mat, cols = -ID, names_to = 'variable', 
                                     values_to = 'arima.SE') %>%
  dplyr::inner_join(tidyr::pivot_longer(results$coef_mat, cols = -ID, names_to = 'variable', 
                                        values_to = 'arima.coef'), 
                    by=c('ID', 'variable')) 

arima_summary$Tvalue<-arima_summary$arima.coef/arima_summary$arima.SE



datatable(arima_summary)