作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以让geom_smooth产生单调递减函数?
第一个例子看起来是单调递减的:
library(tidyverse)
df <- structure(list(x = c(-55, 11, 19, 123, 133, 123, 123, 2, 86,
84, 179, 179, 179, 179, 25, 85, 84, 179, 179, 179, 179, 25, 86,
84, 179, 179, 179, 179, 25, 86, 84, 179, 179, 179, 179, 25, 86,
70, 123, 123, 123, 123, 0, -45, -45, -17, -17, -17, -17, -63,
48, 40, 67, 67, 67, 67, -25, 11, 10, 67, 67, 67, 67, -25, 11,
10, 67, 67, 67, 67, -25, 11), y = c(126, -29, -37, -63, -76,
-70, -58, 23, -17, -26, -74, -72, -70, -73, 6, -24, -10, -54,
-67, -59, -59, 27, -37, -12, -51, -69, -61, -58, 52, -52, -25,
-46, -64, -54, -55, 41, -11, -22, -48, -63, -57, -56, 34, 17,
56, -26, -13, -16, -25, 99, -39, -16, -54, -74, -52, -60, 9,
-32, -17, -62, -66, -50, -65, 60, -34, -24, -62, -76, -62, -58,
27, -36)), row.names = c(NA, -72L), class = "data.frame")
ggplot(df) + geom_point(aes(x, y)) + geom_smooth(aes(x, y))
第二个例子看起来并不单调:
df <- structure(list(x = c(33, -14, -14, -15, -10, -33, 2, 28, -33,
-33, -33, -33, -48, -22, 0, 33, 33, 33, 33, 3, 37, 75, 17, 17,
17, 17, 8, 95, 151, 67, 67, 67, 67, 31, 95, 151, 67, 67, 67,
67, 31, 95, 151, 67, 67, 67, 67, 31, 95, 151, 67, 67, 67, 67,
31, 95, 151, 67, 67, 67, 67, 31, 95, 139, 50, 50, 50, 50, 16,
56, 101, 33), y = c(-50, 75, 77, 137, 36, 97, -42, -67, 147,
163, 176, 132, 384, 100, 65, -17, -53, -11, -49, -48, -77, -87,
-25, -23, -11, 4, -45, -54, -81, -36, -19, 3, -26, -6, -68, -74,
-11, -21, 32, -28, -19, -41, -74, -36, -33, 47, -4, -35, -52,
-69, -8, 47, 0, -45, 26, -48, -71, 19, 14, 18, -40, -71, -44,
-61, 19, 5, -16, 15, 29, -48, -72, 0)), row.names = c(NA, -72L
), class = c("tbl_df", "tbl", "data.frame"))
ggplot(df) + geom_point(aes(x, y)) + geom_smooth(aes(x, y))
您可以看到函数下降,然后在 x = 25 到 65
之间上升,然后再次下降。这不好 - 函数永远不能随着 x 的增加而增加。
我也尝试过使用 nls()
和单调递减函数,例如 y ~ 1/x
或 y ~ exp(1/x)
但由于我有数以千计的数据集,所以未能找到自动查找起始值的有效方法。 geom_smooth 似乎在许多情况下都能很好地工作,除了第二个示例中的凹凸情况。
最佳答案
如果你只是想要一个漂亮的曲线,那么你可以使用这个:
library(tidyverse)
df <- structure(list(x = c(33, -14, -14, -15, -10, -33, 2, 28, -33,
-33, -33, -33, -48, -22, 0, 33, 33, 33, 33, 3, 37, 75, 17, 17,
17, 17, 8, 95, 151, 67, 67, 67, 67, 31, 95, 151, 67, 67, 67,
67, 31, 95, 151, 67, 67, 67, 67, 31, 95, 151, 67, 67, 67, 67,
31, 95, 151, 67, 67, 67, 67, 31, 95, 139, 50, 50, 50, 50, 16,
56, 101, 33), y = c(-50, 75, 77, 137, 36, 97, -42, -67, 147,
163, 176, 132, 384, 100, 65, -17, -53, -11, -49, -48, -77, -87,
-25, -23, -11, 4, -45, -54, -81, -36, -19, 3, -26, -6, -68, -74,
-11, -21, 32, -28, -19, -41, -74, -36, -33, 47, -4, -35, -52,
-69, -8, 47, 0, -45, 26, -48, -71, 19, 14, 18, -40, -71, -44,
-61, 19, 5, -16, 15, 29, -48, -72, 0)), row.names = c(NA, -72L
), class = c("tbl_df", "tbl", "data.frame"))
plot = ggplot(df) +
geom_point(aes(x, y)) +
geom_smooth(aes(x, y),
method = "lm",
formula = y ~ log(x-min(df$x)-1),
se = FALSE)
print(plot)
因为你有负值,我只是以一种笨拙的方式强加了一条对数回归线,但它至少会出现一条漂亮的曲线......
关于R ggplot2 geom_smooth - 单调平滑函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51788043/
我正在寻找一种快速方法来使 pandas 数据帧在 x 中单调。 我当前的解决方案如下: def make_monotonic(df, cols=None): """make df monot
CLOCK_REALTIME 的一个问题是它不是单调的,如果发生 NTP 同步,时间可能会倒退。 像下面这样的事情让它变得单调是否安全? struct timespec GetMonotonicTim
我是一名优秀的程序员,十分优秀!