inst/tree_sampling/tree-sample-test4.R

## SKG
## 3/10/2020
## Calculating likelhood  and making trees
##


devtools::load_all()

## Generate partition list
#part_list <- generate_part_list(n = 25)
#g_weight_list <- get_weight_list(part_list)


K <- 100
inf_params <- c("beta_0" = -1.8, "beta_1" = 2)
max_size <- 50
smear_pos_prob <- .5

forest <- simulate_flip_til_failure(K = K,
                                    inf_params = inf_params,
                                    smear_pos_prob = smear_pos_prob,
                                    max_size = max_size)

bark <- summarize_clusters(forest)



n_trials <- 10000
cluster_summary <- bark
t <- proc.time()[3]
sampled_trees <- sample_uniform_trees(n_vec = cluster_summary$n,
                                      x_vec = cluster_summary$n_pos,
                                      B = n_trials)
print(proc.time()[3] - t)
par <- c("beta_0" = -3, "beta_1" = 2)


best_pars <- optim(par, fn = loglike_fast,
                   data = cluster_summary,
                   sampled_trees_summary = sampled_trees,
                   return_neg = TRUE,
                   hessian = TRUE)
best_pars$par
sqrt(diag(solve(best_pars$hessian)))


##############################

bad <- rep(0, nrow(cluster_summary))
for(ii in 1:nrow(cluster_summary)){
  x <- cluster_summary$n[ii]
  y <- cluster_summary$n_pos[ii]
  ################################
  out <- max_g %>% dplyr::filter(cluster_size == x, cluster_pos == y)

  max_g2 <- sampled_trees %>% dplyr::group_by(cluster_size, cluster_pos, cluster_id) %>%
    dplyr::summarize(max_g = max(gen))

  out2 <- max_g2 %>% dplyr::filter(cluster_size == x, cluster_pos == y)

  #par(mfrow = c(1,2))
  #hist(out2$max_g)
  #hist(out$max_g)
  dog <- ks.test(out$max_g, out2$max_g)
  print(dog)
  if(dog$p.value < .05){
    bad[ii] <- 1
  }
}
## TODO: Need to sample trees of given n, n_pos and compare distributions of i_pos, g, etc.










fast2$par
sqrt(diag(solve(fast2$hessian)))
skgallagher/TBornotTB documentation built on April 21, 2020, 1:19 p.m.