Arvores de decisao condicional

library(tidyverse)
## ── Attaching packages ────────────────────────────────── tidyverse 1.3.2.9000 ──
## ✔ ggplot2   3.3.6      ✔ dplyr     1.0.10
## ✔ tibble    3.1.8      ✔ stringr   1.4.1 
## ✔ tidyr     1.2.1      ✔ forcats   0.5.2 
## ✔ readr     2.1.3      ✔ lubridate 1.8.0 
## ✔ purrr     0.3.5      
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(readr)

setwd("/Users/daniloap/Library/CloudStorage/OneDrive-Pessoal/Laboratorio_BKP/Pedro Brandao/PDCRS/")

roc <- read_delim("pdcrsROC.csv", 
    delim = ";", escape_double = FALSE, trim_ws = TRUE)
## Rows: 69 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ";"
## dbl (16): grupo.clinico, PDCRS, Idade, Escolaridade, FrontalSub, PosteriorCo...
## 
## ℹ 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.
library(caret)
## Loading required package: lattice
## 
## Attaching package: 'caret'
## 
## The following object is masked from 'package:purrr':
## 
##     lift
# Define o controle repeated 10 k-fold
train_control <- trainControl(method = "repeatedcv", number = 10, repeats = 3)

roc$group <- ifelse(roc$grupo.clinico == 0, "NC", "MCI")
roc$group <- as.factor(roc$group)

# train the model
model <- train(group ~ PDCRS + MoCA, 
               data = roc, trControl = train_control, method = "ctree")

# summarize results
print(model)
## Conditional Inference Tree 
## 
## 69 samples
##  2 predictor
##  2 classes: 'MCI', 'NC' 
## 
## No pre-processing
## Resampling: Cross-Validated (10 fold, repeated 3 times) 
## Summary of sample sizes: 62, 62, 63, 61, 63, 62, ... 
## Resampling results across tuning parameters:
## 
##   mincriterion  Accuracy   Kappa    
##   0.01          0.7220238  0.3420859
##   0.50          0.7303571  0.3563716
##   0.99          0.7351190  0.3572499
## 
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mincriterion = 0.99.
plot(model)

model0 <- train(group ~ ImmediateMemo + Naming + SustAttention + WorkingMemo +
                Clock + ClockCopy + DelayedMemo + AlternatedFluency + 
                ActionFluency, data = roc, 
                trControl = train_control, method = "ctree")

# summarize results
print(model0)
## Conditional Inference Tree 
## 
## 69 samples
##  9 predictor
##  2 classes: 'MCI', 'NC' 
## 
## No pre-processing
## Resampling: Cross-Validated (10 fold, repeated 3 times) 
## Summary of sample sizes: 62, 63, 62, 62, 63, 61, ... 
## Resampling results across tuning parameters:
## 
##   mincriterion  Accuracy   Kappa    
##   0.01          0.6984127  0.3189713
##   0.50          0.6888889  0.3064072
##   0.99          0.6301587  0.1777797
## 
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mincriterion = 0.01.
plot(model0)

library(partykit)
## Loading required package: grid
## Loading required package: libcoin
## Loading required package: mvtnorm
ct <- ctree(group ~ ImmediateMemo + Naming + SustAttention + WorkingMemo +
                Clock + ClockCopy + DelayedMemo + AlternatedFluency + 
                ActionFluency, data = roc, mincriterion = 0.95)
ct
## 
## Model formula:
## group ~ ImmediateMemo + Naming + SustAttention + WorkingMemo + 
##     Clock + ClockCopy + DelayedMemo + AlternatedFluency + ActionFluency
## 
## Fitted party:
## [1] root
## |   [2] ImmediateMemo <= 9
## |   |   [3] AlternatedFluency <= 12: MCI (n = 26, err = 26.9%)
## |   |   [4] AlternatedFluency > 12: NC (n = 7, err = 0.0%)
## |   [5] ImmediateMemo > 9: NC (n = 36, err = 16.7%)
## 
## Number of inner nodes:    2
## Number of terminal nodes: 3
plot(ct)

