Hi Max, thank you for the help. I had thought that might be the case, but I still get an error, albeit a slightly different one:
library(tidymodels)
library(readr)
#>
#> Attaching package: 'readr'
#> The following object is masked from 'package:yardstick':
#>
#> spec
#> The following object is masked from 'package:scales':
#>
#> col_factor
library(Rmisc)
#> Loading required package: lattice
#> Loading required package: plyr
#> ------------------------------------------------------------------------------
#> You have loaded plyr after dplyr - this is likely to cause problems.
#> If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
#> library(plyr); library(dplyr)
#> ------------------------------------------------------------------------------
#>
#> Attaching package: 'plyr'
#> The following object is masked from 'package:purrr':
#>
#> compact
#> The following objects are masked from 'package:dplyr':
#>
#> arrange, count, desc, failwith, id, mutate, rename, summarise,
#> summarize
#library(boot)
setwd("~/covid_survey/Cleaned_Up")
df0<- read_csv("PTSD_cleaned.csv")
#>
#> ── Column specification ────────────────────────────────────────────────────────
#> cols(
#> .default = col_double(),
#> URN = col_character(),
#> StartDate = col_character(),
#> CompletionDate = col_character()
#> )
#> ℹ Use `spec()` for the full column specifications.
df0$PCLV_Binary_Score<-as.factor(df0$PCLV_Binary_Score)
df0$Gender<-as.factor(df0$Gender)
df0$Ethnicity<-as.factor(df0$Ethnicity)
df0$Age<-as.factor(df0$Age)
df0$Location<-as.factor(df0$Location)
df0$Income<-as.factor(df0$Income)
df0$Education<-as.factor(df0$Education)
df0$Working_From_Home<-as.factor(df0$Working_From_Home)
PTSD<-na.omit(df0)
set.seed(123)
rs <- vfold_cv(PTSD, v = 5)
lr_mod <- logistic_reg() %>% set_engine("glm")
perf_metrics <- metric_set(accuracy, sensitivity, specificity)
lr_models <-
lr_mod %>%
fit_resamples(PCLV_Binary_Score ~ Traditional_Time_Hrs+
Private_Use+
Public_Use+
Gender+
Ethnicity+
Age+
Location+
Income+
Education+
Working_From_Home+
COVID_Risk+
Anxiety_Diagnosed+
Depression_Diagnosed+
PTSD_Diagnosed+
Lived_Alone,
resamples = rs,
metrics = perf_metrics,
control = control_resamples(save_pred = TRUE))
#> ! Fold1: preprocessor 1/1, model 1/1: glm.fit: algorithm did not converge, glm.fi...
#> ! Fold1: preprocessor 1/1, model 1/1 (predictions): prediction from a rank-defici...
#> ! Fold2: preprocessor 1/1, model 1/1: glm.fit: algorithm did not converge, glm.fi...
#> ! Fold2: preprocessor 1/1, model 1/1 (predictions): prediction from a rank-defici...
#> ! Fold3: preprocessor 1/1, model 1/1: glm.fit: algorithm did not converge, glm.fi...
#> ! Fold3: preprocessor 1/1, model 1/1 (predictions): prediction from a rank-defici...
#> ! Fold4: preprocessor 1/1, model 1/1: glm.fit: algorithm did not converge, glm.fi...
#> ! Fold4: preprocessor 1/1, model 1/1 (predictions): prediction from a rank-defici...
#> ! Fold5: preprocessor 1/1, model 1/1: glm.fit: algorithm did not converge, glm.fi...
#> ! Fold5: preprocessor 1/1, model 1/1 (predictions): prediction from a rank-defici...
collect_metrics(lr_models, summarize = FALSE)
#> # A tibble: 15 x 5
#> id .metric .estimator .estimate .config
#> <chr> <chr> <chr> <dbl> <fct>
#> 1 Fold1 accuracy binary 0.931 Preprocessor1_Model1
#> 2 Fold1 sens binary 0.943 Preprocessor1_Model1
#> 3 Fold1 spec binary 0.8 Preprocessor1_Model1
#> 4 Fold2 accuracy binary 0.793 Preprocessor1_Model1
#> 5 Fold2 sens binary 0.811 Preprocessor1_Model1
#> 6 Fold2 spec binary 0.6 Preprocessor1_Model1
#> 7 Fold3 accuracy binary 0.825 Preprocessor1_Model1
#> 8 Fold3 sens binary 0.86 Preprocessor1_Model1
#> 9 Fold3 spec binary 0.571 Preprocessor1_Model1
#> 10 Fold4 accuracy binary 0.825 Preprocessor1_Model1
#> 11 Fold4 sens binary 0.868 Preprocessor1_Model1
#> 12 Fold4 spec binary 0.25 Preprocessor1_Model1
#> 13 Fold5 accuracy binary 0.807 Preprocessor1_Model1
#> 14 Fold5 sens binary 0.852 Preprocessor1_Model1
#> 15 Fold5 spec binary 0 Preprocessor1_Model1
conf_mat_resampled(lr_models)
#> # A tibble: 4 x 3
#> Prediction Truth Freq
#> <fct> <fct> <dbl>
#> 1 0 0 45.6
#> 2 0 1 2.4
#> 3 1 0 7
#> 4 1 1 2.4
print(collect)
#> function (x, ...)
#> {
#> UseMethod("collect")
#> }
#> <bytecode: 0x7f89fd0be760>
#> <environment: namespace:dplyr>
confusion_matrices <-
lr_models %>%
collect_predictions() %>%
group_nest(id) %>%
mutate(confusion =
map(PTSD, ~ conf_mat(.x, truth = PCLV_Binary_Score, estimate = .pred_class)))
#> Error in UseMethod("conf_mat"): no applicable method for 'conf_mat' applied to an object of class "character"
confusion_matrices$confusion[[1]]
#> Error in eval(expr, envir, enclos): object 'confusion_matrices' not found