0

Summary:
I am creating Basket Analysis Rules with RStudio by using the Arule Package. I am saving the WorkSpace to a file (i.e. x.RDATA). And using the R.DOTNET Nuget PackAge in VS.NET to Query the Rule. I am Calling RFunction sending parameters.

There is no problem up to that point. BUT.... When I send the ProductCode, which does not exists in the ARule Function, it throws an error.

Error in lhs %ain% newBasket : table contains an unknown item label

I use the dataset Groceries in order to reproduce the scenario. In my opinion the product could not be in the model.

It can be a new product OR it does not exist in the rule because of its low CONFIDENCE, SUPORT or LIFT

# Load the libraries
library(arules)
library(arulesViz)
library(datasets)

# Load the data set
data(Groceries)
# Get the rules
rules <- apriori(Groceries, parameter = list(supp = 0.001, conf = 0.8))
# Sorting 
rules<-sort(rules, by="confidence", decreasing=TRUE)

# Redundancies 
subset.matrix <- is.subset(rules, rules)
subset.matrix[lower.tri(subset.matrix, diag=T)] <- NA
redundant <- colSums(subset.matrix, na.rm=T) >= 1
rules.pruned <- rules[!redundant]
rules<-rules.pruned

#-----------------------------------------------------------------------------------
#FUNCTION
fn_findRules <- function(rules, newBasket){
  arules<-subset(rules, subset = lhs %ain% newBasket)
  arules.sorted <- sort(arules, by="lift")
  return (arules)
}
#-----------------------------------------------------------------------------------

When I call the function with parameters "tropical fruit", "yogurt" there is no problem.

#Calling Function For Test 
newBasketItems <- c("tropical fruit", "yogurt")
RuleResult <- fn_findRules(rules, newBasketItems)
inspect(RuleResult[1:5])

result:

    lhs                                                                rhs                support   confidence lift  
264 {tropical fruit,grapes,whole milk,yogurt}                       => {other vegetables} 0.0010168 1.00000    5.1682
269 {ham,tropical fruit,pip fruit,yogurt}                           => {other vegetables} 0.0010168 1.00000    5.1682
276 {tropical fruit,root vegetables,yogurt,oil}                     => {whole milk}       0.0011185 1.00000    3.9136
395 {sausage,tropical fruit,root vegetables,yogurt}                 => {whole milk}       0.0015252 0.93750    3.6690
410 {citrus fruit,tropical fruit,root vegetables,whole milk,yogurt} => {other vegetables} 0.0014235 0.93333    4.8236

But if I call the function with parameters "tropical NEW PRODUCT", "yogurt" it throws an error.

#Calling Function
newBasketItems <- c("tropical NEW PRODUCT", "yoguurt")
RuleResult <- fn_findRules(rules, newBasketItems)

Error in lhs %ain% newBasket : table contains an unknown item label 

How can I handle this error?

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Cem Üney
  • 25
  • 7

0 Answers0