Due date: the homework is due before class on Thursday.
Submission process: submit both the R Markdown file and the corresponding html file on canvas. Please submit both the .Rmd
and the .html
files separately and do not zip the two files together.
Download the RMarkdown file with these homework instructions to use as a template for your work. Make sure to replace “Your Name” in the YAML with your name.
FiveThirtyEight is a website founded by Statistician and writer Nate Silver to publish results from opinion poll analysis, politics, economics, and sports blogging. One of the featured articles considers flying etiquette. This article is based on data collected by FiveThirtyEight and publicly available on github. Use the code below to read in the data from the survey:
fly <- read.csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/flying-etiquette-survey/flying-etiquette.csv")
The next couple of lines of code provide a bit of cleanup of the demographic information by reordering the levels of the corresponding factor variables. Run this code in your session.
fly$Age <- factor(fly$Age, levels=c("18-29", "30-44", "45-60", "> 60", ""))
fly$Household.Income <- factor(fly$Household.Income, levels = c("$0 - $24,999","$25,000 - $49,999", "$50,000 - $99,999", "$100,000 - $149,999", "150000", ""))
fly$Education <- factor(fly$Education, levels = c("Less than high school degree", "High school degree", "Some college or Associate degree", "Bachelor degree", "Graduate degree", ""))
Some people do not travel often by plane. Provide a breakdown of travel frequency (use variable How.often.do.you.travel.by.plane.
). Reorder the levels in the variable by travel frequency from least frequent travel to most frequent. Draw a barchart of travel frequency and comment on it.
In the demographic variables (Education
, Age
, and Houshold.Income
), replace all occurrences of the empty string "" by a missing value NA
. How many responses do not have any missing values? (Hint: the function is.na
might come in handy)
Run the command below and interpret the output. What potential purpose can you see for the chart?
library(ggplot2)
fly$Education = with(fly, factor(Education, levels = rev(levels(Education))))
ggplot(data = fly, aes(x = 1)) +
geom_bar(aes(fill=Education), position="fill") +
coord_flip() +
theme(legend.position="bottom") +
scale_fill_brewer() +
xlab("Ratio")
Rename the variable In.general..is.itrude.to.bring.a.baby.on.a.plane.
to baby.on.plane.
. How many levels does the variable baby.on.plane
have, and what are these levels? Rename the level labeled "" to “Not answered”. Reorder the levels of baby.on.plane
from least rude to most rude. Put the level “Not answered” last. Draw a barchart of variable baby.on.plane
. Interpret the result.
Investigate the relationship between gender and the variables Do.you.have.any.children.under.18.
and baby.on.plane
. How is the attitude towards babies on planes shaped by gender and having children under 18? Find a plot that summarises your findings (use ggplot2
).