Skip to content

Commit 3f56b4b

Browse files
committed
Improve check
1 parent 2f2dd70 commit 3f56b4b

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

.Rbuildignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
^.*\.Rproj$
22
^\.Rproj\.user$
3+
^dev_history\.R$
4+
^README\.Rmd$

DESCRIPTION

+10-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ Authors@R:
88
email = "[email protected]",
99
comment = c(ORCID = "YOUR-ORCID-ID"))
1010
Description: What the package does (one paragraph).
11-
License: `use_mit_license()`, `use_gpl3_license()` or friends to
12-
pick a license
11+
License: file LICENCE
1312
Encoding: UTF-8
1413
LazyData: true
1514
Roxygen: list(markdown = TRUE)
@@ -20,3 +19,12 @@ Suggests:
2019
VignetteBuilder: knitr
2120
URL: https://github.com/c-marc/predictr
2221
BugReports: https://github.com/c-marc/predictr/issues
22+
Imports:
23+
ggplot2,
24+
dplyr,
25+
magrittr,
26+
zeallot,
27+
tidyr,
28+
tibble,
29+
bslib,
30+
scales

R/estimate.R

+51-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#' @param TPF True Positive Fraction
55
#'
66
#' @return a list with positive and negative likelihood ratios
7+
#' @export
8+
#'
9+
#' @examples
10+
#' getLRatios(.4, .6)
711
getLRatios <- function(FPF, TPF){
812
list(
913
negative = (1-TPF) / (1-FPF),
@@ -17,6 +21,11 @@ getLRatios <- function(FPF, TPF){
1721
#' @param TPF True Positive Fraction
1822
#'
1923
#' @return a value
24+
#' @export
25+
#' @importFrom stats qlogis plogis
26+
#'
27+
#' @examples
28+
#' getOR(.4, .6)
2029
getOR <- function(FPF, TPF){
2130
logOR <- qlogis(TPF) - qlogis(FPF)
2231
exp(logOR)
@@ -28,10 +37,12 @@ getOR <- function(FPF, TPF){
2837
#' @param TPF True Positive Fraction
2938
#'
3039
#' @return a tibble with of FPF and TPF values
40+
#' @export
41+
#' @importFrom stats qlogis plogis
3142
getIsoOR <- function(FPF, TPF) {
3243
OR <- getOR(FPF, TPF)
3344
x <- seq(0, 1, length.out = 101)
34-
tibble(FPF = x) %>%
45+
tibble::tibble(FPF = x) %>%
3546
mutate(TPF = ifelse(FPF %in% c(0, 1),
3647
FPF,
3748
plogis(qlogis(x) + log(OR))
@@ -44,26 +55,41 @@ getIsoOR <- function(FPF, TPF) {
4455
#' @param TPF True Positive Fraction
4556
#'
4657
#' @return a tibble with intercept and slope for each LR
58+
#' @export
4759
getIsoLR <- function(FPF, TPF){
4860
lRatios <- getLRatios(FPF, TPF)
49-
tribble(
61+
tibble::tribble(
5062
~type, ~intercept, ~slope,
5163
"positive", 0, lRatios$positive,
5264
"negative", 1 - lRatios$negative, lRatios$negative
5365
)
5466
}
5567

5668

69+
#' Get predictive values
70+
#'
71+
#' @param FPF False Positive Fraction
72+
#' @param TPF True Positive Fraction
73+
#' @param prior a vector of prior probabilities. If NULL, will compute over (0,1)
74+
#'
75+
#' @return a tibble with:
76+
#' - prior
77+
#' - type: "negative" or "positive"
78+
#' - posterior: posterior probabilities
79+
#' @export
80+
#' @importFrom dplyr inner_join mutate
81+
#' @examples
82+
#' getPredValues(.4, .6, .5)
5783
getPredValues <- function(FPF, TPF, prior = NULL) {
5884
# get LR as a list and transform in a tidy format
5985
lRatios <- getLRatios(FPF, TPF) %>%
60-
as_tibble() %>%
61-
pivot_longer(c(positive, negative), names_to = "type")
86+
tibble::as_tibble() %>%
87+
tidyr::pivot_longer(c(positive, negative), names_to = "type")
6288

6389
# if prior unspecified, compute over ]0-1[
6490
if (is.null(prior)) prior <- seq(0, 1, length.out = 101)
6591

66-
dat <- expand_grid(
92+
dat <- tidyr::expand_grid(
6793
prior = prior,
6894
type = c("positive", "negative")
6995
)
@@ -76,10 +102,30 @@ getPredValues <- function(FPF, TPF, prior = NULL) {
76102
}
77103

78104

105+
#' Prettify proportions
106+
#'
107+
#' @param prop A proportion
108+
#' @param digits passed to round()
109+
#'
110+
#' @return A pretty string
111+
#' @export
112+
#'
113+
#' @examples
114+
#' prettyProp(.501)
79115
prettyProp <- function(prop, digits = 0){
80116
paste0(round(100*prop, digits), "%")
81117
}
82118

119+
#' Prettify ratios
120+
#'
121+
#' @param ratio A ratio
122+
#' @param digits passed to signif()
123+
#'
124+
#' @return A pretty string
125+
#' @export
126+
#'
127+
#' @examples
128+
#' prettyRatio(1/2.41)
83129
prettyRatio <- function(ratio, digits = 2){
84130
if(ratio >= 1){
85131
paste0(signif(ratio, digits), " : 1")

vignettes/draft.Rmd

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ knitr::opts_chunk$set(
1515
```
1616

1717
```{r setup}
18-
library(tidyverse)
1918
library(predictr)
19+
library(dplyr)
20+
library(ggplot2)
2021
```
2122

2223
# estimate.R
2324

2425
```{r}
2526
FPF <- .2
2627
TPF <- .85
27-
getLRatio(TPF, FPF)
28+
getLRatios(FPF, TPF)
2829
```
30+
2931
```{r}
3032
ggplot()+
3133
geom_point(aes(x = FPF, y = TPF)) +
@@ -35,8 +37,9 @@ ggplot()+
3537
coord_cartesian(xlim = c(0,1), ylim = c(0,1), expand = F) +
3638
theme_bw()
3739
```
40+
3841
```{r}
39-
getPValues(TPF, FPF) %>%
42+
getPredValues(FPF, TPF) %>%
4043
ggplot(aes(x = prior, y = posterior, group = type)) +
4144
geom_line() +
4245
theme_bw()

0 commit comments

Comments
 (0)