Repository navigation
Expand file tree
/
Copy pathgenerate_test.sql
More file actions
77 lines (64 loc) · 2.5 KB
/
Copy pathgenerate_test.sql
File metadata and controls
77 lines (64 loc) · 2.5 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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
DROP PROCEDURE IF EXISTS generate_test
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [generate_test] @connectionString varchar(300),
@testlength int
AS
BEGIN
DECLARE @inquery NVARCHAR(max) = N'SELECT * from features'
EXEC sp_execute_external_script @language = N'R',
@script = N'
####################################################################################################
## Create train data
####################################################################################################
## ------- User-Defined Parameters ------ ##
lags <- 1:26
ratio <- 1
horizon <- testlength
data <- InputDataSet
library(zoo)
library(plyr)
####################################################################################################
## Create test data
####################################################################################################
test.addlags <- function(df, var, lags){
data.length <- nrow(df)
train.length <- data.length - testlength
test <- df[(train.length+1):data.length, , drop = FALSE]
train <- df[1:train.length, , drop = FALSE]
# Missing data: replace NA with average
train$value[is.na(train$value)] <- mean(train$value, na.rm = TRUE)
# Create lag features
test$horizon <- as.factor(1:testlength)
test.lags <- df[train.length - lags + 1, var]
test.lags <- matrix(rep(test.lags, testlength), nrow = testlength, byrow = TRUE)
colnames(test.lags) <- paste("lag", lags, sep = "")
res <- cbind(test, test.lags)
return(res)
}
test_data <- ddply(data, .variables = .(ID1, ID2), .fun = test.addlags, var = "value", lags = lags)
if(ratio < 1){
downsample <- function(data, ratio){ data[sample(nrow(data), size = ratio*nrow(data)),]}
test_data <- ddply(test_data, .variables = .(ID1, ID2), .fun = downsample, ratio = ratio)
}
test_data$time <- as.character(test_data$time)
test_table <- RxSqlServerData(table = "test",
connectionString = connection_string)
rxDataStep(inData = test_data,
outFile = test_table,
overwrite = TRUE)'
, @input_data_1 = @inquery
, @params = N'@connection_string varchar(300), @testlength int'
, @connection_string = @connectionString
, @testlength = @testlength
END
;
GO