8 Function factories

8.1 Prerequisites

8.2 Factory fundamentals

  1. Q: Base R contains two function factories, approxfun() and ecdf(). Read their documentation and experiment to figure out what the functions do and what they return.

    A:

  2. Q: Create a function pick() that takes an index, i, as an argument and returns a function with an argument x that subsets x with i.

    A:

  3. Q: Create a function that creates functions that compute the ith central moment of a numeric vector. You can test it by running the following code:

    A:

  4. Q: What happens if you don’t use a closure? Make predictions, then verify with the code below.

    A:

  5. Q: What happens if you use <- instead of <<-? Make predictions, then verify with the code below.

    A:

8.3 Graphical factories

  1. Q: Compare and contrast ggplot2::label_bquote() with scales::number_format().

    A:

8.4 Statistical factories

  1. Q: In boot_model(), why don’t I need to force the evaluation of df or model?

    A:

  2. Q: Why might you formulate the Box-Cox transformation like this?

    A:

  3. Q: Why don’t you need to worry that boot_permute() stores a copy of the data inside the function that it generates?

    A:

  4. Q: How much time does ll_poisson2() save compared to ll_poisson1()? Use bench::mark() to see how much faster the optimisation occurs. How does changing the length of x change the results?

    A:

8.5 Function factories + functionals

  1. Q: Which of the following commands is equivalent to with(x, f(z))?

    1. x$f(x$z).
    2. f(x$z).
    3. x$f(z).
    4. f(z).
    5. It depends.

    A:

  2. Q: Compare and contrast the effects of env_bind() vs. attach() for the following code.

    A:

8.6 Old exercises

8.7 Closures

  1. Q: Why are functions created by other functions called closures?
    A: As stated in the book:

    because they enclose the environment of the parent function and can access all its variables.

  2. Q: What does the following statistical function do? What would be a better name for it? (The existing name is a bit of a hint.)

    A: It is the logarithm, when lambda equals zero and x ^ lambda - 1 / lambda otherwise. A better name might be box_cox_transformation (one parametric), you can read about it (here)[https://en.wikipedia.org/wiki/Power_transform].

  3. Q: What does approxfun() do? What does it return?
    A: approxfun basically takes a combination of 2-dimensional data points + some extra specifications as arguments and returns a stepwise linear or constant interpolation function (defined on the range of given x-values, by default).

  4. Q: What does ecdf() do? What does it return?
    A: “ecdf” means empirical density function. For a numeric vector, ecdf() returns the appropriate density function (of class “ecdf”, which is inheriting from class “stepfun”). You can describe it’s behaviour in 2 steps. In the first part of it’s body, the (x,y) pairs for the nodes of the density function are calculated. In the second part these pairs are given to approxfun.

  5. Q: Create a function that creates functions that compute the ith central moment of a numeric vector. You can test it by running the following code:

    A: For a discrete formulation look here

  6. Q: Create a function pick() that takes an index, i, as an argument and returns a function with an argument x that subsets x with i.

    A:

8.8 Case study: numerical integration

  1. Q: Instead of creating individual functions (e.g., midpoint(), trapezoid(), simpson(), etc.), we could store them in a list. If we did that, how would that change the code? Can you create the list of functions from a list of coefficients for the Newton-Cotes formulae?
    A:

  2. Q: The trade-off between integration rules is that more complex rules are slower to compute, but need fewer pieces. For sin() in the range [0, \(\pi\)], determine the number of pieces needed so that each rule will be equally accurate. Illustrate your results with a graph. How do they change for different functions? sin(1 / x^2) is particularly challenging.
    A: