11

I am applying Aprior algorithm, and while plotting there is an error.

I have installed packages arules and arulesviz.

The data has 3 attributes . Two were factorized and one attribute was not factorized. I have taken that attribute attribute separately and applied factor function. The code is below:

New_Train_Wifi = read.xlsx("D:/Train_Test.xls",1)

str(New_Train_Wifi)
'data.frame':   2201 obs. of  3 variables:
 $ Wifi_ID: Factor w/ 4 levels "1st","2nd","3rd",..: 3 3 3 3 3 3 3 3 3 3 ...
 $ Store  : Factor w/ 5 levels "Book_Store","Clothing",..: 3 3 3 3 3 3 3 3 3 3 ...
 $ Mac_ID : num  125 125 125 125 125 125 125 125 125 125 ...

A <- as.factor(Test_ARM_ABC$Wifi_ID)
C <- as.factor(New_Train_Wifi$Mac_ID)
New_Train_Wifi$MacID <- NULL
New_Train_Wifi$MacID <- C
New_Train_Wifi$Mac_ID <- NULL

class(New_Train_Wifi)
[1] "data.frame"
[1] "Wifi_ID" "Store"   "MacID" 
str(New_Train_Wifi)
'data.frame':   2201 obs. of  3 variables:
 $ Wifi_ID: Factor w/ 4 levels "1st","2nd","3rd",..: 3 3 3 3 3 3 3 3 3 3 ...
 $ Store  : Factor w/ 5 levels "Book_Store","Clothing",..: 3 3 3 3 3 3 3 3 3 3 ...
 $ MacID  : Factor w/ 6 levels "100","125","254",..: 2 2 2 2 2 2 2 2 2 2 ...

rules <- apriori(New_Train_Wifi)
inspect(rules)
rules <- apriori(New_Train_Wifi, parameter = list(minlen = 2, supp = 0.10, conf = 0.8), 
                 appearance = list(rhs = c("Wifi_ID=1st", "Wifi_ID=2nd", "Wifi_ID=3rd", 
                 "Wifi_ID=4th"), default="lhs"), control = list(verbose = F))

> inspect(rules.sorted)

#/*Now wen I give below statement in r console */
> plot(rules)
Error in as.double(y) : 
 cannot coerce type 'S4' to vector of type 'double'

Above statement is the error I tried to but couldn't resolve . If anyone can resolve it, I will be really grateful.

The site that I referred to: http://www.rdatamining.com/examples/association-rules

alistaire
  • 42,459
  • 4
  • 77
  • 117
user3292373
  • 483
  • 3
  • 8
  • 25
  • Basically, your `rules` is an S4 object for which there isn't a `plot` method. Best bet is to extract whatever slots contain the data you wish to plot. Like `plot(rules@x,rules@y)` if `x` and `y` were the slotNames. – Carl Witthoft Feb 17 '14 at 15:44
  • lhs rhs support confidence lift 1 {Store=Food_Court} => {Wifi_ID=3rd} 0.1004089 1.0000000 3.117564 2 {Store=Food Court} => {Wifi_ID=4th} 0.2158110 0.8333333 2.072505 . Those are the rules. Should I give as plot(rules@Wifi_ID,rules@Mac_ID) – user3292373 Feb 17 '14 at 15:53
  • Your comment isn't formatted enough to understand. What does `slotNames(rules)` return? – Carl Witthoft Feb 17 '14 at 18:10
  • When I give slotNames(rules) [1] "lhs" "rhs" "quality" "info" . These are the four parameters I am getting . Now, Is there any way to plot the results. – user3292373 Feb 19 '14 at 08:40
  • 2
    I think this is going to turn out to be: you forgot to type `library(arulesViz)` so the plot method for `rules` isn't active. – Carl Witthoft Feb 19 '14 at 12:31
  • I have added all the required packages for arulesViz . Again the same result is occuring . What else might be the prob ?. – user3292373 Feb 24 '14 at 07:08
  • After loading all packages I have run from beginning and it resulted me in new error Below code is my input >rules <- apriori(Training_Rules,parameter = list(minlen=2, supp=0.02, conf=0.8),appearance = list(rhs=c("Mac_ID=751", "Mac_ID=254","Mac_ID=125","Mac_ID=100"), + default="lhs"),control = list(verbose=F)) > plot(rules) Error in .Call.graphics(C_palette2, .Call(C_palette2, NULL)) : invalid graphics state – user3292373 Feb 24 '14 at 10:58

3 Answers3

17

I had this issue too and then realized that I forgot to load the library with

library(arulesViz)

after installing the package with:

install.packages("arulesViz")
alistaire
  • 42,459
  • 4
  • 77
  • 117
user1570017
  • 171
  • 1
  • 3
5

This problem is caused by library installed incomplete(I guess it might put the wrong name(version) in the pack of arulesViz).

You can download https://cran.rstudio.com/bin/windows/contrib/3.3/seriation_1.2-1.zip manually,then use "r-studio menu -> tools -> install packages.." to install above zip file downloaded from the site.

Then try to redo install.packages("arulesViz") and library(arulesViz), it will be workable. Done.

Viktor Mellgren
  • 4,318
  • 3
  • 42
  • 75
Vincent Chang
  • 66
  • 1
  • 1
4

I had the same error, and in my case it was because I forgot to run library(raster).

Steve Walsh
  • 127
  • 1
  • 7
  • I had the same issue. `plot()` functions are available through multiple packages - raster, terra, base etc. If not specified explicitly, R might get confused about which one to use. – close2zero May 12 '23 at 15:49