ExpectedRange: Expected range

Description Usage Arguments Value Examples

View source: R/statistical-functions.R

Description

Calculates the expected range of n observations drawn from a distibution with standard deviation sd

Usage

1

Arguments

sd

standard deviation

n

number of observations

Value

numeric

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
ExpectedRange(5, 15)

## Not run: 
library(tidyverse)
f <- function(x, n) n * x * pnorm(x)^(n - 1) * dnorm(x)
ests <- expand.grid(mean = 0, sd = 2, n = 15, rep = 1:1000, i = 1:15) %>%
  mutate(rdv = rnorm(n(), mean, sd)) %>%
  group_by(mean, sd, n, rep) %>%
  summarise(sd_hat = sd(rdv),
            obs.range = diff(range(rdv))) %>%
  group_by(mean, sd, n, rep) %>%
  mutate(sd.range = 2*sd_hat*integrate(f,-Inf,Inf, n = n)$value,
         exp.range = ExpectedRange(sd, n))
ests %>%
  gather(estimate, value, -mean, -sd, -n, -rep, -sd_hat, -exp.range) %>%
  ggplot(aes(x = estimate, y = value)) +
  geom_boxplot() +
  geom_hline(aes(yintercept = exp.range)) +
  facet_wrap(~n)
  
## End(Not run)

EarthSystemDiagnostics/ecustools documentation built on Jan. 15, 2022, 5:22 p.m.