A pretty neat thing
(about R)

Collin Nolte

12/07/2020

First, the neat things





[This slide intentionally left blank]

What the functional?

GLMs.

\[ \begin{aligned} f(y \ | \theta) &= a(\theta) b(y)\exp(y \ Q(\theta)) \\ E(Y) &= \mu \\ g(\mu) &= \eta = X \beta \end{aligned} \] and we typically go so far as to have a loss function \(L\) to choose between competing models.



fit <- glm(y ~ ., family = binomial(link = "logit"))

Functionals in R


Here, we can give a simple illustration of what this looks like in R. We have a simple bootstrap process, and a statistic that we would like to bootstrap

bootstrap <- function(x, statistic, n = 1000L) {
  replicate(n, expr = {
    samp <- sample(x, replace = TRUE)
    statistic(samp)
  })
}

Standard statistics


x <- rnorm(100L)
bs_mean <- bootstrap(x, mean)
bs_median <- bootstrap(x, median)

Problems arise

Suppose that in addition to the functions above, I also wanted to consider a trimmed mean, to account for potential outliers. We’ll decide to remove the largest and smallest 10%

bootstrap <- function(x, statistic, n = 1000L) {
  replicate(n, expr = {
    samp <- sample(x, replace = TRUE)
    statistic(samp)
  })
}

bs_trim <- bootstrap(x, mean(trim = 0.1))
## Error in mean.default(trim = 0.1): argument "x" is missing, with no default


mean(trim = .1)("Doesn't matter what I put here")
## Error in mean.default(trim = 0.1): argument "x" is missing, with no default

Magic?

bootstrap <- function(x, statistic, n = 1000L) {
  statistic <- not_magic(statistic)
  replicate(n, expr = {
    samp <- sample(x, replace = TRUE)
    statistic(samp)
  })
}

bs_trim <- bootstrap(x, mean(trim = 0.1))

Move to R




Citations