Dice rolls. (3.6, p. 92) If you roll a pair of fair dice, what is the probability of
All possible outcomes (Sample) in terms of sum of pair of fair dice;
row_1 <- c(2,3,4,5,6,7)
row_2 <- c(3,4,5,6,7,8)
row_3 <- c(4,5,6,7,8,9)
row_4 <- c(5,6,7,8,9,10)
row_5 <- c(6,7,8,9,10,11)
row_6 <- c(7,8,9,10,11,12)
dice.roll <- matrix(c(row_1, row_2, row_3, row_4, row_5, row_6), nrow=6, byrow = TRUE)
dice.roll
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 2 3 4 5 6 7
## [2,] 3 4 5 6 7 8
## [3,] 4 5 6 7 8 9
## [4,] 5 6 7 8 9 10
## [5,] 6 7 8 9 10 11
## [6,] 7 8 9 10 11 12
\[0/36 = 0 \]
\[4/36 = 1/9\]
\[1/36\]
Poverty and language. (3.8, p. 93) 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.
Since 4.2% of the survey participants fall into the both categories, living below the poverty line and speaking a foreign language at home are not disjoint. The answer is no.
Creating a venn diagram requested in r
install.packages('VennDiagram', repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Anil Akyildirim/Documents/R/win-library/3.6'
## (as 'lib' is unspecified)
## package 'VennDiagram' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\Anil Akyildirim\AppData\Local\Temp\RtmpmsGPVP\downloaded_packages
library(VennDiagram)
## Loading required package: grid
## Loading required package: futile.logger
grid.newpage()
venn.plot <- draw.pairwise.venn(area1 = 0.146,
area2 = 0.207,
cross.area = 0.042,
category = c("Poverty", "Language"))
venn.plot
## (polygon[GRID.polygon.1], polygon[GRID.polygon.2], polygon[GRID.polygon.3], polygon[GRID.polygon.4], text[GRID.text.5], text[GRID.text.6], text[GRID.text.7], text[GRID.text.8], text[GRID.text.9])
# Americans live below the powerty line
below_poverty <- 0.104+0.042
# Amerincas speak only English
speak_english_only <- 1-(0.165+0.042)
poverty_line_and_speak_english_only <- below_poverty * speak_english_only
poverty_line_and_speak_english_only
## [1] 0.115778
0.165 + 0.042 + 0.104
## [1] 0.311
\[31.1%\]
above_poverty <- 1- below_poverty
above_poverty_and_speak_english_only <- above_poverty * speak_english_only
above_poverty_and_speak_english_only
## [1] 0.677222
\[67.7%\]
(f)If they are independent it should give us
\[P(A) * P(B) = P(A and B)\]
PAB <- 14.6 *20.7
if (PAB==4.2){
print("Lives Below Poverty Line and Speaks a Foreign language at home is independent")
} else{
print("Lives Below Poverty Line and Speaks a Foreign language at home is dependent")
}
## [1] "Lives Below Poverty Line and Speaks a Foreign language at home is dependent"
Assortative mating. (3.18, p. 111) 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.
| # Answer Assortative mating |
| (a) Proabability of Male Blue Eyes or Female Blue Eyes |
| \[P (A or B) = P(A) + P(B) - P(A and B)\] |
| Total Outcome (sample) is 204 Total Male Outcome with Blue eyes is 114 Total Female outcome with Blue eyes is 108 The Male Blue eyes AND Female Blue Eyes is 78 |
r total <- 204 mb <- 114 fb <- 108 mb_and_fb <- 78 mb_or_fb <- mb + fb - mb_and_fb mb_or_fb |
## [1] 144 |
r probability_mb_or_fb <- mb_or_fb / total probability_mb_or_fb |
## [1] 0.7058824 The answer is 0.705 |
| (b) Probability Male Blue Eyes has a Partner with Blue eyes (Female Blue Eyes) |
r total_blue_male <- 114 mb_and_fb / total_blue_male |
## [1] 0.6842105 |
| The answer is 0.68 |
| (c) i) Probability Random Male with Brown Eyes has a partner with blue eyes |
| ```r total_brown_male <- 54 mbr_and_fb <- 19 |
| mbr_and_fb / total_brown_male ``` |
## [1] 0.3518519 The answer is 0.35 |
| ii) Probability male respondent with green eyes partner with blue eyes |
| ```r total_green_male <- 36 mg_and_fb <- 11 |
| mg_and_fb / total_green_male ``` |
## [1] 0.3055556 The answer is 0.30 |
| (d) It is not independent. Basic example is that in any combination |
| \[P(A) * P(B) = P(A and B)\] does not stand. |
Books on a bookshelf. (3.26, p. 114) The table below shows the distribution of books on a bookcase based on whether they are nonfiction or fiction and hardcover or paperback.
t: Total books = 95 h: Hard cover book = 28 pf: Paperback Fiction books= 59
h <-28/95
pf <-59/(95-1) # we need to take one out because of without replacement
h_pf<- h*pf
h_pf
## [1] 0.1849944
The answer is 0.18
t: Total books = 95 f: Fiction book = 72 h: Hard cover book =28
f <- 72/95
h2 <- 28/(95-1) # we need to take out 1 because of without replacement
f_h2 <- f*h2
f_h2
## [1] 0.2257559
The answer is 0.225
t: Total books = 95 f: Fiction book = 72 h: Hard cover book =28
f <- 72/95
h3 <- 28/(95) # we are not taking out the 1 as we placed back the book
f_h3 <- f*h3
f_h3
## [1] 0.2233795
The answer is 0.223
Baggage fees. (3.34, p. 124) 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 bags.
fee_1 : 25$ fee_2 : 35$ fee_3 : 0$ (no luggage no fee)
no_luggage: 54% one_luggage: 34% two_luggage: 12%
more than two luggage is negligible.
Creating a data frame to display this
fee_1 <- 0
fee_2 <- 25
fee_3 <- 35
no_luggage <- 0.54
one_luggage <- 0.34
two_luggage <- 0.12
pas_1 <- "O bags"
pas_2 <- "1 bag"
pas_3 <- "2 bags"
passengers <- c(pas_1, pas_2, pas_3)
luggage <- c(no_luggage, one_luggage, two_luggage)
fee <- c(fee_1, fee_2, fee_3)
df <- data.frame(passengers, luggage, fee)
df
Finding Average Revenue
average_revenue <-(df$luggage*df$fee)/(sum(df$luggage))
average_revenue
## [1] 0.0 8.5 4.2
Average Revenue for o bags per passenger is 0 Average Revenue for 1 bag per passenger is $8.5 Average Revenue for 2 bag per passenger is $4.2
Average Revenue is;
sum(average_revenue)
## [1] 12.7
Finding Standard Deviation with below formula:
# calculate variance
variance <- (df$fee-average_revenue)^2
variance
## [1] 0.00 272.25 948.64
sd <- sqrt(variance)
sd
## [1] 0.0 16.5 30.8
Income and gender. (3.38, p. 128) 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.
Creating the data frame outlined Income vs Total
income <- c("$1 to $9,999 or loss", "$10,000 to $14,999", "$15,000 to $24,999", "$25,000 to $34,999", "$35,000 to $49,000", "$50,000 to $64,999", "$65,000 to $74,999", "$75,000 to $99,999", "$100,000 or more")
total <- c(0.022, 0.047, 0.158, 0.183, 0.212, 0.139, 0.058, 0.084, 0.097)
df_2 <- data.frame(income, total)
df_2
Creating a bar plot to outline the distribution.
install.packages('ggplot2', repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Anil Akyildirim/Documents/R/win-library/3.6'
## (as 'lib' is unspecified)
## package 'ggplot2' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\Anil Akyildirim\AppData\Local\Temp\RtmpmsGPVP\downloaded_packages
## Warning: position_stack requires non-overlapping x intervals
p_less_10<-0.022
p_bet_10_15<-0.047
p_bet_25_15<-0.158
p_bet_35_25<-0.183
p_bet_50_35<-0.212
p_less_50<-p_less_10 + p_bet_10_15 + p_bet_25_15 + p_bet_35_25 + p_bet_50_35
p_less_50
## [1] 0.622
The answer is 0.622
Assuming the gender and income is independent from each other.
p_female : Probability of random chosen female :0.41
p_less_50_female = p_less_50 * p_female
p_female <- 0.41
p_less_50_female <- p_less_50 * p_female
p_less_50_female
## [1] 0.25502
The answer is 0.25