library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
setwd("/Users/subasishdas1/Desktop/Data_Works/dec3_2017")
topc <- read.csv("Top Countermeasures.csv")
names(topc)
## [1] "Ori_Code"  "Ori_Name"  "Count"     "Prop_Name"
head(topc[c(1,3)],15)
##                  Ori_Code Count
## 1             TECH_V2V_FL   244
## 2             TECH_I2V_FL   187
## 3                   NOEFL   152
## 4                SPEED_FL    74
## 5                  DUI_FL    55
## 6           SIGHT_DIST_FL    37
## 7         IDEAL_SIGNAL_FL    24
## 8              NEW_SIG_FL    24
## 9         RETROREFLECT_FL    23
## 10                RLVW_FL    21
## 11           SIG_PHASE_FL    21
## 12                 CSW_FL    17
## 13 WARN_INT_DRV__MERGE_FL    16
## 14        NO_LEFT_SIGN_FL    11
## 15            INT_REDO_FL    10
data1 <- read.csv("MCSS_Imp_Variable_Cat_NwCntms1.csv")

data1$EF007 <- cut(data1$EF007, breaks=c(-1,1, 2,Inf), 
    labels=c("One Lane","Two Lane","Multi Lane"))


data1$CF006 <- cut(data1$CF006, breaks=c(-1,0, 1,Inf), 
    labels=c("Only MC", "One OV","Two or more OV"))



############################################## Countermeasure 1: TECH_V2V_FL
TECH_V2V_FL <- subset(data1, TECH_V2V_FL==1)
TECH_V2V_FL1 <- TECH_V2V_FL[c(2:19)]
names(TECH_V2V_FL1)
##  [1] "CF003"  "EF007"  "CF006"  "FF004"  "CF011"  "CF013"  "EF005" 
##  [8] "CF010"  "EF002"  "EF003"  "EF004"  "EF006"  "EF014"  "EF015" 
## [15] "EF017"  "EF018"  "EF022"  "EF027a"
library(arules)
## Loading required package: Matrix
## 
## Attaching package: 'arules'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following objects are masked from 'package:base':
## 
##     abbreviate, write
trans <- as(TECH_V2V_FL1, "transactions")
summary(trans)
## transactions as itemMatrix in sparse format with
##  244 rows (elements/itemsets/transactions) and
##  134 columns (items) and a density of 0.1341754 
## 
## most frequent items:
##                             CF006=One OV 
##                                      207 
## CF003=collision with other motor vehicle 
##                                      192 
##                           EF015=straight 
##                                      179 
##         EF002=at-grade intersection area 
##                                      177 
##                EF018=no traffic control  
##                                      169 
##                                  (Other) 
##                                     3463 
## 
## element (itemset/transaction) length distribution:
## sizes
##  17  18 
##   5 239 
## 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   17.00   18.00   18.00   17.98   18.00   18.00 
## 
## includes extended item information - examples:
##                                     labels variables
## 1        CF003=collision with fixed object     CF003
## 2    CF003=collision with non-fixed object     CF003
## 3 CF003=collision with other motor vehicle     CF003
##                               levels
## 1        collision with fixed object
## 2    collision with non-fixed object
## 3 collision with other motor vehicle
## 
## includes extended transaction information - examples:
##   transactionID
## 1             6
## 2             8
## 3             9
m <- data.frame(itemFrequency(trans, type = "relative"))
library(data.table)
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
m1 <- setDT(m, keep.rownames = TRUE)[]
colnames(m1)[1] <- "Attribute"
colnames(m1)[2] <- "Perc"
head(m1, 2)
##                                Attribute       Perc
## 1:     CF003=collision with fixed object 0.19262295
## 2: CF003=collision with non-fixed object 0.01229508
library(tidyr)
## 
## Attaching package: 'tidyr'
## The following object is masked from 'package:Matrix':
## 
##     expand
m2 <-arrange(m1,desc(Perc))
m3 <- head(m2, 10)
m3$Perc1 <- round(m3$Perc*100,2)

