Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Bamak
Helper V
Helper V

R and association rules (apriori)

Hi, 

 

the apriori function used with a dataset returns a very small number of rules

 

library(arules)
library(methods)

#specific for Your data type
SalesData <- as(dataset,"transactions")
# matrix
SalesData2=as.data.frame(as(SalesData, "matrix"))
SalesData2=cbind(id=1:nrow(SalesData2),SalesData2)

#
Temp<-apriori(SalesData2, parameter = list(support = 0.006, confidence = 0.25, minlen = 2, maxlen = 2, target = "rules"))
#
output<-inspect(sort(Temp, by = "lift")[1:6])

 

only 6 combinations appear instead of 700

 

While the same function used with a CSV file (same data & columns) returns to me 700 rules

 

# 'dataset' 
library(Matrix)
library(arules)
library(dplyr)

#
SalesData <- read.transactions("...Data.csv", sep = ";")
SalesData<-SalesDate[-1,]
#
Temp<-apriori(SalesData, parameter = list(support = 0.006, confidence = 0.25, minlen = 2, maxlen = 2,target="rules"))

#
#output<-inspect(Temp[1:700])
output<-inspect(Temp[1:length(Temp)])
#output<-inspect(sort(Temp, by = "lift")[1:700])

 

I think I'm using my dataset wrong (query M)
With or without Matrix I return a very low number of lines

 

Thanks for your help

 

Christophe (France power BI "fan")

1 ACCEPTED SOLUTION

I found a workaround

 

library(arules)
library(methods)
# 'dataset' contient les données d'entrée pour ce script
basket_rules<-apriori(dataset, parameter = list(support = 0.006, confidence = 0.10, minlen = 2, maxlen = 2, target = "rules"))

df_basket <- as(basket_rules,"data.frame")
#View(df_basket)

 

This script  provides  all  rules ... 

View solution in original post

2 REPLIES 2
v-yuta-msft
Community Support
Community Support

@Bamak ,

 

This issue should caused by apriori() function:

 

apriori(data, parameter = NULL, appearance = NULL, control = NULL)

 

 

From the R documentation:

parameter: object of class '>APparameter or named list. The default behavior is to mine rules with minimum support of 0.1, minimum confidence of 0.8, maximum of 10 items (maxlen), and a maximal time for subset checking of 5 seconds (maxtime).

 

So you can try changing the maxlen subparameter.

 

Community Support Team _ Jimmy Tao

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

 

I found a workaround

 

library(arules)
library(methods)
# 'dataset' contient les données d'entrée pour ce script
basket_rules<-apriori(dataset, parameter = list(support = 0.006, confidence = 0.10, minlen = 2, maxlen = 2, target = "rules"))

df_basket <- as(basket_rules,"data.frame")
#View(df_basket)

 

This script  provides  all  rules ... 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.