Sunday, February 28, 2021
Confusion Matrix for Any Model in R
#Confusion matrix
# training
p<- predict(model, rail)
tab<- table(p, rail$OPS)
tab
1-sum(diag(tab))/sum(tab)
#testing
p1<- predict(model, test)
tab1<- table(p1, test$OPS)
tab1
1-sum(diag(tab1))/sum(tab1)
#end
library(e1071)
#data
confusionMatrix(rail$OPS, sample(rail$OPS))
newPrior <- c(.05, .8, .15, 0.5, 0.9)
names(newPrior) <- levels(rail$OPS)
cm <-confusionMatrix(rail$OPS, sample(rail$OPS))
#2
# extract the confusion matrix values as data.frame
cm_d <- as.data.frame(cm$table)
# confusion matrix statistics as data.frame
cm_st <-data.frame(cm$overall)
# round the values
cm_st$cm.overall <- round(cm_st$cm.overall,2)
# here we also have the rounded percentage values
cm_p <- as.data.frame(prop.table(cm$table))
cm_d$Perc <- round(cm_p$Freq*100,2)
#3
library(ggplot2) # to plot
library(gridExtra) # to put more
library(grid) # plot together
# plotting the matrix
cm_d_p <- ggplot(data = cm_d, aes(x = Prediction , y = Reference, fill = Freq))+
geom_tile() +
geom_text(aes(label = paste("",Freq,",",Perc,"%")), color = 'red', size = 8) +
theme_light() +
guides(fill=FALSE)
# plotting the stats
cm_st_p <- tableGrob(cm_st)
# all together
grid.arrange(cm_d_p, cm_st_p,nrow = 1, ncol = 2,
top=textGrob("Confusion Matrix and Statistics",gp=gpar(fontsize=25,font=1)))
#search confusion matrix plot
###########################################################################
Tuesday, February 16, 2021
Sankey Plot
#Tawkir_ahmed_code
#Sankey_plot
library(networkD3)
## create a dataframe with 10 nodes
nodes = data.frame("name" = c("Node_0", "Node_1", "Node_2", "Node_3", "Node_4", "Node_5",
"Node_6", "Node_7", "Node_8", "Node_9"))
## create edges with weights
links = as.data.frame(matrix(c(0, 5, 2, # node 0 -> node 5 with weight 2
0, 9, 2, # node 0 -> node 9 with weight 2
0, 6, 2, # node 0 -> node 5 with weight 2
1, 6, 1, # node 1 -> node 6 with weight 1
1, 7, 3, # node 1 -> node 7 with weight 3
1, 8, 2, # node 1 -> node 8 with weight 2
2, 9, 3, # node 2 -> node 9 with weight 3
3, 5, 1, # node 3 -> node 5 with weight 1
3, 8, 1, # node 3 -> node 8 with weight 1
3, 9, 5, # node 3 -> node 9 with weight 5
4, 9, 2, # node 4 -> node 9 with weight 2
4, 6, 2 # node 4 -> node 6 with weight 2
), byrow = TRUE, ncol = 3))
## set column names for links
names(links) = c("source", "target", "value")
## add edge types for coloring purpose
links$group = c("type_0",
"type_0",
"type_0",
"type_1",
"type_1",
"type_1",
"type_2",
"type_3",
"type_3",
"type_3",
"type_4",
"type_4")
## Create custom color list using d3 for each node
node_color <- 'd3.scaleOrdinal() .domain(["Node_0", "Node_1", "Node_2", "Node_3", "Node_4",
"Node_5", "Node_6", "Node_7", "Node_8", "Node_9", "type_0", "type_1", "type_2",
"type_3", "type_4"]) .range(["#bf5b17", "#beaed4", "#fdc086" , "#386cb0", "#7fc97f",
"#bf5b17", "#beaed4", "#fdc086" , "#386cb0", "#7fc97f", "#bf5b17", "#beaed4", "#fdc086" , "#386cb0", "#7fc97f"])'
## Draw Sankey Diagram
p = sankeyNetwork(Links = links, Nodes = nodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
fontSize = 16, nodeWidth = 40,
colourScale = node_color,
LinkGroup="group")
p