sim_fmm_2d_conditional_collapsed.r (1454B)
1 require(ggplot2) 2 require(gridExtra) 3 require(reshape2) 4 5 source('fmm_multivariate_conditional_collapsed.r') 6 7 dimension = 2 8 9 config = list( 10 k = 3 11 , m = dimension 12 , a = 1 13 , l = rep(0, dimension) 14 , b = dimension 15 , w = diag(0.05, dimension) 16 , n = 100 17 ) 18 19 set.seed(222) 20 21 d = list( 22 t(replicate(250, rnorm(config$m, c(5, 5)))) 23 , t(replicate(250, rnorm(config$m, c(-5, -5)))) 24 , t(replicate(500, rnorm(config$m)))) 25 dn = lapply(d, function(j) { data.frame(x = j[,1], y = j[,2]) }) 26 m = melt(dn, id.vars = c('x', 'y')) 27 28 set.seed(222) 29 30 params = inverse_model( 31 config$n, config$k, as.matrix(m[, c('x', 'y')]) 32 , config$a 33 , config$l 34 , config$b, config$w 35 ) 36 37 early = data.frame(x = m$x, y = m$y, variable = params$z[1,]) 38 mid = data.frame(x = m$x, y = m$y, variable = params$z[round(config$n * 1 / 2),]) 39 late = data.frame(x = m$x, y = m$y, variable = params$z[config$n - 1,]) 40 41 p_early = 42 ggplot(early, aes(x, y, colour = factor(variable), fill = factor(variable))) + 43 geom_point(alpha = 0.5) 44 45 p_mid = 46 ggplot(mid, aes(x, y, colour = factor(variable), fill = factor(variable))) + 47 geom_point(alpha = 0.5) 48 49 p_late = 50 ggplot(late, aes(x, y, value, colour = factor(variable), fill = factor(variable))) + 51 geom_point(alpha = 0.5) 52 53 dl = melt(as.data.frame(params$ll)) 54 55 pl = ggplot(dl, aes(x = seq_along(value), y = value)) + 56 geom_line(colour = 'darkblue') 57 58 inferred_plots = grid.arrange(p_early, p_mid, p_late, ncol = 3)