---
title: "HW 3 -- Math 58B"
subtitle: "due: Thursday, February 12, 2026"
author: "your name here"
format: pdf
execute:
  warning: false
  message: false
---


```{r}
library(tidyverse)
library(broom)
library(infer)
library(praise)
```


### Assignment Summary (Goals)
* practice setting up hypotheses
* practice conclusions from complete hypothesis test
* practice simulating under the scenario where the null hypothesis is true

Note:  you'll need many of the skills covered in lab 3 to complete the assignment!  After Tuesday, the solutions to Lab 3 will be posted on Canvas


#### Q1. Kissing the Right Way, (ISCAM, Inv 1.5)

> Most people are right-handed and even the right eye is dominant for most people. Researchers have long believed that late-stage human embryos tend to turn their heads to the right. German biopsychologist Onur Gunturkun (Nature 2003) conjectured that this tendency to turn to the right manifests itself in other ways as well, so he studied kissing couples to see if both people tended to lean to their right more often than to their left (and if so, how strong the tendency is). He and his researchers observed couples from age 13 to 70 in public places such as airports, train stations, beaches, and parks in the United States, Germany, and Turkey. They were careful not to include couples who were holding objects such as luggage that might have affected which direction they turned. We will assume these couples are representative of the overall decision making process when kissing.

> In total, 124 kissing pairs were observed with 80 couples leaning right.

(a) What are the 
* observational units?
* the (single!) variable?  is it categorical or quantitative?
* the statistic?
* the parameter?


(b) Do the data from the kissing study provide convincing evidence that the probability of leaning right is **smaller** than 0.8? 
* State the hypotheses
* report the p-value [describing how you determined it] 
* clarifying what it is in words / terms of the probability of ...
* interpret the strength of evidence against the null hypothesis).  

To find the p-value, use the simulation structure from the **infer** package (but this time, note that the null hypothesis is different from lab 3 because we only have **one** variable!!!).  Much of the simulation code is written below.  You need to do two thing to the code:  (1) toggle to `#| eval: true` and (2) fill in all the blank spaces below.

```{r}
#| eval: false

library(infer)

# to control the randomness
set.seed(47)

# first create a data frame with the kissing data
kissing <- data.frame(side = c(rep("right", ___ ), rep("left", ___ )))

# then find the proportion who kiss right 
p_obs_right <- kissing |>
    specify(response = ___ , ___ = "right") |>  # note, only one variable here!!!
    calculate(stat = "prop") |> 
    pull()

p_obs_right   #  did the computer print the correct number?

# now apply the framework to get the p-value
null_kiss <- ___  |>
  specify(response = ___ , success = "___ ") |>
  hypothesize(null = "point", p = .8) |>    # new null hypothesis!  Here H0: p = 0.8
  generate(reps = 1000, type = "simulate") |> 
  calculate(stat = "prop")

# visualize the null sampling distribution
null_kiss_right |> 
  ggplot(aes(x = ___)) + 
  geom_histogram() + 
  geom_vline(xintercept = ___, color = "red")

# calculate the actual one-sided p-value
null_kiss_right |>
  summarize(p_value = mean(___ <= ___))
```



(c) Do the data from the kissing study provide convincing evidence that the probability of leaning right **differs** from 0.8? (Aha, the alternative hypothesis has changed!)

(Use the code and instructions from part (b) and provide a different visualization - with both upper and lower tails (as "more extreme") indicated - and a different p-value.) 


(d) Reconsider part (b), the one-sided test:  how would your results have differed if you had chosen "kiss left" instead of "kiss right"?   Explain in words and re-run the code providing a full conclusion to the study (in words).    



(e) Reconsider part (b), again the one-sided test:  how would your results have differed if you had the same proportional data but one quarter of the observations  (that is, data were 20 right kissers out of 31)?  Explain in words and re-run the code providing a full conclusion to the study (in words).    




### Q2. Side effects of Avandia 

> Rosiglitazone is the active ingredient in the controversial type 2 diabetes medicine Avandia and has been linked to an increased risk of serious cardiovascular problems such as stroke, heart failure, and death. A common alternative treatment is pioglitazone, the active ingredient in a diabetes medicine called Actos. In a nationwide retrospective observational study of 227,571 Medicare beneficiaries aged 65 years or older, it was found that 2,593 of the 67,593 patients using rosiglitazone and 5,386 of the 159,978 using pioglitazone had serious cardiovascular problems. These data are summarized in the contingency table below. [D.J. Graham et al. "Risk of acute myocardial infarction, stroke, heart failure, and death in elderly Medicare patients treated with rosiglitazone or pioglitazone". In: JAMA 304.4 (2010), p. 411. issn: 0098-7484.]

|           |               | Cardiovascular | problems |         |
|-----------|---------------|----------------|----------|---------|
|           |               | yes            | no       | total   |
| Treatment | Rosiglitazone | 2,593          | 65,000   | 67,593  |
|           | Pioglitazone  | 5,386          | 154,592  | 159,978 |
|           | Total       | 7,979          | 219,592  | 227,571 |

> Determine if each of the following statements is true or false. If false, explain why. Be careful: The reasoning may be wrong even if the statement's conclusion is correct. In such cases, the statement should be considered false.


(a) Since more patients on pioglitazone had cardiovascular problems (5,386 vs. 2,593), we can conclude that the rate of cardiovascular problems for those on a pioglitazone treatment is higher.

(b) The data suggest that diabetic patients who are taking rosiglitazone are more likely to have cardiovascular problems since the rate of incidence was (2,593 / 67,593 = 0.038) 3.8% for patients on this treatment, while it was only (5,386 / 159,978 = 0.034) 3.4% for patients on pioglitazone.


(c) The fact that the rate of incidence is higher for the rosiglitazone group proves that rosiglitazone causes serious cardiovascular problems.

(d) Based on the information provided so far, we cannot tell if the difference between the rates of incidences is due to a relationship between the two variables or due to chance. 

[There is work to be done for part (d).  Hint: with such a huge sample size, the simulation is slow, you might try only a few hundred reps. I've written out the start of the data frame. You have to fill in the blanks with integers. Also, switch to `#| eval: true` so that the code will evaluate.]


```{r}
#| eval: false

# to control the randomness
set.seed(47)

# first create a data frame with the cardio data
cardio<- data.frame(treatment = c(rep("Rosiglitazone", ___), 
                                 rep("Pioglitazone", ___)),
                      outcome = c(rep("problem", ___), 
                                  rep("not", ___),
                                  rep("problem", ___), 
                                  rep("not", ___)))

```


### Q3. Crime concerns in China. 

> A 2013 poll of 563 people found that 24% of Chinese adults see crime as a very big problem.  The standard error for this estimate, which can reasonably be modeled using a normal distribution, is SE = 1.8% (= 0.018)^[Environmental Concerns on the Rise in China. September 19, 2013. Pew Research.]. Suppose an issue will get special attention from the Chinese government if more than 1-in-5 Chinese adults express concern on an issue.


(a) Construct hypotheses regarding whether or not crime should receive special attention by the Chinese government according to the 1-in-5 guideline.  [Remember, each hypothesis is a separate claim (i.e., sentence, although it may look like a mathematical equation) about the **population**.]

**Solution**

$H_0: p = 0.2$  The null hypothesis is that only 20% of adults in China (not more) think crime is an issue.

$H_A: p > 0.2$  The alternative hypothesis (the research question of interest!) is that more than 20% of adult Chinese think crime is an issue.


(b) Discuss the appropriateness of using a one-sided or two-sided test for this exercise. Consider: for this decision process, would we care about one or both directions?

**Solution**

In the example, the goal is to use the data to argue whether or not the issue (here: crime) will get special attention from the Chinese government.  The government is focused on the situation when concern is **over** 20% (the argument is that they will not pursue issues with less than 20% concern).  So the hypothesis (and resulting test) will be a one-sided test.

(c) Consider the hypothesis testing framework (that is, the last few steps where a probability is found and a conclusion is made).

* What is the probability you would need to calculate for this problem?
* If you can use any **coin** (that is, a coin with any particular probability of heads), how could you use the coin to calculate the desired probability? Be specific so someone could follow your instructions. 
* What are the possible conclusion outcomes for the study given the probability calculation?

**Solution**

* We need to know the probability of seeing 24% or more (of 563 people) who believe crime is an issue if in fact only 20% of people are worried in the population.  
* We can use a 20% coin and flip it 563 times to get the proportion of people in the sample who believe that crime is an issue.  We repeat that process many many times and model the possible values for the sample proportion when the true proportion is really 0.2.  To get the p-value, we measure the proportion of times the sample proportion (in the null hypothesis alternative universes) was 0.24 or greater.
* If the p-value is very small (say, less than 0.05), we conclude that more than 20% of people believe that crime is an issue.
* If the p-value is big (say, over 0.05), we cannot claim anything about the value of the true proportion of people who think crime is an issue.


It turns out that the p-value is quite small.  Below, the calculation using a mathematical model which we will cover in class soon. If 20% of Chinese adults think that crime is an issue, it would be pretty unusual (only happen in 1.3% of samples) to see a sample that has 24% or more Chinese adults reporting crime as an issue of concern.

```{r}
library(mosaic)
xpnorm( (0.24 - 0.2) / 0.018, 0, 1, plot = TRUE)
```


```{r}
praise()
```