library(arulesSequences)
tmp_data<-data.frame(item=factor(c("A","B","B","A","B","A","C","A","B","C","B","A","B","A","A","B","A","B")))#必须是factor
tmp_data.tran<-as(tmp_data,"transactions")
transactionInfo(tmp_data.tran)$sequenceID<-c(1,1,1,1,1,2,2,2,2,2,2,3,3,3,4,4,4,4)
transactionInfo(tmp_data.tran)$eventID<-c(10,10,20,30,30,20,20,30,30,30,50,10,30,40,30,30,40,50)
transactionInfo(tmp_data.tran)
#lab1:基本约束support(默认为0.1)
s_result<-cspade(tmp_data.tran,parameter = list(support = 0.75),control = list(verbose = TRUE))
inspect(s_result)
# items support
# 1 <{item=A}> 1.00
# 2 <{item=B}> 1.00
# 3 <{item=A},
# {item=B}> 1.00
# 4 <{item=A,
# item=B}> 0.75
# 5 <{item=B},
# {item=B}> 0.75
# 6 <{item=A,
# item=B},
# {item=B}> 0.75
# 7 <{item=A},
# {item=A}> 1.00
# 8 <{item=B},
# {item=A}> 0.75
#lab2:maxlen约束:每一个序列的event最多只能为n(每一个序列中的“{}”号最多只能为n个)
#英文解释 maxlen: an integer value specifying the maximum number of elements of a sequence (default 10 range > 0).
#element就是指event。所以:sequence包含event(element),event包含item
s_result<-cspade(tmp_data.tran,parameter = list(support = 0.75,maxlen=1),control = list(verbose = TRUE))
inspect(s_result)
# items support
# 1 <{item=A}> 1.00
# 2 <{item=B}> 1.00
# 3 <{item=A,
# item=B}> 0.75
#lab3:maxsize约束:每一个序列的每个event内部最多只能有n个item(每一个序列中的每个{}内部最多有n个items)
#英文解释maxsize: an integer value specifying the maximum number of items of an element of a sequence(default 10, range > 0).
#element就是指event。所以:sequence包含event(element),event包含item
s_result<-cspade(tmp_data.tran,parameter = list(support = 0.75,maxsize=1),control = list(verbose = TRUE))
inspect(s_result)
# items support
# 1 <{item=A}> 1.00
# 2 <{item=B}> 1.00
# 3 <{item=A},
# {item=B}> 1.00
# 4 <{item=B},
# {item=B}> 0.75
# 5 <{item=A},
# {item=A}> 1.00
# 6 <{item=B},
# {item=A}> 0.75
#lab4:mingap:所有的相邻的两个eventID的差大于mingap
#英文解释mingap: an integer value specifying the minimum time difference between consecutive elements of a sequence (default none, range >= 0).
s_result<-cspade(tmp_data.tran,parameter = list(support = 0.75,mingap=19),control = list(verbose = TRUE))
inspect(s_result)
# items support
# 1 <a href="http://4seohunt.biz/rep/bannerweb.upstate.edu">bannerweb.upstate.edu</a> <{item=A}> 1.00
# 2 <{item=B}> 1.00
# 3 <{item=A},
# {item=B}> 1.00
# 4 <{item=A,
# item=B}> 0.75
# 5 <{item=B},
# {item=B}> 0.75
# 6 <{item=A,
# item=B},
# {item=B}> 0.75
#分析:缺少了实验lab1中的
# 7 <{item=A},
# {item=A}> 1.00
#eventid只差分别是:sid=1时,eventid=30-10=20>19。sid=2时,eventid=30-20=10不>19。sid=3时,eventid=40-10=30>19。sid=4时,eventid=40-30=10不>19。即supp=2/4=0.5
# 8 <{item=B},
# {item=A}> 0.75
#eventid只差分别是:sid=1时,eventid=30-10=20>19。sid=2时,无b->a。sid=3时,eventid=40-30=10不>19。sid=4时,eventid=40-30=10不>19。即supp=1/4=0.25
#lab5:maxgap:所有的相邻的两个eventID的差小于等于maxgap
#英文解释maxgap: an integer value specifying the maximum time difference between consecutive elements of a sequence (default none, range >= 0).
s_result<-cspade(tmp_data.tran,parameter = list(support = 0.75,maxgap=19),control = list(verbose = TRUE))
inspect(s_result)
# items support
# 1 <{item=A}> 1.00
# 2 <{item=B}> 1.00
# 3 <{item=A},
# {item=B}> 0.75
# 4 <{item=A,
# item=B}> 0.75
# 5 <{item=B},
# {item=A}> 0.75
#分析:和实验lab1中数据的差别
#lab1中
# 3 <{item=A},
# {item=B}> 1
#lab5中
# 3 <{item=A},
# {item=B}> 0.75
#eventid只差分别是:sid=1时,eventid=30-20=10小于等于19,30-10=20不小于等于19。sid=2时,50-20=30不小于等于19,50-30=20不小于等于19,30-20=10小于等于19。sid=3时,eventid=30-10=20不小于等于19。sid=4时,eventid=40-30=10不小于等于19,50-40=10小于等于19。即supp=3/4=0.75