【发布时间】: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 行和其中的启动功能,只是从另一个线程中改编而来。
现在我的问题和目标:
-
是否有一个包/函数可以或多或少地准确估计哪些线函数最适合这些点?还是我必须尝试各种函数公式,看看哪个最适合?基于
method = "loess"的“趋势线”看起来不错,但我不知道它是根据什么计算得出的。 -
为什么我通过
stat_smooth()应用的线取决于数据中的因子水平,而不是简单地依赖于所有点? -
为什么“趋势线”的虚线图例图标看起来如此糟糕? (我怎样才能改变这个?)
-
如果我随时在这里有一条拟合的非线性回归线,我如何计算它的 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