Learning Objectives for Quiz 3

Functions

Why Use Functions?

  1. Explain how functions are analogous to data structures in terms of the “nouns” versus “verbs” of programming.
  2. Give situations when a block of code should be turned into a function.

Function Syntax in R

  1. State the syntax for defining a function in R using function().
  2. Given a block of code to turn into a function, produce a function using function().
  3. State the names for the inputs and outputs of a function in R.

Named Arguments and Default Values

  1. Include named arguments in a function to pass desired inputs into the function.
  2. State and use the appropriate operator to bind a function argument when calling a function.
  3. Include named arguments with default values in a function.
  4. Call a function using named and unnamed arguments.
  5. Determine the output of a given function with named or unnamed arguments.
  6. Use stopifnot() to check function arguments for desired / necessary characteristics.

Function Environments and Interfacing with Functions

  1. Explain the difference between a global environment and local / internal environment in R.
  2. Describe the rules for how the interval environment of a function interfaces with the global environment.
  3. Given a block of code including a function definition, describe the values stored in the global and internal environments before, during, and after a function call.
  4. State the best-practices for interfacing with a function.

Gradient / Steepest Descent

  1. Explain how to find the minima / maxima of a smooth function of a single variable using calculus.
  2. Describe the procedure for using gradient descent to find the minima of a function, and relate this to the analogy of walking down a hill when you can only see one step ahead of you at any given time.
  3. Explain why gradient descent may not find the global minimum of a function when multiple minima exist, and relate this to the hill analogy.
  4. Explain the rationale for the stopping criteria for gradient descent.
  5. Implement gradient descent, or some portion of the gradient descent algorithm, as a function in R.

Writing a Family of Functions for Related Tasks

  1. Explain why you might want to write a collection of functions to perform tasks related to a single object.
  2. Describe good coding conventions for writing functions for:

Sourcing a File

  1. Compare and contrast an R Markdown file and an R script.
  2. Create an R script in RStudio.
  3. Give the file suffix for R Markdown files and R scripts.
  4. Source functions from an R script using source().

Function Documentation

  1. Explain why you should always (yes, always) write documentation for a function you write.
  2. State the three things (at the very least) you should document when writing a function header.
  3. Use roxygen-style formatting (that’s the #' stuff) to write header comments for a given function.

Graphics in R

plot()

  1. Use plot() to generate scatter plots and line graphs from numeric vectors.
  2. Use the arguments of plot() to:
  3. Use the mfrow argument of par() to create a grid of plots.
  4. Save a PDF of a plot using pdf() in combination with graphics.off()

points(), lines(), and abline()

  1. Add a scatter plot or line graph to an existing plot using points() and lines(), respectively.
  2. Add horizontal and vertical lines to an existing plot using abline.
  3. Add a line with a given intercept and slope to an existing plot using abline.
  4. Construct a multi-layer graph using plot() in combination with points(), lines(), and abline().

hist and density

  1. Create a histogram from a numeric vector using hist().
  2. Use the arguments of hist() to:
  3. Compute and plot a kernel density estimate (more on this in a later lecture) using density().

curve and rug

  1. Use curve() to plot the graph of an expression on a desired interval.
  2. Use curve() to add the plot of the graph of an expression to an existing plot.
  3. Add a rug plot to an existing plot using rug().

Color Palettes

  1. You do not need to memorize the function names for the different color palettes.
  2. State the argument and return value for the functions for the different color palettes in R.
  3. Use colorRampPalette() to generate a custom color palette along a gradient of colors.

ggformula

  1. Explain what the gg in ggformula stands for.
  2. Give the general grammar used by all plotting functions in ggformula.
  3. Use ggformula to generate:
  4. Use the pipe operator %>% to add elements to a ggformula plot.
  5. Use the | operator to condition (“facet”) a graph on a categorical variable.
  6. Use gf_lm and gf_smooth to add a least-squares line / nonparametric smoother to a scatter plot.

Packages in R

  1. Explain what an R package does.
  2. Install an R package from CRAN using install.packages().
  3. Load an R package using library().