用R做关联度分析的整体代码都没问题,不过不会转化需要用到的数据的格式,都是靠手动调整好运行的,不知道有没有人能够指点一下,关于数据格式变换方面的
R做关联度分析
代码:
library(arules)
library(rattle)
setwd('D:/实战期/分析/市场分析/20191122')
Gary=read.csv(file="TW-订单品类数据.csv",header=T)
Gary_trans<- as(split(Gary$item, Gary$ID),"transactions")
rules <- apriori(Gary_trans, parameter=list(support=0.001,confidence=0.2,minlen=2))
inspect(rules)
result.data.frame<-as(rules,"data.frame")
write.table(result.data.frame,file="R-输出结果1.csv",sep=",")
library(arulesViz)
library(RColorBrewer)
itemFrequencyPlot(Gary_trans,topN=10,col="lightblue")
plot(rules,control=list(jitter=2,col=rev(brewer.pal(9,"BrBG"))),shading="lift")
plot(rules,method="grouped",control=list(k=5,col=rev(brewer.pal(9,"YlOrRd")[3:8])))
plot(rules,method = "graph",measure = "confidence",control = list(type="items"),shading = "lift")
dt<-read.csv("TW-订单品类数据2.csv",header=T)
hc<-hclust(dist(t(dt)),method="ward.D")