【问题标题】:Non-linear regression line and R² in ggplot2ggplot2中的非线性回归线和R²
【发布时间】:2015-01-02 19:30:52
【问题描述】:

我有以下数据:

dput(dat)
structure(list(Band = c(1930, 1930, 1930, 1930, 1930, 1930, 1930, 
1930, 1930, 1930, 1930, 1930, 1930, 1930, 1930, 1930, 1930, 1930
), Reflectance = c(25.296494, 21.954657, 18.981184, 15.984661, 
14.381341, 12.485372, 10.592539, 8.51772, 7.601568, 7.075429, 
6.205453, 5.36646, 4.853167, 4.21576, 3.979639, 3.504217, 3.313851, 
2.288752), Number.of.Sprays = c(0, 1, 2, 3, 5, 6, 7, 9, 10, 11, 
14, 17, 19, 21, 27, 30, 36, 49), Legend = structure(c(4L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 5L
), .Label = c("1 x spray between each measurement", "2 x spray between each measurement", 
"3 x spray between each measurement", "Dry soil", "Wet soil"), class = "factor")), .Names =c("Band", 
"Reflectance", "Number.of.Sprays", "Legend"), row.names = c(NA, 
-18L), class = "data.frame")

结果如下图

使用以下代码

g <- ggplot(dat, aes(Number.of.Sprays, Reflectance, colour = Legend)) +
    geom_point (size = 3) +
    geom_smooth (aes(group = 1, colour = "Trendline"), method = "loess", size = 1, linetype = "dashed", se = FALSE) +
    stat_smooth(method = "nls", formula = "y ~ a*x^b", start = list(a = 1, b = 1), se = FALSE)+
    theme_bw (base_family = "Times") +
    labs (title = "Regression between Number of Sprays and Reflectance in Band 1930") +
    xlab ("Number of Sprays") +
    guides (colour = guide_legend (override.aes = list(linetype = c(rep("blank", 4), "dashed", "blank"), shape = c(rep(16, 4), NA, 16)))) +
    scale_colour_manual (values = c("cyan", "green2", "blue", "brown",  "red", "purple")) +
    theme (legend.title = element_text (size = 15), legend.justification = c(1,1),legend.position = c(1,1), legend.background = element_rect (colour = "black", fill = "white"))

注意:我并没有真正了解我的 stat_smooth 行和其中的启动功能,只是从另一个线程中改编而来。

现在我的问题和目标:

  1. 是否有一个包/函数可以或多或少地准确估计哪些线函数最适合这些点?还是我必须尝试各种函数公式,看看哪个最适合?基于method = "loess" 的“趋势线”看起来不错,但我不知道它是根据什么计算得出的。

  2. 为什么我通过stat_smooth() 应用的线取决于数据中的因子水平,而不是简单地依赖于所有点?

  3. 为什么“趋势线”的虚线图例图标看起来如此糟糕? (我怎样才能改变这个?)

  4. 如果我随时在这里有一条拟合的非线性回归线,我如何计算它的 R²? (我知道 R² 在非线性关系上并不是那么“好”,但我还是想这样做)。 summary(lm()) 仅用于线性关系。有没有可能根据非线性回归线的公式计算R²?

我知道这有很多问题,也许其中一些问题比直接与 R 更具统计相关性。在其他问题中找不到答案,所以如果这个问题有问题,请编辑。

感谢您的所有帮助, 帕特里克

【问题讨论】:

  • 1) 传递给nls 的函数应根据数据背后的科学依据进行选择。 loess 更平滑,即非参数拟合。 2) 因为你映射了colour = Legend。 3)“坏”是什么意思? 4)stat.ethz.ch/pipermail/r-help/2002-July/023461.html
  • 1) 好的,所以没有“功能”或工具可以为我做到这一点?对于 Excel,您可以使用 nutonian.com/products/eureqa 作为示例。 2)这是有道理的。如果我删除它,我的代码将不再工作,并且收到一条奇怪的错误消息 =/ 3) 我的意思是图标的粗细看起来不一致,并且有一条大线和一个小点。是否希望/期望 2 个相等的破折号作为符号? 4) 谢谢!
  • 1) 我的意思是你不应该使用这样的工具。

标签: r ggplot2 regression nonlinear-functions


【解决方案1】:

1)也许我错过了这个问题,但我认为你所要求的是一种合理和半自动的方式来估计 NLS 方法的最佳起点,而不是 loess 方法没有为您提供将来可以使用的模型表达式。

如果是这样的话,我去。在您的等式中,a 需要相对接近 Reflectance 的预期值,而 Number of Sprays = 0 和 b 应该可以了解 Reflectance 与 Number of Sprays 的下降情况,因此 Gauss-牛顿算法可以很好地完成它的工作。 a 和 b 的值不需要太接近。您可以尝试以下方法:

fit = lm ( data = dat, Reflectance ~ Number.of.Sprays )

然后,在您的 ggplot 电话中,我将您的 geom_smooth 声明替换为:

stat_smooth(method = "nls", formula = "y ~ a*x^b",  method.args = list(start=c(a=fit$coefficients[[1]], b=fit$coefficients[[2]])), se = FALSE)

关于 NLS 方法的起始值的警告将消失,并且收敛得很好。

4)作为衡量拟合的神性,我建议您计算观察值和预测值之间的相关性。请注意,当包含截距时,R2 只是观察到的结果与观察到的预测值之间的样本相关系数的平方。所以这应该适合你:

r2 =  cor (dat$Reflectance, predict(fit))^2

2,3) 至于这些小问题,我无法给出直接的答案,或者我不是很理解。当您将其用作美学时,情节中的线条基于因子 Legend 的水平,而不是其他情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 2012-01-12
    • 2016-08-01
    • 1970-01-01
    • 2016-11-07
    • 2022-01-11
    • 2016-07-09
    相关资源
    最近更新 更多