Let’s apply what we’ve learned in Chapter 12 to R.

Objectives

New Commands:

Arguments Notes
df$x df is name of dataframe, x is column of first treatment/group
df$y df is name of dataframe, y is column of second treatment/group
mu = expected mean difference if H0 is true (typically 0)
paired = paired = T if paired t-test, paired = F if two-sample
var.equal = var.equal = T if variances equal, var.equal = F if not
R Command Notes
t.test(df$x, df$y, mu =, paired = T) Paired t-test to test whether the mean difference of paired measurements equals a null value
t.test(df$x, df$y, mu =, paired = F, var.equal = T) Two-sample t-test to compare the difference between means of two groups
t.test(df$x~df$y, data = df, var.equal = T) Different syntax for two-sample t-test; use when both groups are in ONE column, and second column is the group names
t.test(df$x, df$y, mu =, paired = F, var.equal = F) Welch’s t-test to compare the difference between means of two groups, where everything is the same for a two-sample t-test EXCEPT var.equal = F, specifying the variance of both groups are NOT equal
var.test(df$x, df$y, ratio = 1) F test to compare variance of two groups; only use if data are normally distributed
var.test(df$x~df$y, ratio = 1) Different syntax for F-test; use when both groups are in ONE column, and second column is the group names


Reminder: Save your script for practice at home! :)

Shortcuts:

Symbol or Command Keyboard Shortcut
<- Alt + -
# Shift + 3
Run one line in script Ctrl + Enter
Run entire script Ctrl + Shift + Enter
Open new script Ctrl + Shift + N


Exercise 10
1. Open RStudio and prepare a new script.

Open a new script. All of your code for today’s exercises, and your notes and comments will go in this script. Write your filename, title, author, date, and description of the script.

# Filename: (what you will save the script as)
# Title: (give script a title)
# Author: (write your full name here)
# Date: Month Day Year (write the actual date here)

# Description: (describe what this script is for)


2. Let’s continue with t-tests, except this time we have two treatments or groups. When conducting research, scientists often expect one treatment or group to differ from the other, and need to compare means to ask whether their data simply meet null expectations or show a significant difference between the two treatments/groups. To do this, we need to perform a hypothesis test. In this case, we will conduct either a paired or two-sample t-test depending on how the dat were collected.

In R, we use the function _t.test( ). Once you read in your data, you need the following information:

  1. df$x - the name of your dataframe, and your first group column
  2. df$y - the name of your dataframe, and your second group column
  3. mu = - the difference in means proposed under the null hypothesis (H0)
  4. paired = - whether your data are paired (T) or two-sample (F)

If paired = F (two-sample t-test), you also need to know:

  1. var.equal = - whether the group variances are equal (T) or false (F). If variances are equal, R will run a two-sample t-test, and if they are not equal, R will run Welch’s t-test. We can test for equal variance beforehand with an F test (normally distributed data) or Barlett’s test (non-normal data).

Let’s practice through example.

Example 1: Hasselquist et al. (1999) studied the effect of high testosterone levels on immunocompetence in 13 red-winged blackbirds, under the hypothesis that males with higher testosterone levels have reduced immunocompotence. To test this, they implanted males with tubes filled with testosterone, and recorded their antibody production before and after implantation. Do higher testosterone levels actually impact immune response?

