我用LOESS平滑器在散点图上跟踪this tutorial,但我希望能够将二阶导数应用于LOESS平滑线,以检查它在哪里达到最大值,这样我就可以分辨出有多少簇是最优的,就好像它是k均值的肘部一样。
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代表平滑的线,而不是实际的数字,这样我就知道要执行多少个主题。
发布于 2020-03-31 09:24:33
一种方法是将黄土拟合在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")

https://stackoverflow.com/questions/60941213
复制相似问题