首先导入arules包,产生相关联的规则
rules <- apriori(Groceries,parameter = list(support=0.01,confidence=0.5))
查看规则
> inspect(rules)
lhs rhs support confidence lift
1 {curd,
yogurt} => {whole milk} 0.01006609 0.5823529 2.279125
2 {other vegetables,
butter} => {whole milk} 0.01148958 0.5736041 2.244885
3 {other vegetables,
domestic eggs} => {whole milk} 0.01230300 0.5525114 2.162336
4 {yogurt,
whipped/sour cream} => {whole milk} 0.01087951 0.5245098 2.052747
5 {other vegetables,
whipped/sour cream} => {whole milk} 0.01464159 0.5070423 1.984385
6 {pip fruit,
other vegetables} => {whole milk} 0.01352313 0.5175097 2.025351
7 {citrus fruit,
root vegetables} => {other vegetables} 0.01037112 0.5862069 3.029608
8 {tropical fruit,
root vegetables} => {other vegetables} 0.01230300 0.5845411 3.020999
9 {tropical fruit,
root vegetables} => {whole milk} 0.01199797 0.5700483 2.230969
10 {tropical fruit,
yogurt} => {whole milk} 0.01514997 0.5173611 2.024770
11 {root vegetables,
yogurt} => {other vegetables} 0.01291307 0.5000000 2.584078
12 {root vegetables,
yogurt} => {whole milk} 0.01453991 0.5629921 2.203354
13 {root vegetables,
rolls/buns} => {other vegetables} 0.01220132 0.5020921 2.594890
14 {root vegetables,
rolls/buns} => {whole milk} 0.01270971 0.5230126 2.046888
15 {other vegetables,
yogurt} => {whole milk} 0.02226741 0.5128806 2.007235
由于支持度和置信度框架的局限性,所以引进兴趣因子lift
当lift=1说明2个变量是独立的,lift<1说明2个变量是负相关的,lift>1说明2个变量是正相关的
下面是查找正相关的规则
x <- subset(rules,subset=rhs%in%"whole milk"&lift>=1.2)
> inspect(sort(x,by="support"))
lhs rhs support confidence lift
1 {other vegetables,
yogurt} => {whole milk} 0.02226741 0.5128806 2.007235
2 {tropical fruit,
yogurt} => {whole milk} 0.01514997 0.5173611 2.024770
3 {other vegetables,
whipped/sour cream} => {whole milk} 0.01464159 0.5070423 1.984385
4 {root vegetables,
yogurt} => {whole milk} 0.01453991 0.5629921 2.203354
5 {pip fruit,
other vegetables} => {whole milk} 0.01352313 0.5175097 2.025351
6 {root vegetables,
rolls/buns} => {whole milk} 0.01270971 0.5230126 2.046888
7 {other vegetables,
domestic eggs} => {whole milk} 0.01230300 0.5525114 2.162336
8 {tropical fruit,
root vegetables} => {whole milk} 0.01199797 0.5700483 2.230969
9 {other vegetables,
butter} => {whole milk} 0.01148958 0.5736041 2.244885
10 {yogurt,
whipped/sour cream} => {whole milk} 0.01087951 0.5245098 2.052747
11 {curd,
yogurt} => {whole milk} 0.01006609 0.5823529 2.279125