# devtools : : install _ github ( " yanyachen / IsolationForest ") install . packages ( " IsolationForest ", repos =" http : / / R - Forge . R - project . org ") library ( IsolationForest ) data ( stackloss ) # train a model of Isolation Foresttr <- IsolationTrees ( stackloss , rFactor = 0 ) # evaluate anomaly scoreas <- AnomalyScore ( stackloss , tr ) # show anomaly scoreas $ outF plot ( stackloss $ Air . Flow , stackloss $ Water . Temp , col = rgb ( as $ outF , 0 , 1 - as $ outF ) , pch = 19 , cex = 1 . 5 ) plot ( stackloss $ Acid . Conc . , stackloss $ stack . loss , col = rgb ( as $ outF , 0 , 1 - as $ outF ) , pch = 19 , cex = 1 . 5 ) # train a model of Isolation Foresttr <- IsolationTrees ( mtcars , ntree = 10000 , rFactor = 1 ) # evaluate anomaly scoreas <- AnomalyScore ( mtcars , tr ) # show anomaly scoreas $ outF sort ( as $ outF , decreasing = T ) [ 1 : 2 ] # UDF for standardize anomaly scorestnd <- function ( x ) { x 1 <- ( x - min ( x ) ) / ( max ( x ) - min ( x ) ) return ( x 1 ) } stndoutF <- stnd ( as $ outF ) plot ( mtcars $ carb , mtcars $ hp , col = rgb ( stndoutF , 0 , 1 - stndoutF ) , pch = 19 , cex = 1 . 5 ) plot ( mtcars $ qsec , mtcars $ disp , col = rgb ( stndoutF , 0 , stndoutF ) , pch = 19 , cex = 1 . 5 ) # check what matters most library ( party ) mtcarsv <- mtcarsmtcarsv $ outF <- as $ outF ctv 1 <- ctree ( outF ~., data = mtcarsv ) plot ( ctv 1 ) install . packages ( " randomForest ") library ( randomForest ) rfv 1 <- randomForest ( outF ~., data = mtcarsv , ntree = 10000 ) varImpPlot ( rfv 1 ) #------- mycsv example ------- mycsv <- read . csv ( " https : / / t 1 . daumcdn . net / cfile / blog / 991 E 7 A 3359 BDDF 6837 ? download ", header = TRUE ) # train a model of Isolation Foresttr <- IsolationTrees ( mycsv , ntree = 20000 , rFactor = 1 , nmin = 3 ) # evaluate anomaly scoreas <- AnomalyScore ( mycsv , tr ) # show anomaly scoreplot ( density ( as $ outF ) ) sort ( as $ outF , decreasing = T ) [ 1 : 2 ] # check what matters most library ( party ) mycsvv <- mycsv mycsvv $ outF <- as $ outF ctv 1 <- ctree ( outF ~., data = mycsvv ) plot ( ctv 1 ) # install . packages ( " randomForest ") library ( randomForest ) rfv 1 <- randomForest ( outF ~., data = mycsvv , nodesize = 3 , ntree = 10000 ) varImpPlot ( rfv 1 ) plot ( mycsv $ VisITedResources , mycsv $ AnnouncementsView , col = rgb ( stndoutF , 0 , 1 - stndoutF ) , pch = 19 , cex = 1 . 5 ) plot ( mycsv $ raisedhands , mycsv $ Discussion , col = rgb ( stndoutF , 0 , stndoutF ) , pch = 19 , cex = 1 . 5 ) #---- kbo -------------- hit 0 <- read . csv ( " https : / / t 1 . daumcdn . net / cfile / blog / 99 B 515435 B 87998 B 07 ? download ") hit 0 <- hit 0 [ , ! ( names ( hit 0 ) % in % c ( " 선수 명 "," 순위 "," 팀 명 "))]# train a model of Isolation Foresttr <- IsolationTrees ( hit 0 , ntree = 20000 , rFactor = 0 . 5 , nmin = 3 ) # evaluate anomaly scoreas <- AnomalyScore ( hit 0 , tr ) # show anomaly scoreplot ( density ( as $ outF ) ) sort ( as $ outF , decreasing = T ) [ 1 : 2 ] stndoutF <- stnd ( as $ outF ) # check what matters most library ( party ) hit 0 v <- hit 0 hit 0 v $ outF <- as $ outF hit 0 v 1 <- hit 0 v [ , ! ( names ( hit 0 v ) % in % " 선수 명 ")] ctv 1 <- ctree ( outF ~., data = hit 0 v 1 ) plot ( ctv 1 ) # install . packages ( " randomForest ") library ( randomForest ) rfv 1 <- randomForest ( outF ~., data = hit 0 v 1 , nodesize = 3 , ntree = 30000 ) varImpPlot ( rfv 1 ) hit 0 <- read . csv ( " https : / / t 1 . daumcdn . net / cfile / blog / 99 B 515435 B 87998 B 07 ? download ") hit 0 $ outF <- as $ outF # head ( hit 0 [ order ( hit 0 $ outF , decreasing = T ) , ] ) par ( mfrow = c ( 1 , 2 ) ) plot ( hit 0 $ X 2 B , hit 0 $ GO , col = rgb ( stndoutF , 0 , 1 - stndoutF ) , pch = 19 , cex = 1 . 5 , main =" KBO Hitting ( 2018 - 08 - 16 ) - Anomaly Detection ", sub =" red : anomaly ") text ( hit 0 $ X 2 B , hit 0 $ GO , labels = ifelse ( hit 0 $ outF > 0 . 6 , as . character ( hit 0 $' 선수 명 '),"")) grid ( ) plot ( hit 0 $ H , hit 0 $ R , col = rgb ( stndoutF , 0 , 1 - stndoutF ) , pch = 19 , cex = 1 . 5 , main =" KBO Hitting ( 2018 - 08 - 16 ) - Anomaly Detection ", sub =" red : anomaly ") text ( hit 0 $ H , hit 0 $ R , labels = ifelse ( hit 0 $ outF > 0 . 6 , as . character ( hit 0 $' 선수 명 '),"")) grid ( ) par ( mfrow = c ( 1 , 1 ) ) plot ( hit 0 $ TB , hit 0 $ RBI , col = rgb ( stndoutF , 0 , 1 - stndoutF ) , pch = 19 , cex = 1 . 5 ) text ( hit 0 $ TB , hit 0 $ RBI , labels = ifelse ( hit 0 $ TB > 250 , as . character ( hit 0 $' 선수 명 '),"")) plot ( hit 0 $ TB , hit 0 $ GO , col = rgb ( stndoutF , 0 , 1 - stndoutF ) , pch = 19 , cex = 1 . 5 ) text ( hit 0 $ TB , hit 0 $ GO , labels = ifelse ( hit 0 $ TB > 250 , as . character ( hit 0 $' 선수 명 '),"")) plot ( hit 0 $ TB , hit 0 $ RBI , col = rgb ( stndoutF , 0 , 1 - stndoutF ) , pch = 19 , cex = 1 . 5 ) text ( hit 0 $ TB , hit 0 $ RBI , labels = ifelse ( hit 0 $ TB > 250 , as . character ( hit 0 $' 선수 명 '),"")) lines ( lowess ( hit 0 $ RBI ~ hit 0 $ TB ) , lty = 2 ) plot ( hit 0 $ TB / hit 0 $ AB , ( hit 0 $ RBI + hit 0 $ R ) / hit 0 $ AB , col = rgb ( stndoutF , 0 , 1 - stndoutF ) , pch = 19 , cex = stnd ( hit 0 $ AB ) * 3 ) text ( hit 0 $ TB / hit 0 $ AB , ( hit 0 $ RBI + hit 0 $ R ) / hit 0 $ AB , labels = hit 0 $' 선수 명 ')# abline ( h = median ( hit 0 $ RBI + hit 0 $ R ) / hit 0 $ AB , lty = 2 ) #----- bankmarketing -----------