1. Learn
  2. /
  3. Courses
  4. /
  5. Hierarchical and Mixed Effects Models in R

Connected

Exercise

Visualizing Maryland crime data

Before fitting a model, plotting the data can be helpful to see if trends or data points jump out, outliers exist, or other attributes of the data require future consideration. Using ggplot2, you can plot lines for county and examine how crimes change through time. For this exercise, examine Maryland crime data (md_crime). This includes the Year, a count of violent Crimes in the county, and the County's name.

To explore this data, first plot the data points for each county through time. This lets you see how each county changes through time. Rather than using an aesthetic such as color, group is used here because there are too many counties to easily distinguish colors. After plotting the raw data, add trend lines for each county.

Both the connect points (geom_line) and trend lines (geom_smooth) provide insight into what, if any, kinds of random effects are required. If all of the points appear to have similar ranges and means, a random-effect intercept may not be important. Likewise, if trends look consistent across counties (i.e., the trend lines look similar or parallel across groups), a random-effect slope may not be required.

Instructions 1/2

undefined XP
    1
    2
  • Plot how Crime (y variable) has changed across Year (x variable) in each County (group variable) using the md_crime data.
  • Add trend lines for each county with geom_smooth(method = 'lm', se = FALSE). se = FALSE creates a less cluttered plot.