Profvis in action
Examine the code on the right that performs a standard data analysis. It loads and selects data, plots the data of interest, and adds in a regression line.
This exercise is part of the course
Writing Efficient R Code
Exercise instructions
- Load the
profvis
package. - Profile the code.
- Wrap the code in curly braces,
{
. - Wrap those curly braces in a call to
profvis()
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Load the data set
data(movies, package = "ggplot2movies")
# Load the profvis package
___
# Profile the following code with the profvis function
___
# Load and select data
comedies <- movies[movies$Comedy == 1, ]
# Plot data of interest
plot(comedies$year, comedies$rating)
# Loess regression line
model <- loess(rating ~ year, data = comedies)
j <- order(comedies$year)
# Add fitted line to the plot
lines(comedies$year[j], model$fitted[j], col = "red")
___ ## Remember the closing brackets!