-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a function to plot sizes over time in parallel with the DE plot…
… function. Changed wording in README to be more accessible.
- Loading branch information
1 parent
416c72b
commit 466f088
Showing
6 changed files
with
119 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#' Plot estimated and observed values over time for a chosen number of individuals based | ||
#' on posterior estimates. Structured to take in the measurement_data tibble constructed by | ||
#' the hmde_extract_estimates function. | ||
#' | ||
#' @param model model name character string | ||
#' @param measurement_data tibble with estimated measurements | ||
#' @param ind_id_vec vector with list of ind_id values | ||
#' @param n_ind_to_plot integer giving number of individuals to plot if not speciried | ||
#' @param xlab character string for replacement x axis label | ||
#' @param ylab character string for replacement y axis label | ||
#' @param title character string for replacement plot title | ||
#' @param colour character string for replacement line colour | ||
#' @param alpha real number for replacement alpha value | ||
#' | ||
#' @return ggplot object | ||
#' @export | ||
#' @import ggplot2 | ||
#' @import dplyr | ||
|
||
hmde_plot_obs_est_inds <- function(ind_id_vec = NULL, | ||
n_ind_to_plot = NULL, | ||
measurement_data = NULL, | ||
xlab = "Time", | ||
ylab = "Y(t)", | ||
title = NULL){ | ||
if(is.null(measurement_data)){ | ||
stop("Measurement data not provided.") | ||
} | ||
|
||
if(!is.null(ind_id_vec)){ | ||
for(i in ind_id_vec){ #Error if ID nujmber not in data. | ||
if(!i %in% measurement_data$ind_id){ | ||
stop(paste0("Ind ID values not recognised: ", i)) | ||
} | ||
} | ||
|
||
plot_data <- measurement_data %>% | ||
filter(ind_id %in% ind_id_vec) | ||
|
||
} else if(is.null(n_ind_to_plot)){ #Error if no info for which inds to plot | ||
stop("Neither ind. ID values nor a number of individuals provided.") | ||
|
||
} else if(length(unique(measurement_data$ind_id)) < n_ind_to_plot){ | ||
stop("Number of individuals to plot larger than sample size, please provide a smaller number.") | ||
|
||
} else { | ||
sample_ids <- sample(unique(measurement_data$ind_id), size=n_ind_to_plot) | ||
plot_data <- measurement_data %>% | ||
filter(ind_id %in% sample_ids) | ||
} | ||
|
||
#Generate plot | ||
plot <- hmde_ggplot_obs_est_inds(plot_data = plot_data, | ||
xlab = xlab, | ||
ylab = ylab, | ||
title = title) | ||
|
||
return(plot) | ||
} | ||
|
||
#' Produce plot for plot_obs_est_inds | ||
#' @keywords internal | ||
#' @noRd | ||
hmde_ggplot_obs_est_inds <- function(plot_data, | ||
xlab, | ||
ylab, | ||
title){ | ||
plot <- ggplot(data=plot_data, aes(group = ind_id)) + | ||
geom_point(aes(x = time, y=y_obs, colour = as.factor(ind_id)), | ||
shape = 1) + | ||
geom_line(aes(x = time, y=y_obs, colour = as.factor(ind_id)), | ||
linetype = "dashed") + | ||
geom_point(aes(x = time, y=y_hat, colour = as.factor(ind_id)), | ||
shape = 2) + | ||
geom_line(aes(x = time, y=y_hat, colour = as.factor(ind_id)), | ||
linetype = "solid") + | ||
labs(x=xlab, y=ylab, colour="Ind. ID") + | ||
theme_classic() | ||
|
||
return(plot) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
test_that("Execution and output: plot_obs_est_inds function", { | ||
plot <- hmde_plot_obs_est_inds(n_ind_to_plot = 2, | ||
measurement_data = Tree_Size_Ests$measurement_data) | ||
|
||
expect_named(plot) | ||
|
||
expect_visible(plot) | ||
|
||
expect_type(plot, "list") | ||
}) | ||
|
||
|
||
test_that("Execution and output: bad input", { | ||
expect_error( | ||
hmde_plot_obs_est_inds(measurement_data = Tree_Size_Ests$measurement_data) | ||
) | ||
|
||
expect_error( | ||
hmde_plot_obs_est_inds() | ||
) | ||
|
||
expect_error( | ||
hmde_plot_obs_est_inds(individual_data = Tree_Size_Ests$measurement_data) | ||
) | ||
|
||
expect_error( | ||
hmde_plot_obs_est_inds(n_ind_to_plot = 10^3, | ||
measurement_data = Tree_Size_Ests$measurement_data) | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters