Exercise

Exercise 7 - Plotting Prediction Results

Create an object called p_hits that contains the proportion of instances when the sign of the actual spread matches the predicted spread for states with 5 or more polls.

Make a barplot based on the result from the previous exercise that shows the proportion of times the sign of the spread matched the actual result for the data in p_hits.

Instructions

100 XP
  • Use the group_by function to group the data by state.
  • Use the filter function to filter for states that have 5 or more polls.
  • Summarize the proportion of values in hit that are true as a variable called proportion_hits. Also create a variable called n for the number of polls in each state using the n() function.
  • To make the plot, follow these steps:
  • Reorder the states in order of the proportion of hits.
  • Using ggplot, set the aesthetic with state as the x-variable and proportion of hits as the y-variable.
  • Use geom_bar to indicate that we want to plot a barplot.
  • Use coord_flip to flip the axes so the states are displayed from top to bottom and proportions are displayed from left to right.