1 Pre-Requistes : Setup R environment for DATA 606.

See https://data606.net/assignments/homework/ for more information. Chapter 6 - Inference for Categorical Data Practice: 6.5, 6.11, 6.27, 6.43, 6.47 Graded: 6.6, 6.12, 6.20, 6.28, 6.44, 6.48

1.1 Install R packages as per DATA606Spring2019-master/R/Setup.R

Refer to “Getting Started with R” in https://data606.net/post/


2 Exercises

2.1 Question#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.39 ### (a) 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 confidence interval applies to the entire population and not just to the sample taken. The confidence interval tells that the proportion of the population is within that range based on the spot estimate and margin of error of the sample. We know for a fact that 46% of this sample supports the decison of the U.S. Supreme Court whereas for the entire population, we are 95% confident that between 43% and 49% of all Americans support the decision.

2.1.1 (b) 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 - The 43% to 49% confidence interval range is a probability wth 95% confidence

2.1.2 (c) 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 - the population proportion will contain 95% as our confidence interval gives us a range of possible values for the true population proportions.

2.1.3 (d) The margin of error at a 90% confidence level would be higher than 3%.

FALSE - For the same standard error (SE), 90% confidence interval will have a lower margin of error since we are throwing a narrower net. The z-score for 90% confidence interval is only 1.645 against 1.96 for a 95% confidence interval.
n<-1012
z<--qnorm(0.05)#90% confidence
p<-0.46
SE<-sqrt(p*(1-p)/n)
ME<-z*SE
paste("Margin of error for 90% confidence level is",ME)
## [1] "Margin of error for 90% confidence level is 0.0257699037392946"

2.2 Question#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. ### (a) Is 48% a sample statistic or a population parameter? Explain.

48% is a sample statistic and not a population parameter. The study included 1259 US residents out of the entire population.

2.2.2 (c) 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.

There are 2 conditions for the sampling distribution of p being nearly normal -
1. Samples are independent : The sample taken must have been and should not be more than 10% of the population. For this survey, we can reasonably say that this is true.
2. Success-Failure condition : The sample size must also be sufficiently large - that is the success and failure rates must be greater than 10. In this case, this condition is true - 45% and 51% of 1,259 are greater than 10.

Hence, we can conclude that the distribution is normal

2.2.3 (d) 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?

1-pnorm(0.5,0.48,SE)
## [1] 0.07774092
No, as we cannot reject the Null Hypothesis since taking the upper boundary of a confidence interval as the true population proportion is bad practice.. It is not justified based on our confidence interval since our upper limit is around 51% and lower limit is 45%. Since this is barely a majority though, it is also probably true that based on our findings, the survey may not be justified as the margin of error may affect the upper limit with higher confidence levels.

2.3 Question#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
ci <- 0.95
z <- qnorm(0.975) # 95% confidence level => 0.95 + (1-0.95)/2
#ME = z * SE
ME = 0.02
SE = ME / z

#SE = sqrt((p*(1-p)/n)); SE # standard error

n = p*(1-p) / (SE^2)
paste("The sample size is",n)
## [1] "The sample size is 2397.07030411313"

2.4 Question#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 di???erence between the proportions of Californians and Oregonians who are sleep deprived and interpret it in context of the data.

n_CA <- 11545
p_CA <- 0.08

n_OR <- 4691
p_OR <- 0.088

ci <- .95
z <- qnorm(ci + (1-ci)/2) # 95% confidence level =>  1.96

SE_CA =  sqrt((p_CA*(1-p_CA)/n_CA)); SE_CA  #standard error for California
SE_OR =  sqrt((p_OR*(1-p_OR)/n_OR)); SE_OR  #standard error for Oregon 
#standard error for the difference in proportion between Oregon and California 
SE = sqrt(SE_CA + SE_OR);  SE_CA_OR

p <- pOR - pCA

ME = z*SE

#confidence interval for the difference between the proportions of Oregonians and Californians who are sleep deprived
LL = p - ME; LL  # lower limit
UL = p + ME; UL  # upper limit
Assuming that the sampling is near normal and thus we can apply the formula for the difference of 2 proportions.
The 95% confidence interval between the difference in the proportion of sleep depreviation between Oregonians and Californians is between ~-15% to ~17%. 
This means that the proportion of Oregonians who are sleep deprived can be as much as 15% less than Californians or the proportions of Oregonians that are sleep deprived can be as much as 17% more than Californians for 95% of the samples taken with the given sample sizes. Thus, there may also be no difference in the proportion of sleep deprived Californians and Oregonians since the interval includes 0.

