if (Sys.info()["sysname"] == "Windows") {
setwd("~/Masters/DATA606/Week6/Homework")
} else {
setwd("~/Documents/Masters/DATA606/Week6/Homework")
}
require(ggplot2)
## Loading required package: ggplot2
Answer:
False. A confidence interval estimates the population proportion and not the sample proportion.
Answer:
True. The statement indicates that the margin of error was calculated based on a 95% confidence level which resulted in a value of 3% and a mean of 46%.
Answer:
True. This is the definition of a confidence interval.
Answer:
False, the margin of error would be lower since the z value at a 90% confidence level would be less than that for a 95% confidence interval and the formula for margin of error is as follows:
\[ME\quad =\quad z*\sqrt { \frac { p(1-p) }{ n } }\]
Answer:
It is a sample statistic since it represents the average proportion of the people surveyed responding in the affirmative.
Answer:
ns = 1259
p_hat = 0.48
z = 1.96
se <- sqrt(p_hat * (1 - p_hat)/ns)
ME = 1.96 * se
p_hat - ME
## [1] 0.4524028
p_hat + ME
## [1] 0.5075972
lb <- p_hat - se * 4
ub <- p_hat + se * 4
z1 <- round(p_hat - ME, digits = 2)
z2 <- round(p_hat + ME, digits = 2)
pick_line1 <- z1
pick_line2 <- z2
ggplot(data.frame(x = c(lb, ub)), aes(x)) + stat_function(fun = dnorm,
args = list(mean = p_hat, sd = se)) + stat_function(fun = dnorm,
args = list(mean = p_hat, sd = se), xlim = c(z1, z2), geom = "area",
alpha = 0.5) + geom_vline(xintercept = pick_line1, color = "black",
alpha = 0.75) + geom_text(aes(x = pick_line1, y = 10, label = sprintf("p = %s\n",
pick_line1)), color = "black", angle = 90) + geom_vline(xintercept = pick_line2,
color = "black", alpha = 0.75) + geom_text(aes(x = pick_line2,
y = 10, label = sprintf("p = %s\n", pick_line2)), color = "black",
angle = 90) + labs(x = "proportion")
We are 95% confident that the number of US residents who think the use of marijuana should be made legal is between 45.2% and 50.8%.
Answer:
We must check the survey for the following elements:
Independence: Given that this is an organized survey, and has supporting documentation provided in the footnotes, we can assume that the surveys were sufficiently performed in order to justify that the sample observations were independent.
success-failure condition: The number of successes (affirmatives) are 604.32 and failures (negatives) are 654.68. The minimum number of successes and failures have been met.
Since both of these conditions are met, we can assume the data follows an approximately normal distribution.
Answer:
No. The conficence interval spans between 45.2% and 50.8%. This means that we are 95% confident that the true mean falls betwen these values which are above (majority) and below (minority) 50%.
Answer:
First, let’s test the conditions for inference:
1a. Independence: The reports are indicated to be based on a simple random sample, so it appears okay to proceed.
1b. success-failure condition:
phat_CA <- 0.08
n_CA <- 11545
phat_OR <- 0.088
n_OR <- 4691
CA_success <- formatC(phat_CA * n_CA, format = "d", big.mark = ",")
CA_failure <- formatC((1 - phat_CA) * n_CA, format = "d", big.mark = ",")
OR_success <- formatC(phat_OR * n_OR, format = "d", big.mark = ",")
OR_failure <- formatC((1 - phat_OR) * n_OR, format = "d", big.mark = ",")
The number of successes (insufficient sleep) are 923 and failures (other) are 10,621 for California residents. The number of successes (insufficient sleep) are 412 and failures (other) are 4,278 for Oregon residents. The minimum number of successes and failures have been met.
The confidence interval for the difference between the proportions of Californians and Oregonians who are sleep deprived is:
z_value <- 1.96
se2_CA <- phat_CA * (1 - phat_CA)/n_CA
se2_OR <- phat_OR * (1 - phat_OR)/n_OR
se <- sqrt(se2_CA + se2_OR)
p_hat <- phat_CA - phat_OR
ME <- z_value * se
p_hat - ME
## [1] -0.01749813
p_hat + ME
## [1] 0.001498128
lb <- p_hat - se * 4
ub <- p_hat + se * 4
z1 <- round(p_hat - ME, digits = 4)
z2 <- round(p_hat + ME, digits = 4)
pick_line1 <- z1
pick_line2 <- z2
ggplot(data.frame(x = c(lb, ub)), aes(x)) + stat_function(fun = dnorm,
args = list(mean = p_hat, sd = se)) + stat_function(fun = dnorm,
args = list(mean = p_hat, sd = se), xlim = c(z1, z2), geom = "area",
alpha = 0.5) + geom_vline(xintercept = pick_line1, color = "black",
alpha = 0.75) + geom_text(aes(x = pick_line1, y = 40, label = sprintf("p = %s\n",
pick_line1)), color = "black", angle = 90) + geom_vline(xintercept = pick_line2,
color = "black", alpha = 0.75) + geom_text(aes(x = pick_line2,
y = 40, label = sprintf("p = %s\n", pick_line2)), color = "black",
angle = 90) + labs(x = "proportion")
We are 95% confident that the difference between the proportions of Californians and Oregonians who are sleep deprived is between -0.0175 and 0.0015. Since the value of 0 is within the confidence interval, it appears that we would fail to reject the hypothesis that there is no difference between the two groups.
Answer:
\[{ H }_{ O }:\quad The\quad observed\quad data\quad is\quad a\quad random\quad sample,\quad\\ i.e.\quad there\quad is\quad no\quad prefered\quad habit\quad for\quad deer\quad to\quad forage\quad in,\\\quad and\quad the\quad observed\quad counts\quad reflect\quad natural\quad sampling\quad fluctuations\\ \quad \\ { H }_{ A }:\quad The\quad observed\quad data\quad is\quad not\quad the\quad result\quad of\quad a\quad random\quad sample,\\\quad i.e.\quad there\quad is\quad a\quad bias\quad in\quad the\quad preference\quad of\quad foraged\quad areas\]
Answer:
We can use a chi-square test.
Answer:
The conditions for inference for a chi-square test is as follows:
Since the number of beds/forage sites were counted and wouldn’t be in two places at once, it can be assumed each counts in the table is independent of other cases.
total_sites <- 426
woods_area <- 0.048
grassplot_area <- 0.147
forest_area <- 0.396
other_area <- 1 - woods_area - grassplot_area - forest_area
(woods_expected <- woods_area * total_sites)
## [1] 20.448
(grassplot_expected <- grassplot_area * total_sites)
## [1] 62.622
(forest_expected <- forest_area * total_sites)
## [1] 168.696
(other_expected <- other_area * total_sites)
## [1] 174.234
There is one scenario that may not meet the requirements (Woods = 4); however, the expected number of deer in the woods area is 20.4, which is over the minimum of 5, so we can continue.
Answer:
chisq_woods <- (4 - woods_expected)^2/woods_expected
chisq_grassplot <- (16 - grassplot_expected)^2/grassplot_expected
chisq_forest <- (67 - forest_expected)^2/forest_expected
chisq_other <- (345 - other_expected)^2/other_expected
chi_square_value <- chisq_woods + chisq_grassplot + chisq_forest +
chisq_other
df_chi_square <- 5 - 1
pchisq(q = chi_square_value, df = df_chi_square, lower.tail = FALSE)
## [1] 1.197044e-58
lb <- 0
ub <- 300
z1 <- round(chi_square_value, digits = 2)
z2 <- round(chi_square_value, digits = 2)
pick_line1 <- z1
pick_line2 <- z2
ggplot(data.frame(x = c(lb, ub)), aes(x)) + stat_function(fun = dchisq,
args = list(df = df_chi_square)) + stat_function(fun = dchisq,
args = list(df = df_chi_square), xlim = c(z1, ub), geom = "area",
alpha = 0.5) + geom_vline(xintercept = pick_line1, color = "black",
alpha = 0.75) + geom_text(aes(x = pick_line1, y = 0.1, label = sprintf("Chi-square = %s\n",
pick_line1)), color = "black", angle = 90) + geom_vline(xintercept = pick_line2,
color = "black", alpha = 0.75) + geom_text(aes(x = pick_line2,
y = 0.1, label = sprintf("Chi-square = %s\n", pick_line2)),
color = "black", angle = 90) + labs(x = "Chi Square Value")
Using a 95% confidence level, there is singificant evidence to reject the null hypothesis that barking deer do not prefer to forage in certain habitats over others.
Answer:
A chi-square test for two way tables
Answer:
HO: There is no difference in the clinical depression rates of women between the groups of caffeinated coffee consumption.
HA; There is some difference in the clinical depression rates of women between the groups of caffeinated coffee consumption.
Answer:
depression_yes <- round(2607/50739, digits = 2)
depression_no <- round(48132/50739, digits = 2)
The proportion of women in this test who suffer from depression is 0.05 and the proportion who do not suffer from depression is 0.95.
Answer:
row1_total <- 2607
table_total <- 50739
column2_total <- 6617
expected_count <- (row1_total/table_total) * column2_total
test_statistic <- (373 - expected_count)^2/expected_count
test_statistic
## [1] 3.205914
Answer:
First, we must determine the degrees of freedom:
table_rows <- 2
table_columns <- 5
(df_table <- (table_rows - 1) * (table_columns - 1))
## [1] 4
Now, we can deterime the p-value of the chi-square quantile:
p_value <- pchisq(q = 20.93, df = df_table, lower.tail = FALSE)
lb <- 0
ub <- 35
z1 <- 20.93
z2 <- 20.93
pick_line1 <- z1
pick_line2 <- z2
ggplot(data.frame(x = c(lb, ub)), aes(x)) + stat_function(fun = dchisq,
args = list(df = df_chi_square)) + stat_function(fun = dchisq,
args = list(df = df_chi_square), xlim = c(z1, ub), geom = "area",
alpha = 0.5) + geom_vline(xintercept = pick_line1, color = "black",
alpha = 0.75) + geom_text(aes(x = pick_line1, y = 0.1, label = sprintf("Chi-square = %s\n",
pick_line1)), color = "black", angle = 90) + geom_vline(xintercept = pick_line2,
color = "black", alpha = 0.75) + geom_text(aes(x = pick_line2,
y = 0.1, label = sprintf("Chi-square = %s\n", pick_line2)),
color = "black", angle = 90) + labs(x = "Chi Square Value")
The p-value is 0.000327
Answer:
The p-value is less than a significance level of \(\alpha = 0.05\); therefore, we reject the null hypothesis that there is no difference in the clinical depression rates of women between the groups of caffeinated coffee consumption.