Skip to content

Commit

Permalink
Solve issue #1
Browse files Browse the repository at this point in the history
  • Loading branch information
jovoni committed Apr 8, 2024
1 parent c051d8a commit 27589d6
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 13 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Depends:
R (>= 3.4.0)
Imports:
cli,
crayon,
data.table,
dplyr,
ggforce,
Expand All @@ -36,6 +37,6 @@ VignetteBuilder:
Additional_repositories: https://mc-stan.org/r-packages/
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
SystemRequirements: GNU make
LazyData: true
1 change: 1 addition & 0 deletions R/fit_task1.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fit <- function(
if (!(inherits(x, "bipod"))) stop("Input must be a bipod object")
if (!(growth_type %in% c("exponential", "logistic", "both"))) stop("growth_type must be one of 'exponential' and 'logistic'")
if (!(factor_size > 0)) stop("factor_size must be positive")
if (factor_size > min(x$counts$count)) stop(paste0("given the input, factor_size must be smaller or equal to ", min(x$counts$count)))
sampling_type <- if (variational) "variational inference" else "MCMC sampling"

if (growth_type == "both") {
Expand Down
2 changes: 2 additions & 0 deletions R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ init <- function(counts, sample, break_points = NULL) {
} else {
break_points <- check_break_points(d = counts, break_points = break_points)
bipod$counts$group <- bp_to_groups(counts, break_points)
min_group_numerosity <- bipod$counts$group %>% table() %>% min()
if (min_group_numerosity <= 1) { stop("With the given breakpoints some time windows contain less than 2 observations, which makes the inference not possible") }
}

bipod$metadata$breakpoints <- break_points
Expand Down
6 changes: 3 additions & 3 deletions R/print.bipod.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ print.bipod = function(x, ...) {
dplyr::tibble(
Parameter = p,
Mean = mean(draws),
Sd = sd(draws),
Sd = stats::sd(draws),
p05 = stats::quantile(draws, .05),
p50 = stats::quantile(draws, .50),
p95 = stats::quantile(draws, .95),
Expand Down Expand Up @@ -112,7 +112,7 @@ print.bipod = function(x, ...) {

par_tibble <- lapply(x$two_pop_fit$parameters, function(p) {
draws <- x$two_pop_fit$draws[,grepl(p, colnames(x$two_pop_fit$draws), fixed = T)] %>% as.vector() %>% unlist()
dplyr::tibble(Parameter = p, Mean = mean(draws), Sd = sd(draws), p05 = stats::quantile(draws, .05), p50 = stats::quantile(draws, .50), p95 = stats::quantile(draws, .95))
dplyr::tibble(Parameter = p, Mean = mean(draws), Sd = stats::sd(draws), p05 = stats::quantile(draws, .05), p50 = stats::quantile(draws, .50), p95 = stats::quantile(draws, .95))
}) %>% do.call("bind_rows", .)
print(par_tibble)
}
Expand Down Expand Up @@ -156,7 +156,7 @@ print.bipod = function(x, ...) {

par_tibble <- lapply(x$fit$parameters, function(p) {
draws <- x$fit$draws[,grepl(p, colnames(x$fit$draws), fixed = T)] %>% as.vector() %>% unlist()
dplyr::tibble(Parameter = p, Mean = mean(draws), Sd = sd(draws), p05 = stats::quantile(draws, .05), p50 = stats::quantile(draws, .50), p95 = stats::quantile(draws, .95))
dplyr::tibble(Parameter = p, Mean = mean(draws), Sd = stats::sd(draws), p05 = stats::quantile(draws, .05), p50 = stats::quantile(draws, .50), p95 = stats::quantile(draws, .95))
}) %>% do.call("bind_rows", .)
print(par_tibble)
}
Expand Down
14 changes: 7 additions & 7 deletions inst/cmdstan/infer_changepoints.stan
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

functions {

real mean_t (int n0, real t0, real t, real[] changing_times, real[] rho_array) {
real mean_t (int n0, real t0, real t, vector changing_times, vector rho_array) {
real res = n0;
int J = num_elements(changing_times);

Expand All @@ -27,20 +27,20 @@ data {
int<lower=1> S; // Number of steps
int<lower=1> G; // Number of wondows

int <lower=0> N[S]; // observations
real T[S]; // observations
array[S] int<lower=0> N; // observations
array[S] real T; // observations

real changing_times_prior[G - 1];
array[G-1] real changing_times_prior;
real dt;
}

parameters {
real rho[G];
real<lower=-dt, upper=dt> changing_times_unit[G-1];
vector[G] rho;
array[G-1] real<lower=-dt, upper=dt> changing_times_unit;
}

transformed parameters {
real changing_times[G-1];
vector[G-1] changing_times;

for (i in 2:G) {
changing_times[i-1] = changing_times_prior[i-1] + changing_times_unit[i-1];
Expand Down
2 changes: 1 addition & 1 deletion man/breakpoints_inference.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vignettes/a1_introduction.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ mouse_id <- 529
biPOD::init(
counts = xenografts %>% dplyr::filter(mouse == mouse_id) %>% dplyr::mutate(count = tumour_volume),
sample = mouse_id,
break_points = c(0, 5)
break_points = c(0, 20)
)
```
If you look at the modified counts you will see that the observations have been grouped

0 comments on commit 27589d6

Please sign in to comment.