ggplot(m3, aes(x = reorder(Attribute, -Perc1), y = Perc1), fill="#798E87") +
         geom_bar(stat = "identity")+theme_bw(base_size = 16)+coord_flip()+labs(x="Attribute", y="Percentage")+geom_text(aes(label=Perc1), vjust=1, color="white", size=3.5)

### http://ethen8181.github.io/machine-learning/association_rule/R/apriori.html
rules <- apriori( 
    trans,
    parameter = list( support = 0.25, confidence = 0.75, minlen = 2, maxlen = 3))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##        0.75    0.1    1 none FALSE            TRUE       5    0.25      2
##  maxlen target   ext
##       3  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 61 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[123 item(s), 244 transaction(s)] done [0.00s].
## sorting and recoding items ... [24 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3
## Warning in apriori(trans, parameter = list(support = 0.25, confidence =
## 0.75, : Mining stopped (maxlen reached). Only patterns up to a length of 3
## returned!
##  done [0.00s].
## writing ... [284 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
rules_dt <- data.table(lhs = labels(lhs(rules)), 
                        rhs = labels(rhs(rules)), 
                        quality(rules))[order(-lift),]
library(dplyr)
mm <- rules_dt %>% mutate_if(is.numeric, funs(round(., 3)))
DT::datatable(mm)
##################################################################

############################################ Countermeasure 2: TECH_I2V_FL
TECH_V2V_FL <- subset(data1, TECH_I2V_FL==1)
TECH_V2V_FL1 <- TECH_V2V_FL[c(2:19)]
names(TECH_V2V_FL1)
##  [1] "CF003"  "EF007"  "CF006"  "FF004"  "CF011"  "CF013"  "EF005" 
##  [8] "CF010"  "EF002"  "EF003"  "EF004"  "EF006"  "EF014"  "EF015" 
## [15] "EF017"  "EF018"  "EF022"  "EF027a"
trans <- as(TECH_V2V_FL1, "transactions")
m <- data.frame(itemFrequency(trans, type = "relative"))
library(data.table)
m1 <- setDT(m, keep.rownames = TRUE)[]
colnames(m1)[1] <- "Attribute"
colnames(m1)[2] <- "Perc"
head(m1, 2)
##                                Attribute       Perc
## 1:     CF003=collision with fixed object 0.30481283
## 2: CF003=collision with non-fixed object 0.01604278
library(tidyr)
m2 <-arrange(m1,desc(Perc))
m3 <- head(m2, 10)
m3$Perc1 <- round(m3$Perc*100,2)

ggplot(m3, aes(x = reorder(Attribute, -Perc1), y = Perc1), fill="#798E87") +
         geom_bar(stat = "identity")+theme_bw()+coord_flip()+labs(x="Attribute", y="Percentage")+geom_text(aes(label=Perc1), vjust=1, color="white", size=3.5)

rules <- apriori( 
    trans,
    parameter = list( support = 0.25, confidence = 0.75, minlen = 2, maxlen = 3))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##        0.75    0.1    1 none FALSE            TRUE       5    0.25      2
##  maxlen target   ext
##       3  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 46 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[121 item(s), 187 transaction(s)] done [0.00s].
## sorting and recoding items ... [26 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3
## Warning in apriori(trans, parameter = list(support = 0.25, confidence =
## 0.75, : Mining stopped (maxlen reached). Only patterns up to a length of 3
## returned!
##  done [0.00s].
## writing ... [179 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
rules_dt <- data.table(lhs = labels(lhs(rules)), 
                        rhs = labels(rhs(rules)), 
                        quality(rules))[order(-lift),]
mm <- rules_dt %>% mutate_if(is.numeric, funs(round(., 3)))
DT::datatable(mm)
#########################################################

