【问题标题】:How to execute RxSqlServerData method in R?如何在 R 中执行 RxSqlServerData 方法?
【发布时间】:2018-09-26 11:26:14
【问题描述】:

我想创建一个模型并将其部署到 SQL Server 中,以便将它与新的 PREDICT() 内置函数一起使用。 但是,我似乎坚持使用 R 中的 RxSqlServerData 方法。 每当我运行我的脚本时,我都会收到此错误:

rxExecJob 中的错误(rxCallInfo(matchCall, .rxDeprecated = "covariance"), : 数据必须是 RxSqlServerData 数据源 这个计算上下文。

到目前为止,这是我的代码:

#Logistic plain select sql query
#input_query = 'SELECT app.ClientAgeToApplicationDate AS Age, IIF(conc.FirstInstallmentDelay>60,1,0) AS FPD60 FROM dim.Application app JOIN dim.Contract con ON app.ApplicationID = con.ApplicationID JOIN dim.Contract_Calculated conc ON con.ContractID = conc.ContractId'

#LinReg aggregated query
input_query = '
        *SQL QUERY, too long to paste...*
    '

connStr <- paste("Driver=SQL Server; Server=", "czphaddwh01\\dev",
                 ";Database=", "DWH_Staging", ";Trusted_Connection=true", sep = "");

#Set compute context to SQL Server. Does not load any data into a memory of the local client. OBDC can't.
cc <- RxInSqlServer(connectionString = connStr);
rxSetComputeContext(cc)

input_data <- RxSqlServerData(sqlQuery = input_query, connectionString = connStr)
risk <- rxImport(input_data)
#head(risk)

#Binary regression for non-aggregated sql query
#logit_model <- rxLogit(Age ~ FPD60, data = risk)

#LinReg for aggregated sql query
LinReg_model <- rxLinMod(RiskFPD60 ~ Age, data = risk)

我是 R 新手。任何帮助将不胜感激。

【问题讨论】:

    标签: r sql-server microsoft-r


    【解决方案1】:

    当你跑步时

    cc <- RxInSqlServer(connectionString = connStr);
    rxSetComputeContext(cc)
    

    您告诉 R 在 SQL 计算上下文中运行任何 Microsoft 分析函数(基本上是那些以 rx 开头的函数)。这意味着所有的处理都将在数据库内部进行。 R 本质上是充当 SQL 的外壳。

    当然,这要求您正在使用的数据集必须实际位于数据库中:表、视图或返回结果集的查询。

    然后运行时

    risk <- rxImport(input_data)
    LinReg_model <- rxLinMod(RiskFPD60 ~ Age, data = risk)
    

    您将数据导入本地数据框,然后尝试在其上拟合模型。但是您之前告诉 R 在数据库中进行数字运算,并且您的数据是本地的。所以它会抱怨。

    解决方案是将您的RxSqlServerData 对象直接传递给rxLinMod:

    LinReg_model <- rxLinMod(RiskFPD60 ~ Age, data = input_data)
    

    【讨论】:

    • 谢谢你,Hong Ooi。我会在使用计算机后立即尝试并通知您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 2021-07-22
    • 2020-10-11
    • 1970-01-01
    相关资源
    最近更新 更多