pat*_*t-s 5 regression r nonlinear-functions ggplot2
我有以下数据:
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")
Run Code Online (Sandbox Code Playgroud)
这导致以下情节

使用以下代码
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"))
Run Code Online (Sandbox Code Playgroud)
注意:我并没有真正了解我的stat_smooth线路和其中的开始功能,只是从另一个线程中对其进行了改编。
现在我的问题和目标:
是否有一个包/函数可以或多或少地准确估计哪些线函数最适合这些点?或者我必须尝试各种函数公式,看看哪个最合适?基于的“趋势线”method = "loess"看起来不错,但我不知道它是根据什么计算的。
为什么我应用的直线stat_smooth()取决于数据中的因子水平,而不是简单地依赖所有点?
为什么“趋势线”的虚线图例图标看起来如此糟糕?(我怎样才能改变这个?)
如果我在任何时候都有拟合非线性回归线,我该如何计算 R²?(我知道 R² 在非线性关系上不是那么“好”,但无论如何我都想这样做)。summary(lm())仅适用于线性关系。是否有可能根据非线性回归线的公式计算 R²?
我知道有很多问题,也许其中一些问题与统计相关,而不是直接与 R 相关。在其他问题中找不到答案,所以如果这个问题有问题,请进行编辑。
谢谢你的帮助,帕特里克
1)也许我误解了问题,但我认为您所要求的是一种合理的半自动方法来估计 NLS 方法的最佳起点,因为该方法loess没有为您提供模型表达式将来可以使用。
如果是这样的话,我就去吧。在你的方程中,a需要相对接近 的预期值Reflectance,Number of Sprays = 0并且b应该给出 的下降情况Reflectance,以便Number of Sprays高斯-牛顿算法可以很好地完成它的工作。a和的值b不需要太大。您可以尝试以下操作:
fit = lm ( data = dat, Reflectance ~ Number.of.Sprays )
Run Code Online (Sandbox Code Playgroud)
然后,在您的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)
Run Code Online (Sandbox Code Playgroud)
有关 NLS 方法起始值的警告将会消失,并且会很好地收敛。
4)作为拟合正确性的衡量标准,我建议您计算观测值和预测值之间的相关性。请注意,当包含截距时,R2 只是观察到的结果和观察到的预测变量值之间的样本相关系数的平方。所以这应该适合你:
r2 = cor (dat$Reflectance, predict(fit))^2
Run Code Online (Sandbox Code Playgroud)
2,3)对于这些小问题,我无法直接回答,或者我不太理解。图中的线条是基于当Legend您将其用作美学时的因素水平,而不是其他情况。