################################################### Countermeasure 3: SPEED_FL
TECH_V2V_FL <- subset(data1, SPEED_FL==1| SPEED_FL==2)
TECH_V2V_FL1 <- TECH_V2V_FL[c(2:19)]
names(TECH_V2V_FL1)
##  [1] "CF003"  "EF007"  "CF006"  "FF004"  "CF011"  "CF013"  "EF005" 
##  [8] "CF010"  "EF002"  "EF003"  "EF004"  "EF006"  "EF014"  "EF015" 
## [15] "EF017"  "EF018"  "EF022"  "EF027a"
trans <- as(TECH_V2V_FL1, "transactions")
m <- data.frame(itemFrequency(trans, type = "relative"))
library(data.table)
m1 <- setDT(m, keep.rownames = TRUE)[]
colnames(m1)[1] <- "Attribute"
colnames(m1)[2] <- "Perc"
head(m1, 2)
##                                Attribute      Perc
## 1:     CF003=collision with fixed object 0.5362319
## 2: CF003=collision with non-fixed object 0.0000000
m2 <-arrange(m1,desc(Perc))
m3 <- head(m2, 10)
m3$Perc1 <- round(m3$Perc*100,2)

ggplot(m3, aes(x = reorder(Attribute, -Perc1), y = Perc1), fill="#798E87") +
         geom_bar(stat = "identity")+theme_bw()+coord_flip()+labs(x="Attribute", y="Percentage")+geom_text(aes(label=Perc1), vjust=1, color="white", size=3.5)

rules <- apriori( 
    trans,
    parameter = list( support = 0.25, confidence = 0.75, minlen = 2, maxlen = 3))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##        0.75    0.1    1 none FALSE            TRUE       5    0.25      2
##  maxlen target   ext
##       3  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 17 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[96 item(s), 69 transaction(s)] done [0.00s].
## sorting and recoding items ... [26 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3
## Warning in apriori(trans, parameter = list(support = 0.25, confidence =
## 0.75, : Mining stopped (maxlen reached). Only patterns up to a length of 3
## returned!
##  done [0.00s].
## writing ... [158 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
rules_dt <- data.table(lhs = labels(lhs(rules)), 
                        rhs = labels(rhs(rules)), 
                        quality(rules))[order(-lift),]
mm <- rules_dt %>% mutate_if(is.numeric, funs(round(., 3)))
DT::datatable(mm)
######################################################################



############################################## Countermeasure 4: DUI_FL
TECH_V2V_FL <- subset(data1, DUI_FL==1| DUI_FL==2| DUI_FL==3 )
TECH_V2V_FL1 <- TECH_V2V_FL[c(2:19)]
names(TECH_V2V_FL1)
##  [1] "CF003"  "EF007"  "CF006"  "FF004"  "CF011"  "CF013"  "EF005" 
##  [8] "CF010"  "EF002"  "EF003"  "EF004"  "EF006"  "EF014"  "EF015" 
## [15] "EF017"  "EF018"  "EF022"  "EF027a"
trans <- as(TECH_V2V_FL1, "transactions")
m <- data.frame(itemFrequency(trans, type = "relative"))
library(data.table)
m1 <- setDT(m, keep.rownames = TRUE)[]
colnames(m1)[1] <- "Attribute"
colnames(m1)[2] <- "Perc"
head(m1, 2)
##                                Attribute      Perc
## 1:     CF003=collision with fixed object 0.5652174
## 2: CF003=collision with non-fixed object 0.0000000
library(dplyr)
library(tidyr)
m2 <-arrange(m1,desc(Perc))
m3 <- head(m2, 10)
m3$Perc1 <- round(m3$Perc*100,2)

ggplot(m3, aes(x = reorder(Attribute, -Perc1), y = Perc1), fill="#798E87") +
         geom_bar(stat = "identity")+theme_bw()+coord_flip()+labs(x="Attribute", y="Percentage")+geom_text(aes(label=Perc1), vjust=1, color="white", size=3.5)

rules <- apriori( 
    trans,
    parameter = list( support = 0.25, confidence = 0.75, minlen = 2, maxlen = 3))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##        0.75    0.1    1 none FALSE            TRUE       5    0.25      2
