从 R 将预测结果返回到 SQL Server

Posted

技术标签:

【中文标题】从 R 将预测结果返回到 SQL Server【英文标题】:Return forecast results to SQL Server from R 【发布时间】:2018-05-05 12:30:53 【问题描述】:

为了进行预测,我使用库 sqldf 将数据从我的 SQL Server 获取到 R。

library("RODBC")
library(sqldf)

dbHandle <- odbcDriverConnect("driver=SQL Server;server=MYSERVER;database=MYBASE;trusted_connection=true")

sql <- 
  "select
yearMon
,  new
from dbo.mytable

w <- sqlQuery(dbHandle, sql)

现在执行简单的预测

w=ts(w$new,frequency = 12,start=c(2015,1)) 

#forecast for the next months
library("forecast")
m <- stats::HoltWinters(w)
test=forecast:::forecast.HoltWinters(m,h=4) #h is how much month do you want to predict

#result of forecast

测试

如何将此预测结果返回给 SQL Server?有表dbo. mytableforecast,我必须将预测数据插入到这个表中。

这是一个可重现的例子:

w=

structure(list(yearMon = structure(c(9L, 7L, 15L, 1L, 17L, 13L, 
11L, 3L, 23L, 21L, 19L, 5L, 10L, 8L, 16L, 2L, 18L, 14L, 12L, 
4L, 24L, 22L, 20L, 6L), .Label = c("1-Apr-15", "1-Apr-16", "1-Aug-15", 
"1-Aug-16", "1-Dec-15", "1-Dec-16", "1-Feb-15", "1-Feb-16", "1-Jan-15", 
"1-Jan-16", "1-Jul-15", "1-Jul-16", "1-Jun-15", "1-Jun-16", "1-Mar-15", 
"1-Mar-16", "1-May-15", "1-May-16", "1-Nov-15", "1-Nov-16", "1-Oct-15", 
"1-Oct-16", "1-Sep-15", "1-Sep-16"), class = "factor"), new = c(8575L, 
8215L, 16399L, 16415L, 15704L, 19805L, 17484L, 18116L, 19977L, 
14439L, 9258L, 12259L, 4909L, 9539L, 8802L, 11253L, 11971L, 7838L, 
2095L, 4157L, 3910L, 1306L, 3429L, 1390L)), .Names = c("yearMon", 
"new"), class = "data.frame", row.names = c(NA, -24L))

【问题讨论】:

这样的? ***.com/questions/4253804/… @RomanLuštrik 我已经阅读了这个主题但没有找到答案,考虑到我使用 FROM R 的预测。如果你认为有答案,你能告诉我我的代码吗?这个答案更多用于 SQL)) 【参考方案1】:

这基本上来自this post。见代码中的 cmets。

library(DBI)

db <- dbConnect(RSQLite::SQLite(), ":memory:")

# load iris dataset into memory sqldf
dbWriteTable(conn = db, name = "iris", value = iris)

# generate new variable values
set.seed(357)
to_add <- rnorm(nrow(iris), mean = 10, sd = 10)

# add new column into the database
dbExecute(conn = db, "ALTER TABLE iris ADD COLUMN prediction REAL")
dbGetQuery(conn = db, statement = "SELECT * FROM iris", n = 6)

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species prediction
1          5.1         3.5          1.4         0.2  setosa         NA
2          4.9         3.0          1.4         0.2  setosa         NA
3          4.7         3.2          1.3         0.2  setosa         NA
4          4.6         3.1          1.5         0.2  setosa         NA
5          5.0         3.6          1.4         0.2  setosa         NA
6          5.4         3.9          1.7         0.4  setosa         NA

# insert values based on row id
dbExecute(conn = db, "UPDATE iris SET prediction = :to_add WHERE rowid = :id", 
          params = data.frame(to_add = to_add, id = rownames(iris)))

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species prediction
1          5.1         3.5          1.4         0.2  setosa  -2.411173
2          4.9         3.0          1.4         0.2  setosa   4.167950
3          4.7         3.2          1.3         0.2  setosa  13.947471
4          4.6         3.1          1.5         0.2  setosa  25.042111
5          5.0         3.6          1.4         0.2  setosa  17.667997
6          5.4         3.9          1.7         0.4  setosa  13.174604

或者,您可以获取表,将预测附加到 R 中,然后将表放回数据库中。

【讨论】:

【参考方案2】:

鉴于您的代码以及我手头没有数据库 ATM 来测试我的代码的事实,您的代码应该如下所示:

# install.packages("forecast")
library(RODBC)
library(forecast)

w <-  structure(list(yearMon = structure(c(9L, 7L, 15L, 1L, 17L, 13L, 
         11L, 3L, 23L, 21L, 19L, 5L, 10L, 8L, 16L, 2L, 18L, 14L, 12L, 
         4L, 24L, 22L, 20L, 6L),
         .Label = c("1-Apr-15", "1-Apr-16", "1-Aug-15", 
         "1-Aug-16", "1-Dec-15", "1-Dec-16", "1-Feb-15", "1-Feb-16", "1-Jan-15", 
         "1-Jan-16", "1-Jul-15", "1-Jul-16", "1-Jun-15", "1-Jun-16", "1-Mar-15", 
         "1-Mar-16", "1-May-15", "1-May-16", "1-Nov-15", "1-Nov-16", "1-Oct-15", 
         "1-Oct-16", "1-Sep-15", "1-Sep-16"), class = "factor"),
         new = c(8575L, 8215L, 16399L, 16415L, 15704L, 19805L, 17484L, 18116L, 19977L, 
         14439L, 9258L, 12259L, 4909L, 9539L, 8802L, 11253L, 11971L, 7838L, 
         2095L, 4157L, 3910L, 1306L, 3429L, 1390L)), .Names = c("yearMon", 
         "new"), class = "data.frame", row.names = c(NA, -24L))


dbHandle <- odbcDriverConnect("driver=SQL Server;server=MYSERVER;database=MYBASE;trusted_connection=true")

# we already have w in this code example
# w <- sqlQuery(dbHandle, ""select yearMon, new from dbo.mytable")

w    <- ts(w$new,frequency = 12,start=c(2015,1)) 

#forecast for the next months
m    <- stats::HoltWinters(w)
test <- forecast:::forecast.HoltWinters(m,h=4) #h is how much month do you want to predict

sqlSave(dbHandle, as.data.frame(test), "dbo.mytableforecast", verbose = TRUE)  # use "append = TRUE" to add rows to an existing table

odbcClose(dbHandle)

不需要包sqlDF - 所有数据库功能都来自RODBC

PS:如果确实有较大的预测结果,则包 odbc(如 Roman Luštrik 的回答中所示)是获得良好性能的正确方法(请参阅 https://github.com/r-dbi/odbc)。

【讨论】:

以上是关于从 R 将预测结果返回到 SQL Server的主要内容,如果未能解决你的问题,请参考以下文章

从存储过程执行 SQL Server 代理作业并返回作业结果

Tensorflow 2 图像批量预测返回结果

sql server和oracle中查询结果返回指定行数的语句

SQL Server 文本列影响返回到经典 ASP 的结果

SQL Server / T-SQL:CHOOSE是评估所有结果,还是只返回返回的结果?

Python:将大型 SQL Server 查询结果导出到 .txt 文件