-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBayesMultiMode.R
executable file
·58 lines (43 loc) · 1.32 KB
/
BayesMultiMode.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
### Trying out the BayesMultiMode package
#install.packages("BayesMultiMode")
library(BayesMultiMode)
# Load data
#y <- galaxy
#y <- BayesMultiMode::cyclone$max_wind
## Unimodal (data-gen normal)
# -> density() fit is far from that (wiggly and spurious mode(s))
#set.seed(25)
y <- rnorm(100)
## Bimodal
#set.seed(123)
# data1 <- rnorm(100, mean = 5, sd = 1)
# data2 <- rnorm(100, mean = 10, sd = 1)
# y <- c(data1, data2)
## Exponential
# -> Amoroso also looks promising here
# lambda <- 0.5 #rate
# n <- 100
# y <- rexp(n, rate = lambda)
## Normal
y <- rnorm(100)
# Estimate univariate mixture with unknown nr of components
set.seed(123) #seed for reproducibility
mix_mcmc <- bayes_fit(data = y,
K = 10,
dist = "normal",
nb_iter = 2000, #default
burnin = 1000) #default
# Get mcmc draws (excl burnin)
mix_mcmc$mcmc
summary(mix_mcmc)
# Plot estimated density for 100s draws
plot(mix_mcmc)
# Detect modes with fixed-point algorithm (since we estimated Gaussian mixture)
mode_mcmc <- bayes_mode(mix_mcmc)
summary(mode_mcmc)
# Get probability that density is unimodal
(p_unimodal <- mode_mcmc$p1) #probability of the density being unimodal
# Plot various NP and P methods
# res <- estimate_methods(y)
#broom.mixed
#tidy()