我使用 Microsoft 的“ Data Science End to End Walkthrough ”为自己设置了 R Server,他们的示例运行良好。
该示例(纽约出租车数据)使用非分类变量(即距离、出租车费等)来预测分类变量(1 或 0 表示是否支付了小费)。
我正在尝试使用分类变量作为输入,使用线性回归(rxLinMod 函数)来预测类似的二进制输出,并且出现错误。
该错误表明参数的数量与变量的数量不匹配,但在我看来,number of variables
实际上是每个因子(变量)内的级别数。
复制
在 SQL Server 中创建一个名为 example 的表:
USE [my_database];
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
CREATE TABLE [dbo].[example](
[Person] [nvarchar](max) NULL,
[City] [nvarchar](max) NULL,
[Bin] [integer] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY];
将数据放入其中:
insert into [dbo].[example] values ('John','London',0);
insert into [dbo].[example] values ('Paul','New York',0);
insert into [dbo].[example] values ('George','Liverpool',1);
insert into [dbo].[example] values ('Ringo','Paris',1);
insert into [dbo].[example] values ('John','Sydney',1);
insert into [dbo].[example] values ('Paul','Mexico City',1);
insert into [dbo].[example] values ('George','London',1);
insert into [dbo].[example] values ('Ringo','New York',1);
insert into [dbo].[example] values ('John','Liverpool',1);
insert into [dbo].[example] values ('Paul','Paris',0);
insert into [dbo].[example] values ('George','Sydney',0);
insert into [dbo].[example] values ('Ringo','Mexico City',0);
我还使用了一个 SQL 函数,它以表格式返回变量,因为这就是 Microsoft 示例所需要的。创建函数formatAsTable
:
USE [my_database];
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
CREATE FUNCTION [dbo].[formatAsTable] (
@City nvarchar(max)='',
@Person nvarchar(max)='')
RETURNS TABLE
AS
RETURN
(
-- Add the SELECT statement with parameter references here
SELECT
@City AS City,
@Person AS Person
);
我们现在有一个包含两个分类变量的表 -Person
和City
。
让我们开始预测。在 R 中,运行以下命令:
library(RevoScaleR)
# Set up the database connection
connStr <- "Driver=SQL Server;Server=<servername>;Database=<dbname>;Uid=<uid>;Pwd=<password>"
sqlShareDir <- paste("C:\\AllShare\\",Sys.getenv("USERNAME"),sep="")
sqlWait <- TRUE
sqlConsoleOutput <- FALSE
cc <- RxInSqlServer(connectionString = connStr, shareDir = sqlShareDir,
wait = sqlWait, consoleOutput = sqlConsoleOutput)
rxSetComputeContext(cc)
# Set the SQL which gets our data base
sampleDataQuery <- "SELECT * from [dbo].[example] "
# Set up the data source
inDataSource <- RxSqlServerData(sqlQuery = sampleDataQuery, connectionString = connStr,
colClasses = c(City = "factor",Bin="logical",Person="factor"
),
rowsPerRead=500)
现在,建立线性回归模型。
isWonObj <- rxLinMod(Bin ~ City+Person,data = inDataSource)
查看模型对象:
isWonObj
请注意,它看起来像这样:
...
Total independent variables: 11 (Including number dropped: 3)
...
Coefficients:
Bin
(Intercept) 6.666667e-01
City=London -1.666667e-01
City=New York 4.450074e-16
City=Liverpool 3.333333e-01
City=Paris 4.720871e-16
City=Sydney -1.666667e-01
City=Mexico City Dropped
Person=John -1.489756e-16
Person=Paul -3.333333e-01
Person=George Dropped
Person=Ringo Dropped
它说有 11 个变量,这很好,因为这是因子中水平的总和。
现在,当我尝试Bin
基于City
and预测值时Person
,我得到一个错误:
首先我格式化City
并且Person
我想预测为一个表格。然后,我预测将其用作输入。
sq<-"SELECT City, Person FROM [dbo].[formatAsTable]('London','George')"
pred<-RxSqlServerData(sqlQuery = sq,connectionString = connStr
, colClasses = c(City = "factor",Person="factor"))
如果您检查该pred
对象,它看起来与预期的一样:
> head(pred)
City Person
1 London George
现在,当我尝试预测时,我得到了一个错误。
scoredOutput <- RxSqlServerData(
connectionString = connStr,
table = "binaryOutput"
)
rxPredict(modelObject = isWonObj, data = pred, outData = scoredOutput,
predVarNames = "Score", type = "response", writeModelVars = FALSE, overwrite = TRUE,checkFactorLevels = FALSE)
错误说:
INTERNAL ERROR: In rxPredict, the number of parameters does not match the number of variables: 3 vs. 11.
我可以看到 11 来自哪里,但我只为预测查询提供了 2 个值 - 所以我看不到 3 来自哪里,或者为什么会出现问题。
任何帮助表示赞赏!