-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path22.25 - Multinomial Logistic Regresyon 6 - Modeller Üzerinden Tahminler.R
executable file
·151 lines (97 loc) · 3.18 KB
/
22.25 - Multinomial Logistic Regresyon 6 - Modeller Üzerinden Tahminler.R
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#install.packages("nnet")
library(tidyverse)
library(nnet)
modelData <- read.csv('heart.csv')
View(modelData)
modelData <- modelData[ , -which(names(modelData) == "target")]
table(modelData$cp)
modelData <- modelData[modelData$cp != 3 , ]
table(modelData$cp)
modelData <- modelData %>% mutate(
cp = as.factor(cp),
slope = as.factor(slope),
ca = as.factor(ca),
thal = as.factor(thal),
restecg = as.factor(restecg)
)
table(modelData$restecg)
## Train ve Test Ayrımı
trainTestSplit <- function(data , dvName , seed){
tbl <- table(data[,dvName])
classes <- names(tbl)
minClass <- min(tbl)
lengthClass <- length(tbl)
train <- data.frame()
test <- data.frame()
for(i in 1:lengthClass){
selectedClass <- data[,dvName] == classes[i]
set.seed(seed)
sampleIndex <- sample(1:nrow(data[selectedClass , ]) , size = minClass*0.8)
train <- rbind(train , data[selectedClass , ][sampleIndex , ])
test <- rbind(test , data[selectedClass , ][-sampleIndex , ])
}
return(list(train , test))
}
train <- trainTestSplit(modelData , "cp" , 125)[[1]]
test <- trainTestSplit(modelData , "cp" , 125)[[2]]
table(train$cp)
table(test$cp)
### Keşfecidici Veri Analizi
par(mfrow= c(2,2))
plot(train$cp , train$age , main = "Age")
plot(train$cp , train$trestbps , main = "trestbps")
plot(train$cp , train$chol , main = "Chol")
plot(train$cp , train$thalach , main = "Thalach")
dev.off()
plot(train$cp , train$oldpeak , main = "Oldpeak")
table(train$cp , train$sex)
## Cinsiyet
chisq.test(table(train$cp , train$sex))
## Exang
chisq.test(table(train$cp , train$exang))
## Slope
chisq.test(table(train$cp , train$slope))
table(train$cp , train$slope)
# Ca
chisq.test(table(train$cp , train$ca))
# Fbs
chisq.test(table(train$cp , train$fbs))
# Thal
chisq.test(table(train$cp , train$thal))
# RestECG
chisq.test(table(train$cp , train$restecg))
# Multinomial Model Oluşturma
#install.packages("nnet")
library(e1071)
library(tidyverse)
library(nnet)
library(caret)
modelBase <- multinom(cp ~ . , data = train)
summary(modelBase)
modelBase$fitted.values
modelBase$decay
### Farklı Model Karşılaştırmaları
model2 <- multinom(cp ~ sex + fbs + restecg + thalach + exang + oldpeak + slope + ca + thal
, data = train)
# Model 2
summary(model2)
# Model Base
summary(modelBase)
model3 <- multinom(cp ~ thalach + exang + oldpeak + slope + ca + thal
, data = train)
# Model 2
summary(model3)
# Model Base
summary(modelBase)
#### Modeller Üzerinden Tahminler
library(caret)
caret::varImp(modelBase)
predModelBase <- predict(modelBase , test)
predModelBase
predModel2 <- predict(model2 , test)
predModel2
predModel3 <- predict(model3 , test)
predModel3
caret::confusionMatrix(predModelBase , test$cp , mode = "prec_recall")
caret::confusionMatrix(predModel2 , test$cp , mode = "prec_recall")
caret::confusionMatrix(predModel3 , test$cp , mode = "prec_recall")