Sql server and R, data mining

2

I'm working on Microsoft SQL Management Studio 2016, using the feature that make me to add an R script into the SQL code. My goals is to achieve an aPriori algorithm procedure, that puts the data in a manner that I like, i.e. a table with x, first object, y, second object.

I am stuck here, because in my opinion I have some problem in data. The error is this.

A 'R' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004.

An external script error occurred: Error in eval(expr, envir, enclos) : bad allocation Calls: source -> withVisible -> eval -> eval -> .Call

Here my code. The source data are a table of two column like this:

A   B
a   f
f   a
b   c
...
y   z

And here the code:

GO
    create procedure dbo.apriorialgorithm as

-- delete old table
IF OBJECT_ID('Data') IS NOT NULL
    DROP TABLE Data

-- create a table that store the query result.
CREATE TABLE Data ( art1 nvarchar(100),  art2 nvarchar(100)); 

-- store the query
INSERT INTO Data ( art1,  art2)
select
  firstfield as art1,
  secondfield as art2
  from allthefields
  ; 

IF OBJECT_ID('output') IS NOT NULL
    DROP TABLE output
-- create table of the results of the analysis.

CREATE TABLE output (x nvarchar(100), y nvarchar(100)); 

INSERT INTO output (x, y)
-- R script.
 EXECUTE  sp_execute_external_script
                @language = N'R'
              , @script = N'

Now the R script. The data that I get from the query are numeric, but for the apriori, I need factors, so first I bend the data to factor;

                     df<-data.frame(x=as.factor("art1"),y=as.factor("art2"))

Then, I can apply the apriori:

                        library("arules");
                        library("arulesViz");

                        rules = apriori(df,parameter=list(minlen=2,support=0.05, confidence=0.05));

I need the data without the format of the rules, but simply the objects:

                        ruledf <- data.frame(
                              lhs <- labels(lhs(rules)),
                              rhs <- labels(rhs(rules)), 
                              rules@quality)

                        a<-substr(ruledf$lhs,7,nchar(as.character( ruledf$lhs))-1)
                        b<-substr(ruledf$rhs,7,nchar(as.character( ruledf$rhs))-1)

                        ruledf2<-data.frame(a,b)
                        '

And the last part:

              , @input_data_1 = N'SELECT * from Data'
              , @output_data_1_name = N'ruledf2'
              , @input_data_1_name = N'ruledf2';

GO

I do not know where I am failing, because doing the same things in R using RODBC to catch the db data, everything is ok. Could you help me? Thanks in advance!

sql-server
r
data-mining
asked on Stack Overflow Oct 19, 2016 by s__ • edited Dec 19, 2016 by s__

1 Answer

3

The problem was here, the R script is better this way:

EXECUTE  sp_execute_external_script
                @language = N'R'
              , @script = N'                

                        library("arules");

                        rules = apriori(df[, c("art1", "art2")], parameter=list(minlen=2,support=0.0005, confidence=0.0005));

                        ruledf <- data.frame(
                              lhs <- labels(lhs(rules)),
                              rhs <- labels(rhs(rules)), 
                              rules@quality)

                        ruledf2<-data.frame(
                              lhs2<-substr(ruledf$lhs,7,nchar(as.character( ruledf$lhs))-1),
                              rhs2<-substr(ruledf$rhs,7,nchar(as.character( ruledf$rhs))-1)
                                           )

                        colnames(ruledf2)<-c("a","b")  '

Then it needs to have the right input and output:

      , @input_data_1 = N'SELECT * from Data'
      , @input_data_1_name = N'df'
      , @output_data_1_name = N'ruledf2'

So the result is going to be a table named output like this

x    y
artA artB
artB artA
...
artY artZ

Very helpful this.

answered on Stack Overflow Dec 19, 2016 by s__ • edited Oct 8, 2018 by s__

User contributions licensed under CC BY-SA 3.0