microsoft / microsoft/sql-server-samples

conducting regression analysis using R via SQL 2017

Aperta
#398 0 commenti 0 reazioni 1 assegnatario Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
PowerShell
Stelle
11.2k
Fork
9.1k
Merge medio
2g 7h
PR unite (30g)
14

Descrizione

I want perform regression analysis using R code via SQL Server 2017 (it's integrated here).

Here is the native R code working with the csv

The main matter of code that we perform regression separately by groups [CustomerName]+[ItemRelation]+[DocumentNum]+[DocumentYear]

df=read.csv("C:/Users/synthex/Desktop/re.csv", sep=";",dec=",")

#load needed library

library(tidyverse)
library(broom)
#order dataset
df=df[ order(df[,5]),]
df=df[ order(df[,6]),]
#delete signs
df$Customer<-gsub("\\-","",df$Customer)

#create lm function for separately by group regression

my_lm <- function(df) {
  lm(SaleCount~IsPromo, data = df)
}

reg=df %>%
  group_by(CustomerName,ItemRelation,DocumentNum,DocumentYear) %>%
  nest() %>%
  mutate(fit = map(data, my_lm),
         tidy = map(fit, tidy)) %>%
  select(-fit, - data) %>%
  unnest()

w=aggregate(df$action, by=list(CustomerName=df$CustomerName,ItemRelation=df$ItemRelation, DocumentNum=df$DocumentNum, DocumentYear=df$DocumentYear), FUN=sum)
View(w)

# multiply each group by the number of days of the action
EA<-data.frame(reg$CustomerName,reg$ItemRelation,reg$DocumentNum,reg$DocumentYear, reg$estimate*w$x)

#del intercepts
toDelete <- seq(2, nrow(EA), 2)
newdat=EA[ toDelete ,]
View(newdat)

The finished result: this code runs in SSMS

So what I did:

EXECUTE sp_execute_external_script
      @language = N'R'
    , @script = N' OutputDataSet <- InputDataSet;'
    , @input_data_1 = N' SELECT     [CustomerName]
      ,[ItemRelation]
      ,[SaleCount]
      ,[DocumentNum]
      ,[DocumentYear]
      ,[IsPromo]
 FROM [Action].[dbo].[promo_data];'

    WITH RESULT SETS (([CustomerName] nvarchar(max) NOT NULL, [ItemRelation] int NOT NULL,
     [SaleCount] int NOT NULL,[DocumentNum]  int NOT NULL,
    [DocumentYear] int NOT NULL, [IsPromo] int NOT NULL));


df=as.data.frame(InputDataSet)

Message 102, level 15, state 1, line 17
Incorrect syntax near the "=" construct.

So, how perform regression analysis in SQL separately by groups?

Note, all coefficients must be saved, because new data come to the sql, should already automatically calculate by the equation of constructed model for each group.

The above code simply estimates the impact of the action, the beta coefficients of each group multiplies by the number of days of the action for each group.

If it is needed, here is a reproducible example:

df=structure(list(CustomerName = structure(c(1L, 2L, 3L, 3L, 1L, 
2L, 3L, 3L, 4L, 4L, 4L, 1L, 2L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 1L, 2L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
1L, 2L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 
2L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("Attacks of the vehicle", 
"Auchan TS", "Tape of the vehicle", "X5 Retail Group"), class = "factor"), 
    ItemRelation = c(13322L, 13322L, 158121L, 158122L, 13322L, 
    13322L, 158121L, 158122L, 11592L, 13189L, 13191L, 13322L, 
    13322L, 158121L, 158122L, 11592L, 13189L, 13191L, 158121L, 
    158121L, 158122L, 158122L, 13322L, 13322L, 158121L, 158122L, 
    11592L, 13189L, 13191L, 157186L, 157192L, 158009L, 158010L, 
    158121L, 158121L, 158122L, 158122L, 13322L, 13322L, 158121L, 
    158122L, 11592L, 13189L, 13191L, 157186L, 157192L, 158009L, 
    158010L, 158121L, 158121L, 158122L, 158122L, 13322L, 13322L, 
    158121L, 158122L, 11514L, 11592L, 11623L, 13189L, 13191L), 
    SaleCount = c(10L, 35L, 340L, 260L, 3L, 31L, 420L, 380L, 
    45L, 135L, 852L, 1L, 34L, 360L, 140L, 14L, 62L, 501L, 0L, 
    560L, 640L, 0L, 0L, 16L, 0L, 0L, 15L, 66L, 542L, 49L, 228L, 
    3360L, 5720L, 980L, 0L, 0L, 1280L, 9L, 29L, 200L, 120L, 46L, 
    68L, 569L, 52L, 250L, 2360L, 3140L, 1640L, 0L, 0L, 1820L, 
    5L, 33L, 260L, 220L, 665L, 25L, -10L, 62L, 281L), DocumentNum = c(36L, 
    4L, 41L, 41L, 36L, 4L, 41L, 41L, 33L, 33L, 33L, 36L, 4L, 
    41L, 41L, 33L, 33L, 33L, 63L, 62L, 62L, 63L, 36L, 4L, 41L, 
    41L, 33L, 33L, 33L, 57L, 56L, 12L, 12L, 62L, 63L, 63L, 62L, 
    36L, 4L, 41L, 41L, 33L, 33L, 33L, 57L, 56L, 12L, 12L, 62L, 
    63L, 63L, 62L, 36L, 4L, 41L, 41L, 60L, 33L, 71L, 33L, 33L
    ), DocumentYear = c(2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
    2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
    2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
    2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
    2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
    2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
    2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
    2017L), IsPromo = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("CustomerName", "ItemRelation", 
"SaleCount", "DocumentNum", "DocumentYear", "IsPromo"), class = "data.frame", row.names = c(NA, 
-61L))

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.