2.5 Question#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.62 Woods Cultivated grassplot Deciduous forests Other Total 4 16 67 345 426

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

Ho: The barking deer does not prefer a certain habitat to forage and the observed differences in counts reflect natural sampling fluctuation
Ha: The barking deer has certain habitats that it prefer to forage.

2.5.2 (b) What type of test can we use to answer this research question?

A chi-square test for one-way table. Since we have cases that can be classified into several groups (deer habitats where they forage), we can determine if the forage habitats proportion is representative of the land make up.

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

Inference requirements
1. Independent : If deers in one habitat dont influence deers favoring deer in other habitats then we can assume the samples are independent 
2. Random : Assuming it is random since sample size is less than 10%
3. Normal Distribution : The sucess and failure counts are usually greater than 5. The woods habitat has only 4 cases - so this is in the lower boundary of which is an acceptable count.

2.5.4 (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.

habitats <- c(4, 16, 61, 345)
region <- c(20.45, 62.62, 168.70, 174.23)

chisq.test(x = habitats, p = c(0.048,0.147,0.396,0.409))
## 
##  Chi-squared test for given probabilities
## 
## data:  habitats
## X-squared = 284.06, df = 3, p-value < 2.2e-16
k <- length(habitats)
df <- k - 1
# Compute the chi2 test statistic
chi <- (habitats - region ) ^ 2 / region
chi <- sum(chi)

# check the chi2 test statistic and find p-val
p_Val <- 1 - pchisq(chi, df)
paste("The chi-Square value is large enough that the p-value is 0. Hence, we can reject Null hypothesis and can conclude the alternate hypothesis that there is convincing evidence the barking deer forage in certain habitats over others.")
## [1] "The chi-Square value is large enough that the p-value is 0. Hence, we can reject Null hypothesis and can conclude the alternate hypothesis that there is convincing evidence the barking deer forage in certain habitats over others."

2.6 Question#6.48 Coffee and Depression.

Researchers conducted a study investigating the relationship between ca???einated 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 caffeinated 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 ca???einated coffee consumption. Caffeinated coffee consumption ???1 2-6 1 2-3 $ 4 cup/week cups/week cup/day cups/day cups/day Total Clinical Yes 670 373 905 564 95 2,607 depression No 11,545 6,244 16,329 11,726 2,288 48,132 Total 12,215 6,617 17,234 12,290 2,383 50,739 ### (a) What type of test is appropriate for evaluating if there is an association between coffee intake and depression?

We can use chi-square test to test for independence in 2 way table

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

Ho: There is no relationship between coffee consumption and clinical depression.
Ha: There is a relationship between coffee consumption and clinical depression.

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

n_depress <- 2607
n_not_depress <- 48132
n_total <- n_depress + n_not_depress

pct_depressed = (n_depress/n_total) * 100;
pct_normal = (n_not_depress/n_total) * 100;

paste("The percent of depressed women are ",pct_depressed,"compared to percent of not depressed women are ",pct_normal)
## [1] "The percent of depressed women are  5.1380594808727 compared to percent of not depressed women are  94.8619405191273"

2.6.3 (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.

n_2cup <- 373
n_no2cup <- 6244
n_total2cup <- 6617
Ek <- n_depress*n_2cup / n_total
chiTest <- (n_2cup - Ek)^2 / Ek
paste("The expected count for the highlighted value is",chiTest)
## [1] "The expected count for the highlighted value is 6532.71501940106"

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

found <- data.frame(Yes = c(670,373,905,564,95),
                    No =c(11545,6244,16329,11726,2288)
                    )
chisq.test(found)
## 
##  Pearson's Chi-squared test
## 
## data:  found
## X-squared = 20.932, df = 4, p-value = 0.0003267
n <- 5
k <- 2

df <- (n-1)*(k-1)
chiTest <- 20.93

p_value <- 1 - pchisq(chiTest, df)
paste("The p value is",p_value)
## [1] "The p value is 0.000326950725917041"

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

Since the p-value is below 0.05, we cannot reject the null hypothesis that coffee doesnt cause depression.

2.6.6 (g) One of the authors of this study was quoted on the NYTimes as saying it was “too early to recommend that women load up on extra coffee” based on just this study.64

Do you agree with this statement? Explain your reasoning.

Based on the above conclusion, according to the chi-square test, we found no link between coffee consumption and depression.