├── .Rbuildignore
├── .gitignore
├── DESCRIPTION
├── LICENSE
├── LICENSE.md
├── NAMESPACE
├── R
├── beta_pal.R
├── beta_plot.R
├── get_objective.R
├── globals.R
└── utils-pipe.R
├── README.Rmd
├── README.md
├── archive
└── beta_pal_firsttry.R
├── docs
├── 404.html
├── LICENSE-text.html
├── LICENSE.html
├── articles
│ ├── betapal.html
│ ├── betapal_files
│ │ ├── accessible-code-block-0.0.1
│ │ │ └── empty-anchor.js
│ │ ├── figure-html
│ │ │ ├── unnamed-chunk-11-1.png
│ │ │ ├── unnamed-chunk-12-1.png
│ │ │ └── unnamed-chunk-6-1.png
│ │ └── header-attrs-2.3
│ │ │ └── header-attrs.js
│ └── index.html
├── authors.html
├── bootstrap-toc.css
├── bootstrap-toc.js
├── docsearch.css
├── docsearch.js
├── index.html
├── link.svg
├── pkgdown.css
├── pkgdown.js
├── pkgdown.yml
└── reference
│ ├── beta_pal.html
│ ├── beta_plot.html
│ ├── figures
│ ├── README-unnamed-chunk-11-1.png
│ ├── README-unnamed-chunk-12-1.png
│ ├── README-unnamed-chunk-13-1.png
│ ├── README-unnamed-chunk-14-1.png
│ ├── README-unnamed-chunk-15-1.png
│ ├── README-unnamed-chunk-5-1.png
│ ├── README-unnamed-chunk-6-1.png
│ ├── README-unnamed-chunk-7-1.png
│ ├── README-unnamed-chunk-8-1.png
│ └── README-unnamed-chunk-9-1.png
│ ├── get_objective.html
│ ├── index.html
│ └── pipe.html
├── inst
├── _pkgdown.yml
└── tutorials
│ └── ppalhelp
│ ├── README.md
│ ├── css
│ └── miceeseese.css
│ └── parameterpal_help.Rmd
├── man
├── beta_pal.Rd
├── beta_plot.Rd
├── figures
│ ├── README-unnamed-chunk-11-1.png
│ ├── README-unnamed-chunk-12-1.png
│ ├── README-unnamed-chunk-13-1.png
│ ├── README-unnamed-chunk-14-1.png
│ ├── README-unnamed-chunk-15-1.png
│ ├── README-unnamed-chunk-5-1.png
│ ├── README-unnamed-chunk-6-1.png
│ ├── README-unnamed-chunk-7-1.png
│ ├── README-unnamed-chunk-8-1.png
│ └── README-unnamed-chunk-9-1.png
├── get_objective.Rd
└── pipe.Rd
├── parameterpal.Rproj
├── tests
├── testthat.R
└── testthat
│ ├── test-beta_plot.R
│ └── test-betapal.R
└── vignettes
├── .gitignore
└── betapal.Rmd
/.Rbuildignore:
--------------------------------------------------------------------------------
1 | ^parameterpal\.Rproj$
2 | ^\.Rproj\.user$
3 | ^LICENSE\.md$
4 | ^README\.Rmd$
5 | ^doc$
6 | ^Meta$
7 | ^_pkgdown\.yml$
8 | ^docs$
9 | ^pkgdown$
10 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .Rproj.user
2 | inst/doc
3 | doc
4 | Meta
5 |
--------------------------------------------------------------------------------
/DESCRIPTION:
--------------------------------------------------------------------------------
1 | Package: parameterpal
2 | Title: Intepretable parameters for the beta distribution
3 | Version: 0.0.1
4 | Authors@R: c(
5 | person(given = "Charles",
6 | family = "Gray",
7 | role = c("aut", "cre"),
8 | email = "charlestigray@gmail.com",
9 | comment = c(ORCID = "0000-0002-9978-011X")),
10 | person(given = "Hien",
11 | family = "Nguyen",
12 | role = "ctb"),
13 | person(given = "Matthew",
14 | family = "Grainger",
15 | role = "ctb"),
16 | person(given = "Matthew",
17 | family = "Henderson",
18 | role = "ctb"),
19 | person(given = "Daniel",
20 | family = "Oberski",
21 | role = "ctb"),
22 | person(given = "Yanina",
23 | family = "Saibene",
24 | role = "ctb"),
25 | person(given = "Laura",
26 | family = "Acion",
27 | role = "ctb"),
28 | person(given = "Heather",
29 | family = "Turner",
30 | role = "ctb"))
31 | Description: From the measure we expect, how much we expect yea much of the measure falling within, we interpret beta distribution parameters.
32 | License: MIT + file LICENSE
33 | Encoding: UTF-8
34 | LazyData: true
35 | Roxygen: list(markdown = TRUE)
36 | RoxygenNote: 7.1.1
37 | Suggests:
38 | rmarkdown,
39 | testthat
40 | VignetteBuilder: knitr
41 | Imports:
42 | magrittr,
43 | tibble,
44 | ggplot2,
45 | stringr,
46 | assertthat,
47 | knitr,
48 | ggthemes
49 | Date: 2020
50 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | YEAR: 2020
2 | COPYRIGHT HOLDER: Charles Gray
3 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | # MIT License
2 |
3 | Copyright (c) 2020 Charles Gray
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/NAMESPACE:
--------------------------------------------------------------------------------
1 | # Generated by roxygen2: do not edit by hand
2 |
3 | export("%>%")
4 | export(beta_pal)
5 | export(beta_plot)
6 | importFrom(magrittr,"%>%")
7 |
--------------------------------------------------------------------------------
/R/beta_pal.R:
--------------------------------------------------------------------------------
1 | #' Beta parameters from interpretable conditions
2 | #'
3 | #' Rather than knowing intrinsically what parameters are required for a
4 | #' distribution, scientists tend to have a sense of what value they
5 | #' *expect* a measure to take, *how much* they expect observations to fall
6 | #' *within* a certain distance of that value.
7 | #'
8 | #' This solution coded by by this most helpful
9 | #' [gist](https://gist.github.com/daob/1422e978ff98bdf466fbcb4d9bf3e53e).
10 | #'
11 | #' Visualise the distribution with [beta_plot].
12 | #' See `vignette("beta_pal")` for derivations and more information.
13 | #'
14 | #' @param expected_value Expected value of beta distrbution from \[0,1\].
15 | #' @param within Specify distance `this_much` falls within.
16 | #' @param this_much What proportion falls `within` the specified interval.
17 | #'
18 | #' @return List of parameters appropriate for [rbeta] family of functions.
19 | #'
20 | #' @export
21 |
22 | beta_pal <- function(expected_value,
23 | within,
24 | this_much) {
25 | # check inputs
26 | assertthat::assert_that(expected_value > 0 &
27 | expected_value < 1,
28 | msg = "expected_value must be from (0,1)")
29 | assertthat::assert_that(within > 0 &
30 | within < 1,
31 | msg = "within must be from (0,1)")
32 | assertthat::assert_that(this_much > 0 & this_much < 1,
33 | msg =
34 | "this_much must be a value from [0,1].")
35 |
36 | # Function that takes desrired mean, distance, and probability, and outputs
37 | # another function to be optimized.
38 | my_objective <-
39 | get_objective(
40 | desired_mean = expected_value,
41 | desired_dist = within,
42 | desired_mass = this_much
43 | )
44 |
45 | res <-
46 | optim(1,
47 | my_objective,
48 | method = "Brent",
49 | lower = 1e-3,
50 | upper = 10)
51 |
52 | shape1_est <- res$par
53 | shape2_est <- shape1_est * ((1 / expected_value) - 1)
54 |
55 | list(shape1_est = shape1_est, shape2_est = shape2_est)
56 | }
57 |
--------------------------------------------------------------------------------
/R/beta_plot.R:
--------------------------------------------------------------------------------
1 | #' Plot beta distribution
2 | #'
3 | #' This function utlises [beta_pal] to produce a visualisation of the beta
4 | #' distribution, with user-specified interpretable conditions.
5 | #'
6 | #'
7 | #' @inheritParams beta_pal
8 | #' @param caption_width Option to control the width of the caption, defaults to
9 | #' 70.
10 | #' @param theme_default Currently defaults to `ggthemes::theme_tufte`.
11 | #'
12 | #' @export
13 |
14 | beta_plot <- function(expected_value,
15 | within,
16 | this_much,
17 | caption_width = 70,
18 | theme_default = TRUE) {
19 | # calculate parameters
20 | par <-
21 | beta_pal(expected_value = expected_value,
22 | within = within,
23 | this_much = this_much)
24 |
25 | # return plot of beta distribution with parameters
26 | tibble::tibble(x = c(0, 1)) %>%
27 | ggplot2::ggplot(ggplot2::aes(x = x)) +
28 |
29 | # desired quantiles
30 | ggplot2::geom_rect(
31 | xmin = expected_value - within,
32 | xmax = expected_value + within,
33 | ymin = 0,
34 | ymax = Inf,
35 | alpha = 0.2
36 | ) +
37 |
38 | ggplot2::geom_vline(xintercept = expected_value,
39 | linetype = "dashed",
40 | alpha = 0.8) +
41 | ggplot2::stat_function(
42 | fun = dbeta,
43 | linetype = "dotted",
44 | args = list(
45 | shape1 = par$shape1_est,
46 | shape2 = par$shape2_est
47 | )
48 | ) +
49 | ggplot2::labs(
50 | title = sprintf("beta(%.1f, %.1f)",
51 | par$shape1_est,
52 | par$shape2_est),
53 | y = NULL,
54 | x = NULL,
55 | caption = sprintf(
56 | "Assuming a beta distribution (dotted line), with expected value %g (vertical dashed line), and %g per cent of falling within %g of %g, parameterpal:: approximates the distribution beta(%g, %g). Parameters in the title are rounded. The shaded region shows the desired region %g per cent of values falling within %g of %g, under the approximated distribution, beta(%g, %g), %g per cent of values fall within this region.",
57 | expected_value,
58 | this_much * 100,
59 | within,
60 | expected_value,
61 | par$shape1_est,
62 | par$shape2_est,
63 | this_much * 100,
64 | within,
65 | expected_value,
66 | par$shape1_est,
67 | par$shape2_est,
68 | (
69 | pbeta(q = expected_value + within, par$shape1_est, par$shape2_est) -
70 | pbeta(q = expected_value - within, par$shape1_est, par$shape2_est)
71 | ) * 100
72 | ) %>% stringr::str_wrap(width = caption_width)
73 | ) +
74 | ggplot2::theme(
75 | axis.text.y = ggplot2::element_blank(),
76 | axis.ticks.y = ggplot2::element_blank()
77 | ) -> bplot
78 |
79 | if (isTRUE(theme_default)) {
80 | bplot + ggthemes::theme_tufte(base_size = 12) + ggplot2::theme(
81 | axis.text.y = ggplot2::element_blank(),
82 | axis.ticks.y = ggplot2::element_blank()
83 | )
84 |
85 | } else {
86 | bplot
87 | }
88 |
89 | }
90 |
--------------------------------------------------------------------------------
/R/get_objective.R:
--------------------------------------------------------------------------------
1 | #' Objective function
2 | #'
3 | #' Function that takes desrired mean, distance, and probability, and outputs
4 | #' another function to be optimized. Adapted into [beta_pal].
5 | #'
6 | #' This solution coded by by this most helpful
7 | #' [gist](https://gist.github.com/daob/1422e978ff98bdf466fbcb4d9bf3e53e).
8 | #'
9 | #'
10 | #' @param desired_mean expected_value.
11 | #' @param desired_dist within.
12 | #' @param desired_mass this_much.
13 | #'
14 | #' This function is called by [beta_pal] internally.
15 |
16 | get_objective <- function(desired_mean, desired_dist, desired_mass) {
17 | # Return a function that can be passed to optim()
18 | function(shape1) {
19 | # Enforce desired mean:
20 | shape2 <- shape1 * ((1 / desired_mean) - 1)
21 |
22 | # Use R internals to get the definite integral:
23 | current_mass <- pbeta(q = desired_mean + desired_dist,
24 | shape1 = shape1, shape2 = shape2) -
25 | pbeta(q = desired_mean - desired_dist,
26 | shape1 = shape1, shape2 = shape2)
27 |
28 | # Loss is squared difference between desired and obtained measure:
29 | (current_mass - desired_mass)^2
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/R/globals.R:
--------------------------------------------------------------------------------
1 | # following this advice:
2 | # https://community.rstudio.com/t/how-to-solve-no-visible-binding-for-global-variable-note/28887
3 |
4 | utils::globalVariables(c("x", "dbeta"))
5 |
--------------------------------------------------------------------------------
/R/utils-pipe.R:
--------------------------------------------------------------------------------
1 | #' Pipe operator
2 | #'
3 | #' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details.
4 | #'
5 | #' @name %>%
6 | #' @rdname pipe
7 | #' @keywords internal
8 | #' @export
9 | #' @importFrom magrittr %>%
10 | #' @usage lhs \%>\% rhs
11 | NULL
12 |
--------------------------------------------------------------------------------
/README.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | output: github_document
3 | ---
4 |
5 |
6 |
7 | ```{r, include = FALSE}
8 | knitr::opts_chunk$set(
9 | collapse = TRUE,
10 | comment = "#>",
11 | fig.path = "man/figures/README-",
12 | out.width = "100%",
13 | fig.height = 3
14 | )
15 | ```
16 |
17 | # parameterpal
18 |
19 |
20 |
21 |
22 | Rather than knowing intrinsically what parameters are required for a distribution, scientists tend to have a sense of what value they *expect* a measure to take, *how many* observations should fall *within* a certain distance of that value.
23 |
24 | For the normal distribution, this is straightforward, as the parameters reflect the expected value and variance. However, for the beta distribution, the parameters are not so readily interpretable.
25 |
26 | `parameterpal::` provides a means of obtaining the parameters required for the beta distribution from interpretable conditions.
27 |
28 | ## installation
29 |
30 | ```{r eval=FALSE}
31 | devtools::install_github("softloud/parameterpal", build_vignettes = TRUE)
32 | ```
33 |
34 | ## launch tutorial
35 |
36 | After installation, the `parameterpal::` tutorial will be available in the Tutorial pane of Rstudio.
37 |
38 | ```{r eval=FALSE}
39 | # executing this code will launch the tutorial in your browser
40 | learnr::run_tutorial("ppalhelp", package = "parameterpal")
41 | ```
42 |
43 |
44 |
45 | ## documentation
46 |
47 | See this package's [`pkgdown::`](https://pkgdown.r-lib.org/articles/pkgdown.html)-generated [site for more information](https://softloud.github.io/parameterpal/).
48 |
49 |
50 |
51 | ## intended user
52 |
53 | This package was developed for a friend and collaborator, computational ecologist [Dr Matthew Grainger](https://github.com/DrMattG), who previously used browser-based tools to obtain beta parameters to inform his rstats workflow. I hope he finds this package a useful augment to his codeflow.
54 |
55 |
56 | # vignette
57 |
58 | See `vignette("betapal")` for ~~more information~~ the same information, but from the handy ease of your Rstudio Viewer pane.
59 |
60 | Full disclosure, I need to update it after I
61 | fully understand this [gist](https://gist.github.com/daob/1422e978ff98bdf466fbcb4d9bf3e53e) I was provided with after posting the first version of this software to twitter. Open science at its best. Blogpost incoming on open science and why it's good to share bad math and beta research code. So cool I've ended up with a better, more mathematically correct solution after posting it.
62 |
63 | # other distributions
64 |
65 | No reason the same math can't be applied to other distributions. Open an issue if you'd like me to provide parameters from interpretable conditions for another distribution.
66 |
67 | # development on hold
68 |
69 | Currently softloud is finishing her dissertation, and with it so close, she's focussed on wrapping that up before extending any software beyond what's required for the thesis.
70 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | # parameterpal
5 |
6 |
7 |
8 |
9 |
10 | Rather than knowing intrinsically what parameters are required for a
11 | distribution, scientists tend to have a sense of what value they
12 | *expect* a measure to take, *how many* observations should fall *within*
13 | a certain distance of that value.
14 |
15 | For the normal distribution, this is straightforward, as the parameters
16 | reflect the expected value and variance. However, for the beta
17 | distribution, the parameters are not so readily interpretable.
18 |
19 | `parameterpal::` provides a means of obtaining the parameters required
20 | for the beta distribution from interpretable conditions.
21 |
22 | ## installation
23 |
24 | ``` r
25 | devtools::install_github("softloud/parameterpal", build_vignettes = TRUE)
26 | ```
27 |
28 | ## launch tutorial
29 |
30 | After installation, the `parameterpal::` tutorial will be available in
31 | the Tutorial pane of Rstudio.
32 |
33 | ``` r
34 | # executing this code will launch the tutorial in your browser
35 | learnr::run_tutorial("ppalhelp", package = "parameterpal")
36 | ```
37 |
38 | ## documentation
39 |
40 | See this package’s
41 | [`pkgdown::`](https://pkgdown.r-lib.org/articles/pkgdown.html)-generated
42 | [site for more information](https://softloud.github.io/parameterpal/).
43 |
44 | ## intended user
45 |
46 | This package was developed for a friend and collaborator, computational
47 | ecologist [Dr Matthew Grainger](https://github.com/DrMattG), who
48 | previously used browser-based tools to obtain beta parameters to inform
49 | his rstats workflow. I hope he finds this package a useful augment to
50 | his codeflow.
51 |
52 | # vignette
53 |
54 | See `vignette("betapal")` for ~~more information~~ the same information,
55 | but from the handy ease of your Rstudio Viewer pane.
56 |
57 | Full disclosure, I need to update it after I fully understand this
58 | [gist](https://gist.github.com/daob/1422e978ff98bdf466fbcb4d9bf3e53e) I
59 | was provided with after posting the first version of this software to
60 | twitter. Open science at its best. Blogpost incoming on open science and
61 | why it’s good to share bad math and beta research code. So cool I’ve
62 | ended up with a better, more mathematically correct solution after
63 | posting it.
64 |
65 | # other distributions
66 |
67 | No reason the same math can’t be applied to other distributions. Open an
68 | issue if you’d like me to provide parameters from interpretable
69 | conditions for another distribution.
70 |
71 | # development on hold
72 |
73 | Currently softloud is finishing her dissertation, and with it so close,
74 | she’s focussed on wrapping that up before extending any software beyond
75 | what’s required for the thesis.
76 |
--------------------------------------------------------------------------------
/archive/beta_pal_firsttry.R:
--------------------------------------------------------------------------------
1 | #' Beta parameters from interpretable conditions
2 | #'
3 | #' Rather than knowing intrinsically what parameters are required for a
4 | #' distribution, scientists tend to have a sense of what value they
5 | #' *expect* a measure to take, *how much* they expect observations to fall
6 | #' *within* a certain distance of that value.
7 | #'
8 | #' Visualise the distribution with [beta_plot].
9 | #' See `vignette("beta_pal")` for derivations and more information.
10 | #'
11 | #' @param expected_value Expected value of beta distrbution from \[0,1\].
12 | #' @param within Specify distance `this_much` falls within.
13 | #' @param this_much What proportion falls `within` the specified interval.
14 | #' Defaults to 0.95.
15 | #'
16 | #' @return List of parameters appropriate for [rbeta] family of functions.
17 | #'
18 | #' @export
19 |
20 | beta_pal <- function(expected_value,
21 | within,
22 | this_much = 0.95) {
23 |
24 | # check inputs
25 | assertthat::assert_that(expected_value > 0 &
26 | expected_value < 1,
27 | msg = "expected_value must be from (0,1)")
28 | assertthat::assert_that(within > 0 &
29 | within < 1,
30 | msg = "within must be from (0,1)")
31 | assertthat::assert_that(
32 | this_much > 0 & this_much < 1,
33 | msg = "within expected_valuest be a value from [0,1].")
34 |
35 |
36 | # calculate beta distribution parameters
37 | gamma <- (1 - this_much)^-1
38 |
39 | shape1 <- expected_value * (
40 | (
41 | expected_value^2 * (1 / expected_value - 1)
42 | ) / (
43 | within^2 * (1 - this_much)
44 | ) - 1
45 | )
46 |
47 | shape2 <- shape1 / expected_value - shape1
48 |
49 | # return list of parameters
50 | return(list(shape1 = shape1,
51 | shape2 = shape2))
52 | }
53 |
--------------------------------------------------------------------------------
/docs/404.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
127 |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
128 |
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Specifying the width enables us to escape the ubiquitous tyranny of the arbitary bounds of 95 per cent, which doesn’t necessarily reflect our intuition, suppose we thought only
Rather than knowing intrinsically what parameters are required for a distribution, scientists tend to have indirect knowledge. A researcher may have a sense of what value they expect a measure to take, how many observations should fall within a certain distance of that value. As well as the overall shape of the data, the distribution. Distributions are not defined in the intuitive terms the scientist has, but equational parameters commonly (but not always) \(\mu\) and \(\sigma\) for the normal distribution, \(\lambda\) for the exponential, and \(\alpha\) and \(\beta\) for the beta distribution. However, calculating the required parameters is not necessarily straightfoward, despite the conditioning we receive from the normal distribution.
138 |
# set normal distribution example parameters
139 | norm_expected_value<-0.5
140 | norm_within<-0.2
141 |
For the normal distribution, obtaining the parameters from these assumptions is straightforward. The expected value and variance, which is to say, centre and spread, of the normal distribution, are both easily interpretable and translate directly to the parameters required for the distribution. So, to sample three values from a normal distribution where we expect a value of 0.5 with two-thirds of values falling within 0.2 of 0.5, we simply run the following code.
The first parameter is the expected value, and the second, the variance. For the normal distribution, the standard deviation is interpretable. We know two-thirds of values fall within one standard deviation of the mean. If we shade this area in a visualisation, it’s convincing that two-thirds of values fall within this range.
The beta distribution, on the other hand, requires two shape parameters, shape1 and shape2, which do not immediately reflect our intuition of what value we expect the measure to take, nor how much variance we expect.
171 |
But if we wished to sample from beta distribution, however, the parameters, shape1 and shape2, are not readily interpretable from expected value and variance. parameterpal:: provides a means of obtaining the parameters required for the beta distribution from interpretable conditions.
172 |
173 |
174 |
175 | beta distribution
176 |
The beta distribution has some really nice properties. It is bounded by a minimum value of 0 and a maximum of 1, so is perfect for modeling proportions.
177 |
Under some circumstances it can mimic a truncated bell curve, as well as flexibility for other shapes, such as a truncated parabola.
178 |
But this is not intuitive. What shape do we expect \(\mathrm{beta}(1,2)\) to take? It’s hard to intuit from the parameters 1 and 2. But, we likely do not expect a negatively sloped line.
This code was developed for softloud/simeta::, research software that supported softloud’s dissertation. This problem is a very small component of a larger collection of simulation functions for randomly generating meta-analysis data.
191 |
In this case, given a population \(N\), what proportion are allocated to the case and control groups? A desire to reflect the uncertainty of designed experiments motivated this code.
192 |
For example, even if case and control groups were assigned evenly, in experimental science there are many reasons individuals may drop out of the groups. Thus a proportion of \(N\) was sampled from a beta distribution (which is bounded between 0 and 1, as proportions are, too), with an intuition of how many drop outs are anticipated.
193 |
194 |
195 |
196 | citing this package
197 |
citation(package="parameterpal")
198 | #> Warning in citation(package = "parameterpal"): could not determine year for
199 | #> 'parameterpal' from package DESCRIPTION file
200 | #>
201 | #> To cite package 'parameterpal' in publications use:
202 | #>
203 | #> Charles Gray (NA). parameterpal: Intepretable parameters for the beta
204 | #> distribution. R package version 0.0.1.
205 | #>
206 | #> A BibTeX entry for LaTeX users is
207 | #>
208 | #> @Manual{,
209 | #> title = {parameterpal: Intepretable parameters for the beta distribution},
210 | #> author = {Charles Gray},
211 | #> note = {R package version 0.0.1},
212 | #> }
Rather than knowing intrinsically what parameters are required for a distribution, scientists tend to have a sense of what value they expect a measure to take, how many observations should fall within a certain distance of that value.
84 |
For the normal distribution, this is straightforward, as the parameters reflect the expected value and variance. However, for the beta distribution, the parameters are not so readily interpretable.
85 |
parameterpal:: provides a means of obtaining the parameters required for the beta distribution from interpretable conditions.
This package was developed for a friend and collaborator, computational ecologist Dr Matthew Grainger, who previously used browser-based tools to obtain beta parameters to inform his rstats workflow. I hope he finds this package a useful augment to his codeflow.
107 |
108 |
109 |
110 |
111 | vignette
112 |
See vignette("betapal") for more information the same information, but from the handy ease of your Rstudio Viewer pane.
113 |
Full disclosure, I need to update it after I fully understand this gist I was provided with after posting the first version of this software to twitter. Open science at its best. Blogpost incoming on open science and why it’s good to share bad math and beta research code. So cool I’ve ended up with a better, more mathematically correct solution after posting it.
114 |
115 |
116 |
117 | other distributions
118 |
No reason the same math can’t be applied to other distributions. Open an issue if you’d like me to provide parameters from interpretable conditions for another distribution.
119 |
120 |
121 |
122 | development on hold
123 |
Currently softloud is finishing her dissertation, and with it so close, she’s focussed on wrapping that up before extending any software beyond what’s required for the thesis.
Rather than knowing intrinsically what parameters are required for a
131 | distribution, scientists tend to have a sense of what value they
132 | expect a measure to take, how much they expect observations to fall
133 | within a certain distance of that value.
134 |
135 |
136 |
beta_pal(expected_value, within, this_much)
137 |
138 |
Arguments
139 |
140 |
141 |
142 |
expected_value
143 |
Expected value of beta distrbution from [0,1].
144 |
145 |
146 |
within
147 |
Specify distance this_much falls within.
148 |
149 |
150 |
this_much
151 |
What proportion falls within the specified interval.
152 |
153 |
154 |
155 |
Value
156 |
157 |
List of parameters appropriate for rbeta family of functions.
158 |
Details
159 |
160 |
This solution coded by by this most helpful
161 | gist.
162 |
Visualise the distribution with beta_plot.
163 | See vignette("beta_pal") for derivations and more information.
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
--------------------------------------------------------------------------------
/inst/_pkgdown.yml:
--------------------------------------------------------------------------------
1 | home:
2 | title: parameterpal
3 | description: Beta parameters from interpretable conditions
4 |
5 | template:
6 | params:
7 | bootswatch: sandstone
8 | opengraph:
9 | twitter:
10 | creator: "@cantabile"
11 | site: "@MMetaresearch"
12 |
--------------------------------------------------------------------------------
/inst/tutorials/ppalhelp/README.md:
--------------------------------------------------------------------------------
1 | # parameterparTutorial
2 | This Learnr tutorial was made for the `parameterpar`package made by @sofloud
3 |
4 | To bundle a tutorial into an R package:
5 |
6 | 1. Create a tutorials directory within the `inst` sub-directory of your package and then create a directory for your tutorial there (e.g. inst/tutorials/parameterparTutorial).
7 | 2. Add the .Rmd associated with your tutorial in that directory. (You may see a ..._files/_ directory or a .html file that periodically get generated adjacent to your .Rmd tutorial. You can safely ignore, delete, or exclude these from your package and from version control. They are temporary artifacts produced while rendering the tutorial on your specific system and aren’t likely to be portable to other environments.)
8 | 3. Add the `css` folder with the css file for the personalized styles for you package.
9 |
--------------------------------------------------------------------------------
/inst/tutorials/ppalhelp/css/miceeseese.css:
--------------------------------------------------------------------------------
1 | /******
2 | This css is based in Allison Horst custom css https://github.com/allisonhorst/explore-na/blob/master/css/custom_css.css
3 | *******/
4 |
5 | body {
6 | background-color: #ffffff;
7 | }
8 |
9 | em {
10 | color: purple;
11 | }
12 |
13 | .ace-tm {
14 | background-color: #ffffff;
15 | color: black;
16 | }
17 |
18 | .panel, pre {
19 | border-radius: 0;
20 | border-color: #e0e0e0;
21 | background-color: #ffffff;
22 | }
23 |
24 | .panel-default > .panel-heading {
25 | color: #555555;
26 | background-color: #ffffff;
27 | border-color: #dddddd;
28 | }
29 |
30 | .topicsList .topic {
31 | line-height: 40px;
32 | font-size: 15px;
33 | padding-left: 10px;
34 | cursor: pointer;
35 | background-repeat: no-repeat;
36 | background-size: 2px 80px;
37 | background-position: left 100%;
38 | background-position-y: 100%;
39 | border-bottom: 1px solid purple;
40 | -webkit-transition-property: background-position;
41 | transition-property: background-position;
42 | -webkit-transition-duration: 0.25s;
43 | transition-duration: 0.25s;
44 | }
45 |
46 | .topicsList .topic.current {
47 | background-color: purple;
48 | color: #ffffff;
49 | border: 0px;
50 | }
51 |
52 | code {
53 | color: gray;
54 | background-color: #eee1fc;
55 | font-size: 12px;
56 | }
57 |
58 | a {
59 | color: purple;
60 | text-decoration: none;
61 | }
62 |
63 | .btn-primary, .btn-success, .btn-info {
64 | color: #ffffff;
65 | background-color: purple;
66 | background-image: none;
67 | border: none;
68 | }
69 |
70 | .btn-default {
71 | color: #ffffff;
72 | background-color: purple;
73 | background-image: none;
74 | border: none;
75 | }
76 |
77 | h2 {
78 | color: purple;
79 | }
80 |
81 | h3 {
82 | color: purple;
83 | }
84 |
85 | h4 {
86 | color: purple;
87 | }
88 |
89 | h5 {
90 | color: purple;
91 | background-color: white;
92 | display: inline-block;
93 | }
--------------------------------------------------------------------------------
/inst/tutorials/ppalhelp/parameterpal_help.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "Obtain beta parameters from interpretable conditions."
3 | author: "Charles T. Gray"
4 | date: "`r Sys.Date()`"
5 | output:
6 | learnr::tutorial:
7 | progressive: true
8 | allow_skip: true
9 | css: css/miceeseese.css
10 | runtime: shiny_prerendered
11 | tutorial:
12 | version: 1.0
13 | description: "This tutorial presents a interactive version of vignettes for the parameterpal package"
14 | ---
15 |
16 | ```{r setup, include=FALSE}
17 | library(learnr)
18 | library(parameterpal)
19 | library(ggplot2)
20 | knitr::opts_chunk$set(
21 | collapse = TRUE,
22 | comment = "#>",
23 | fig.width = 5
24 | )
25 | ```
26 |
27 | ## Introduction
28 |
29 | Do you use beta distributions in your science? Do you find the parameters uninterpretable?
30 |
31 | Obtain parameters for the beta distribution from interpretable conditions.
32 |
33 | ### installation
34 |
35 | ```{r eval=FALSE}
36 | devtools::install_github("softloud/parameterpal", build_vignettes = TRUE)
37 | ```
38 |
39 |
40 | ## Using parameterpal::
41 |
42 | ```{r}
43 | library(parameterpal)
44 | ```
45 |
46 |
47 |
48 | ```{r}
49 | usage_example <- list(
50 | expected_value = 0.3,
51 | within = 0.2,
52 | this_much = 0.8
53 | )
54 | ```
55 |
56 | Suppose we expect a value of `r usage_example$expected_value`, with `r 100 * usage_example$this_much` per cent of observations falling within a distance of `r usage_example$within` from `r usage_example$expected_value`.
57 |
58 | That is, we expect `r 100 * usage_example$this_much` per cent of observations to fall within (`r usage_example$expected_value - usage_example$within`, `r usage_example$expected_value + usage_example$within`). Assuming data follow a beta distribution, what are its parameters?
59 |
60 | ```{r}
61 | beta_pal(expected_value = usage_example$expected_value,
62 | within = usage_example$within,
63 | this_much = usage_example$this_much)
64 | ```
65 |
66 | We can plot this intuition to see the shape of the resulting beta distribution.
67 |
68 | ```{r }
69 | beta_plot(expected_value = usage_example$expected_value,
70 | within = usage_example$within,
71 | this_much = usage_example$this_much)
72 | ```
73 |
74 | Specifying the width enables us to escape the ubiquitous tyranny of the arbitary bounds of 95 per cent, which doesn't necessarily reflect our intuition, suppose we thought only
75 |
76 | ```{r}
77 | new_example_width <- 0.3
78 |
79 | ```
80 |
81 |
82 | `r new_example_width * 100` per cent of values fall within the interval.
83 |
84 | ```{r }
85 | beta_pal(expected_value = usage_example$expected_value,
86 | within = usage_example$within,
87 | this_much = new_example_width)
88 | ```
89 |
90 | ## Exercises
91 |
92 | ### Plotting
93 |
94 | Lest try to use `parameterpal` in this interactive code chunks. Whenever you encounter one, you can click _Submit Answer_ to run (or re-run) the code in the chunk. If there is a _Solution_ button, you can click it to see a proposed answer.
95 |
96 | Complete the code below with your own values and run de code to see the result
97 |
98 | ```{r using_parameterpal_plot, exercise=TRUE}
99 |
100 | beta_plot(expected_value = ___,
101 | within = ___,
102 | this_much = ___)
103 |
104 | ```
105 |
106 | ```{r using_parameterpal_plot-hint}
107 |
108 | Remeber that you _expect a value_, with a per cent of observations falling _within_ a distance from that _expected_value_.
109 |
110 | ```
111 |
112 | ```{r using_parameterpal_plot-solution}
113 |
114 | # This is an example solution,
115 | # you can use any value that you need in every argument of the beta_plot function
116 | beta_plot(expected_value = 0.5,
117 | within = 0.3,
118 | this_much = 0.9)
119 |
120 | ```
121 |
122 | ### Getting the parameters
123 |
124 | Now, you can try to get the parameters. Complete the code below with your own values and run de code to see the result.
125 |
126 |
127 | ```{r using_parameterpal, exercise=TRUE}
128 | beta_pal(expected_value = ___,
129 | within = ___,
130 | this_much = ___)
131 | ```
132 |
133 | ```{r using_parameterpal-hint}
134 |
135 | Remeber that you _expect a value_, with a per cent of observations falling _within_ a distance from that _expected_value_.
136 |
137 | ```
138 |
139 | ```{r using_parameterpal-solution}
140 | # This is an example solution,
141 | # you can use any value that you need in every argument of the beta_plot function
142 |
143 | beta_pal(expected_value = 0.5,
144 | within = 0.3,
145 | this_much = 0.9)
146 |
147 | ```
148 |
149 | ## Motivation
150 |
151 | Rather than knowing intrinsically what parameters are required for a distribution, scientists tend to have indirect knowledge. A researcher may have a sense of what value they *expect* a measure to take, *how many* observations should fall *within* a certain distance of that value. As well as the overall shape of the data, the distribution. Distributions are not defined in the intuitive terms the scientist has, but equational parameters commonly (but not always) $\mu$ and $\sigma$ for the normal distribution, $\lambda$ for the exponential, and $\alpha$ and $\beta$ for the beta distribution. However, calculating the required parameters is not necessarily straightfoward, despite the conditioning we receive from the normal distribution.
152 |
153 | ```{r}
154 | # set normal distribution example parameters
155 | norm_expected_value <- 0.5
156 | norm_within <- 0.2
157 | ```
158 |
159 |
160 | For the normal distribution, obtaining the parameters from these assumptions is straightforward. The expected value and variance, which is to say, centre and spread, of the normal distribution, are both easily interpretable and translate directly to the parameters required for the distribution. So, to sample three values from a normal distribution where we expect a value of `r norm_expected_value` with two-thirds of values falling within `r norm_within` of `r norm_expected_value`, we simply run the following code.
161 |
162 | ```{r}
163 | rnorm(n = 3,
164 | mean = norm_expected_value,
165 | sd = norm_within)
166 | ```
167 |
168 |
169 | The first parameter is the expected value, and the second, the variance. For the normal distribution, the standard deviation is interpretable. We know two-thirds of values fall within one standard deviation of the mean. If we shade this area in a visualisation, it's convincing that two-thirds of values fall within this range.
170 |
171 | ```{r}
172 | library(ggplot2)
173 | ggplot() +
174 | xlim(0, 1) +
175 | geom_rect(
176 | aes(
177 | xmin = norm_expected_value - norm_within,
178 | xmax = norm_expected_value + norm_within,
179 | ymin = 0,
180 | ymax = Inf,
181 | alpha = 0.2)) +
182 | stat_function(
183 | fun = dnorm,
184 | args = list(mean = norm_expected_value, sd = norm_within)
185 | ) +
186 | theme(legend.position = "none") +
187 | labs(
188 | y = NULL,
189 | title = sprintf("normal(%g, %g)", norm_expected_value, norm_within),
190 | caption = sprintf("Two-thirds of normally distributed values fall within %g of %g.", norm_expected_value, norm_within)
191 | )
192 | ```
193 |
194 | The beta distribution, on the other hand, requires two shape parameters, `shape1` and `shape2`, which do not immediately reflect our intuition of what value we expect the measure to take, nor how much variance we expect.
195 |
196 |
197 |
198 | But if we wished to sample from beta distribution, however, the parameters, `shape1` and `shape2`, are not readily interpretable from expected value and variance. `parameterpal::` provides a means of obtaining the parameters required for the beta distribution from interpretable conditions.
199 |
200 | ## beta distribution
201 |
202 | The beta distribution has some really nice properties. It is bounded by a minimum value of 0 and a maximum of 1, so is perfect for modeling proportions.
203 |
204 | Under some circumstances it can mimic a truncated bell curve, as well as flexibility for other shapes, such as a truncated parabola.
205 |
206 | But this is not intuitive. What shape do we expect $\mathrm{beta}(1,2)$ to take? It's hard to intuit from the parameters 1 and 2. But, we likely do not expect a negatively sloped line.
207 |
208 | ```{r}
209 | ggplot() +
210 | xlim(0, 1) +
211 | stat_function(
212 | fun = dbeta,
213 | args = list(shape1 = 1, shape2 = 2)
214 | )
215 | ```
216 |
217 |
218 | ## Original application
219 |
220 | This code was developed for [`softloud/simeta::`](https://github.com/softloud/simeta), research software that supported `softloud`'s dissertation. This problem is a very small component of a larger collection of simulation functions for randomly generating meta-analysis data.
221 |
222 | In this case, given a population $N$, what proportion are allocated to the case and control groups? A desire to reflect the uncertainty of designed experiments motivated this code.
223 |
224 | For example, even if case and control groups were assigned evenly, in experimental science there are many reasons individuals may drop out of the groups. Thus a proportion of $N$ was sampled from a beta distribution (which is bounded between 0 and 1, as proportions are, too), with an intuition of how many drop outs are anticipated.
225 |
226 | ## Citing this package
227 |
228 | ```{r warning=FALSE}
229 | citation(package = "parameterpal")
230 | ```
231 |
232 | ## Contributing and Maintaining
233 |
234 | Contributions are very welcome. By submitting your work, you are agreeing that it may incorporated in either original or edited form and released under the same license as the rest of this material (see License). If your contribution is incorporated, I will add you to the acknowledgments (unless you request otherwise).
235 |
236 | The source code for this package is stored on GitHub at:
237 |
238 | https://github.com/softloud/parameterpal
239 |
240 | If you know how to use Git and GitHub and would like to change, fix, or add something, please submit a pull request. If you want to report an error, ask a question, or make a suggestion, please file an issue in the repository. You need to have a GitHub account in order to do this, but do not need to know how to use Git.
241 |
242 | Finally, I always enjoy hearing how people have used this package.
243 |
244 |
245 | ## License
246 |
247 | Copyright (c) 2020 Charles Gray
248 |
249 | This packages is licensed under the [MIT License ](https://opensource.org/licenses/MIT)
250 |
251 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
252 |
253 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
254 |
255 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/man/beta_pal.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/beta_pal.R
3 | \name{beta_pal}
4 | \alias{beta_pal}
5 | \title{Beta parameters from interpretable conditions}
6 | \usage{
7 | beta_pal(expected_value, within, this_much)
8 | }
9 | \arguments{
10 | \item{expected_value}{Expected value of beta distrbution from [0,1].}
11 |
12 | \item{within}{Specify distance \code{this_much} falls within.}
13 |
14 | \item{this_much}{What proportion falls \code{within} the specified interval.}
15 | }
16 | \value{
17 | List of parameters appropriate for \link{rbeta} family of functions.
18 | }
19 | \description{
20 | Rather than knowing intrinsically what parameters are required for a
21 | distribution, scientists tend to have a sense of what value they
22 | \emph{expect} a measure to take, \emph{how much} they expect observations to fall
23 | \emph{within} a certain distance of that value.
24 | }
25 | \details{
26 | This solution coded by by this most helpful
27 | \href{https://gist.github.com/daob/1422e978ff98bdf466fbcb4d9bf3e53e}{gist}.
28 |
29 | Visualise the distribution with \link{beta_plot}.
30 | See \code{vignette("beta_pal")} for derivations and more information.
31 | }
32 |
--------------------------------------------------------------------------------
/man/beta_plot.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/beta_plot.R
3 | \name{beta_plot}
4 | \alias{beta_plot}
5 | \title{Plot beta distribution}
6 | \usage{
7 | beta_plot(
8 | expected_value,
9 | within,
10 | this_much,
11 | caption_width = 70,
12 | theme_default = TRUE
13 | )
14 | }
15 | \arguments{
16 | \item{expected_value}{Expected value of beta distrbution from [0,1].}
17 |
18 | \item{within}{Specify distance \code{this_much} falls within.}
19 |
20 | \item{this_much}{What proportion falls \code{within} the specified interval.}
21 |
22 | \item{caption_width}{Option to control the width of the caption, defaults to
23 | 70.}
24 |
25 | \item{theme_default}{Currently defaults to \code{ggthemes::theme_tufte}.}
26 | }
27 | \description{
28 | This function utlises \link{beta_pal} to produce a visualisation of the beta
29 | distribution, with user-specified interpretable conditions.
30 | }
31 |
--------------------------------------------------------------------------------
/man/figures/README-unnamed-chunk-11-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/softloud/parameterpal/003bd0c0da0c1ff877aa9ae2c9c1934bc73467dd/man/figures/README-unnamed-chunk-11-1.png
--------------------------------------------------------------------------------
/man/figures/README-unnamed-chunk-12-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/softloud/parameterpal/003bd0c0da0c1ff877aa9ae2c9c1934bc73467dd/man/figures/README-unnamed-chunk-12-1.png
--------------------------------------------------------------------------------
/man/figures/README-unnamed-chunk-13-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/softloud/parameterpal/003bd0c0da0c1ff877aa9ae2c9c1934bc73467dd/man/figures/README-unnamed-chunk-13-1.png
--------------------------------------------------------------------------------
/man/figures/README-unnamed-chunk-14-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/softloud/parameterpal/003bd0c0da0c1ff877aa9ae2c9c1934bc73467dd/man/figures/README-unnamed-chunk-14-1.png
--------------------------------------------------------------------------------
/man/figures/README-unnamed-chunk-15-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/softloud/parameterpal/003bd0c0da0c1ff877aa9ae2c9c1934bc73467dd/man/figures/README-unnamed-chunk-15-1.png
--------------------------------------------------------------------------------
/man/figures/README-unnamed-chunk-5-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/softloud/parameterpal/003bd0c0da0c1ff877aa9ae2c9c1934bc73467dd/man/figures/README-unnamed-chunk-5-1.png
--------------------------------------------------------------------------------
/man/figures/README-unnamed-chunk-6-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/softloud/parameterpal/003bd0c0da0c1ff877aa9ae2c9c1934bc73467dd/man/figures/README-unnamed-chunk-6-1.png
--------------------------------------------------------------------------------
/man/figures/README-unnamed-chunk-7-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/softloud/parameterpal/003bd0c0da0c1ff877aa9ae2c9c1934bc73467dd/man/figures/README-unnamed-chunk-7-1.png
--------------------------------------------------------------------------------
/man/figures/README-unnamed-chunk-8-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/softloud/parameterpal/003bd0c0da0c1ff877aa9ae2c9c1934bc73467dd/man/figures/README-unnamed-chunk-8-1.png
--------------------------------------------------------------------------------
/man/figures/README-unnamed-chunk-9-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/softloud/parameterpal/003bd0c0da0c1ff877aa9ae2c9c1934bc73467dd/man/figures/README-unnamed-chunk-9-1.png
--------------------------------------------------------------------------------
/man/get_objective.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/get_objective.R
3 | \name{get_objective}
4 | \alias{get_objective}
5 | \title{Objective function}
6 | \usage{
7 | get_objective(desired_mean, desired_dist, desired_mass)
8 | }
9 | \arguments{
10 | \item{desired_mean}{expected_value.}
11 |
12 | \item{desired_dist}{within.}
13 |
14 | \item{desired_mass}{this_much.
15 |
16 | This function is called by \link{beta_pal} internally.}
17 | }
18 | \description{
19 | Function that takes desrired mean, distance, and probability, and outputs
20 | another function to be optimized. Adapted into \link{beta_pal}.
21 | }
22 | \details{
23 | This solution coded by by this most helpful
24 | \href{https://gist.github.com/daob/1422e978ff98bdf466fbcb4d9bf3e53e}{gist}.
25 | }
26 |
--------------------------------------------------------------------------------
/man/pipe.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/utils-pipe.R
3 | \name{\%>\%}
4 | \alias{\%>\%}
5 | \title{Pipe operator}
6 | \usage{
7 | lhs \%>\% rhs
8 | }
9 | \description{
10 | See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details.
11 | }
12 | \keyword{internal}
13 |
--------------------------------------------------------------------------------
/parameterpal.Rproj:
--------------------------------------------------------------------------------
1 | Version: 1.0
2 |
3 | RestoreWorkspace: No
4 | SaveWorkspace: No
5 | AlwaysSaveHistory: Default
6 |
7 | EnableCodeIndexing: Yes
8 | UseSpacesForTab: Yes
9 | NumSpacesForTab: 2
10 | Encoding: UTF-8
11 |
12 | RnwWeave: knitr
13 | LaTeX: pdfLaTeX
14 |
15 | AutoAppendNewline: Yes
16 | StripTrailingWhitespace: Yes
17 |
18 | BuildType: Package
19 | PackageUseDevtools: Yes
20 | PackageInstallArgs: --no-multiarch --with-keep.source
21 | PackageRoxygenize: rd,collate,namespace,vignette
22 |
--------------------------------------------------------------------------------
/tests/testthat.R:
--------------------------------------------------------------------------------
1 | library(testthat)
2 | library(parameterpal)
3 |
4 | test_check("parameterpal")
5 |
--------------------------------------------------------------------------------
/tests/testthat/test-beta_plot.R:
--------------------------------------------------------------------------------
1 | expected_value <- 0.3
2 | this_much <- 0.9
3 | within <- 0.2
4 |
5 | # should this be an S3 method
6 |
7 | test_that("beta plot makes a plot", {
8 | expect_s3_class(beta_plot(expected_value, this_much, within), "ggplot")
9 | })
10 |
--------------------------------------------------------------------------------
/tests/testthat/test-betapal.R:
--------------------------------------------------------------------------------
1 | expected_value <- 0.3
2 | this_much <- 0.9
3 | within <- 0.2
4 |
5 | test_that("betapal produces list", {
6 | expect_type(beta_pal(expected_value, this_much, within), "list")
7 | })
8 |
9 |
10 | test_that("betapal gets correct answers", {
11 | betapars <- beta_pal(expected_value, this_much, within)
12 |
13 | approx_mean <- rbeta(1e5, betapars[[1]], betapars[[2]]) %>% mean()
14 |
15 | approx_mass <-
16 | pbeta(q = expected_value + within, betapars[[1]], betapars[[2]]) -
17 | pbeta(q = expected_value - within, betapars[[1]], betapars[[2]])
18 |
19 | expect_true(abs(approx_mean - expected_value) < 0.1)
20 |
21 | expect_true(abs(approx_mass - this_much) < 0.1)
22 |
23 |
24 | })
25 |
--------------------------------------------------------------------------------
/vignettes/.gitignore:
--------------------------------------------------------------------------------
1 | *.html
2 | *.R
3 |
--------------------------------------------------------------------------------
/vignettes/betapal.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "Obtain beta parameters from interpretable conditions"
3 | author: "Charles T. Gray"
4 | date: "`r Sys.Date()`"
5 | output: rmarkdown::html_vignette
6 | vignette: >
7 | %\VignetteIndexEntry{betapal}
8 | %\VignetteEngine{knitr::rmarkdown}
9 | %\VignetteEncoding{UTF-8}
10 | ---
11 |
12 | ```{r, include = FALSE}
13 | knitr::opts_chunk$set(
14 | collapse = TRUE,
15 | comment = "#>",
16 | fig.width = 5
17 | )
18 |
19 | options(rmarkdown.html_vignette.check_title = FALSE)
20 |
21 | ```
22 |
23 |
24 | Do you use beta distributions in your science? Do you find the parameters uninterpretable?
25 |
26 | Obtain parameters for the beta distribution from interpretable conditions.
27 |
28 | ## installation
29 |
30 | ```{r eval=FALSE}
31 | devtools::install_github("softloud/parameterpal", build_vignettes = TRUE)
32 | ```
33 |
34 |
35 | ## using `parameterpal::`
36 |
37 | ```{r}
38 | library(parameterpal)
39 | ```
40 |
41 |
42 |
43 | ```{r}
44 | usage_example <- list(
45 | expected_value = 0.3,
46 | within = 0.2,
47 | this_much = 0.8
48 | )
49 |
50 |
51 | ```
52 |
53 | Suppose we expect a value of `r usage_example$expected_value`, with `r 100 * usage_example$this_much` per cent of observations falling within a distance of `r usage_example$within` from `r usage_example$expected_value`.
54 |
55 | That is, we expect `r 100 * usage_example$this_much` per cent of observations to fall within (`r usage_example$expected_value - usage_example$within`, `r usage_example$expected_value + usage_example$within`). Assuming data follow a beta distribution, what are its parameters?
56 |
57 | ```{r}
58 | beta_pal(expected_value = usage_example$expected_value,
59 | within = usage_example$within,
60 | this_much = usage_example$this_much)
61 |
62 | ```
63 |
64 | We can plot this intuition to see the shape of the resulting beta distribution.
65 |
66 | ```{r }
67 | beta_plot(expected_value = usage_example$expected_value,
68 | within = usage_example$within,
69 | this_much = usage_example$this_much)
70 |
71 | ```
72 |
73 | Specifying the width enables us to escape the ubiquitous tyranny of the arbitary bounds of 95 per cent, which doesn't necessarily reflect our intuition, suppose we thought only
74 |
75 | ```{r}
76 | new_example_width <- 0.3
77 |
78 | ```
79 |
80 |
81 | `r new_example_width * 100` per cent of values fall within the interval.
82 |
83 | ```{r }
84 | beta_pal(expected_value = usage_example$expected_value,
85 | within = usage_example$within,
86 | this_much = new_example_width)
87 |
88 | ```
89 |
90 | ## motivation
91 |
92 | Rather than knowing intrinsically what parameters are required for a distribution, scientists tend to have indirect knowledge. A researcher may have a sense of what value they *expect* a measure to take, *how many* observations should fall *within* a certain distance of that value. As well as the overall shape of the data, the distribution. Distributions are not defined in the intuitive terms the scientist has, but equational parameters commonly (but not always) $\mu$ and $\sigma$ for the normal distribution, $\lambda$ for the exponential, and $\alpha$ and $\beta$ for the beta distribution. However, calculating the required parameters is not necessarily straightfoward, despite the conditioning we receive from the normal distribution.
93 |
94 | ```{r}
95 | # set normal distribution example parameters
96 | norm_expected_value <- 0.5
97 | norm_within <- 0.2
98 |
99 | ```
100 |
101 |
102 | For the normal distribution, obtaining the parameters from these assumptions is straightforward. The expected value and variance, which is to say, centre and spread, of the normal distribution, are both easily interpretable and translate directly to the parameters required for the distribution. So, to sample three values from a normal distribution where we expect a value of `r norm_expected_value` with two-thirds of values falling within `r norm_within` of `r norm_expected_value`, we simply run the following code.
103 |
104 | ```{r}
105 | rnorm(n = 3,
106 | mean = norm_expected_value,
107 | sd = norm_within)
108 |
109 | ```
110 |
111 |
112 | The first parameter is the expected value, and the second, the variance. For the normal distribution, the standard deviation is interpretable. We know two-thirds of values fall within one standard deviation of the mean. If we shade this area in a visualisation, it's convincing that two-thirds of values fall within this range.
113 |
114 | ```{r}
115 |
116 | library(ggplot2)
117 |
118 | ggplot() +
119 | xlim(0, 1) +
120 | geom_rect(
121 | aes(
122 | xmin = norm_expected_value - norm_within,
123 | xmax = norm_expected_value + norm_within,
124 | ymin = 0,
125 | ymax = Inf,
126 | alpha = 0.2)) +
127 | stat_function(
128 | fun = dnorm,
129 | args = list(mean = norm_expected_value, sd = norm_within)
130 | ) +
131 | theme(legend.position = "none") +
132 | labs(
133 | y = NULL,
134 | title = sprintf("normal(%g, %g)", norm_expected_value, norm_within),
135 | caption = sprintf("Two-thirds of normally distributed values fall within %g of %g.", norm_within, norm_expected_value)
136 | )
137 |
138 |
139 | ```
140 |
141 | The beta distribution, on the other hand, requires two shape parameters, `shape1` and `shape2`, which do not immediately reflect our intuition of what value we expect the measure to take, nor how much variance we expect.
142 |
143 |
144 |
145 | But if we wished to sample from beta distribution, however, the parameters, `shape1` and `shape2`, are not readily interpretable from expected value and variance. `parameterpal::` provides a means of obtaining the parameters required for the beta distribution from interpretable conditions.
146 |
147 | ## beta distribution
148 |
149 | The beta distribution has some really nice properties. It is bounded by a minimum value of 0 and a maximum of 1, so is perfect for modeling proportions.
150 |
151 | Under some circumstances it can mimic a truncated bell curve, as well as flexibility for other shapes, such as a truncated parabola.
152 |
153 | But this is not intuitive. What shape do we expect $\mathrm{beta}(1,2)$ to take? It's hard to intuit from the parameters 1 and 2. But, we likely do not expect a negatively sloped line.
154 |
155 | ```{r}
156 | ggplot() +
157 | xlim(0, 1) +
158 | stat_function(
159 | fun = dbeta,
160 | args = list(shape1 = 1, shape2 = 2)
161 | )
162 |
163 |
164 |
165 | ```
166 |
167 |
168 | ## original application
169 |
170 | This code was developed for [`softloud/simeta::`](https://github.com/softloud/simeta), research software that supported `softloud`'s dissertation. This problem is a very small component of a larger collection of simulation functions for randomly generating meta-analysis data.
171 |
172 | In this case, given a population $N$, what proportion are allocated to the case and control groups? A desire to reflect the uncertainty of designed experiments motivated this code.
173 |
174 | For example, even if case and control groups were assigned evenly, in experimental science there are many reasons individuals may drop out of the groups. Thus a proportion of $N$ was sampled from a beta distribution (which is bounded between 0 and 1, as proportions are, too), with an intuition of how many drop outs are anticipated.
175 |
176 | # citing this package
177 |
178 | ```{r}
179 | citation(package = "parameterpal")
180 |
181 | ```
182 |
183 |
--------------------------------------------------------------------------------