Skip to content

Commit

Permalink
Update to 0.2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
lcpilling committed Jan 29, 2024
1 parent 26863d0 commit 4015cc2
Show file tree
Hide file tree
Showing 17 changed files with 56 additions and 79 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
15 changes: 8 additions & 7 deletions R/annotate_textp.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions R/gg_themes.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@
#'
#' @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)',
#' caption = 'Data: {palmerpenguins} R package'
#' )
#'
#' 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
Expand Down
6 changes: 0 additions & 6 deletions R/p_values.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#' @examples
#' z = 10
#' get_p(z)
#' #> [1] 1.523971e-23
#'
#' @export

Expand All @@ -40,7 +39,6 @@ get_p = function(z) {
#' @examples
#' p = 1e-10
#' get_z(p)
#' #> [1] 6.466951
#'
#' @export

Expand Down Expand Up @@ -68,7 +66,6 @@ get_z = function(p,
#' @examples
#' z = 50
#' get_p_extreme(z)
#' #> [1] "2.16e-545"
#'
#' @export
#'
Expand Down Expand Up @@ -100,7 +97,6 @@ get_p_extreme = function(z) {
#' @examples
#' z = 50
#' get_p_neglog10(z)
#' #> [1] 544.0977
#'
#' @export
#'
Expand Down Expand Up @@ -132,7 +128,6 @@ get_p_neglog10 = function(z,
#' z = 50
#' n = 100000
#' get_p_neglog10_n(z, n)
#' #> [1] 537.9851
#'
#' @export
#'
Expand Down Expand Up @@ -167,7 +162,6 @@ get_p_neglog10_n = function(z,
#' lci = 0.1
#' uci = 0.3
#' get_se(lci, uci)
#' #> [1] 0.05102041
#'
#' @export

Expand Down
8 changes: 6 additions & 2 deletions R/transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
#'
Expand All @@ -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
#'
Expand Down
50 changes: 14 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
My library of R functions I sometimes find useful

<!-- badges: start -->
[![](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)
Expand All @@ -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:
Expand Down Expand Up @@ -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
#> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <chr>
#> 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
#> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
#> 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
#> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
#> 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")
Expand All @@ -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")
Expand 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
Expand Down
9 changes: 5 additions & 4 deletions man/annotate_textp.Rd

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

1 change: 0 additions & 1 deletion man/get_p.Rd

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

1 change: 0 additions & 1 deletion man/get_p_extreme.Rd

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

1 change: 0 additions & 1 deletion man/get_p_neglog10.Rd

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

1 change: 0 additions & 1 deletion man/get_p_neglog10_n.Rd

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

1 change: 0 additions & 1 deletion man/get_se.Rd

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

1 change: 0 additions & 1 deletion man/get_z.Rd

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

4 changes: 3 additions & 1 deletion man/inv_norm.Rd

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

14 changes: 7 additions & 7 deletions man/theme_minimal_modified.Rd

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

4 changes: 3 additions & 1 deletion man/z_trans.Rd

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

0 comments on commit 4015cc2

Please sign in to comment.