##  maxlen target   ext
##       3  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 11 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[92 item(s), 46 transaction(s)] done [0.00s].
## sorting and recoding items ... [27 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3
## Warning in apriori(trans, parameter = list(support = 0.25, confidence =
## 0.75, : Mining stopped (maxlen reached). Only patterns up to a length of 3
## returned!
##  done [0.00s].
## writing ... [123 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
rules_dt <- data.table(lhs = labels(lhs(rules)), 
                        rhs = labels(rhs(rules)), 
                        quality(rules))[order(-lift),]
mm <- rules_dt %>% mutate_if(is.numeric, funs(round(., 3)))
DT::datatable(mm)
##################################################################

############################################ Countermeasure 5: SIGHT_DIST_FL
TECH_V2V_FL <- subset(data1, SIGHT_DIST_FL==1)
TECH_V2V_FL1 <- TECH_V2V_FL[c(2:19)]
names(TECH_V2V_FL1)
##  [1] "CF003"  "EF007"  "CF006"  "FF004"  "CF011"  "CF013"  "EF005" 
##  [8] "CF010"  "EF002"  "EF003"  "EF004"  "EF006"  "EF014"  "EF015" 
## [15] "EF017"  "EF018"  "EF022"  "EF027a"
trans <- as(TECH_V2V_FL1, "transactions")
m <- data.frame(itemFrequency(trans, type = "relative"))
library(data.table)
m1 <- setDT(m, keep.rownames = TRUE)[]
colnames(m1)[1] <- "Attribute"
colnames(m1)[2] <- "Perc"
head(m1, 2)
##                                Attribute       Perc
## 1:     CF003=collision with fixed object 0.21621622
## 2: CF003=collision with non-fixed object 0.02702703
library(dplyr)
library(tidyr)
m2 <-arrange(m1,desc(Perc))
m3 <- head(m2, 10)
m3$Perc1 <- round(m3$Perc*100,2)

ggplot(m3, aes(x = reorder(Attribute, -Perc1), y = Perc1), fill="#798E87") +
         geom_bar(stat = "identity")+theme_bw()+coord_flip()+labs(x="Attribute", y="Percentage")+geom_text(aes(label=Perc1), vjust=1, color="white", size=3.5)

rules <- apriori( 
    trans,
    parameter = list( support = 0.25, confidence = 0.75, minlen = 2, maxlen = 3))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##        0.75    0.1    1 none FALSE            TRUE       5    0.25      2
##  maxlen target   ext
##       3  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 9 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[88 item(s), 37 transaction(s)] done [0.00s].
## sorting and recoding items ... [27 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3
## Warning in apriori(trans, parameter = list(support = 0.25, confidence =
## 0.75, : Mining stopped (maxlen reached). Only patterns up to a length of 3
## returned!
##  done [0.00s].
## writing ... [364 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
rules_dt <- data.table(lhs = labels(lhs(rules)), 
                        rhs = labels(rhs(rules)), 
                        quality(rules))[order(-lift),]
mm <- rules_dt %>% mutate_if(is.numeric, funs(round(., 3)))
DT::datatable(mm)
#########################################################

################################################### Countermeasure 6: IDEAL_SIGNAL_FL
TECH_V2V_FL <- subset(data1, IDEAL_SIGNAL_FL==1)
TECH_V2V_FL1 <- TECH_V2V_FL[c(2:19)]
names(TECH_V2V_FL1)
##  [1] "CF003"  "EF007"  "CF006"  "FF004"  "CF011"  "CF013"  "EF005" 
##  [8] "CF010"  "EF002"  "EF003"  "EF004"  "EF006"  "EF014"  "EF015" 
## [15] "EF017"  "EF018"  "EF022"  "EF027a"
trans <- as(TECH_V2V_FL1, "transactions")
m <- data.frame(itemFrequency(trans, type = "relative"))
library(data.table)
m1 <- setDT(m, keep.rownames = TRUE)[]
colnames(m1)[1] <- "Attribute"
colnames(m1)[2] <- "Perc"
head(m1, 2)
##                                Attribute       Perc
## 1:     CF003=collision with fixed object 0.08333333
## 2: CF003=collision with non-fixed object 0.00000000
m2 <-arrange(m1,desc(Perc))
m3 <- head(m2, 10)
m3$Perc1 <- round(m3$Perc*100,2)

