R programing . auto correlation in apriori algorithm

60 views Asked by At

I am working on association problem using Apriori algorithm . Although I'm getting the output but there is self-relation or so as to say autocorrelation problem getting into it . The carmodels should show relations with other carmodels but it is showing the relation with the same carmodels . The input columns have repeatation . I've just put a chunk of input from a large dataset . Is there anyway to remove the autocorrelation problem from the output .

The source code is provided below:-

     mydata <- read.table(header=TRUE, text="
                         cookieid                              pageinfo
                       l8nqwetygUoySgkFHTG                  datsuncarsgtyoplus 
                        Deniju1uufQfOLQSZszdOdLok           marutisuzukicarwiftdzire 
                       l8nofddggreerweUoySgkFHTG            hondacarsmobiliom
                       qrtyftg1z7UoySgkFHTG                 fordcarsfigosd 
                      ")


 carmodels<-data.frame(mydata)
 head(carmodels)

 #Exporting the new copy of webVisitors(copywebVisitors) into excel file named as "done.csv"
 write.csv(carmodels, "C:\\Users\\Desktop\\done.csv")
 df_cars <- read.csv("done.csv")

 library(plyr)
df_itemcars <- ddply(df_cars,c("cookieid"), function(df1){paste(df1$pageinfo,collapse = ",")})

 View(df_itemcars)
 df_itemcars$cookieid <- NULL
 colnames(df_itemcars) <- c("carmodels")
 head(df_itemcars)
  write.csv(df_itemcars,"Itemcars.csv", row.names = TRUE)

 library(Matrix)
 library(arules)
 txn = read.transactions(file="Itemcars.csv", rm.duplicates= TRUE, format="basket",sep=",",cols=NULL)

 df_basket0 <- as(txn,"data.frame")
 View(df_basket0)
 basket_rules <- apriori(txn,parameter = list(sup = 0.01, conf = 0.5,target="rules"))

 inspect(basket_rules)
 df_basket <- as(basket_rules,"data.frame")
 View(df_basket)

And in the output I'm getting autocorrelation problem i.e its showing relation with itself only . Need some help . Any changes that can be made to remove this autocorrelation? The output looks like:- enter image description here

1

There are 1 answers

0
lukeA On

One can only guess from your screenshot that you should remove trailing quotes and whitespaces from the items:

library(arules)
a_list <- list(
  c('volkswagenvento "','" volkswagenvento'),
  c('hondacarscity "','volkswagenvento'))
names(a_list) <- paste("Tr",c(1:2), sep = "")
trans1 <- as(a_list, "transactions")
rules <- apriori(trans1)
inspect(rules)
# lhs                    rhs                 support confidence lift
# [1] {" volkswagenvento} => {volkswagenvento "} 0.5     1          2   
# [2] {volkswagenvento "} => {" volkswagenvento} 0.5     1          2   
# [3] {hondacarscity "}   => {volkswagenvento}   0.5     1          2   
# [4] {volkswagenvento}   => {hondacarscity "}   0.5     1          2 

Now, we substitute quotes and whitespaces at the start and end:

b_list <- lapply(a_list, gsub, pattern='^"\\s*|\\s*"$', replace="") 
trans2 <- as(b_list, "transactions") 
rules2 <- apriori(trans2) inspect(rules2)
# lhs                rhs               support confidence lift
# [1] {}              => {volkswagenvento} 1.0     1          1   
# [2] {hondacarscity} => {volkswagenvento} 0.5     1          1