Learning Objectives for Quiz 3
Functions
Why Use Functions?
- Explain how functions are analogous to data structures in terms of the “nouns” versus “verbs” of programming.
- Give situations when a block of code should be turned into a function.
Function Syntax in R
- State the syntax for defining a function in R using function().
- Given a block of code to turn into a function, produce a function using function().
- State the names for the inputs and outputs of a function in R.
Named Arguments and Default Values
- Include named arguments in a function to pass desired inputs into the function.
- State and use the appropriate operator to bind a function argument when calling a function.
- Include named arguments with default values in a function.
- Call a function using named and unnamed arguments.
- Determine the output of a given function with named or unnamed arguments.
- Use stopifnot()to check function arguments for desired / necessary characteristics.
Function Environments and Interfacing with Functions
- Explain the difference between a global environment and local / internal environment in R.
- Describe the rules for how the interval environment of a function interfaces with the global environment.
- 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.
- State the best-practices for interfacing with a function.
Gradient / Steepest Descent
- Explain how to find the minima / maxima of a smooth function of a single variable using calculus.
- 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.
- Explain why gradient descent may not find the global minimum of a function when multiple minima exist, and relate this to the hill analogy.
- Explain the rationale for the stopping criteria for gradient descent.
- Implement gradient descent, or some portion of the gradient descent algorithm, as a function in R.
- Explain why you might want to write a collection of functions to perform tasks related to a single object.
- Describe good coding conventions for writing functions for:
- the same kind of object
- the same kind of task
 
Sourcing a File
- Compare and contrast an R Markdown file and an R script.
- Create an R script in RStudio.
- Give the file suffix for R Markdown files and R scripts.
- Source functions from an R script using source().
Function Documentation
- Explain why you should always (yes, always) write documentation for a function you write.
- State the three things (at the very least) you should document when writing a function header.
- Use roxygen-style formatting (that’s the #'stuff) to write header comments for a given function.
Graphics in R
plot()
- Use plot()to generate scatter plots and line graphs from numeric vectors.
- Use the arguments of plot()to:
- Set the main (title) text.
- Label the horizontal and vertical axes.
- Set the horizontal and vertical limits of the plot.
- Set the color of points / lines.
- Set the type and size of points.
- Set the type and line width of lines.
 
- Use the mfrowargument ofpar()to create a grid of plots.
- Save a PDF of a plot using pdf()in combination withgraphics.off()
points(), lines(), and abline()
- Add a scatter plot or line graph to an existing plot using points()andlines(), respectively.
- Add horizontal and vertical lines to an existing plot using abline.
- Add a line with a given intercept and slope to an existing plot using abline.
- Construct a multi-layer graph using plot()in combination withpoints(),lines(), andabline().
hist and density
- Create a histogram from a numeric vector using hist().
- Use the arguments of hist()to:
- Toggle between a frequency and density histogram.
- Set the placement of the bins for the histogram.
- Add a new histogram to an existing histogram.
 
- Compute and plot a kernel density estimate (more on this in a later lecture) using density().
curve and rug
- Use curve()to plot the graph of an expression on a desired interval.
- Use curve()to add the plot of the graph of an expression to an existing plot.
- Add a rug plot to an existing plot using rug().
Color Palettes
- You do not need to memorize the function names for the different color palettes.
- State the argument and return value for the functions for the different color palettes in R.
- Use colorRampPalette()to generate a custom color palette along a gradient of colors.
- Explain what the gginggformulastands for.
- Give the general grammar used by all plotting functions in ggformula.
- Use ggformulato generate:
- histograms
- density plots
- boxplots
- rug plots
- scatter plots
 
- Use the pipe operator %>%to add elements to aggformulaplot.
- Use the |operator to condition (“facet”) a graph on a categorical variable.
- Use gf_lmandgf_smoothto add a least-squares line / nonparametric smoother to a scatter plot.
Packages in R
- Explain what an R package does.
- Install an R package from CRAN using install.packages().
- Load an R package using library().