SQL Server R Services中的多项式回归对象不是矩阵

时间:2018-07-03 16:55:12

标签: r tsql lm

我正尝试建立按StudyID,RespID和ProductNumber分组的c1到c19字段平均值的多项式回归。我创建了以下存储过程:

ALTER PROCEDURE [dbo].[spRegressionPeak]
@StudyID int
AS
BEGIN
Declare @sStudyID VARCHAR(50)
Set @sStudyID = CONVERT(VARCHAR(50),@StudyID)

--We are selecting the distinct StudyID, Productnumber, ResponseID and mean 
values 1 thorugh 6 from the CodeMeans table.  
--Note that spCodeMeans must be run before running this stored procedure to 
ensure response data exists in the CodeMeans table.
--We use IsNull values to pass zeroes where an average wasn't calculated os 
that the polynomial regression can be calculated.
DECLARE @inquery  AS NVARCHAR(MAX) = '
    Select Distinct 
        cm.StudyID, cm.ProductNumber, cr.RespID, 
        ISNULL(cm.c1_mean,0) as c1, ISNULL(cm.c2_mean,0) as c2, ISNULL(cm.c3_mean,0) as c3, ISNULL(cm.c4_mean, 0) as c4,
        ISNULL(cm.c5_mean,0) as c5, ISNULL(cm.c6_mean,0) as c6, ISNULL(cm.c7_mean,0) as c7, ISNULL(cm.c8_mean,0) as c8, 
        ISNULL(cm.c9_mean,0) as c9, ISNULL(cm.c10_mean,0) as c10, ISNULL(cm.c11_mean,0) as c11, ISNULL(cm.c12_mean,0) as c12,
        ISNULL(cm.c13_mean,0) as c13, ISNULL(cm.c14_mean,0) as c14, ISNULL(cm.c15_mean,0) as c15, ISNULL(cm.c16_mean,0) as c16,
        ISNULL(cm.c17_mean,0) as c17, ISNULL(cm.c18_mean,0) as c18, ISNULL(cm.c19_mean,0) as c19
        from CodeMeans cm
        inner join ClosedStudyResponses cr on cm.StudyID = cr.StudyID
        where 
        cm.StudyID = @StudyID
        '


--We are setting @inquery aka InputDataSet to be our initial dataset.  
--R Services requires that a data.frame be passed to any calculations being 
generated.  As such, df is simply data framing the @inquery data.
--The res object holds the polynomial regression results by ProductNumber for 
each of the averages in the @inquery resultset.
EXEC sp_execute_external_script @language = N'R'
, @script = N'
    studymeans <- InputDataSet

    df <- data.frame(studymeans)

    means <- c(df$c1, df$c2, df$c3, df$c4, df$c5, df$c6, df$c7, df$c8, df$c9, df$c10, df$c11, df$c12, df$c13, df$c14,
    df$c15, df$c16, df$c17, df$c18, df$c19)
    res <- lm(df$StudyID ~ df$ProductNumber ~ df$RespID ~ poly(means,2))

'
, @input_data_1 = @inquery
, @output_data_1_name = N'res'
, @params = N'@StudyID int'
,@StudyID = @StudyID 
--- Edit this line to handle the output data frame.
WITH RESULT SETS (([StudyID] int, [ProductNumber] int, [RespID] int, res 
varchar(max)));
END;

@inquery的输出如下所示: enter image description here

运行存储过程时,收到以下错误: enter image description here

如何解决错误并修改存储过程的R脚本部分以解决多项式回归?

0 个答案:

没有答案