SPEA V706 — Statistics for Research in Public Affairs
What is a package?
Installing and loading: install.packages(), library(), and pacman
Packages are kind of … bad.
Approved stack of packages for this course
Downloads it from CRAN and puts it in your library.
Installing does not make the functions available. It just puts them on the shelf
package::function reaches into a package without loading itif (!requireNamespace("data.table", quietly = TRUE)) {
install.packages("data.table")
}
if (!requireNamespace("fixest", quietly = TRUE)) {
install.packages("fixest")
}
if (!requireNamespace("ggplot2", quietly = TRUE)) {
install.packages("ggplot2")
}
library(data.table)
library(fixest)
library(ggplot2)This is fine. But a bit ugly looking.
pacmanpacman is a package for loading packages. It does the install-if-missing check for you.
p_load() installs any package that aren’t already installed, and then it loads them all.Fancier is to make sure pacman itself is installed, and then use it to load the rest.
Arthur C. Clarke – Any sufficiently advanced technology is indistinguishable from magic.
This is not Hogwarts.
We want to know how it all works. If your code feels like magic then go back and learn what it is doing.
A dependency is a package your code needs in order to work.
Our rule: do not use a package unless you need it.
For assignments in this course, package use is regulated and generally discouraged
Stick to the approved stack below, and ask if you want something else
This is a teaching rule but I follow this rule in my own work.
Six approved packages.
But it’s really three important ones. The other three are convenient and/or handy for later-in-your-life.
| Package | Job |
|---|---|
| data.table | Reading, filtering, reshaping, summarizing data |
| fixest | Regressions |
| ggplot2 | Graphics |
| collapse | Fast grouped operations, when you need the speed |
| pacman | Loads the others, installing anything missing |
| here | Builds file paths from the project root |
data.table, fixest, and ggplot2 are really helpful.collapse is a speed booster for big data.pacman and here are convenience packages but I like them because they help you make your code more portable, which is a noble goal.here and the Project RootThe problem: this path works on exactly one computer.
here() finds your project’s root folder, then builds the path down from therehere() Keys Offhere() looks upward from where you are until it finds a marker of the project root.Rproj file, which RStudio creates for you.Rproj in itDon’t hardcode paths using setwd().
fixestBase R fits a regression using lm(), which is perfectly good.
mpg ~ wt + hp is a formula: outcome on the left, regressors on the rightsummary() prints coefficients, standard errors, and the usual fit statisticsfixest is a package for regression that is faster and more flexible for big data, fixed effects, cluster robust inference, etc.feols() takes the same formula and gives the same coefficientsggplot2ggplot(data, aes(...)) says which data, and which columns map to which axesaes() is the aesthetic mapping: x, y, colour, shapegeom_line() says draw it as a line. geom_point() for a scatter, geom_col() for barswidth and height, or the size depends on whatever window was opendata.tabledt[i, j, by]data.table.collapseA package is borrowed code that
Packages hide mechanics and drag in dependencies, which costs you learning and reproducibility
Be a minimalist: pacman, here, data.table, fixest, ggplot2, collapse, and ask before adding more