非线性回归线与R²;在ggplot2中

非线性回归线与R²;在ggplot2中,r,ggplot2,regression,nonlinear-functions,R,Ggplot2,Regression,Nonlinear Functions,我有以下数据: 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.5

我有以下数据:

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"))
g1)也许我误解了de问题,但我认为您要求的是一种合理的、半自动的方法来估计NLS方法的最佳起点,因为
黄土
方法不能为您提供一个未来可以使用的模型表达式

如果是这样的话,那我就去吧。在您的等式中,
a
需要相对接近
反射率的预期值,当
喷洒次数=0
时,
b
应该给出
反射率随着
喷洒次数的减少而减少的想法,因此高斯-牛顿算法可以很好地完成它的工作。
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)对于这些小问题,我不能给出一个直接的答案,或者我不太理解它们。绘图中的线条是基于因素
图例的级别,当您将其用作美学,而不是其他方面。

1)传递给
nls
的函数应根据数据背后的科学进行选择<代码>黄土
是一种更平滑的拟合,即非参数拟合。2) 因为您映射了
color=Legend
。3) 你说的“坏”是什么意思?4) 1)好的,所以没有“功能”或工具可以为我做到这一点?对于Excel,您可以使用它作为示例。2) 这是有道理的。如果我删除它,我的代码将不再工作,我会收到一条奇怪的错误消息=/3)我的意思是图标的厚度看起来不一致,并且有一条大线和一个小点。希望/期望2个相等的破折号作为符号?4) 谢谢大家!!1) 我的意思是你不应该使用这样的工具。