From 8a17e0063cd96fb9b10974c62db92c28890563b6 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Mon, 1 Jun 2020 22:01:46 -0400 Subject: [PATCH] Full R code for question 3 --- a3.r | 97 +++++++++++++++++++++++++++++++++++++++-- documents/references.md | 7 +-- 2 files changed, 98 insertions(+), 6 deletions(-) diff --git a/a3.r b/a3.r index 6b03677..c778e59 100644 --- a/a3.r +++ b/a3.r @@ -1,5 +1,5 @@ ### - # R code for assignment. + # R code for assignment 3. ## @@ -7,7 +7,8 @@ cat('\014') -# install.packages('arules', dependencies=TRUE) +# Install and import arules package. +install.packages('arules', dependencies=TRUE) library(arules) @@ -30,6 +31,11 @@ catn <- function(message, trailing='\n') { # Tests the build-in R "apriori" function using the in class example from the powerpoints. ## in_class_example <- function() { + catn('\n========================================') + catn('\n\n') + catn('Generating rules for in-class example.') + catn('') + # Create our initial dataset. # Note that we use True/False so R knows that each value is either present or not. items <- c( "A", "B", "C", "D", "E") @@ -50,7 +56,92 @@ in_class_example <- function() { catn('') print(rules) inspect(rules) + catn('') +} + + +### + # R code for question 3, part b. + # Generates rules for dataset with min_support of 30%. + ## +question_3_b <- function() { + catn('\n========================================') + catn('\n\n') + catn('Generating rules for Qestion 3b.') + catn('') + + # Create our initial dataset. + # Note that we use True/False so R knows that each value is either present or not. + items <- c( "a", "b", "c", "d", "e") + values <- c( TRUE, TRUE, FALSE, TRUE, TRUE, + FALSE, TRUE, TRUE, TRUE, FALSE, + TRUE, TRUE, FALSE, TRUE, TRUE, + TRUE, FALSE, TRUE, TRUE, TRUE, + FALSE, TRUE, TRUE, TRUE, TRUE, + FALSE, TRUE, FALSE, TRUE, TRUE, + FALSE, FALSE, TRUE, TRUE, FALSE, + TRUE, TRUE, TRUE, FALSE, FALSE, + TRUE, FALSE, FALSE, TRUE, TRUE, + FALSE, TRUE, FALSE, TRUE, FALSE) + + # Create our dataset in the format the apriori function wants. + dataset <- data.frame(matrix(values, nrow=10, byrow=TRUE)) + colnames(dataset) <- items + + # Generate rules from apriori function. + rules <- apriori(dataset, parameter=list(supp=0.3, conf=0)) + rules <- sort(rules, decreasing=TRUE, by="support") + + # Display all rules. + catn('') + catn('') + print(rules) + inspect(rules) + catn('') +} + + +### + # R code for question 3, part c. + # Generates rules for dataset with min_support of 30% and min_conf of 50%. + ## +question_3_c <- function() { + catn('\n========================================') + catn('\n\n') + catn('Generating rules for Qestion 3c.') + catn('') + + # Create our initial dataset. + # Note that we use True/False so R knows that each value is either present or not. + items <- c( "a", "b", "c", "d", "e") + values <- c( TRUE, TRUE, FALSE, TRUE, TRUE, + FALSE, TRUE, TRUE, TRUE, FALSE, + TRUE, TRUE, FALSE, TRUE, TRUE, + TRUE, FALSE, TRUE, TRUE, TRUE, + FALSE, TRUE, TRUE, TRUE, TRUE, + FALSE, TRUE, FALSE, TRUE, TRUE, + FALSE, FALSE, TRUE, TRUE, FALSE, + TRUE, TRUE, TRUE, FALSE, FALSE, + TRUE, FALSE, FALSE, TRUE, TRUE, + FALSE, TRUE, FALSE, TRUE, FALSE) + + # Create our dataset in the format the apriori function wants. + dataset <- data.frame(matrix(values, nrow=10, byrow=TRUE)) + colnames(dataset) <- items + + # Generate rules from apriori function. + rules <- apriori(dataset, parameter=list(supp=0.3, conf=0.5)) + rules <- sort(rules, decreasing=TRUE, by="support") + + # Display all rules. + catn('') + catn('') + print(rules) + inspect(rules) + catn('') } -in_class_example() +# in_class_example() +question_3_b() +question_3_c() diff --git a/documents/references.md b/documents/references.md index d49cd6b..458cbb1 100644 --- a/documents/references.md +++ b/documents/references.md @@ -9,9 +9,10 @@ All references to external logic. Includes anything from stack overflow links to ### R Packages and Apriori Turns out that R has "packages", and they already have an implementation of Apriori. Hours wasted trying to implement it myself, rip.<br> -* <https://datascienceplus.com/implementing-apriori-algorithm-in-r/> -* <https://www.rdocumentation.org/packages/arules/versions/1.6-5/topics/apriori> -* <https://www.rdocumentation.org/packages/arules/versions/1.6-5/topics/rules-class> +* General Apriori and Package Importing - <https://datascienceplus.com/implementing-apriori-algorithm-in-r/> +* Apriori Function - <https://www.rdocumentation.org/packages/arules/versions/1.6-5/topics/apriori> +* Parsing Returned Rules - <https://www.rdocumentation.org/packages/arules/versions/1.6-5/topics/rules-class> +* Sorting Rules - <https://rdrr.io/cran/arules/man/sort.html> ### Run Entire Script in R Studio <https://stackoverflow.com/a/12766667> -- GitLab