In the past I have run Apriori in R using the "arules" package. In the past I have done this using flat files in R studio with the following code:
# install.packages('arules');
library(arules);
# the following is how I bring in flat files:
ds = read.csv('somedata.csv', header = FALSE)
# and here is how I import this data but as a sparse matrix:
dsSparse = read.transactions('somedata.csv', sep = ',', rm.duplicates = TRUE)
For the first time I am working with data in SQL Server and using R Tools in visual studio.
Here is the script I'm running:
#Connection to SQL Server.
connStr = paste("Driver=SQL Server; Server=", "MyServer", ";Database=", "MyDatabase", ";Trusted_Connection=true;", sep = "");
#Get data from SQL Query
SQL_ds = RxSqlServerData(sqlQuery = "SELECT * FROM dbo.SomeData", connectionString = connStr, returnDataFrame = TRUE);
#Run the query and store the data into the table
ds = rxImport(SQL_ds);
Is there a method I can use to then convert this to a sparse matrix like I do with the static file?
I could write a T-SQL query to pivot the data and create a sparse matrix that way but I'd like to know if I can do it efficiently in R.
Here is a sample of data Im working with:
CREATE TABLE #SomeData
(
SaleId INT
, Item1 NVARCHAR (500)
, Item2 NVARCHAR (500)
, Item3 NVARCHAR (500)
, Item4 NVARCHAR (500)
, Item5 NVARCHAR (500)
, Item6 NVARCHAR (500)
, Item7 NVARCHAR (500)
, Item8 NVARCHAR (500)
, Item9 NVARCHAR (500)
, Item10 NVARCHAR (500)
, Item11 NVARCHAR (500)
, Item12 NVARCHAR (500)
, Item13 NVARCHAR (500)
, Item14 NVARCHAR (500)
, Item15 NVARCHAR (500)
, Item16 NVARCHAR (500)
, Item17 NVARCHAR (500)
, Item18 NVARCHAR (500)
, Item19 NVARCHAR (500)
, Item20 NVARCHAR (500)
)
INSERT INTO #SomeData
VALUES
(1, N'shrimp', N'almonds', N'avocado', N'vegetables mix', N'green grapes', N'whole weat flour', N'yams', N'cottage cheese', N'energy drink', N'tomato juice', N'low fat yogurt', N'green tea', N'honey', N'salad', N'mineral water'
, N'salmon', N'antioxydant juice', N'frozen smoothie', N'spinach', N'olive oil')
, (2, N'burgers', N'meatballs', N'eggs', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
, (3, N'chutney', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
, (4, N'turkey', N'avocado', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
, (5, N'mineral water', N'milk', N'energy bar', N'whole wheat rice', N'green tea', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
SELECT * FROM #SomeData
Thanks
If I've understood well you have a table similar to this:
id item1 item2 ... itemn
1 a s n1
2 a s n2
3 c d n4
4 c e n3
...
m m1 m2 mn
Unluckily I've worked with R (RStudio) and MSSMS+R (embed R code in SQL), but not with Visual Studio, so I can give you some pseudo-code as reasoning and hint:
First of all, you have to reduce your table to a two-column table, with the ID and the products: if we have a fake table like this:
library(arules)
library(tidyverse)
fake <- data.frame(id = c(1,2,3,4,5),
item1 = c('a','a','a',NA,'b'),
item2 = c('d','d','d',NA,NA),
item3 = c('e','e','c','k','b'))
> fake
id item item item
1 1 a d e
2 2 a d e
3 3 a d c
4 4 <NA> <NA> k
5 5 b <NA> b
colnames(fake) <- c('id','item','item','item')
df <- rbind(fake[,c(1,2)],fake[,c(1,3)],fake[,c(1,4)])
# here we go
> df
id item
1 1 a
2 2 a
3 3 a
4 4 <NA>
5 5 b
6 1 d
7 2 d
8 3 d
9 4 <NA>
10 5 <NA>
11 1 e
12 2 e
13 3 c
14 4 k
15 5 b
To be more precise, you'd remove the rows with NA
, but the idea is the same.
Now you can create your transaction matrix:
df <- df %>%
select(id, item) %>%
distinct() %>%
mutate(value = 1) %>%
spread(item, value, fill = 0)
> df
id a b d c e k <NA>
1 1 1 0 1 0 1 0 0
2 2 1 0 1 0 1 0 0
3 3 1 0 1 1 0 0 0
4 4 0 0 0 0 0 1 1
5 5 0 1 0 0 0 0 1
# here is necessary the arules package
itemMatrix <- as(as.matrix(df[, -1]), "transactions")
> itemMatrix
transactions in sparse format with
5 transactions (rows) and
7 items (columns)
Last, you can apply your apriori algorithm:
rules <- apriori(itemMatrix, parameter = list(supp = 0.4, conf = 0.8, target = "rules"))
rules_conf <- sort (rules, by="support", decreasing=TRUE)
inspect(rules_conf)
lhs rhs support confidence lift count
[1] {d} => {a} 0.6 1 1.666667 3
[2] {a} => {d} 0.6 1 1.666667 3
[3] {e} => {d} 0.4 1 1.666667 2
[4] {e} => {a} 0.4 1 1.666667 2
[5] {d,e} => {a} 0.4 1 1.666667 2
[6] {a,e} => {d} 0.4 1 1.666667 2
As further information, take a look also to the package sqldf
and RODBC
, to manage data.frame with query in R environment and to connect R via ODBC.