diff --git a/DESCRIPTION b/DESCRIPTION index 4c8bd9a..3143a9d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,13 +12,14 @@ Encoding: UTF-8 LazyData: true RoxygenNote: 7.2.1 Imports: - brio, - dplyr, - magrittr, - robustbase (>= 0.93-6), - stringr, - tidyr, - tidyselect + brio, + dplyr, + magrittr, + robustbase (>= 0.93-6), + stats, + stringr, + tidyr, + tidyselect Suggests: testthat (>= 3.1.5) Config/testthat/edition: 3 diff --git a/NAMESPACE b/NAMESPACE index 20d5cfe..73d66fc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,4 +4,5 @@ export(find_outliers) export(parse_MEA_file) export(parse_designfile) export(treatment_ratio) +export(treatment_stats) import(magrittr) diff --git a/R/treatment_stats.R b/R/treatment_stats.R new file mode 100644 index 0000000..e26e6e9 --- /dev/null +++ b/R/treatment_stats.R @@ -0,0 +1,16 @@ +#' Generate statistics for a treatment dataframe +#' +#' @param ratio_df resulting dataframe of the `treatment_ratio` function +#' +#' @return dataframe with summary statistics +#' @export +treatment_stats <- function(ratio_df){ + + df <- ratio_df |> + dplyr::group_by(Metric_type, Parameter, Group) |> + dplyr::summarize(n_wells = dplyr::n(), + Ratio_avg = mean(Treatment_ratio, na.rm=T), + Ratio_stdv = stats::sd(Treatment_ratio), + Ratio_SEM = stats::sd(Treatment_ratio)/sqrt(n_wells)) + return(df) +} diff --git a/man/treatment_stats.Rd b/man/treatment_stats.Rd new file mode 100644 index 0000000..a915608 --- /dev/null +++ b/man/treatment_stats.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/treatment_stats.R +\name{treatment_stats} +\alias{treatment_stats} +\title{Generate statistics for a treatment dataframe} +\usage{ +treatment_stats(ratio_df) +} +\arguments{ +\item{ratio_df}{resulting dataframe of the `treatment_ratio` function} +} +\value{ +dataframe with summary statistics +} +\description{ +Generate statistics for a treatment dataframe +} diff --git a/tests/testthat/output_treatment_ratio.Rda b/tests/testthat/output_treatment_ratio.Rda new file mode 100644 index 0000000..bcd2080 Binary files /dev/null and b/tests/testthat/output_treatment_ratio.Rda differ diff --git a/tests/testthat/test-treatment_stats.R b/tests/testthat/test-treatment_stats.R new file mode 100644 index 0000000..a7da2e1 --- /dev/null +++ b/tests/testthat/test-treatment_stats.R @@ -0,0 +1,34 @@ +test_that("Summary is calculated correctly",{ + load("output_treatment_ratio.Rda") + + statsum <- treatment_stats(output_treatment_ratio) + expect_true("tbl_df" %in% class(statsum)) + expect_equal(nrow(statsum), 288) + + expected_cols <- c("Ratio_avg", + "Ratio_stdv", + "Ratio_SEM", + "n_wells", + "Group", + "Metric_type", + "Parameter") + for(cname in expected_cols){ + expect_true(cname %in% names(statsum), + info=paste("Variable:",cname)) + } + + # there are 8 wells per group in the example data + expect_true(mean(statsum$n_wells) == 8) + + # check some values + vals <- statsum[statsum$Parameter == "Mean Firing Rate (Hz)",]$Ratio_SEM + vals <- round(vals, digits = 8) + known_vals <- c(0.15674431, + 0.04957502, + 0.09320916, + 0.12364473, + 0.28510038, + 0.12974997) + expect_true(all(known_vals %in% vals)) + +})