如何采样看起来像双变量月亮的数据?

时间:2019-05-17 17:44:54

标签: r

enter image description here

这里有300个双变量月亮形状。我想通过R复制这些数据,但是没有找到合适的工具。 R包“ clusterSim”中有一个名为“ shapes.two.moon”的函数,但我发现它会生成弓形而不是月亮:

enter image description here

这里的区别是月亮有两个尖锐的末端,而弓则保持相同的半径。

非常感谢任何对月球情节了解的人!

2 个答案:

答案 0 :(得分:0)

使用基于密度的聚类算法。我用DBScan算法来做下面的例子。

  • 由于缺乏数据可用性或可能调整可复制示例的原因,我使用了其他数据。
library(dbscan) # for data
library(fpc)
library(dplyr)
data("moons")
plot(moons, pch=20)

enter image description here

df <- moons %>% 
  dplyr::filter(Y < 1)
plot(df, pch=20)

enter image description here

set.seed(1)
db <- fpc::dbscan(df, eps = 0.4, MinPts = 2)
plot(db, df, main = "DBSCAN", frame = FALSE)

enter image description here

答案 1 :(得分:0)

library(dplyr); library(ggplot2)

moon_maker <- function(n = 1000, noise = 0.1,
                       x_center = 0, y_center = 0, radius = 1) {
  moon <- tibble(
    i = 1:n,
    x = x_center + radius * cos(pi * i/n) + rnorm(n, 0, sd = noise * radius),
    y = y_center + radius * sin(pi * i/n) + rnorm(n, 0, sd = noise * radius),
  )
  moon
}

ggplot(data = moon_maker()) +
  geom_point(color = "red", aes(x,y)) +
  geom_point(data = moon_maker(x_center = 0.75), aes(x, -y), color = "blue" )

enter image description here