Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@DSMom
Created August 21, 2018 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DSMom/b890be4fa18a4315074b0aa8f7b57b26 to your computer and use it in GitHub Desktop.
Save DSMom/b890be4fa18a4315074b0aa8f7b57b26 to your computer and use it in GitHub Desktop.
#### MBA analysis ####
library(arules)
library(tidyr)
library(dplyr)
subdata <- data[data$CustomerID %in% RFM$CustomerID,]
trandata <- subdata[,c('InvoiceNo', 'Description')]
trandata$Description <- trimws(trandata$Description, which = "both")
trandata <- trandata[trandata$Description!='Manual',]
trandata <- trandata[!duplicated(trandata),]
# tmp <- trandata %>% group_by(Description) %>% summarise(n=n())
trandata$Description <- factor(trandata$Description)
trandata <- split(trandata$Description, trandata$InvoiceNo)
trandata <- as(trandata, 'transactions')
rules <- apriori(trandata, parameter = list(supp = 0.01, conf = 0.8, maxlen=2), control = list(verbose=FALSE))
rules <- rules[!is.redundant(rules)]
rules <- sort(rules, by="confidence", decreasing=TRUE)
TblRule <- as(rules, "data.frame")
TblRule$rules <- gsub("[{|}]","", TblRule$rules)
TblRule <- separate(data = TblRule, col = rules, into = c("Purchased", "AlsoPurchased"), sep = "=>")
TblRule$Purchased <- trimws(TblRule$Purchased, which = 'both')
TblRule$AlsoPurchased <- trimws(TblRule$AlsoPurchased, which = 'both')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment