Writing Efficient R Code
Colin Gillespie
Jumping Rivers & Newcastle University
The general idea is to:
Rprof()
comes with R and does exactly thisdata(movies, package = "ggplot2movies")
dim(movies)
58788 24
braveheart = movies[7288,]
Year | Length | Rating |
---|---|---|
1995 | 177 | 8.3 |
# Load data data(movies, package = "ggplot2movies") braveheart <- movies[7288,] movies <- movies[movies$Action==1,]
plot(movies$year, movies$rating, xlab = "Year", ylab = "Rating")
# local regression line model <- loess(rating ~ year, data = movies) j <- order(movies$year) lines(movies$year[j], model$fitted[j], col = "forestgreen")
points(braveheart$year, braveheart$rating, pch = 21, bg = "steelblue")
Profile -> Profile Selected lines
library("profvis")
profvis({
data(movies, package = "ggplot2movies") # Load data braveheart <- movies[7288,] movies <- movies[movies$Action == 1,] plot(movies$year, movies$rating, xlab = "Year", ylab="Rating") model <- loess(rating ~ year, data = movies) # loess regression line j <- order(movies$year) lines(movies$year[j], model$fitted[j], col="forestgreen", lwd=2) points(braveheart$year, braveheart$rating, pch = 21, bg = "steelblue", cex = 3)
})
Which line do you think will be the slowest?
Writing Efficient R Code