0

目標: レポートごとに部門ごとに上位 5 つの関連付けルール (信頼度による) のリストを作成します。

私の既存の構文とテストデータ:

# Create fake data; 1= used report, 0 = didn't use report
data <- data.frame(Dept=c('A','A','A','B','B','B'), 
              Rep1=c(1,1,1,1,1,1), 
              Rep2=c(0,0,0,1,1,1), 
              Rep3=c(1,1,1,0,0,0),
              Rep4=c(0,1,0,1,1,0),
              Rep5=c(0,0,0,0,0,0),
              Rep6=c(1,1,0,0,1,0),
              Rep7=c(1,1,1,1,1,0),
              Rep8=c(0,0,0,1,1,0),
              Rep9=c(1,0,0,1,1,0),
              Rep10=c(1,1,0,0,1,1)
              )

# Turn all variables to factors
data<-data.frame(lapply(data, factor))

# Changes 0s to NAs, only interested in rules where the report was used
data[data==0]<-NA

# lapply command to run apriori on the data when split by Dept
rules <- lapply(split(data, list(data$Dept)), function(x) {
  # Turn split data into transactions
  temp <- as(x[ , 2:length(x)], "transactions")
  # Create rules; artificially low parameters for testing
  temp <- apriori(temp, parameter = list(support=0.01, confidence=0.1,  minlen=2, maxlen=2))
  # Order rules by confidence, eventually will select top 5 (I'm able to do that), and change it to a data frame for later use
  temp <- as(sort(temp, by = "confidence")[0:length(temp)], "data.frame")
})  

# Breaks out the results into separate data.frames
list2env(rules,.GlobalEnv)

これにより、部門ごとに約 50 のルールが作成されます。ただし、それらは部門のグローバル レベルにあります。たとえば、部門 A の data.frame には...

rules                support   confidence  lift
{Rep9=1}=>{Rep6=1}  .3333333   1.00000000  1.5
{Rep4=1}=>{Rep6=1}  .3333333   1.00000000  1.5
...    

理想的には、私の data.frames は次のように見えるはずです...

部門 A とレポート 9 のみ data.frame

rules                support   confidence  lift
{Rep9=1}=>{Rep6=1}  .3333333   1.00000000  1.5
{Rep9=1}=>{Rep10=1}  .3333333   1.00000000  1.5    
...

部門 A とレポート 4 のみ data.frame

rules                support   confidence  lift
{Rep4=1}=>{Rep6=1}  .3333333   1.00000000  1.5
{Rep4=1}=>{Rep10=1}  .3333333   1.00000000  1.5    
...
4

1 に答える 1