Repository navigation
Expand file tree
/
Copy pathscore_regression_model.sql
More file actions
60 lines (49 loc) · 2.34 KB
/
Copy pathscore_regression_model.sql
File metadata and controls
60 lines (49 loc) · 2.34 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
SET ANSI_NULLS ON
GO
/****** Create the Stored Procedure for PM Template ******/
DROP PROCEDURE IF EXISTS score_regression_model
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [score_regression_model] @modelname varchar(20),
@connectionString varchar(300)
AS
BEGIN
declare @inquery NVARCHAR(max) = N'SELECT * FROM score_Features_Normalized';
declare @model varbinary(max) = (select model from [PM_Models] where model_name = @modelname);
EXEC sp_execute_external_script @language = N'R',
@script = N'
####################################################################################################
## Get score table data for prediction
####################################################################################################
prediction_df <- InputDataSet
####################################################################################################
## Rregression prediction
####################################################################################################
model <- unserialize(model)
prediction <- rxPredict(modelObject = model,
data = prediction_df,
predVarNames = "Prediction",
overwrite = TRUE)
####################################################################################################
## Write score results to SQL
####################################################################################################
predictions <- cbind(prediction_df$id, prediction_df$cycle_orig, prediction)
colnames(predictions) <- c("id", "cycle", "Prediction")
prediction_table <- RxSqlServerData(table = paste("Regression_score", modelname, sep = "_"),
connectionString = connection_string)
rxDataStep(inData = predictions,
outFile = prediction_table,
overwrite = TRUE)'
, @input_data_1 = @inquery
, @params = N'@model varbinary(max), @modelname varchar(20), @connection_string varchar(300)'
, @model = @model
, @modelname = @modelname
, @connection_string = @connectionString
END
GO