-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.Rmd
87 lines (65 loc) · 1.45 KB
/
test.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
```{r}
df <- read.csv("./data/raw/framingham.csv")
df
```
```{r}
sampl <- sapply(df, class)
sampl
```
```{r}
library(psych)
describe(df)
```
```{r}
library(purrr)
encode_ordinal <- function(x, order = unique(x)) {
x <- as.numeric(factor(x, levels = order, exclude = NULL))
x
}
colTypes <- map(df, class)
for (col in colnames(df)) {
if (colTypes[col] == 'character') {
df[[col]] <- encode_ordinal(df[[col]])
}
}
df
```
```{r}
df <- df[complete.cases(df),]
df
```
```{r}
as.factor(df$'TenYearCHD')
```
```{r}
library(caret)
df$'TenYearCHD' <- as.factor(df$'TenYearCHD')
inTrain <- createDataPartition(as.factor('TenYearCHD'), times = 1, p = .75, list = F)
data_train <- df[inTrain, ]
data_test <- df[-inTrain, ]
```
```{r}
logit <- train(as.factor(TenYearCHD)~., data = df, method = 'ranger', metric='Accuracy')
logit
```
```{r}
data_test$pred <- predict(logit, data_test)
data_test$factor_pred <- as.factor(data_test$pred)
data_test$factor_truth <- as.factor(data_test$TenYearCHD)
precision <- posPredValue(data_test$factor_truth, data_test$factor_pred)
recall <- sensitivity(data_test$factor_truth, data_test$factor_pred)
cm <- confusionMatrix(data_test$pred, data_test$TenYearCHD)
accuracy <- cm$overall[1]
accuracy
cm
```
```{r}
predict <- predict(logit, data_test, type = 'response')
# confusion matrix
table_mat <- table(data_test$TenYearCHD, predict > 0.5)
table_mat
```
```{r}
accuracy_Test <- sum(diag(table_mat)) / sum(table_mat)
accuracy_Test
```