AE-22-Hypo-Testing-III

Discussion around alpha

What is alpha?

This is our significance level that we use to help make decisions and conclusions for a hypothesis test. It also is the probability of making a type 1 error

Why would a researcher choose to have a larger or smaller \(\alpha\) value?

The larger the alpha, the less evidence you will need to reject the null hypothesis. However, the larger the alpha, the more likely it is to reject the null hypothesis incorrectly.

Iris Data

The Iris Dataset contains four features (length and width of sepals and petals) of 50 samples of three species of Iris (Iris setosa, Iris virginica and Iris versicolor). A sepal is the outer parts of the flower (often green and leaf-like) that enclose a developing bud. The petal are parts of a flower that are the pollen producing part of the flower that are often conspicuously colored. The difference between sepals and petals can be seen below.

The data were collected in 1936 at the Gaspé Peninsula, in Canada. For the first question of the exam, you will use this data sets to investigate a variety of relationships to learn more about each of these three flower species. The data set is prepackaged in R, and is called iris.

── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.0     ✔ purrr   0.3.5
✔ tibble  3.1.8     ✔ dplyr   1.0.9
✔ tidyr   1.2.1     ✔ stringr 1.4.1
✔ readr   2.1.3     ✔ forcats 0.5.2
Warning: package 'ggplot2' was built under R version 4.2.2
Warning: package 'tidyr' was built under R version 4.2.2
Warning: package 'readr' was built under R version 4.2.2
Warning: package 'purrr' was built under R version 4.2.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
── Attaching packages ────────────────────────────────────── tidymodels 1.0.0 ──
✔ broom        1.0.1     ✔ rsample      1.1.0
✔ dials        1.1.0     ✔ tune         1.0.1
✔ infer        1.0.3     ✔ workflows    1.1.0
✔ modeldata    1.0.1     ✔ workflowsets 1.0.0
✔ parsnip      1.0.3     ✔ yardstick    1.1.0
✔ recipes      1.0.3     
Warning: package 'broom' was built under R version 4.2.2
Warning: package 'dials' was built under R version 4.2.2
Warning: package 'parsnip' was built under R version 4.2.2
Warning: package 'recipes' was built under R version 4.2.2
── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
✖ scales::discard() masks purrr::discard()
✖ dplyr::filter()   masks stats::filter()
✖ recipes::fixed()  masks stringr::fixed()
✖ dplyr::lag()      masks stats::lag()
✖ yardstick::spec() masks readr::spec()
✖ recipes::step()   masks stats::step()
• Search for functions across packages at https://www.tidymodels.org/find/
Loading required package: airports
Loading required package: cherryblossom
Loading required package: usdata

Attaching package: 'openintro'

The following object is masked from 'package:modeldata':

    ames
data(iris)

From last AE, calculate and report the sample statistic below:

iris |> 
  filter(Species != "virginica") |>
  group_by(Species) |>
  summarize(m = mean(Sepal.Length))
# A tibble: 2 × 2
  Species        m
  <fct>      <dbl>
1 setosa      5.01
2 versicolor  5.94

Save your response below as estimate

#define your difference in means as your estimate below

estimate <- -0.93

To “standardize our statistic”… we need to divide by the standard error. The standard error of the different in sample means can be calculated as follows:

Below, define each piece of the standard error accordingly

sd1 <- iris |> 
  filter(Species == "setosa") |>
  summarise(sd_s = sd(Sepal.Length)) |>
  as.numeric()
  
  
  
n1 <- 50

sd2 <- iris |> 
  filter(Species == "versicolor") |>
  summarise(sd_s = sd(Sepal.Length)) |>
  as.numeric()

n2 <- 50  

Now, use the information to calculate your t-statistic below:

tscore <- (estimate - 0) / sqrt(((sd1^2)/n1) + ((sd2^2)/n2))

We use pt to calculate our p-value. Recall that we need to define our degrees of freedom! Commonly, we use min(n1-1; n2-1)

pt(tscore , df = 49)
[1] 1.815135e-14

Write an appropriate scope of inference in the context of the problem below:

Random Sampling did not happen

Random Assignment did not happen

We do not have random sampling, so any results we find are generalized to our sample or a similar sample.

There is an association between Species and Sepal length in our sample or a similar sample.

Two Categorical Variables: Case study: CPR and blood thinner

Cardiopulmonary resuscitation (CPR) is a procedure used on individuals suffering a heart attack when other emergency resources are unavailable. This procedure is helpful in providing some blood circulation to keep a person alive, but CPR chest compressions can also cause internal injuries. Internal bleeding and other injuries that can result from CPR complicate additional treatment efforts. For instance, blood thinners may be used to help release a clot that is causing the heart attack once a patient arrives in the hospital. However, blood thinners negatively affect internal injuries.

Here we consider an experiment with patients who underwent CPR for a heart attack and were subsequently admitted to a hospital. Each patient was randomly assigned to either receive a blood thinner (treatment group) or not receive a blood thinner (control group). The outcome variable of interest was whether the patient survived for at least 24 hours.

data(cpr)

Calculate your sample statistic below. Next, write out your sample statistic using proper notation (died - survived).

cpr |>
  group_by(group, outcome) |>
  summarize(props = n())
`summarise()` has grouped output by 'group'. You can override using the
`.groups` argument.
# A tibble: 4 × 3
# Groups:   group [2]
  group     outcome  props
  <fct>     <fct>    <int>
1 control   died        39
2 control   survived    11
3 treatment died        26
4 treatment survived    14

Now create an appropriate visualization of these data below.

cpr |>
  ggplot(
    aes(x = group , y = ..count..)
  ) + 
  geom_bar(aes(fill = outcome), position = "stack")
Warning: The dot-dot notation (`..count..`) was deprecated in ggplot2 3.4.0.
ℹ Please use `after_stat(count)` instead.

Next, explain how one dot (observation) is created on the permutation null distribution used to test our hypotheses…

Assuming that group is independent of outcome, we permute outcome to new values of group. Next, we calculate the new proportions of died for each permuted group. We subtract these difference in proportions. That is a dot!

Now, calculate the p-value below:

set.seed(1234)

null_dist <- cpr |>
  specify(response = outcome , explanatory = group , success = "died") |>
  hypothesize(null = "independence") |>
  generate(reps = 1000, type = "permute") |>
  calculate(stat = "diff in props" , order = c("control", "treatment"))
visualize(null_dist) +
 shade_p_value(obs_stat = .13, direction = "two-sided") 

null_dist |>
  get_p_value(obs_stat = .13, direction = "two-sided")
# A tibble: 1 × 1
  p_value
    <dbl>
1   0.216