From 4015cc242f0ecd71e27875a968504718b4c579db Mon Sep 17 00:00:00 2001 From: "Luke C. Pilling" Date: Mon, 29 Jan 2024 12:06:30 +0000 Subject: [PATCH] Update to 0.2.7 --- DESCRIPTION | 2 +- NEWS.md | 3 ++- R/annotate_textp.R | 15 ++++++----- R/gg_themes.R | 14 +++++----- R/p_values.R | 6 ----- R/transform.R | 8 ++++-- README.md | 50 ++++++++++------------------------- man/annotate_textp.Rd | 9 ++++--- man/get_p.Rd | 1 - man/get_p_extreme.Rd | 1 - man/get_p_neglog10.Rd | 1 - man/get_p_neglog10_n.Rd | 1 - man/get_se.Rd | 1 - man/get_z.Rd | 1 - man/inv_norm.Rd | 4 ++- man/theme_minimal_modified.Rd | 14 +++++----- man/z_trans.Rd | 4 ++- 17 files changed, 56 insertions(+), 79 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index ca67d16..6025639 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: lukesRlib Title: My library of R functions I sometimes find useful -Version: 0.2.6.9000 +Version: 0.2.7 Authors@R: c(person("Luke", "Pilling", email = "L.Pilling@exeter.ac.uk", role = c("aut", "cre"), diff --git a/NEWS.md b/NEWS.md index e6fed2d..067456a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,9 +1,10 @@ -# lukesRlib 0.2.6.9000 (13th Dec 2023) +# lukesRlib 0.2.7 (29th Jan 2024) * `get_assoc()` - add `return_all_terms` argument. Default is false. If TRUE, adds a new col `terms` and returns the estimates for all independent variables in the models. * `get_assoc()` - remove `subset_d` argument * `get_assoc()` - fixed if exposure or outcome needs to be protected by backticks (previously failed if variable was something like "bmi-1") * `get_assoc()` - fixed problem where covariate estimates were also included in certain situations (e.g., if x="age" do not get covariate "percentage") +* Update examples for many functions # lukesRlib 0.2.6 (13 Nov 2023) diff --git a/R/annotate_textp.R b/R/annotate_textp.R index 2785ccb..87d5dae 100644 --- a/R/annotate_textp.R +++ b/R/annotate_textp.R @@ -18,31 +18,32 @@ #' @param alpha Numeric. Transparency (0:1). Default=NA #' #' @examples -#' qplot(1:10,1:10) + annotate_textp('Text annotation\nx=1, y=0, hjust=1', x=1, y=0, hjust=1) -#' qplot(1:10,1:10) + annotate_textp('Text annotation\nx=0.1, y=0.9, hjust=0', x=0, y=1, hjust=0) -#' qplot(1:10,1:10) + annotate_textp('Text annotation\nx = 0.5, y=0.5, hjust=0.5\nbox_just=c(0.5,0.5)', x=0.5, y=0.5, hjust=0.5, box_just=c(0.5,0.5)) -#' qplot(1:10,1:10) + annotate_textp('Text annotation\nx = 0.5, y=0.5, hjust=0.5\nbox_just=c(0.5,0.5)\nsize=14, alpha=0.5', x=0.5, y=0.5, hjust=0.5, box_just=c(0.5,0.5), size=14, alpha=0.5) +#' p1 = ggplot2::ggplot(data.frame(x=1:10,y=1:10), ggplot2::aes(x,y)) + ggplot2::geom_point() +#' p1 + annotate_textp('Text annotation\nx=1, y=0, hjust=1', x=1, y=0, hjust=1) +#' p1 + annotate_textp('Text annotation\nx=0.1, y=0.9, hjust=0', x=0, y=1, hjust=0) +#' p1 + annotate_textp('Text annotation\nx = 0.5, y=0.5, hjust=0.5\nbox_just=c(0.5,0.5)', x=0.5, y=0.5, hjust=0.5, box_just=c(0.5,0.5)) +#' p1 + annotate_textp('Text annotation\nx = 0.5, y=0.5, hjust=0.5\nbox_just=c(0.5,0.5)\nsize=14, alpha=0.5', x=0.5, y=0.5, hjust=0.5, box_just=c(0.5,0.5), size=14, alpha=0.5) #' #' @export #' annotate_textp = function(label, x, y, facets=NULL, hjust=0, vjust=0, color='black', alpha=NA, family=thm$text$family, size=thm$text$size, fontface=1, lineheight=1.0, - box_just=ifelse(c(x,y)<0.5,0,1), margin=unit(size/2, 'pt'), thm=theme_get()) { + box_just=ifelse(c(x,y)<0.5,0,1), margin=unit(size/2, 'pt'), thm=ggplot2::theme_get()) { x = scales::squish_infinite(x) y = scales::squish_infinite(y) data = if (is.null(facets)) data.frame(x=NA) else data.frame(x=NA, facets) tg = grid::textGrob( label, x=0, y=0, hjust=hjust, vjust=vjust, - gp=grid::gpar(col=alpha(color, alpha), fontsize=size, fontfamily=family, fontface=fontface, lineheight=lineheight) + gp=grid::gpar(col=scales::alpha(color, alpha), fontsize=size, fontfamily=family, fontface=fontface, lineheight=lineheight) ) ts = grid::unit.c(grid::grobWidth(tg), grid::grobHeight(tg)) vp = grid::viewport(x=x, y=y, width=ts[1], height=ts[2], just=box_just) tg = grid::editGrob(tg, x=ts[1]*hjust, y=ts[2]*vjust, vp=vp) inner = grid::grobTree(tg, vp=grid::viewport(width=unit(1, 'npc')-margin*2, height=unit(1, 'npc')-margin*2)) - layer( + ggplot2::layer( data = NULL, stat = StatIdentity, position = PositionIdentity, diff --git a/R/gg_themes.R b/R/gg_themes.R index f6bdd9d..728fbf2 100644 --- a/R/gg_themes.R +++ b/R/gg_themes.R @@ -12,14 +12,14 @@ #' #' @examples #' penguins = palmerpenguins::penguins |> -#' filter(!is.na(sex)) +#' dplyr::filter(!is.na(sex)) #' #' basic_plot = penguins |> -#' ggplot(aes(x = bill_length_mm, y = body_mass_g, fill = species)) + -#' geom_point(shape = 21, size = 5, alpha = 0.85, color = 'grey10') + -#' labs( +#' ggplot2::ggplot(ggplot2::aes(x = bill_length_mm, y = body_mass_g, fill = species)) + +#' ggplot2::geom_point(shape = 21, size = 5, alpha = 0.85, color = 'grey10') + +#' ggplot2::labs( #' x = 'Bill length (in mm)', -#' y = element_blank(), +#' y = ggplot2::element_blank(), #' fill = 'Species', #' title = 'Penguins from the Palmer Archipelago', #' subtitle = 'Penguin weights (in g)', @@ -27,8 +27,8 @@ #' ) #' #' basic_plot + -#' theme_minimal(base_size = 16, base_family = 'Source Sans Pro') + -#' scale_fill_manual(values = c('Adelie'= '#da2c38', 'Chinstrap'= '#FED18C', 'Gentoo'= '#30C5FF')) + +#' ggplot2::theme_minimal(base_size = 16, base_family = 'Source Sans Pro') + +#' ggplot2::scale_fill_manual(values = c('Adelie'= '#da2c38', 'Chinstrap'= '#FED18C', 'Gentoo'= '#30C5FF')) + #' theme_minimal_modified() #' #' @export diff --git a/R/p_values.R b/R/p_values.R index 2bab9c3..764c8c5 100644 --- a/R/p_values.R +++ b/R/p_values.R @@ -13,7 +13,6 @@ #' @examples #' z = 10 #' get_p(z) -#' #> [1] 1.523971e-23 #' #' @export @@ -40,7 +39,6 @@ get_p = function(z) { #' @examples #' p = 1e-10 #' get_z(p) -#' #> [1] 6.466951 #' #' @export @@ -68,7 +66,6 @@ get_z = function(p, #' @examples #' z = 50 #' get_p_extreme(z) -#' #> [1] "2.16e-545" #' #' @export #' @@ -100,7 +97,6 @@ get_p_extreme = function(z) { #' @examples #' z = 50 #' get_p_neglog10(z) -#' #> [1] 544.0977 #' #' @export #' @@ -132,7 +128,6 @@ get_p_neglog10 = function(z, #' z = 50 #' n = 100000 #' get_p_neglog10_n(z, n) -#' #> [1] 537.9851 #' #' @export #' @@ -167,7 +162,6 @@ get_p_neglog10_n = function(z, #' lci = 0.1 #' uci = 0.3 #' get_se(lci, uci) -#' #> [1] 0.05102041 #' #' @export diff --git a/R/transform.R b/R/transform.R index fa789c4..a23d8d1 100644 --- a/R/transform.R +++ b/R/transform.R @@ -11,7 +11,9 @@ #' @param x a numeric vector to be inverse normalized #' #' @examples -#' inv_norm(x) +#' summary(example_data$sbp) +#' sbp_in = inv_norm(example_data$sbp) +#' summary(sbp_in) #' #' @export #' @@ -36,7 +38,9 @@ inv_norm = function(x) { #' @param x a numeric vector to be z-transformed #' #' @examples -#' z_trans(x) +#' summary(example_data$sbp) +#' sbp_in = z_trans(example_data$sbp) +#' summary(sbp_in) #' #' @export #' diff --git a/README.md b/README.md index 55da792..f2ed3fd 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ My library of R functions I sometimes find useful -[![](https://img.shields.io/badge/version-0.2.6.9000-informational.svg)](https://github.com/lukepilling/lukesRlib) +[![](https://img.shields.io/badge/version-0.2.7-informational.svg)](https://github.com/lukepilling/lukesRlib) [![](https://img.shields.io/github/last-commit/lukepilling/lukesRlib.svg)](https://github.com/lukepilling/lukesRlib/commits/master) [![](https://img.shields.io/badge/lifecycle-experimental-orange)](https://www.tidyverse.org/lifecycle/#experimental) [![DOI](https://zenodo.org/badge/590063045.svg)](https://zenodo.org/badge/latestdoi/590063045) @@ -14,18 +14,9 @@ My library of R functions I sometimes find useful ## List of functions - [Hypothesis testing](#hypothesis-testing) - - [tidy_ci()](#tidy_ci) - - [get_assoc()](#get_assoc) - [Data Transformation](#data-transformation) - - [carrec()](#carrec) - - [inv_norm()](#inv_norm) - - [z_trans()](#z_trans) - [Working with test statistics](#working-with-test-statistics) - - [get_se()](#get_se), [get_z()](#get_z), [get_p()](#get_p) - - [get_p_extreme()](#get_p_extreme), [get_p_neglog10()](#get_p_neglog10), [get_p_neglog10_n()](#get_p_neglog10_n) - [Plotting-related](#plotting-related) - - [annotate_textp()](#annotate_textp) - - [theme_minimal_modified()](#theme_minimal_modified) ## Installation To install the development version from GitHub use the `remotes` package: @@ -88,37 +79,23 @@ Automatically identified the input as from a coxph model and exponentiated estim ### get_assoc() -`get_assoc()` (phonetically: "get-a-sock") makes PheWAS in R easy and fast, utilizing the {purrr} `map2()` function. It gets the tidy model output for categorical or continuous exposures, from linear, logistic, or CoxPH models: includes N and N cases, outcome, and model info. User can provide multiples exposures and outcomes. +`get_assoc()` (phonetically: "get-a-sock") makes PheWAS in R easy and fast. It gets the tidy model output for categorical or continuous exposures, from linear, logistic, or CoxPH models. Output includes N and N cases, outcome, and model info. User can provide multiples exposures and outcomes. -See the [`get_assoc()` Wiki](https://github.com/lukepilling/lukesRlib/wiki/get_assoc()) details +#### Example: Categorical exposure in logistic regression -#### Examples - -##### Get tidy model output for an exposure/outcome combination -```r -get_assoc(x="height", y="weight", z="+age+sex", d=ukb) -#> A tibble: 1 x 10 -#> outcome exposure estimate std.error statistic p.value conf.low conf.high n model -#> -#> 1 weight height 0.762 0.0296 25.7 3.36e-137 0.704 0.820 4981 lm -``` - -The above example is equivalent to `tidy_ci(glm(weight~height+age+sex, data=ukb))` with added `n` - -##### Categorical exposure in logistic regression - stratified analysis ```r -get_assoc(x="smoking_status", y="chd", z="+age", d=ukb |> filter(sex==1), model="logistic", af=TRUE, note="Males") +get_assoc(x="smoking_status", y="chd", z="+age", d=ukb, model="logistic", af=TRUE) #> A tibble: 3 x 12 -#> outcome exposure estimate std.error statistic p.value conf.low conf.high n n_cases model note -#> -#> 1 chd smoking_status-0 NA NA NA NA NA NA 1073 146 logistic Males -#> 2 chd smoking_status-1 1.24 0.126 1.72 0.0852 0.970 1.59 918 180 logistic Males -#> 3 chd smoking_status-2 1.48 0.181 2.16 0.0311 1.04 2.11 285 52 logistic Males +#> outcome exposure estimate std.error statistic p.value conf.low conf.high n n_cases model +#> +#> 1 chd smoking_status-0 NA NA NA NA NA NA 1073 146 logistic +#> 2 chd smoking_status-1 1.24 0.126 1.72 0.0852 0.970 1.59 918 180 logistic +#> 3 chd smoking_status-2 1.48 0.181 2.16 0.0311 1.04 2.11 285 52 logistic ``` -In the above example, the `estimate` is the Odds Ratio from a logistic regression model. The `n` and `n_cases` are directly from the model object and reflect those included in the model after excluding missing participants. The exposure is categorical, and a reference category line has been added to include the N and Ncases for that group. +The `estimate` is the Odds Ratio from a logistic regression model, and `n` and `n_cases` show the numbers for each category of the exposure (here `af=TRUE` i.e., treat as factor), including the reference group. -##### Multiple exposures on single outcome (i.e., a "PheWAS") +#### Example: Multiple exposures on single outcome (i.e., a "PheWAS") ```r x_vars = c("bmi","ldl","sbp") get_assoc(x=x_vars, y="chd", z="+age+sex", d=ukb, model="logistic") @@ -130,9 +107,9 @@ get_assoc(x=x_vars, y="chd", z="+age+sex", d=ukb, model="logistic") #> 3 chd sbp 1.01 0.00333 3.53 4.2e- 4 1.01 1.02 4561 316 logistic ``` -Multiple exposures and outcomes can be provided simultaneously. +Multiple exposures and outcomes can be provided simultaneously. Here, the exposures are continuous. -##### Tidyverse formatted. Data is first argument. `get_assoc()` can be on the right-side of other `dplyr` functions +#### Example: stratified analyses ```r res_all = ukb |> get_assoc(x=x_vars, y="chd", z="+age+sex", model="logistic", note="All") @@ -146,6 +123,7 @@ res_females = ukb |> res = list_rbind(list(res_all, res_males, res_females)) ``` +Data is first argument. `get_assoc()` can be on the right-side of other `dplyr` functions. Stratified models can be therefore easily performed. The `note` argument means output is labelled and can be combined into a single data frame easily. ## Data Transformation diff --git a/man/annotate_textp.Rd b/man/annotate_textp.Rd index 2b4a5fc..1b328b3 100644 --- a/man/annotate_textp.Rd +++ b/man/annotate_textp.Rd @@ -44,10 +44,11 @@ One can specify the relative position of the figure easily. Configurable margin, From Stack Overflow comment https://stackoverflow.com/questions/22488563/ggplot2-annotate-layer-position-in-r } \examples{ -qplot(1:10,1:10) + annotate_textp('Text annotation\nx=1, y=0, hjust=1', x=1, y=0, hjust=1) -qplot(1:10,1:10) + annotate_textp('Text annotation\nx=0.1, y=0.9, hjust=0', x=0, y=1, hjust=0) -qplot(1:10,1:10) + annotate_textp('Text annotation\nx = 0.5, y=0.5, hjust=0.5\nbox_just=c(0.5,0.5)', x=0.5, y=0.5, hjust=0.5, box_just=c(0.5,0.5)) -qplot(1:10,1:10) + annotate_textp('Text annotation\nx = 0.5, y=0.5, hjust=0.5\nbox_just=c(0.5,0.5)\nsize=14, alpha=0.5', x=0.5, y=0.5, hjust=0.5, box_just=c(0.5,0.5), size=14, alpha=0.5) +p1 = ggplot2::ggplot(data.frame(x=1:10,y=1:10), ggplot2::aes(x,y)) + ggplot2::geom_point() +p1 + annotate_textp('Text annotation\nx=1, y=0, hjust=1', x=1, y=0, hjust=1) +p1 + annotate_textp('Text annotation\nx=0.1, y=0.9, hjust=0', x=0, y=1, hjust=0) +p1 + annotate_textp('Text annotation\nx = 0.5, y=0.5, hjust=0.5\nbox_just=c(0.5,0.5)', x=0.5, y=0.5, hjust=0.5, box_just=c(0.5,0.5)) +p1 + annotate_textp('Text annotation\nx = 0.5, y=0.5, hjust=0.5\nbox_just=c(0.5,0.5)\nsize=14, alpha=0.5', x=0.5, y=0.5, hjust=0.5, box_just=c(0.5,0.5), size=14, alpha=0.5) } \author{ diff --git a/man/get_p.Rd b/man/get_p.Rd index 8d79ac8..7c76d1d 100644 --- a/man/get_p.Rd +++ b/man/get_p.Rd @@ -18,7 +18,6 @@ Return p-value for a z (or t) statistic \examples{ z = 10 get_p(z) -#> [1] 1.523971e-23 } \author{ diff --git a/man/get_p_extreme.Rd b/man/get_p_extreme.Rd index d0d7f92..511bbec 100644 --- a/man/get_p_extreme.Rd +++ b/man/get_p_extreme.Rd @@ -18,7 +18,6 @@ Return p-value for any z (or t) statistic as a string \examples{ z = 50 get_p_extreme(z) -#> [1] "2.16e-545" } \author{ diff --git a/man/get_p_neglog10.Rd b/man/get_p_neglog10.Rd index eae9d4e..14aac43 100644 --- a/man/get_p_neglog10.Rd +++ b/man/get_p_neglog10.Rd @@ -20,7 +20,6 @@ Get -log10 p-value from a provided z statistic (or p-value) \examples{ z = 50 get_p_neglog10(z) -#> [1] 544.0977 } \author{ diff --git a/man/get_p_neglog10_n.Rd b/man/get_p_neglog10_n.Rd index 53a26aa..6e1c07b 100644 --- a/man/get_p_neglog10_n.Rd +++ b/man/get_p_neglog10_n.Rd @@ -23,7 +23,6 @@ Get -log10 p-value from a provided z statistic (or p-value) and n z = 50 n = 100000 get_p_neglog10_n(z, n) -#> [1] 537.9851 } \author{ diff --git a/man/get_se.Rd b/man/get_se.Rd index d684714..4a89318 100644 --- a/man/get_se.Rd +++ b/man/get_se.Rd @@ -25,7 +25,6 @@ Return SE from CIs lci = 0.1 uci = 0.3 get_se(lci, uci) -#> [1] 0.05102041 } \author{ diff --git a/man/get_z.Rd b/man/get_z.Rd index 2ef7967..607ecaf 100644 --- a/man/get_z.Rd +++ b/man/get_z.Rd @@ -20,7 +20,6 @@ Return a z-statistic from a given p-value \examples{ p = 1e-10 get_z(p) -#> [1] 6.466951 } \author{ diff --git a/man/inv_norm.Rd b/man/inv_norm.Rd index 6045f90..a8ef834 100644 --- a/man/inv_norm.Rd +++ b/man/inv_norm.Rd @@ -16,7 +16,9 @@ Returns a vector Inverse normalize a variable (force a normal distribution) } \examples{ -inv_norm(x) +summary(example_data$sbp) +sbp_in = inv_norm(example_data$sbp) +summary(sbp_in) } \author{ diff --git a/man/theme_minimal_modified.Rd b/man/theme_minimal_modified.Rd index d7badd0..30ead83 100644 --- a/man/theme_minimal_modified.Rd +++ b/man/theme_minimal_modified.Rd @@ -16,14 +16,14 @@ https://github.com/AlbertRapp/3mw_newsletters/blob/master/37_Jul_19_2023/37_Jul_ } \examples{ penguins = palmerpenguins::penguins |> - filter(!is.na(sex)) + dplyr::filter(!is.na(sex)) basic_plot = penguins |> - ggplot(aes(x = bill_length_mm, y = body_mass_g, fill = species)) + - geom_point(shape = 21, size = 5, alpha = 0.85, color = 'grey10') + - labs( + ggplot2::ggplot(ggplot2::aes(x = bill_length_mm, y = body_mass_g, fill = species)) + + ggplot2::geom_point(shape = 21, size = 5, alpha = 0.85, color = 'grey10') + + ggplot2::labs( x = 'Bill length (in mm)', - y = element_blank(), + y = ggplot2::element_blank(), fill = 'Species', title = 'Penguins from the Palmer Archipelago', subtitle = 'Penguin weights (in g)', @@ -31,8 +31,8 @@ basic_plot = penguins |> ) basic_plot + - theme_minimal(base_size = 16, base_family = 'Source Sans Pro') + - scale_fill_manual(values = c('Adelie'= '#da2c38', 'Chinstrap'= '#FED18C', 'Gentoo'= '#30C5FF')) + + ggplot2::theme_minimal(base_size = 16, base_family = 'Source Sans Pro') + + ggplot2::scale_fill_manual(values = c('Adelie'= '#da2c38', 'Chinstrap'= '#FED18C', 'Gentoo'= '#30C5FF')) + theme_minimal_modified() } diff --git a/man/z_trans.Rd b/man/z_trans.Rd index 168edcb..78a6050 100644 --- a/man/z_trans.Rd +++ b/man/z_trans.Rd @@ -16,7 +16,9 @@ Returns a vector Z-transform a variable. Mean=0, SD=1. Maintains original distribution. } \examples{ -z_trans(x) +summary(example_data$sbp) +sbp_in = z_trans(example_data$sbp) +summary(sbp_in) } \author{