Environment Setup

## -- Attaching packages ---------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 2.2.1     v purrr   0.2.4
## v tibble  1.4.2     v dplyr   0.7.4
## v tidyr   0.8.0     v stringr 1.2.0
## v readr   1.1.1     v forcats 0.2.0
## -- Conflicts ------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## 
## Attaching package: 'magrittr'
## The following object is masked from 'package:purrr':
## 
##     set_names
## The following object is masked from 'package:tidyr':
## 
##     extract
## Loading required package: combinat
## 
## Attaching package: 'combinat'
## The following object is masked from 'package:utils':
## 
##     combn
## 
## Attaching package: 'prob'
## The following objects are masked from 'package:dplyr':
## 
##     intersect, setdiff, union
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, union
## Loading required package: grid
## Loading required package: futile.logger

2.6 Dice rolls. If you roll a pair of fair dice, what is the probability of

# get sample space of rolling die possibilites
# add sum column
ss <- rolldie(2) %>%
  mutate(sum = X1 + X2)

poss <- nrow(ss)

sumof1 <- ss %>%
  filter(sum == 1) %>%
  count

sumof1/poss %>%
  print
## [1] 36
##   n
## 1 0
sumof5 <- ss %>%
  filter(sum == 5) %>%
  count

sumof5/poss %>%
  print
## [1] 36
##           n
## 1 0.1111111
sumof12 <- ss %>%
  filter(sum == 12) %>%
  count

sumof12/poss %>%
  print
## [1] 36
##            n
## 1 0.02777778

2.8 Poverty and language. The American Community Survey is an ongoing survey that

provides data every year to give communities the current information they need to plan investments and services. The 2010 American Community Survey estimates that 14.6% of Americans live below the poverty line, 20.7% speak a language other than English (foreign language) at home, and 4.2% fall into both categories.

pov <- .146
lote <- .207
both <- .042
grid.newpage()
draw.pairwise.venn(area1 = .146, area2 = .207, cross.area = .042,
                   category = c("Below Poverty", "LOTE"))

## (polygon[GRID.polygon.11], polygon[GRID.polygon.12], polygon[GRID.polygon.13], polygon[GRID.polygon.14], text[GRID.text.15], text[GRID.text.16], text[GRID.text.17], text[GRID.text.18], text[GRID.text.19])
pov - both
## [1] 0.104
pov + lote - both
## [1] 0.311
1 - (pov + lote - both)
## [1] 0.689

2.20 Assortative mating. Assortative mating is a nonrandom mating pattern where individuals with similar genotypes and/or phenotypes mate with one another more frequently than what would be expected under a random mating pattern. Researchers studying this topic collected data on eye colors of 204 Scandinavian men and their female partners. The table below summarizes the results. For simplicity, we only include heterosexual relationships in this exercise.

(108 + 114 - 78)/204
## [1] 0.7058824
78/204
## [1] 0.3823529
BrBl <- (19/204) %>%
  print
## [1] 0.09313725
GrBl <- (13/204) %>%
  print
## [1] 0.06372549

2.30 Books on a bookshelf. The table below shows the distribution of books on a bookcase based on whether they are nonfiction or fiction and hardcover or paperback.

books <- matrix(c(13,  59, 15, 8), nrow = 2, ncol = 2, byrow = TRUE)
sum(books[,1])/sum(books) * books[1,2]/(sum(books) -1)
## [1] 0.1849944
sum(books[1,])/sum(books) * sum(books[,1])/(sum(books) - 1)
## [1] 0.2257559
sum(books[1,])/sum(books) * sum(books[,1])/(sum(books))
## [1] 0.2233795

2.38 Baggage fees. An airline charges the following baggage fees: $25 for the first bag and $35 for the second. Suppose 54% of passengers have no checked luggage, 34% have one piece of checked luggage and 12% have two pieces. We suppose a negligible portion of people check more than two bag.

bags <- c(0,1,2)
fees <- c(0,25,25+35)
prob <- c(.52, .34, .12)
model <- data_frame(bags, fees, prob)
avgrev <- sum(fees * prob) %>%
  print
## [1] 15.7
sd <- (((fees - avgrev)^2) * prob) %>%
  sum %>%
  sqrt %>%
  print
## [1] 19.82625
rev120 <- (avgrev * 120) %>%
  print
## [1] 1884
sd120 <- (sd^2 * 120) %>%
  sqrt %>%
  print
## [1] 217.1857

2.44 Income and gender. The relative frequency table below displays the distribution of annual total personal income (in 2009 inflation-adjusted dollars) for a representative sample of 96,420,486 Americans. These data come from the American Community Survey for 2005-2009. This sample is comprised of 59% males and 41% females.

inc <- c("1 to 9,999", "10,000 to 14,999", "15,000 to 24,999", "25,000 to 34,999",
         "35,000 to 49,999", "50,000 to 64,999", "65,000 to 74,999",
         "75,000 to 99,999", "100,000 or more")
total <- c(.022, .047, .158, .183, .212, .139, .058, .084, .097)
dis <- data_frame(inc, total)

male <- .59
female <- .41
barplot(dis$total, names.arg = inc, xlab = "Income", ylab = "Total")

The distribution is unimodal, skewed left, and asymmetrical. * (b) What is the probability that a randomly chosen US resident makes less than $50,000 per year?

lt50k <- sum(dis$total[1:5]) %>%
  print
## [1] 0.622
flt50k <- (lt50k * female) %>%
  print
## [1] 0.25502