【问题标题】:How to solve for highest value (regression peak) in multivariate polynomial regression using R?如何使用 R 求解多元多项式回归中的最大值(回归峰值)?
【发布时间】:2018-07-15 07:57:31
【问题描述】:

我的 SQL Server 2017 数据库中有一个表,其中部分包含以下数据:

我的目的是为 19 列中的每一列创建多元多项式回归,其中 LikingOrder 是我的因变量,给定 RespID 的 19 列值中的每一列都是自变量。

最终结果应该是每个 RespID 的 C1 到 C19 列的最高回归值。最终结果应如下所示:

我已阅读有关 polym 并尝试在以下脚本中使用它:

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

--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
c.StudyID, c.RespID, c.LikingOrder, avg(C1) as C1, avg(C2) as C2, avg(C3) as 
C3, avg(C4) as C4, avg(C5) as C5, avg(C6) as C6, avg(C7) as C7, avg(C8) as 
C8, avg(C9) as C9, avg(C10) as C10,
avg(C11) as C11, avg(C12) as C12, avg(C13) as C13, avg(C14) as C14, avg(C15) 
as C15, avg(C16) as C16, avg(C17) as C17, avg(isnull(C18,0)) as C18, avg(C19) 
as C19
from ClosedStudyResponses c
where c.StudyID = @StudyID
group by StudyID, RespID, LikingOrder
order by RespID 

--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 RespondentID and 
LikingOrder 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) 

    res1 <- lm(df$LikingOrder ~ polym(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, degree = 1, raw = TRUE)) 
    res <- data.frame(res1)

'
, @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 ((RespID int, res varchar(max)));
END;

上述存储过程在提供有效的 StudyID 时会出现以下错误:

Error in model.frame.default(formula = df$LikingOrder ~ polym(df$c1, df$c2,  
: 
variable lengths differ (found for 'polym(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, degree = 1, raw = TRUE)')
Calls: source ... lm -> eval -> eval -> <Anonymous> -> model.frame.default
In addition: There were 19 warnings (use warnings() to see them)

这是对 polym 的正确使用吗?如果不是,我如何实现计算 19 个独立回归的目标?最后,我如何以编程方式确定每个回归的最大值?

【问题讨论】:

  • 我对您的要求有点困惑,如果您创建一个最小的可验证示例会有所帮助。这里的答案可能会有所帮助stats.stackexchange.com/questions/25975/…
  • 我认为你正在做的是运行一个包含所有变量的模型,而不是运行许多单独的模型,每个模型都有一个变量。
  • @Elin 感谢您的意见。为了确认,您建议为 C1 到 C19 的每一列创建 19 个单独的模型?这会像 res1

标签: r lm


【解决方案1】:

根据cmets中的提问和讨论,assumptions发的有:

  • RespID: 是 categorical parameter,不用于模型拟合
  • StudyID: 在样本数据中被忽略
  • LinkingOrder: 是因变量,即response(非分类)
  • C1 to C19: independent variables 是数字

  • Objective:识别linear fit到变量C1C19的回归系数

  • Note:未添加 polynomial fit,因为最终请求的表似乎没有列出迭代项。
  • ResourceISLR 中的第 3、5 章

创建示例数据框

StudyID <- rep(10001, 100)
RespID <- c(rep(117,25), rep(119,25), rep(120,25), rep(121,25))
LinkingOrder <- floor(runif(100, 1, 9))
df <- data.frame(StudyID, RespID, LinkingOrder)
# Create columns C1 to C19
for (i in c(1:19)){
  vari <- paste("C", i, sep = "")
  df[vari] <-  floor(runif(100, 0, 9))
}

# Convert RespID to categorical variable
df$RespID <- as.factor(RespID)

拟合 lm() 并以表格格式存储系数

注意:Intercept 项包含在表格中

# Fit lm() and store coefficients in a table
final_table <- data.frame()
for (respid in unique(df$RespID)){
  data <- df[df['RespID']==respid, ]
  data <- subset(data, select = -c(StudyID, RespID))

  lm.fit <- lm(LinkingOrder ~ ., data=data)

  # Save to table
  final_table <- rbind(final_table, data.frame(t(unlist(lm.fit$coefficients))))
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 2018-12-19
    • 2019-07-02
    • 2018-07-08
    相关资源
    最近更新 更多