【问题标题】:rgp (R genetic programming) package - not able to do regressionrgp(R 基因编程)包 - 无法进行回归
【发布时间】:2016-12-04 12:14:51
【问题描述】:

我正在尝试使用 R 基因包 (rgp) 使用此处使用的技术进行非线性回归:Fitting a curve to specific data(参见第二种方法)。我正在使用 R 包 drc 获取 heartrate 数据:

library(drc)

head(heartrate)
#  pressure   rate
#1    50.85 348.76
#2    54.92 344.45
#3    59.23 343.05
#4    61.91 332.92
#5    65.22 315.31
#6    67.79 313.50

library(rgp)

res <- symbolicRegression(rate ~ pressure, data=heartrate)

(symbreg <- res$population[[which.min(sapply(res$population, res$fitnessFunction))]])
#function (pressure) 
#pressure + (pressure/0.853106872646055 + pressure)

ggplot() + 
    geom_point(data=heartrate, aes(pressure,rate), size = 3) +
    geom_line(data=data.frame(symbx=heartrate$pressure, 
                              symby=sapply(heartrate$pressure, symbreg)), 
              aes(symbx, symby), colour = "red")

但是,我得到的回归线显然是不正确的。数据点的分布表明随着压力的增加与速率降低呈曲线关系(反向关联)。但是,生成的回归线是线性的并且方向错误。

错误在哪里?

编辑:

按照@cuttlefish44 在 cmets 中的建议使用增加的步骤:

res = symbolicRegression(rate ~ pressure, data = heartrate, stopCondition = makeStepsStopCondition(45000))

(symbreg <- res$population[[which.min(sapply(res$population, res$fitnessFunction))]])
#function (pressure) 
#exp(exp(exp(cos(cos(-9.23878724686801/pressure)))))

完成需要 8 分钟。剧情是:

回归线的方向比上面的好(!),但这表明它需要很长时间才能到达明显的方向。与@cuttlefish44 得到的函数的回归线也很相似,不是很合适。

【问题讨论】:

  • 也许stopCondition、makeTimeStopCondition(5)的默认值对你来说太短了? symbolicRegression(rate ~ pressure, data = heartrate, stopCondition = makeStepsStopCondition(45000))给了我symbreg; function (pressure) exp(exp(exp(exp(tan(6.91310722380877/pressure - sin(0.932394750416279)) * cos(exp(sin(-9.12634917534888)))))))
  • 我已经在上面添加了这个有问题的结果。

标签: r regression rgp


【解决方案1】:

您可能已经阅读过这篇文章,但我认为您的答案隐藏在 Oliver Flasch 所写的 this introduction to RGP package 中的某个地方。

我对@9​​87654323@package 一无所知,但如果你只想要线性回归,为什么不使用基本包中的lm()function 呢?

至少您可以估计普通最小二乘回归的 β0 和 β1 参数:

速率 = β1*压力 + β0

     linear.model <- lm(rate ~ pressure, data=heartrate)

     ggplot(data=heartrate, aes(x=pressure,y=rate)) + 
         geom_point() + 
         geom_smooth(method="lm", col="red")

linear regression with ggplot2

您可以通过linear.model$coefficients访问系数

您仍然可以使用linear.model$fitted.values 操作预测值

您可以通过以下方式访问残差:linear.model$residuals

如果您想更准确地拟合曲线,线性模型可能不够用,您可以尝试glm,或多项式回归并选择具有 AIC 或 BIC 标准的最佳模型。

【讨论】:

  • 我不想要线性回归,这正是我使用 rgp 包的原因。我看过这个 rgp pdf 文件简​​介,但也推荐了非常相似的方法。为什么不使用 rgp 包并尝试获得正确的结果。
  • 对不起,我没有看你的个人资料,你是对的,你有没有看到所有警告表明你在 symbolicRegression 过程中生成了 NAN?
  • 这是一个非常好的观点。我之前没有看到 warnings() 。您应该将此放在答案中以及如何纠正此问题,而不是如何进行线性回归。
  • 我已编辑问题以澄清我正在尝试进行非线性回归。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-02
  • 2017-07-20
  • 1970-01-01
  • 2020-08-01
  • 2013-04-10
相关资源
最近更新 更多