从第三次观察开始在 R 中进行黄土曲线拟合
Loess curve fitting Plotly in R starting in third observation
我正在关注 this tutorial 使用 LOESS 平滑器的散点图,但我希望能够将二阶导数应用于 LOESS 平滑线以检查它达到最大值的位置,以便我可以判断有多少集群是最佳的,就好像它是 k-means 的肘部一样。
perplexi <- structure(list(Perplexity = c(NA, NA, 660, 596, 552, 480, 464,
415, 399, 370, 349, 340, 327, 314, 288), Clusters = c(1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)), class = "data.frame", row.names = c(NA,
-15L))
library(plotly)
p <- plot_ly(perplexi[3:15,],
x = ~Clusters,
color = I("black")) %>%
add_markers(y = ~Perplexity) %>%
add_lines(y = ~fitted(loess(Perplexity ~ Clusters)),
line = list(color = 'lightblue'),
name = "Loess Smoother",
showlegend = F) %>%
layout(xaxis = list(title = 'Clusters'),
yaxis = list(title = 'Perplexity')) %>%
add_trace(y = ~Perplexity,
name = 'Perplexity',
mode = 'markers',
showlegend = F)
p
d1 <- diff(perplex); k <- which.max(abs(diff(d1) / diff(perplex[-1])))
有人可以指出下一步该怎么做吗?我希望 k 代表平滑线而不是实际数字,所以我知道要执行多少主题。
一种方法是在 plotly 之外拟合黄土,然后取导数。
loess.result <-loess.smooth(perplexi$Clusters, y=perplexi$Perplexity, evaluation = 20)
slopes <- diff(loess.result$x)/diff(loess.result$y)
plot_ly(perplexi[3:15,],
x = ~Clusters,
color = I("black")) %>%
add_markers(y = ~Perplexity) %>%
add_lines(y = ~fitted(loess(Perplexity ~ Clusters)),
line = list(color = 'lightblue'),
name = "Loess Smoother") %>%
layout(xaxis = list(title = 'Clusters'),
yaxis = list(title = 'Perplexity')) %>%
add_trace(y = ~Perplexity,
name = 'Perplexity',
mode = 'markers',
showlegend = F) %>%
add_trace(x = loess.result$x[-1], y = slopes * -10000, mode = "line", name = "Loess First Derivative")
我正在关注 this tutorial 使用 LOESS 平滑器的散点图,但我希望能够将二阶导数应用于 LOESS 平滑线以检查它达到最大值的位置,以便我可以判断有多少集群是最佳的,就好像它是 k-means 的肘部一样。
perplexi <- structure(list(Perplexity = c(NA, NA, 660, 596, 552, 480, 464,
415, 399, 370, 349, 340, 327, 314, 288), Clusters = c(1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)), class = "data.frame", row.names = c(NA,
-15L))
library(plotly)
p <- plot_ly(perplexi[3:15,],
x = ~Clusters,
color = I("black")) %>%
add_markers(y = ~Perplexity) %>%
add_lines(y = ~fitted(loess(Perplexity ~ Clusters)),
line = list(color = 'lightblue'),
name = "Loess Smoother",
showlegend = F) %>%
layout(xaxis = list(title = 'Clusters'),
yaxis = list(title = 'Perplexity')) %>%
add_trace(y = ~Perplexity,
name = 'Perplexity',
mode = 'markers',
showlegend = F)
p
d1 <- diff(perplex); k <- which.max(abs(diff(d1) / diff(perplex[-1])))
有人可以指出下一步该怎么做吗?我希望 k 代表平滑线而不是实际数字,所以我知道要执行多少主题。
一种方法是在 plotly 之外拟合黄土,然后取导数。
loess.result <-loess.smooth(perplexi$Clusters, y=perplexi$Perplexity, evaluation = 20)
slopes <- diff(loess.result$x)/diff(loess.result$y)
plot_ly(perplexi[3:15,],
x = ~Clusters,
color = I("black")) %>%
add_markers(y = ~Perplexity) %>%
add_lines(y = ~fitted(loess(Perplexity ~ Clusters)),
line = list(color = 'lightblue'),
name = "Loess Smoother") %>%
layout(xaxis = list(title = 'Clusters'),
yaxis = list(title = 'Perplexity')) %>%
add_trace(y = ~Perplexity,
name = 'Perplexity',
mode = 'markers',
showlegend = F) %>%
add_trace(x = loess.result$x[-1], y = slopes * -10000, mode = "line", name = "Loess First Derivative")