Search code examples
rdata-miningarules

Mine sequence rules with specific consequent (RHS) in R


There is a sample data set with informations about diabetic people. I would like to find sequence rules (using arulesSequences library), which have only one element in rhs - "id_58". I am posting below code which download the data set (565 KB) and mine some rules. Could you tell me what should I do to reach what I scheduled?

library(arulesSequences)
download.file('http://staff.ii.pw.edu.pl/~gprotazi/dydaktyka/dane/diab_trans.data','diab_trans.data')

# some operations which result in transaction form of data
diab.df <- read.csv("diab_trans.data", header=TRUE, stringsAsFactors = FALSE)
write.table(diab.df, "diab_trans2.data", sep = "," , row.names = FALSE, col.names = FALSE )
diabSeq <- read_baskets(con = "diab_trans2.data", sep =",", info = c("sequenceID","eventID"))

# setting parameter, mining frequent sequential patterns and rules
seqParam = new ("SPparameter",support = 0.6, maxsize = 4, mingap=600, maxgap =150000, maxlen = 3)
patSeq= cspade(diabSeq,seqParam, control = list(verbose = TRUE, tidLists = FALSE, summary= TRUE))
seqRules = ruleInduction(patSeq,confidence = 0.8)
# inspect(seqRules)

# ideas which do not work
# finalSeq <- subset(seqRules, subset = (rhs %in% "id_58"))
# finalSeq <- subset(seqRules, rhs(seqRules) %in% c('id_58'))

Solution

  • Your item labels contain double quotes:

    > itemLabels(rhs(seqRules))
     [1] "\"id_33\"" "\"id_60\"" "\"id_62\"" "2"         "\"id_64\"" "\"id_34\""
     [7] "0"         "3"         "4"         "5"         "6"         "7"        
    [13] "72"        "8"         "9"         "\"id_57\"" "\"id_58\""
    

    So you need to clean them or use:

    finalSeq <- subset(seqRules, rhs(x) %ain% "\"id_58\"")