ggplot(m3, aes(x = reorder(Attribute, -Perc1), y = Perc1), fill="#798E87") +
         geom_bar(stat = "identity")+theme_bw()+coord_flip()+labs(x="Attribute", y="Percentage")+geom_text(aes(label=Perc1), vjust=1, color="white", size=3.5)

rules <- apriori( 
    trans,
    parameter = list( support = 0.25, confidence = 0.75, minlen = 2, maxlen = 3))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##        0.75    0.1    1 none FALSE            TRUE       5    0.25      2
##  maxlen target   ext
##       3  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 6 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[64 item(s), 24 transaction(s)] done [0.00s].
## sorting and recoding items ... [28 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3
## Warning in apriori(trans, parameter = list(support = 0.25, confidence =
## 0.75, : Mining stopped (maxlen reached). Only patterns up to a length of 3
## returned!
##  done [0.00s].
## writing ... [1437 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
rules_dt <- data.table(lhs = labels(lhs(rules)), 
                        rhs = labels(rhs(rules)), 
                        quality(rules))[order(-lift),]
mm <- rules_dt %>% mutate_if(is.numeric, funs(round(., 3)))
DT::datatable(mm)
######################################################################


############################################## Countermeasure 7: NEW_SIG_FL
TECH_V2V_FL <- subset(data1, NEW_SIG_FL==1)
TECH_V2V_FL1 <- TECH_V2V_FL[c(2:19)]
names(TECH_V2V_FL1)
##  [1] "CF003"  "EF007"  "CF006"  "FF004"  "CF011"  "CF013"  "EF005" 
##  [8] "CF010"  "EF002"  "EF003"  "EF004"  "EF006"  "EF014"  "EF015" 
## [15] "EF017"  "EF018"  "EF022"  "EF027a"
trans <- as(TECH_V2V_FL1, "transactions")
m <- data.frame(itemFrequency(trans, type = "relative"))
library(data.table)
m1 <- setDT(m, keep.rownames = TRUE)[]
colnames(m1)[1] <- "Attribute"
colnames(m1)[2] <- "Perc"
head(m1, 2)
##                                Attribute      Perc
## 1:     CF003=collision with fixed object 0.2083333
## 2: CF003=collision with non-fixed object 0.0000000
library(dplyr)
library(tidyr)
m2 <-arrange(m1,desc(Perc))
m3 <- head(m2, 10)
m3$Perc1 <- round(m3$Perc*100,2)

ggplot(m3, aes(x = reorder(Attribute, -Perc1), y = Perc1), fill="#798E87") +
         geom_bar(stat = "identity")+theme_bw()+coord_flip()+labs(x="Attribute", y="Percentage")+geom_text(aes(label=Perc1), vjust=1, color="white", size=3.5)

rules <- apriori( 
    trans,
    parameter = list( support = 0.25, confidence = 0.75, minlen = 2, maxlen = 3))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##        0.75    0.1    1 none FALSE            TRUE       5    0.25      2
##  maxlen target   ext
##       3  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 6 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[57 item(s), 24 transaction(s)] done [0.00s].
## sorting and recoding items ... [28 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3
## Warning in apriori(trans, parameter = list(support = 0.25, confidence =
## 0.75, : Mining stopped (maxlen reached). Only patterns up to a length of 3
## returned!
##  done [0.00s].
## writing ... [1446 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
rules_dt <- data.table(lhs = labels(lhs(rules)), 
                        rhs = labels(rhs(rules)), 
                        quality(rules))[order(-lift),]
mm <- rules_dt %>% mutate_if(is.numeric, funs(round(., 3)))
DT::datatable(mm)
##################################################################