#png("ctree.01.png", res=300, height=8, width=14, units="in")
#plot(as.simpleparty(ct))
#dev.off()
## 
## Model formula:
## group ~ PDCRS
## 
## Fitted party:
## [1] root
## |   [2] PDCRS <= 77: MCI (n = 11, err = 0.0%)
## |   [3] PDCRS > 77
## |   |   [4] PDCRS <= 96: NC (n = 32, err = 40.6%)
## |   |   [5] PDCRS > 96: NC (n = 26, err = 3.8%)
## 
## Number of inner nodes:    2
## Number of terminal nodes: 3

## 
## Model formula:
## group ~ PDCRS + MoCA
## 
## Fitted party:
## [1] root
## |   [2] MoCA <= 21: MCI (n = 14, err = 7.1%)
## |   [3] MoCA > 21
## |   |   [4] PDCRS <= 95: NC (n = 26, err = 42.3%)
## |   |   [5] PDCRS > 95: NC (n = 29, err = 3.4%)
## 
## Number of inner nodes:    2
## Number of terminal nodes: 3

## 
## Model formula:
## group ~ ImmediateMemo + Naming + SustAttention + WorkingMemo + 
##     Clock + ClockCopy + DelayedMemo + AlternatedFluency + ActionFluency
## 
## Fitted party:
## [1] root
## |   [2] ClockCopy <= 9
## |   |   [3] SustAttention <= 8: MCI (n = 8, err = 0.0%)
## |   |   [4] SustAttention > 8: MCI (n = 12, err = 50.0%)
## |   [5] ClockCopy > 9
## |   |   [6] WorkingMemo <= 6: NC (n = 17, err = 47.1%)
## |   |   [7] WorkingMemo > 6
## |   |   |   [8] ActionFluency <= 17: NC (n = 12, err = 25.0%)
## |   |   |   [9] ActionFluency > 17: NC (n = 20, err = 0.0%)
## 
## Number of inner nodes:    4
## Number of terminal nodes: 5

## 
## Model formula:
## group ~ ImmediateMemo + Naming + SustAttention + WorkingMemo + 
##     Clock + DelayedMemo + AlternatedFluency + ActionFluency
## 
## Fitted party:
## [1] root
## |   [2] DelayedMemo <= 5
## |   |   [3] WorkingMemo <= 6: MCI (n = 18, err = 11.1%)
## |   |   [4] WorkingMemo > 6: NC (n = 14, err = 28.6%)
## |   [5] DelayedMemo > 5
## |   |   [6] ActionFluency <= 17: NC (n = 17, err = 29.4%)
## |   |   [7] ActionFluency > 17: NC (n = 20, err = 0.0%)
## 
## Number of inner nodes:    3
## Number of terminal nodes: 4

## 
## Model formula:
## group ~ MoCA + ImmediateMemo + Naming + SustAttention + WorkingMemo + 
##     Clock + ClockCopy + DelayedMemo + AlternatedFluency + ActionFluency
## 
## Fitted party:
## [1] root
## |   [2] MoCA <= 21: MCI (n = 14, err = 7.1%)
## |   [3] MoCA > 21
## |   |   [4] WorkingMemo <= 6: NC (n = 18, err = 44.4%)
## |   |   [5] WorkingMemo > 6
## |   |   |   [6] Naming <= 17: NC (n = 12, err = 25.0%)
## |   |   |   [7] Naming > 17
## |   |   |   |   [8] MoCA <= 27: NC (n = 18, err = 0.0%)
## |   |   |   |   [9] MoCA > 27: NC (n = 7, err = 14.3%)
## 
## Number of inner nodes:    4
## Number of terminal nodes: 5

## 
## Model formula:
## group ~ FrontalSub + PosteriorCort
## 
## Fitted party:
## [1] root
## |   [2] FrontalSub <= 58: MCI (n = 22, err = 22.7%)
## |   [3] FrontalSub > 58
## |   |   [4] FrontalSub <= 74: NC (n = 26, err = 30.8%)
## |   |   [5] FrontalSub > 74: NC (n = 21, err = 0.0%)
## 
## Number of inner nodes:    2
## Number of terminal nodes: 3