Step 1: Read in the data (available here: https://github.com/lczawadzki/biostats/raw/main/data/blackbirds.csv)

blackbirds <- read.csv("https://github.com/lczawadzki/biostats/raw/main/data/blackbirds.csv")

Step 2: Read the problem/look at the data and ask yourself, what sample design is being used? Is this a paired or two-sample design? Since each male is sampled twice, this is a paired design. That is, the sampling unit is each male bird, and the samples taken are antibody production before and after implantation.

Step 3: Once you have identified the sample design, ask yourself, which t-test should I run? Paired design = paired t-test.

Step 4: State your null and alternate hypotheses. This will help you decide what mu = in your t.test. Here, the H0: Mean antibody production does not differ after implantation (\(µ_d\) = 0), and HA: Mean antibody production differs after implantation (\(µ_d \not =\) 0). Therefore, mu = 0 (no difference).

Step 5: Run your paired t-test using t.test( ). Note for this data, we want to use the log-transformed data columns.

t.test(blackbirds$logBeforeImplant, blackbirds$logAfterImplant, mu = 0, paired = T)

Step 6: Interpret your output and state your conclusion.

Let’s read through the output. The output tells you a number of things about your data:

  1. Line 1 tells you what data you used for the test.
  2. Line 2 states specifically your test statistic (t), the degrees of freedom (df), and your P -value. Remember, the P-value is the probability of obtaining a mean difference as extreme, or more extreme, than your sample mean difference. That is, it is the probability of obtaining your mean difference under the null hypothesis.
  3. Line 3 tells you the alternative hypothesis.
  4. Line 4 tells you the 95% confidence interval of the proportion, which are values between which you are 95% confident that the true population proportion lies between.
  5. Line 5 tells you information about your sample estimates, specifically, the mean difference of your sample data.

So, from our output, the first thing we notice is that R ran a “Paired t-test”. This is exactly what we wanted to run, so we’re good so far. Now, let’s look at each line:

  1. Our data comes from the dataframe blackbirds and our variables are logBeforeImplant and logAfterImplant.
  2. Our test statistic (t) is -1.2714, our degrees of freedom (df) are 12, and our P -value = 0.2277.
  3. Our alternative hypothesis is that the difference in means is not equal to 0.
  4. We are 95% confident that the true mean difference lies between -0.152 and 0.0401. Recall these are (natural) log-transformed values. You can transform them back using e^x to be more interpretable.
  5. Our sample mean difference (the estimate) equals -0.056, again a (natural) log-transformed value.

The point of running a paired t-test is to test if there is an effect. This is a hypothesis test! Running the test alone is insufficient, we now need to draw our conclusions. What conclusions can you draw from your output? “P > 0.05, therefore our results are NOT statistically significant, and we fail to reject the H0. Our data match our null expectations, and are likely to occur due to chance. We conclude that testosterone has no effect on immunocompetence in red-winged blackbirds.”

Example 2: Loggerhead shrikes prey on horned lizards. Young et al. (2004) wanted to investigate whether lizards predated on had shorter horns than living lizards, under the speculation that these horns help protect lizards from predation. To test this, they measured horns from 30 lizards killed by shrikes, and 154 living lizards. Do living horned lizards have longer horns than dead ones?

Step 1: Read in the data (available here: https://github.com/lczawadzki/biostats/raw/main/data/hornedlizards.csv)

lizards <- read.csv("https://github.com/lczawadzki/biostats/raw/main/data/hornedlizards.csv")

Step 2: Read the problem/look at the data and ask yourself, what sample design is being used? Is this a paired or two-sample design? Since each lizard is only sampled once, but we have two groups, this is an unpaired design. That is, the sampling unit is each individual lizard, and the sample taken is horn length.

Step 3: Once you have identified the sample design, ask yourself, which t-test should I run? Unpaired design = two-sample t-test.

Step 4: State your null and alternate hypotheses. Here, the H0: Living and killed lizards do not differ in mean horn length (\(µ_1\) = \(µ_2\)), and HA: Living and killed lizards differ in mean horn length (\(µ_1 \not = µ_2\)).

Step 5: Before we run our two-sample t-test, first check that you meet assumptions of equal variance for between groups using var.test( ). Note that we use the notation x~y, since our numerical data are grouped in ONE column. (F-tests assume data are normally distributed. We will discuss checking for normality in the next exercise. If data are non-normal, you would use Bartlett’s test here).

var.test(lizards$squamosalHornLength~lizards$Survival, ratio = 1)

We see that the P -value equals 0.7859, so we fail to reject the H0 that variances are equal. Therefore, we can assume equal variance, and use var.equal = T.

Step 6: Now that you know var.equal = T, run your t-test. Note that we use the notation x~y, since our numerical data are grouped in ONE column.

t.test(lizards$squamosalHornLength~lizards$Survival, data = lizards, var.equal = T)

Step 7: Interpret your output and state your conclusion.

Let’s read through the output. It has the same format as a paired t-test.

So, from our output, the first thing we notice is that R ran a “Two-sample t-test”. This is exactly what we wanted to run, so we’re good so far. Now, let’s look at each line:

  1. Our data comes from the dataframe lizards and our variables are SquamosalHornLength by Survival.
  2. Our test statistic (t) is -4.394, our degrees of freedom (df) are 182, and our P -value = 2.27e-05.
  3. Our alternative hypothesis is that the difference in means is not equal to 0.
  4. We are 95% confident that the true mean difference in horn length between living and killed lizards lies between -3.335 mm and -1.254 mm.
  5. Our sample means (the estimates) equals 21.987 mm for killed lizards, and 24.281 mm for living lizards.

The point of running a two-sample t-test is to test if there is an effect. This is a hypothesis test! Running the test alone is insufficient, we now need to draw our conclusions. What conclusions can you draw from your output? “P < 0.05, therefore our results are statistically significant, and we reject the H0. Our data deviate from our null expectations, and are not due to chance. We conclude that living lizards have longer horns than killed lizards.”

What if the variance was NOT equal? (p < 0.05) If the variance in Step 5 was not equal (F-test had a P < 0.05), you would run Welch’s t-test instead, using the same syntax, except var.equal = F rather than T.

t.test(lizards$squamosalHornLength~lizards$Survival, data = lizards, var.equal = F)

Notice that the output is very similar to the two-sample t-test we conducted above, except that the first line of the output tells us that R conducted a Welch’s Two Sample t-test.

Now it’s time to practice on your own.

  1. Let’s revisit our dataset on seal oxygen levels: https://github.com/lczawadzki/biostats/raw/main/data/sealoxygen.csv. This dataset contains information on metabolic costs of feeding and non-feeding dives (in ml O2/kg) for ten Weddell seals. It is hypothesized that feeding changes the metabolic costs of a dive. Test this hypothesis with your sample.

    a. Is this a paired or two-sample design?
    b. Which type of t-test should you use?
    c. Report your null hypothesis, run your test in R, and state your conclusion (statistical and in language relevant to the problem). If using a two-sample t-test, be sure to check for equal variances with an F-test before running your t-test.

  2. Patient data on heart attacks is in the GitHub: https://github.com/lczawadzki/biostats/raw/main/data/heart_attack.csv. Each datapoint is a single individual. It is hypothesized that the age at which you have a heart attack depends on whether a patient is male or female, with males experiencing heart attacks earlier than females. Test this hypothesis with your sample. NOTE: when you read in the data for this sample, include the argument, “stringsAsFactors = T”.

    a. Is this a paired or two-sample design?
    b. Which type of t-test should you use?
    c. Report your null hypothesis, run your test in R, and state your conclusion (statistical and in language relevant to the problem). If using a two-sample t-test, be sure to check for equal variances with an F-test before running your t-test.