-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp.R
62 lines (53 loc) · 1.95 KB
/
temp.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
# - - - - - - - - - - - - - - - - - - - - #
# File: temp.R
# Objective: Temporary file for running ui and server related code for the DecisionCurve shiny app
# Date created: 1/24/2019
# Modified by: C.K.
# Date modified: 1/24/2019
# - - - - - - - - - - - - - - - - - - - - #
# - - - - - - - - - - - - - - - - - - - - #
# Setting up ----
# - - - - - - - - - - - - - - - - - - - - #
# load libraries
# shiny packages
library(shiny)
library(shinydashboard)
# for summary statistics
library(DT)
library("psych")
library(dplyr)
library(formattable)
# prediction models
library(rpart)
library(randomForest)
library(e1071)
library(xgboost)
# plotting
library(ggfortify)
library(plotly)
library(pROC)
library(rpart.plot)
library(caret)
# fit stats
library(stargazer)
library("MASS")
# - - - - - - - - - - - - - - - - - - - - #
# Plotting Confusion Matrix ----
# - - - - - - - - - - - - - - - - - - - - #
mod_glm <- glm(low ~ age + race + lwt + smoke, data = birthwt, family = binomial)
as.factor(mod_glm$y)
cm <- confusionMatrix(if_else(mod_glm$fitted.values >= 0.5, as.factor(names(table(birthwt[,"low"])))[1], as.factor(names(table(birthwt[,"low"])))[2]), as.factor(birthwt[,"low"]))
mod_glm$fitted.values
fourfoldplot(cm$table)
mod_rf <- randomForest(as.factor(low) ~ age + race, data=birthwt,
ntree = 1000, mtry = 2)
mod_svm <- svm(low ~ age + race, data = birthwt)
mod_glm$fitted.values
pred <- predict(mod_rf, birthwt, type = "prob" )[,2]
pred <- predict(mod_svm, birthwt)
cm <- confusionMatrix(if_else(pred >= 0.5, as.factor(names(table(birthwt[,"low"])))[1], as.factor(names(table(birthwt[,"low"])))[2]), birthwt %>% select_("low") %>% mutate_("low" = "as.factor(low)") %>% unlist)
fourfoldplot(confusionMatrix(xtab)$table)
cm <- confusionMatrix(if_else(pred >= 0.5, as.factor(names(table(birthwt[,"low"])))[1], as.factor(names(table(birthwt[,"low"])))[2]), as.factor(birthwt[,"low"]))
fourfoldplot(cm$table)
cm$overall
cm$byClass