Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Package: peppwR
Title: Power Analysis For Phosphopeptide Abundance Hypothesis Test
Version: 0.0.0.9000
Version: 0.0.0.9001
Authors@R:
person("Dan", "MacLean", , "dan.maclean@tsl.ac.uk", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-1032-0887"))
Description: Estimate best fit distributions and do power analysis for hypothesis tests on phosphopeptide abundance data.
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
URL: https://github.com/danmaclean/peppwR
BugReports: https://github.com/danmaclean/peppwR/issues
Imports:
Expand Down
13 changes: 13 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# Generated by roxygen2: do not edit by hand

export(avail_dists)
export(d2n)
export(do_fits)
export(evaldist)
export(fake_leg)
export(fill_scale)
export(find_fits)
export(parse_fitdist)
export(parse_univariateML)
export(plot_best)
export(plot_failed)
export(single_fit)
export(squash_fits)
76 changes: 74 additions & 2 deletions R/fits.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@

#' Fit a Single Distribution
#'
#' @description This fits a specified distribution to the first column of a data frame.
#'
#' @param df The data frame containing the data to fit.
#' @param dist The distribution name or a list of distribution models to try fitting. One of "nbinom", "gamma", "snorm", "norm",
#' "invgamma", "invgauss", "lnorm", "lgamma", and "pareto"
#'
#'@return A list containing the fit result or the distribution name in case there is an error.
#'
#' @export
single_fit <- function(df, dist){
if (dist %in% c("nbinom")){
result <- tryCatch(
Expand All @@ -16,7 +26,15 @@ single_fit <- function(df, dist){

}


#' Available Distributions
#'
#' @description This provides a vector of available distributions for fitting.
#'
#' @return A character vector of available distribution names.
#'
#'
#'
#' @export
avail_dists <- function() {
c("gamma", "norm",
"snorm", "invgamma",
Expand All @@ -25,13 +43,35 @@ avail_dists <- function() {
"nbinom")
}


#' Distribution Name to Normalized Name
#'
#' @description This converts distribution tags to names that humans can read and understand.
#'
#' @param tag A character string representing the distribution tag.
#'
#' @return A character string with the human-readable distribution name.
#'
#'
#' @export
d2n <- function(tag){
v <- c("Gamma", "Normal", "Skew Normal", "InvGamma", "Inverse Gaussian",
"Lognormal", "Log Gamma", "Pareto", "Negative Binomial")
names(v) <- avail_dists()
v[tag]
}


#' Fit Multiple Distributions
#'
#' @description This is used to fits multiple distributions to the first column of a data frame.
#'
#' @param df A data frame with the data to fit.
#'
#' @return A data frame with the fit results for each distribution.
#'
#'
#' @export
do_fits <- function(df) {
dists <- avail_dists()

Expand All @@ -41,6 +81,16 @@ do_fits <- function(df) {

}


#' Squash Fit Results
#'
#' @description This processes fit results into a tidy format.
#'
#' @param fit A list containing the fit result.
#'
#' @return A tibble with the distribution name, log-likelihood, and Akaike Information Criterion (AIC).
#'
#' @export
squash_fits <- function(fit){

if (length(fit) == 1){
Expand All @@ -58,6 +108,18 @@ squash_fits <- function(fit){

}



#' Parse Fit Results from fitdistrplus
#'
#' @description This converts fit results from the `fitdistrplus` package into a tidy format.
#'
#' @param fit An object returned by `fitdistrplus::fitdist`.
#'
#' @return A tibble with the distribution name, log-likelihood, and AIC.
#'
#'
#' @export
parse_fitdist <- function(fit){
tibble::tibble(
dist = fit$distname,
Expand All @@ -66,6 +128,16 @@ parse_fitdist <- function(fit){
)
}


#' Parse Fit Results from univariateML
#'
#' @description This converts fit results from the `univariateML` package into a tidy format.
#'
#' @param fit An object returned by `univariateML::model_select`.
#'
#' @return A tibble with the distribution name, log-likelihood, and AIC.
#'
#' @export
parse_univariateML <- function(fit){
tibble::tibble(
dist = attr(fit, "model"),
Expand Down
13 changes: 12 additions & 1 deletion R/peppwr.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@

#' Find Fits for Nested Data
#'
#' @description This groups the data frame by specified columns, nests the data, and applies the fitting functions to each group.
#'
#' @param df A data frame containing the data to be fitted.
#' @param id_col A character string specifying the column name for the identifier. Default is "id".
#' @param group_col A character string specifying the column name for the group. Default is "group".
#' @param value_col A character string specifying the column name for the values to be fitted. Default is "value".
#'
#' @return A data frame with nested data and the fit results for each group.
#'
#' @export
find_fits <- function(df, id_col="id", group_col="group", value_col="value"){
tidyr::nest(df, .by = tidyr::all_of(c(id_col, group_col )), data = {{value_col}} ) |>
dplyr::mutate(
Expand Down
58 changes: 58 additions & 0 deletions R/plots.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@

#' Fill Scale for Distributions
#'
#' @description This generates a fill scale for distributions using the Set3 palette from RColorBrewer.
#'
#' @param name A character string specifying the name of the fill scale.
#'
#' @return A ggplot2 scale fill manual object.
#'
#'
#' @export
fill_scale <- function(name = "name"){
cols <- RColorBrewer::brewer.pal(length(avail_dists()), "Set3")
names(cols) <- d2n(avail_dists())
ggplot2::scale_fill_manual(name = name, values = cols)
}



#' Plot Best Fitted Models
#'
#' @description This plots the best-fitted models based on Log-Likelihood and AIC from the fits data frame.
#'
#' @param fits_df A data frame containing the fit results.
#' @param fit_col A character string specifying the column name that contains the nested fit results. Default is "fits".
#'
#' @return A ggplot object showing the best-fitted models.
#'
#' @export
plot_best <- function(fits_df, fit_col="fits") {
#TODO check and test df has fit_col 'fits'

Expand Down Expand Up @@ -49,6 +71,17 @@ plot_best <- function(fits_df, fit_col="fits") {
}



#' Plot Failed Models
#'
#' @description This plots the count of failed model fits from the fits data frame.
#'
#' @param fits_df A data frame containing the fit results.
#' @param fit_col A character string specifying the column name that contains the nested fit results. Default is "fits".
#'
#' @return A ggplot object showing the count of failed models.
#'
#' @export
plot_failed <- function(fits_df, fit_col = "fits"){

if (! fit_col %in% colnames(fits_df)){
Expand Down Expand Up @@ -79,6 +112,17 @@ plot_failed <- function(fits_df, fit_col = "fits"){

}



#' Fake Legend
#'
#' @description This generates a fake legend for the distribution plots.
#'
#' @param name A character string specifying the name of the legend. Default is "Dist".
#'
#' @return A ggplot2 legend object.
#'
#' @export
fake_leg <- function(name="Dist") {
p <- data.frame(
dist = d2n(avail_dists()),
Expand All @@ -93,6 +137,20 @@ fake_leg <- function(name="Dist") {
cowplot::get_legend(p)
}




#' Evaluate Distributions
#'
#' @description This evaluates and plots the best-fitted and failed models from the fits data frame.
#'
#' @param fits_df A data frame containing the fit results.
#'
#' @param fit_col A character string specifying the column name that contains the nested fit results. Default is "fits".
#'
#' @return A ggplot object showing the evaluation of fitted models.
#'
#' @export
evaldist <- function(fits_df, fit_col="fits"){
a <- plot_best(fits_df, fit_col = fit_col)
b <- plot_failed(fits_df, fit_col=fit_col)
Expand Down
14 changes: 14 additions & 0 deletions man/avail_dists.Rd

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

17 changes: 17 additions & 0 deletions man/d2n.Rd

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

17 changes: 17 additions & 0 deletions man/do_fits.Rd

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

19 changes: 19 additions & 0 deletions man/evaldist.Rd

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

17 changes: 17 additions & 0 deletions man/fake_leg.Rd

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

17 changes: 17 additions & 0 deletions man/fill_scale.Rd

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

23 changes: 23 additions & 0 deletions man/find_fits.Rd

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

Loading