library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.4.4
library('DATA606')          # Load the package
## Warning: package 'RCurl' was built under R version 3.4.4
## Warning: package 'maps' was built under R version 3.4.4
##
## Welcome to CUNY DATA606 Statistics and Probability for Data Analytics
## This package is designed to support this course. The text book used
## is OpenIntro Statistics, 3rd Edition. You can read this by typing
## vignette('os3') or visit www.OpenIntro.org.
##
## The getLabs() function will return a list of the labs available.
##
## The demo(package='DATA606') will list the demos that are available.
library(knitr)

# 6.6 2010 Healthcare Law. On June 28, 2012 the U.S. Supreme Court upheld the much debated 2010 healthcare law, declaring it constitutional. A Gallup poll released the day after this decision indicates that 46% of 1,012 Americans agree with this decision. At a 95% confidence level, this sample has a 3% margin of error. Based on this information, determine if the following statements are true or false, and explain your reasoning.

1. We are 95% confident that between 43% and 49% of Americans in this sample support the decision of the U.S. Supreme Court on the 2010 healthcare law.

False. The confident interval is used for estimating population proportion.

1. We are 95% confident that between 43% and 49% of Americans support the decision of the U.S. Supreme Court on the 2010 healthcare law.

True. 95% Confident interval is $$46\% \pm 3\% = (43\%, 49\%)$$.

1. If we considered many random samples of 1,012 Americans, and we calculated the sample proportions of those who support the decision of the U.S. Supreme Court, 95% of those sample proportions will be between 43% and 49%.

True. By definition of confidence interval, if we take many random samples and built a confidence interval from each sample, 95% of times of those intervals would contain the actual population proportion.

1. The margin of error at a 90% confidence level would be higher than 3%.

False. At 90% confidence level z-value will be 1.6448536 and at 95% confidence level z-value will be 1.959964. As z-value reduces margin of error value reduces.

# 6.12 Legalization of marijuana, Part I.:

The 2010 General Social Survey asked 1,259 US residents:“Do you think the use of marijuana should be made legal, or not?” 48% of the respondents said it should be made legal.

1. Is 48% a sample statistic or a population parameter? Explain.

It is a sample statistic because 48% of the 1,259 US residents and not the total population of US.

1. Construct a 95% confidence interval for the proportion of US residents who think marijuana should be made legal, and interpret it in the context of the data.
s <- 1259
p <- .48
z <- 1.96
se <- sqrt((p*(1-p))/s)
l_lim <- p - (z * se)
l_lim
##  0.4524028
u_lim <- p + (z * se)
u_lim
##  0.5075972

Interval is 45.24% to 50.75%

1. A critic points out that this 95% confidence interval is only accurate if the statistic follows a normal distribution, or if the normal model is a good approximation. Is this true for these data? Explain.

True. Both (1259 x .48) > 10 and (1259 x(1-.48)) > 10, the distribution is normal and the CI is correct.

1. A news piece on this survey’s findings states, “Majority of Americans think marijuana should be legalized.” Based on your confidence interval, is this news piece’s statement justified?

Yes, With the confidence interval being between 45% and 51% it can be said that over 50% of the Americans think marijuana should be legal.

# 6.20 Legalize Marijuana, Part II.:

As discussed in Exercise 6.12, the 2010 General Social Survey reported a sample where about 48% of US residents thought marijuana should be made legal. If we wanted to limit the margin of error of a 95% confidence interval to 2%, about how many Americans would we need to survey ?

p <- 0.48
me <- 0.02
z <- qnorm(0.975)
se <- me/z
n <- (p * (1-p)) / se^2
n
##  2397.07

Need to survey 2,397 Americans.

# 6.28 Sleep deprivation, CA vs. OR, Part I.:

According to a report on sleep deprivation by the Centers for Disease Control and Prevention, the proportion of California residents who reported insufficient rest or sleep during each of the preceding 30 days is 8.0%, while this proportion is 8.8% for Oregon residents. These data are based on simple random samples of 11,545 California and 4,691 Oregon residents. Calculate a 95% confidence interval for the difference between the proportions of Californians and Oregonians who are sleep deprived and interpret it in context of the data.

ncali <- 11545
noregon <- 4691
pcali <- 0.08
poregon <- 0.088
pdiff <- poregon - pcali
se <- sqrt( ((pcali * (1 - pcali)) / ncali) +  ((poregon * (1 - poregon)) / noregon))
me <- qnorm(0.975) * se
l_lim <- pdiff - me
l_lim
##  -0.001497954
u_lim <- pdiff + me
u_lim
##  0.01749795

We can say that with a 95% confidence level that the proportions are not very different between California and Oregon.