############################################ Countermeasure 8: RETROREFLECT_FL
TECH_V2V_FL <- subset(data1, RETROREFLECT_FL==1)
TECH_V2V_FL1 <- TECH_V2V_FL[c(2:19)]
names(TECH_V2V_FL1)
##  [1] "CF003"  "EF007"  "CF006"  "FF004"  "CF011"  "CF013"  "EF005" 
##  [8] "CF010"  "EF002"  "EF003"  "EF004"  "EF006"  "EF014"  "EF015" 
## [15] "EF017"  "EF018"  "EF022"  "EF027a"
trans <- as(TECH_V2V_FL1, "transactions")
m <- data.frame(itemFrequency(trans, type = "relative"))
library(data.table)
m1 <- setDT(m, keep.rownames = TRUE)[]
colnames(m1)[1] <- "Attribute"
colnames(m1)[2] <- "Perc"
head(m1, 2)
##                                Attribute      Perc
## 1:     CF003=collision with fixed object 0.6521739
## 2: CF003=collision with non-fixed object 0.0000000
library(dplyr)
library(tidyr)
m2 <-arrange(m1,desc(Perc))
m3 <- head(m2, 10)
m3$Perc1 <- round(m3$Perc*100,2)

ggplot(m3, aes(x = reorder(Attribute, -Perc1), y = Perc1), fill="#798E87") +
         geom_bar(stat = "identity")+theme_bw()+coord_flip()+labs(x="Attribute", y="Percentage")+geom_text(aes(label=Perc1), vjust=1, color="white", size=3.5)

rules <- apriori( 
    trans,
    parameter = list( support = 0.25, confidence = 0.75, minlen = 2, maxlen = 3))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##        0.75    0.1    1 none FALSE            TRUE       5    0.25      2
##  maxlen target   ext
##       3  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 5 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[76 item(s), 23 transaction(s)] done [0.00s].
## sorting and recoding items ... [31 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3
## Warning in apriori(trans, parameter = list(support = 0.25, confidence =
## 0.75, : Mining stopped (maxlen reached). Only patterns up to a length of 3
## returned!
##  done [0.00s].
## writing ... [501 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
rules_dt <- data.table(lhs = labels(lhs(rules)), 
                        rhs = labels(rhs(rules)), 
                        quality(rules))[order(-lift),]
mm <- rules_dt %>% mutate_if(is.numeric, funs(round(., 3)))
DT::datatable(mm)
#########################################################

################################################### Countermeasure 9: RETROREFLECT_FL
TECH_V2V_FL <- subset(data1, RETROREFLECT_FL==1)
TECH_V2V_FL1 <- TECH_V2V_FL[c(2:19)]
names(TECH_V2V_FL1)
##  [1] "CF003"  "EF007"  "CF006"  "FF004"  "CF011"  "CF013"  "EF005" 
##  [8] "CF010"  "EF002"  "EF003"  "EF004"  "EF006"  "EF014"  "EF015" 
## [15] "EF017"  "EF018"  "EF022"  "EF027a"
trans <- as(TECH_V2V_FL1, "transactions")
m <- data.frame(itemFrequency(trans, type = "relative"))
library(data.table)
m1 <- setDT(m, keep.rownames = TRUE)[]
colnames(m1)[1] <- "Attribute"
colnames(m1)[2] <- "Perc"
head(m1, 2)
##                                Attribute      Perc
## 1:     CF003=collision with fixed object 0.6521739
## 2: CF003=collision with non-fixed object 0.0000000
m2 <-arrange(m1,desc(Perc))
m3 <- head(m2, 10)
m3$Perc1 <- round(m3$Perc*100,2)

ggplot(m3, aes(x = reorder(Attribute, -Perc1), y = Perc1), fill="#798E87") +
         geom_bar(stat = "identity")+theme_bw()+coord_flip()+labs(x="Attribute", y="Percentage")+geom_text(aes(label=Perc1), vjust=1, color="white", size=3.5)

rules <- apriori( 
    trans,
    parameter = list( support = 0.25, confidence = 0.75, minlen = 2, maxlen = 3))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##        0.75    0.1    1 none FALSE            TRUE       5    0.25      2
