【问题标题】:Return forecast results to SQL Server from R从 R 将预测结果返回到 SQL Server
【发布时间】: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))

【问题讨论】:

  • @RomanLuštrik 我已经阅读了这个主题但没有找到答案,考虑到我使用 FROM R 的预测。如果你认为有答案,你能告诉我我的代码吗?这个答案更多用于 SQL))

标签: r time-series forecasting rodbc sqldf


【解决方案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)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-06
      • 1970-01-01
      • 1970-01-01
      • 2018-12-22
      • 2020-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多