## 6.44 Barking deer.:

Microhabitat factors associated with forage and bed sites of barking deer in Hainan Island, China were examined from 2001 to 2002. In this region woods make up 4.8% of the land, cultivated grass plot makes up 14.7% and deciduous forests makes up 39.6%. Of the 426 sites where the deer forage, 4 were categorized as woods, 16 as cultivated grassplot, and 61 as deciduous forests. The table below summarizes these data.

Woods Cultivated grassplot Deciduous forests Other Total

## (a) Write the hypotheses for testing if barking deer prefer to forage in certain habitats over others.

Null Hypothesis $$H_{0}$$: there is no difference in the proportion of deer that forage in certain habitats.

Alternative Hypothesis $$H_{A}$$: there is a difference in the proportion of deer that forage in certain habitats.

Chi-square test

## (c) Check if the assumptions and conditions required for this test are satisfied.

Independent observations: True as 461 is less than 10% total population.

Sample size (at least 10): True.

## (d) Do these data provide convincing evidence that barking deer prefer to forage in certain habitats over others? Conduct an appropriate hypothesis test to answer this research question.

chisq.test(x=c(4,16,67,345),p=c(0.048,0.147,0.396,0.409))
##
##  Chi-squared test for given probabilities
##
## data:  c(4, 16, 67, 345)
## X-squared = 272.69, df = 3, p-value < 2.2e-16

The p value is (<0.05) so we can conclude barking deer forage in some habitats more than others.

## 6.48 Coffee and Depression.:

Researchers conducted a study investigating the relationship between caffeinated coffee consumption and risk of depression in women. They collected data on 50,739 women free of depression symptoms at the start of the study in the year 1996, and these women were followed through 2006. The researchers used questionnaires to collect data on caffieinated coffee consumption, asked each individual about physician-diagnosed depression, and also asked about the use of antidepressants. The table below shows the distribution of incidences of depression by amount of caffeinated coffee consumption.

## (a) What type of test is appropriate for evaluating if there is an association between co???ee intake and depression?

Chi squared test.

## (b) Write the hypotheses for the test you identified in part (a).

Null Hypothesis $$H_{0}$$: There is no relationship between coffee consumption and clinical depression.

Alternative Hypothesis $$H_{A}$$: There is a relationship between coffee consumption and clinical depression.

## (c) Calculate the overall proportion of women who do and do not suffer from depression.

depression <- 2607/50739
depression
##  0.05138059
not_depress <-  48132/50739
not_depress
##  0.9486194

## (d) Identify the expected count for the highlighted cell, and calculate the contribution of this cell to the test statistic, i.e. $$(Observed - Expected)^2/Expected$$.

Expected: $$\frac{row \space total * column \space total}{sample \space size} = \frac{2607 * 6617}{50739}$$ = 339.9853958.

$$Expectedcount:$$ $$(Observed - Expected)^2/Expected = (373 - 340)^2/340$$ = 3.2029412

## (e) The test statistic is #2 = 20.93. What is the p-value?

chisq <- 20.93
df <-  (5-1)*(2-1)
p <- 1-pchisq(chisq, df)
p
##  0.0003269507

The p value is .00033

## (f) What is the conclusion of the hypothesis test?

Since p-value is less 5% significance level, we reject null hypothesis.

## (g) One of the authors of this study was quoted on the NYTimes as saying it was “too early torecommend that women load up on extra coffee” based on just this study.Do you agree with this statement? Explain your reasoning.

Yes, the study only establishes statistical significance. It establishes only an insignificant relationship.

# Appendix

library(ggplot2)
library(knitr)
s <- 1259
p <- .48
z <- 1.96
se <- sqrt((p*(1-p))/s)
l_lim <- p - (z * se)
l_lim
u_lim <- p + (z * se)
u_lim
p <- 0.48
me <- 0.02
z <- qnorm(0.975)
se <- me/z
n <- (p * (1-p)) / se^2
n
ncali <- 11545
noregon <- 4691
pcali <- 0.08
poregon <- 0.088
pdiff <- poregon - pcali
se <- sqrt( ((pcali * (1 - pcali)) / ncali) +  ((poregon * (1 - poregon)) / noregon))
me <- qnorm(0.975) * se
l_lim <- pdiff - me
l_lim
u_lim <- pdiff + me
u_lim
chisq.test(x=c(4,16,67,345),p=c(0.048,0.147,0.396,0.409))
depression <- 2607/50739
depression

not_depress <-  48132/50739
not_depress
chisq <- 20.93
df <-  (5-1)*(2-1)
p <- 1-pchisq(chisq, df)
p