##  maxlen target   ext
##       3  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 5 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[76 item(s), 23 transaction(s)] done [0.00s].
## sorting and recoding items ... [31 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3
## Warning in apriori(trans, parameter = list(support = 0.25, confidence =
## 0.75, : Mining stopped (maxlen reached). Only patterns up to a length of 3
## returned!
##  done [0.00s].
## writing ... [501 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
rules_dt <- data.table(lhs = labels(lhs(rules)), 
                        rhs = labels(rhs(rules)), 
                        quality(rules))[order(-lift),]
mm <- rules_dt %>% mutate_if(is.numeric, funs(round(., 3)))
DT::datatable(mm)
######################################################################



############################################## Countermeasure 10: RLVW_FL
TECH_V2V_FL <- subset(data1, RLVW_FL==1)
TECH_V2V_FL1 <- TECH_V2V_FL[c(2:19)]
names(TECH_V2V_FL1)
##  [1] "CF003"  "EF007"  "CF006"  "FF004"  "CF011"  "CF013"  "EF005" 
##  [8] "CF010"  "EF002"  "EF003"  "EF004"  "EF006"  "EF014"  "EF015" 
## [15] "EF017"  "EF018"  "EF022"  "EF027a"
trans <- as(TECH_V2V_FL1, "transactions")
m <- data.frame(itemFrequency(trans, type = "relative"))
library(data.table)
m1 <- setDT(m, keep.rownames = TRUE)[]
colnames(m1)[1] <- "Attribute"
colnames(m1)[2] <- "Perc"
head(m1, 2)
##                                Attribute      Perc
## 1:     CF003=collision with fixed object 0.0952381
## 2: CF003=collision with non-fixed object 0.0000000
library(dplyr)
library(tidyr)
m2 <-arrange(m1,desc(Perc))
m3 <- head(m2, 10)
m3$Perc1 <- round(m3$Perc*100,2)

ggplot(m3, aes(x = reorder(Attribute, -Perc1), y = Perc1), fill="#798E87") +
         geom_bar(stat = "identity")+theme_bw()+coord_flip()+labs(x="Attribute", y="Percentage")+geom_text(aes(label=Perc1), vjust=1, color="white", size=3.5)

rules <- apriori( 
    trans,
    parameter = list( support = 0.25, confidence = 0.75, minlen = 2, maxlen = 3))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##        0.75    0.1    1 none FALSE            TRUE       5    0.25      2
##  maxlen target   ext
##       3  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 5 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[73 item(s), 21 transaction(s)] done [0.00s].
## sorting and recoding items ... [22 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3
## Warning in apriori(trans, parameter = list(support = 0.25, confidence =
## 0.75, : Mining stopped (maxlen reached). Only patterns up to a length of 3
## returned!
##  done [0.00s].
## writing ... [779 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
rules_dt <- data.table(lhs = labels(lhs(rules)), 
                        rhs = labels(rhs(rules)), 
                        quality(rules))[order(-lift),]
mm <- rules_dt %>% mutate_if(is.numeric, funs(round(., 3)))
DT::datatable(mm)
##################################################################

############################################ Countermeasure 11: SIG_PHASE_FL
TECH_V2V_FL <- subset(data1,SIG_PHASE_FL==1)
TECH_V2V_FL1 <- TECH_V2V_FL[c(2:19)]
names(TECH_V2V_FL1)
##  [1] "CF003"  "EF007"  "CF006"  "FF004"  "CF011"  "CF013"  "EF005" 
##  [8] "CF010"  "EF002"  "EF003"  "EF004"  "EF006"  "EF014"  "EF015" 
## [15] "EF017"  "EF018"  "EF022"  "EF027a"
trans <- as(TECH_V2V_FL1, "transactions")
m <- data.frame(itemFrequency(trans, type = "relative"))
library(data.table)
m1 <- setDT(m, keep.rownames = TRUE)[]
colnames(m1)[1] <- "Attribute"
colnames(m1)[2] <- "Perc"
head(m1, 2)
##                                Attribute      Perc
## 1:     CF003=collision with fixed object 0.0952381
## 2: CF003=collision with non-fixed object 0.0000000
library(dplyr)
library(tidyr)
m2 <-arrange(m1,desc(Perc))
m3 <- head(m2, 10)
m3$Perc1 <- round(m3$Perc*100,2)

