|
| 1 | +# Rsq |
| 2 | +rsq <- function(output, x, par = "p") { |
| 3 | + |
| 4 | + # Population means |
| 5 | + pop <- extract_long(fit, par, id = "pop") %>% |
| 6 | + spread(par, val) %>% |
| 7 | + rename_at(vars(one_of(par)), ~"mu_pop") |
| 8 | + |
| 9 | + # Plot level effects |
| 10 | + plt <- extract_long(fit, pars = "u", id = "plt") %>% |
| 11 | + spread(par, val) |
| 12 | + |
| 13 | + # Treatment effects |
| 14 | + trt <- extract_long(fit, pars = "beta", id = "ref") %>% |
| 15 | + spread(par, val) %>% |
| 16 | + mutate(trt = as.numeric(ref) + max(x$grp)) |
| 17 | + |
| 18 | + left_join(x, pop) %>% |
| 19 | + left_join(plt) %>% |
| 20 | + left_join(trt) %>% |
| 21 | + mutate(beta = if_else(is.na(beta), 1, beta), |
| 22 | + mu_trt = mu_pop * beta, |
| 23 | + mu_plt = mu_pop * u, |
| 24 | + mu_all = mu_trt * u) %>% |
| 25 | + select(sample, abun_std, matches("mu")) %>% |
| 26 | + gather(level, pred, -sample, -abun_std) %>% |
| 27 | + mutate(e = abun_std - pred) %>% |
| 28 | + group_by(level, sample) %>% |
| 29 | + summarise(rsq = var(pred) / (var(pred) + var(e))) %>% |
| 30 | + quantiles("rsq") |
| 31 | +} |
| 32 | + |
| 33 | +diagnostics <- function(output) { |
| 34 | + diagnostics <- summary(output$fit)$summary %>% |
| 35 | + as.data.frame() %>% |
| 36 | + rownames_to_column("par") %>% |
| 37 | + select(par, Rhat, n_eff) |
| 38 | + |
| 39 | + message("Worst sampled parameters") |
| 40 | + print(summarise(diagnostics, |
| 41 | + r_hat = max(Rhat), |
| 42 | + n_eff = min(n_eff))) |
| 43 | + |
| 44 | + posterior_summary(diagnostics) |
| 45 | +} |
| 46 | + |
| 47 | +extract_long <- function(fit, pars, id = NA) { |
| 48 | + divergent <- get_sampler_params(fit, inc_warmup = F) %>% |
| 49 | + map_df(., as.data.frame) %>% |
| 50 | + rename_all(~ gsub("_", "", .)) %>% |
| 51 | + select(divergent) %>% |
| 52 | + mutate_all(~ if_else(. == 1, T, F)) %>% |
| 53 | + rowid_to_column("sample") |
| 54 | + |
| 55 | + samples <- extract(fit, pars = pars, |
| 56 | + permuted = F, |
| 57 | + inc_warmup = F) %>% |
| 58 | + as.data.frame() %>% |
| 59 | + rowid_to_column("iteration") %>% |
| 60 | + gather(chain, val, -iteration) %>% |
| 61 | + separate(chain, c("chain", "par"), sep = "\\.") %>% |
| 62 | + spread(par, val) %>% |
| 63 | + rowid_to_column("sample") %>% |
| 64 | + gather(par, val, -chain, -iteration, -sample) %>% |
| 65 | + left_join(divergent) |
| 66 | + |
| 67 | + if(!is.na(id)) { |
| 68 | + samples <- separate(samples, par, c("par", id), |
| 69 | + sep = "\\[|\\]", extra = "drop") %>% |
| 70 | + mutate_at(vars(matches(id)), as.numeric) |
| 71 | + } |
| 72 | + |
| 73 | + samples <- spread(samples, par, val) |
| 74 | + return(samples) |
| 75 | +} |
| 76 | + |
| 77 | +quantiles <- function(df, par) { |
| 78 | + summarise_at(df, vars(one_of(par)), |
| 79 | + .funs = list(mean = ~ mean(.), |
| 80 | + low = ~ quantile(., 0.025), |
| 81 | + high = ~ quantile(., 0.975))) |
| 82 | +} |
| 83 | + |
| 84 | +posterior <- function(output) { |
| 85 | + |
| 86 | + x <- get_x(get_y(output$fields, quietly = T)) |
| 87 | + |
| 88 | + message("Loading model") |
| 89 | + model <- output$model |
| 90 | + |
| 91 | + expose_stan_functions(output$model_file) |
| 92 | + |
| 93 | + pars <- switch(model, |
| 94 | + f1 = c("p"), |
| 95 | + f1b = c("p"), |
| 96 | + f2 = c("p0", "pK"), |
| 97 | + f2b = c("p0", "pK"), |
| 98 | + f3 = c("p0", "pK", "tK", "tmax"), |
| 99 | + f3b = c("p0", "pK", "tK", "tmax")) |
| 100 | + |
| 101 | + pop <- extract_long(output$fit, pars = pars, id = "pop") %>% |
| 102 | + left_join(get_p(x)) |
| 103 | + |
| 104 | + # Plot level effects |
| 105 | + plt <- extract_long(output$fit, pars = "u", id = "plt") %>% |
| 106 | + left_join(get_u(x)) |
| 107 | + |
| 108 | + # Treatment effects |
| 109 | + trt <- extract_long(output$fit, pars = "beta", id = "ref") %>% |
| 110 | + mutate(trt = as.numeric(ref) + max(x$grp)) %>% |
| 111 | + left_join(get_g(x)) |
| 112 | + |
| 113 | + # Time steps |
| 114 | + ts <- output$data_list$T |
| 115 | + |
| 116 | + post <- left_join(pop, plt) %>% |
| 117 | + left_join(trt) %>% |
| 118 | + left_join(x) |
| 119 | + |
| 120 | + rm(list = c("output", "pop", "plt", "trt")) |
| 121 | + gc() |
| 122 | + |
| 123 | + if (grepl("f2", model)) { |
| 124 | + growth <- extract_long(output$fit, pars = "rK", id = "grp") %>% |
| 125 | + left_join(get_p(x)) |
| 126 | + |
| 127 | + post = left_join(post, growth) |
| 128 | + |
| 129 | + } else if (grepl("f3", model)) { |
| 130 | + pred = mutate(pop, pred = beta_curve(p0, pK, tK, tmax, t, n())) |
| 131 | + } |
| 132 | + |
| 133 | + return(pred) |
| 134 | +} |
0 commit comments