---
title: "BIOS:4120 - Lab 10: Paired T-test, Power/Sample Size, and Fisher/Chi-Squared Tests"
output:
  html_document:
    toc: TRUE
    toc_float: TRUE
date: "March 26-27, 2024"
---


## Objectives

1. Review one-sample continuous data using a t-test
2. Perform a paired t-test
3. Practice power and sample size calculations in R
4. Perform Fisher's Exact test and Chi-squared test

## Load data

Today we will be using four datasets: 

The first dataset (lipids) has various lipid levels of the 3026 adults in a study. 

The second dataset (anorexia) includes weights of young female anorexia patients before and after treatment (paired data). There are three different treatments in the dataset but we are only going to look at the family treatment group in this lab.

The third dataset (corn) presents 15 paired data corresponding to the final height of corn data, one produced by cross-fertilization and the other by self-fertilization.

The last dataset (lister) compares the survival of surgery patients and whether sterilization practices were used in 1867.   

```{r message=FALSE, warning=FALSE}

#Part 1
lipids <- read.delim('https://raw.githubusercontent.com/IowaBiostat/data-sets/main/lipids/lipids.txt')

#Part 2
anorexia_alltreat <- read.delim("https://raw.githubusercontent.com/IowaBiostat/data-sets/main/anorexia/anorexia.txt")
# We need to index to just get the weights for those who received family treatment.
anorexia <- anorexia_alltreat[anorexia_alltreat$Treat == "FT",]

#Part 3
corn <- read.delim('https://raw.githubusercontent.com/IowaBiostat/data-sets/main/corn/corn.txt')

#Part 4
lister <- read.delim('https://github.com/IowaBiostat/data-sets/raw/main/lister/lister.txt')

```

## Part 1: One-sample continuous 

In the lipids dataset, the variable TRG is continuous. Suppose the national average for TRG is said to be 120.  Is our data significantly different than the national average?  

We can do this in R using the t.test() function as shown below. For one-sample continuous data, insert the continuous observations into the first argument. The default tests the null hypothesis that $\mu = 0$. We want to test whether $\mu$ is equal to 120 in this case. Therefore, you must specify your null hypothesis by saying $\mu=120$. 

```{r}
t.test(lipids$TRG, mu = 120)
```

*Interpret your results. Compare the confidence interval to the $\mu= 120$. The upper bound of the confidence interval is barely less than the null. Why do you think the p-value is still fairly low?*

<details><summary>Answer</summary>

The sample size is very large so the results will be highly significant even for small differences from the null value.

</details>


## Part 2: Paired t-test 

If we have data that is paired and we want to determine if there is a difference between the two groups, we can use a paired t-test. 

**For the anorexia dataset, we want to determine whether the patient's weight improved post-treatment compared to pre-treatment.**

First, we want to calculate the difference in weight for each patient. 

```{r}
# create a new column
anorexia$diff <- anorexia$Postwt-anorexia$Prewt
```

*Take a look at the new column "diff". Are most of the values positive or negative? What does this indicate?*

Now calculate the standard error:

```{r}
SE <- sd(anorexia$diff)/sqrt(17)
```

Find the t-statistic. Note that the numerator is simply the mean of the differences calculated. 

```{r}
t <- (mean(anorexia$diff) - 0) / SE

2*(1-pt(t, df=16))
```


A "paired t-test" does the same thing as the t-test above however it does it on the difference between the paired observations. To run a paired t-test in R we will need to use the code below:

```{r}
## Test for Post - Prior
t.test(anorexia$Postwt, anorexia$Prewt, paired = TRUE)
```

The t.test() function has other arguments. We have shown you the paired = TRUE input for when you want to run a paired t-test. If you do not put paired = TRUE for a paired t-test it will assume that the two sets of numbers are two separate samples and will run a two-sample t-test. We can also change the conf.level = .95 to other values to get confidence intervals other than a 95% CI.  

### Practice Problem 1