ggplot(m3, aes(x = reorder(Attribute, -Perc1), y = Perc1), fill="#798E87") +
         geom_bar(stat = "identity")+theme_bw()+coord_flip()+labs(x="Attribute", y="Percentage")+geom_text(aes(label=Perc1), vjust=1, color="white", size=3.5)

rules <- apriori( 
    trans,
    parameter = list( support = 0.25, confidence = 0.75, minlen = 2, maxlen = 3))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##        0.75    0.1    1 none FALSE            TRUE       5    0.25      2
##  maxlen target   ext
##       3  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 5 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[67 item(s), 21 transaction(s)] done [0.00s].
## sorting and recoding items ... [21 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3
## Warning in apriori(trans, parameter = list(support = 0.25, confidence =
## 0.75, : Mining stopped (maxlen reached). Only patterns up to a length of 3
## returned!
##  done [0.00s].
## writing ... [707 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
rules_dt <- data.table(lhs = labels(lhs(rules)), 
                        rhs = labels(rhs(rules)), 
                        quality(rules))[order(-lift),]
mm <- rules_dt %>% mutate_if(is.numeric, funs(round(., 3)))
DT::datatable(mm)
#########################################################

################################################### Countermeasure 12: CSW_FL
TECH_V2V_FL <- subset(data1, CSW_FL==1)
TECH_V2V_FL1 <- TECH_V2V_FL[c(2:19)]
names(TECH_V2V_FL1)
##  [1] "CF003"  "EF007"  "CF006"  "FF004"  "CF011"  "CF013"  "EF005" 
##  [8] "CF010"  "EF002"  "EF003"  "EF004"  "EF006"  "EF014"  "EF015" 
## [15] "EF017"  "EF018"  "EF022"  "EF027a"
trans <- as(TECH_V2V_FL1, "transactions")
m <- data.frame(itemFrequency(trans, type = "relative"))
library(data.table)
m1 <- setDT(m, keep.rownames = TRUE)[]
colnames(m1)[1] <- "Attribute"
colnames(m1)[2] <- "Perc"
head(m1, 2)
##                                Attribute       Perc
## 1:     CF003=collision with fixed object 0.58823529
## 2: CF003=collision with non-fixed object 0.05882353
m2 <-arrange(m1,desc(Perc))
m3 <- head(m2, 10)
m3$Perc1 <- round(m3$Perc*100,2)

ggplot(m3, aes(x = reorder(Attribute, -Perc1), y = Perc1), fill="#798E87") +
         geom_bar(stat = "identity")+theme_bw()+coord_flip()+labs(x="Attribute", y="Percentage")+geom_text(aes(label=Perc1), vjust=1, color="white", size=3.5)

rules <- apriori( 
    trans,
    parameter = list( support = 0.25, confidence = 0.75, minlen = 2, maxlen = 3))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##        0.75    0.1    1 none FALSE            TRUE       5    0.25      2
##  maxlen target   ext
##       3  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 4 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[73 item(s), 17 transaction(s)] done [0.00s].
## sorting and recoding items ... [21 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3
## Warning in apriori(trans, parameter = list(support = 0.25, confidence =
## 0.75, : Mining stopped (maxlen reached). Only patterns up to a length of 3
## returned!
##  done [0.00s].
## writing ... [550 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
rules_dt <- data.table(lhs = labels(lhs(rules)), 
                        rhs = labels(rhs(rules)), 
                        quality(rules))[order(-lift),]
mm <- rules_dt %>% mutate_if(is.numeric, funs(round(., 3)))
DT::datatable(mm)
######################################################################