Repository navigation
Expand file tree
/
Copy pathscore_regression_rf.sql
More file actions
81 lines (72 loc) · 3.16 KB
/
Copy pathscore_regression_rf.sql
File metadata and controls
81 lines (72 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
SET ANSI_NULLS ON
GO
/****** Create the Stored Procedure for PM Template ******/
SET QUOTED_IDENTIFIER ON
GO
DROP PROCEDURE IF EXISTS score_regression_rf
GO
TRUNCATE TABLE RetailForecasting_models_rf
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [score_regression_rf] @connectionString varchar(300),
@nTree int,
@maxDepth int
AS
BEGIN
DECLARE @inquery NVARCHAR(max) = N'SELECT * from train'
INSERT INTO RetailForecasting_models_rf
EXEC sp_execute_external_script @language = N'R',
@script = N'
sql_share_directory <- paste("c:\\AllShare\\", Sys.getenv("USERNAME"), sep = "")
dir.create(sql_share_directory, recursive = TRUE)
sql <- RxInSqlServer(connectionString = connection_string,
shareDir = sql_share_directory)
local <- RxLocalSeq()
train <- InputDataSet
####################################################################################################
## Regression formula
####################################################################################################
train_vars <- rxGetVarNames(train)
train_vars <- train_vars[train_vars != "time"]
formula <- as.formula(paste("value~", paste(train_vars, collapse = "+")))
####################################################################################################
## Decision forest modeling
####################################################################################################
# TODO: There is a bug in colClasses for sql compute context
# As the work around, setting it as local
# should change it back to sql compute context after it is fixed in RTM
rxSetComputeContext(local)
forest_model <- rxDForest(formula = formula,
data = train,
nTree = nTree,
maxDepth = maxDepth,
seed = 0)
test_table <- RxSqlServerData(table = "test",
connectionString = connection_string)
prediction_df <- rxImport(test_table)
predictions <- rxPredict(modelObject = forest_model,
data = prediction_df,
predVarNames = "forecast.DecFore",
overwrite = TRUE)
prediction_df <- cbind(prediction_df, predictions)
prediction_df$forecast.DecFore <- exp(prediction_df$forecast.DecFore)
prediction_df$value <- NULL
rxSetComputeContext(local)
prediction_table <- RxSqlServerData(table = "Score_Decision_Forest",
connectionString = connection_string)
rxDataStep(inData = prediction_df,
outFile = prediction_table,
overwrite = TRUE)
trained_model <- data.frame(model = as.raw(serialize(forest_model, connection = NULL)));'
, @input_data_1 = @inquery
, @output_data_1_name = N'trained_model'
, @params = N'@connection_string varchar(300), @nTree int, @maxDepth int'
, @connection_string = @connectionString
, @nTree = @nTree
, @maxDepth = @maxDepth
END
GO