15 types of corn are measured in the "Corn" dataset and their heights are recorded. We are interested in whether the heights of corn are higher when they were produced by cross-fertilization instead of self-fertilization. Conduct a paired t-test in R to find if there is a significant difference in the effectiveness of these two methods.

<details><summary>Answer</summary>

```{r}
# Using R function
t.test(corn$cross, corn$self, paired = TRUE)

# By hand
corn$corndiff <- corn$cross - corn$self
SE <- sd(corn$corndiff)/sqrt(15)
t <- (mean(corn$corndiff) - 0) / SE

2*(1-pt(t, df = 14))
```

</details>


## Part 3: Power and sample size

In R there is a power.t.test() function that is useful. It takes the parameters of delta (expected difference in means), sd, n, and power. Let's say we want to conduct a study with high power where we collected paired samples. Let's assume that based on other similar studies the expected difference in means is 6 and the expected standard deviation of this difference is 4.

When using the power.t.test() function, the parameter you do not specify will be the parameter the function will calculate. But you must specify 3 of the 4 parameters (difference, sd, n, power). 

#### How many patients would we need in our study to achieve a power of .99?  

In this example we would use the following code to find the **sample size**:

```{r}
# n is not specified 
power.t.test(delta = 6,
             power = .99, 
             sd = 4,
             type = "paired")
```

The results give us the estimated number of pairs needed to achieve a power of at least .99 (given the parameters are accurate). We need at least 11 pairs.

*Why do you think it only takes 11 pairs to achieve a power of 0.99?*  

<details><summary>Answer</summary>

This is because the difference between the groups (delta) is very large as it is over 1 standard deviation away from 0.

</details>

#### Find power

This function can also be used to find power. So lets say we have a sample size of 20 in this study. What would the power be? We can use power.t.test function and omit the power parameter.  

```{r}
power.t.test(delta = 6,
             n = 20, 
             sd = 4, 
             type = "paired")
```

### Practice Problem 2

Find the power of a paired study where the expected difference in the means is 3, the expected standard deviation of this difference is 5, and the sample size is 15.

<details><summary>Answer</summary>

```{r}
power.t.test(delta = 3,
             n = 15,
             sd = 5, 
             type = "paired")
```
The power is approximately 58%.

</details>


<br>


## Part 4: Chi-squared test

In class you have learned how to calculate the chi-squared statistic by hand. In R, we can perform a Fisher's Exact and Chi-squared test quickly by putting the data into a table and running the chisq.test() function or the fisher.test() function.

For this example, create a table from the lister dataset:

```{r}
tab <- table(lister)
tab
```

The table above saved as "tab" can be placed directly into the functions to receive the results of a chi-squared test and fisher's exact test. **NOTE:** Having the correct=FALSE argument will provide the same test statistic as calculated by hand.  

```{r}
chisq.test(tab, correct = FALSE)

fisher.test(tab)
```

Looking at these two outputs, we can see that the chi-squared test gives the test statistic, DF, and p-value whereas the fisher's exact test gives us the p-value, CI for the odds ratio, and the odds ratio. The odds ratio is something we will be talking about within the next two weeks. 


### Practice problem 3


a) (By hand) Consider a problem in which we observe students to see if they took notes by hand or by laptop and recorded whether they passed or failed. Of the 67 that used a laptop, 44 passed. Of the 76 that wrote by hand, 56 of them passed. Construct an observed and expected table. Compute the chi-squared statistic and find the p-value from the chi-squared table. Interpret your results. 

**Solution in supplement**

b) (Using R) Do this same problem using R. To start, use the following code:

```{r}
chidata <- data.frame("Method" = c(rep("Laptop", 67), rep("Hand", 76)), 
                      "Result" = c(rep("Passed", 44), rep("Failed", 23), rep("Passed", 56), rep("Failed", 20)))

tab <- table(chidata)
```

<details><summary>Answer</summary>

```{r}
chisq.test(tab, correct = FALSE)

fisher.test(tab)
```
The p-values from these tests matched what we saw from our hand calculations. There is not significant evidence that note taking method affects test performance.

</details>
