Lab 5 Inference in R


Data Science in Biomedical Sciences

UCI COSMOS 2026
Noah Benjamin
(adapted material from and Mine Dogucu and Zhaoxia Yu)

Objective

  • We want to explore and examine possible relationships between two variables.
  • We first focus on the case where we are investigating a relationship between one binary categorical variable (e.g., gender) and one numerical variable (e.g., body temperature).
  • Then, we examine the relationship between two numerical variables (e.g., years of education and income).
  • Finally, we discuss the relationship between two categorical variables (e.g., treatment and survival status).

Relationship Between a Numerical Variable and a Binary Variable

  • In these situations, the binary variable typically represents two different groups or two different experimental conditions.
  • We treat the binary variable (factor) as the explanatory variable in our analysis.
  • Thus, the numerical variable is regarded as the response (target) variable (e.g., body temperature).

Relationship Between a Numerical Variable and a Binary Variable

Dot plots of vitamin C content (numerical) by cultivar (categorical) for the cabbages data set from the MASS package.

Relationship Between a Numerical Variable and a Binary Variable

A more common way of visualizing the relationship between a numerical variable and a categorical variable is to create boxplots like we did yesterday.

Relationship Between a Numerical Variable and a Binary Variable

  • In general, we say that two variables are related if the distribution of one of them changes as the other one varies.
  • We can measure changes in the distribution of the numerical variable by obtaining its summary statistics for different levels of the categorical variable.
  • it is common to use the difference of means when examining the relationship between a numerical variable and a categorical variable.
  • In the above example, the difference of means of vitamin C content is \(64.4 - 51.5 = 12.9\) between the two cultivars. Is this difference significant? (this is what we test)

Two sample t-test

  • In general, we can denote the population means of two groups as \(\mu_{1}\) and \(\mu_{2}\).
  • The null hypothesis indicates that the population means are equal, \(H_{0}: \mu_{1} = \mu_{2}\).
  • In contrast, the alternative hypothesis is one the following: \[\begin{array}[t]{l@{\quad}p{6.7cm}} H_{A}: \mu_{1} > \mu_{2} \\ H_{A}: \mu_{1} < \mu_{2} \\ H_{A}: \mu_{1} \ne \mu_{2} \\ \end{array}\]

Two sample t-test

  • We can also express these hypotheses in terms of the difference in the means:

\[\begin{array}[t]{l@{\quad}p{6.7cm}} H_{A}: \mu_{1} - \mu_{2} > 0 \\ H_{A}: \mu_{1} - \mu_{2} < 0 \\ H_{A}: \mu_{1} - \mu_{2} \ne 0 \\ \end{array}\]

  • Then the corresponding null hypothesis is that there is no difference in the population means, \[H_{0}: \mu_{1} - \mu_{2} = 0\]

Two sample t-test

  • Previously, we used the sample mean \(\bar{X}\) to perform statistical inference regarding the population mean \(\mu\).
  • To evaluate our hypothesis regarding the difference between two means, \(\mu_{1} - \mu_{2}\), it is reasonable to examine the difference between the sample means, \(\bar{X}_{1} - \bar{X}_{2}\), as our test statistic.
  • For this, we can simply use the t.test() function in R.

Two sample t-test

Cabbages example:

library(MASS)
data(cabbages)

t.test(VitC ~ Cult, data=cabbages)

    Welch Two Sample t-test

data:  VitC by Cult
t = -6.3909, df = 56.376, p-value = 3.405e-08
alternative hypothesis: true difference in means between group c39 and group c52 is not equal to 0
95 percent confidence interval:
 -16.94296  -8.85704
sample estimates:
mean in group c39 mean in group c52 
             51.5              64.4 

Paired t-test

  • While we hope that the two samples taken from the population are comparable except for the characteristic that defines the grouping, this is not guaranteed in general.
  • To mitigate the influence of other important factors (e.g., age) that are not the focus of our study, we sometimes pair (match) each individual in one group with an individual in the other group so that the paired individuals are very similar to each other except for the variable that defines the grouping.
  • For example, we might recruit twins and assign one of them to the treatment group and the other one to the placebo group.
  • Sometimes, the subjects in the two groups are the same individuals under two different conditions.

Paired t-test

  • When the individuals in the two groups are paired, we use the paired \(t\)-test to take the pairing of the observations between the two groups into account.
  • Using the difference, \(D\), between the paired observations, the hypothesis testing problem reduces to a single sample \(t\)-test problem.
  • In practice, we can use the function t.test() with the option paired=TRUE.

Paired t-test

  • As an example, we use the study of the effect of tobacco smoke on platelet function by Levine (1973).
  • In his study, for a group of eleven people, platelet aggregation was measured before and after smoking a cigarette.
  • Therefore, observations in the Before sample and After sample are from the same subjects.
  • For each subject, an observation in the Before sample is paired with an observation in the After sample.

Paired t-test

Platelet example:

Platelet<- read.table("../data/Platelet.txt", header=T, sep="")
glimpse(Platelet)
Rows: 11
Columns: 2
$ Before <int> 25, 25, 27, 44, 30, 67, 53, 53, 52, 60, 28
$ After  <int> 27, 29, 37, 56, 46, 82, 57, 80, 61, 59, 43

Paired t-test

Let’s run a paired t-test

t.test(Platelet$Before, Platelet$After, paired = TRUE)

    Paired t-test

data:  Platelet$Before and Platelet$After
t = -4.2716, df = 10, p-value = 0.001633
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
 -15.63114  -4.91431
sample estimates:
mean difference 
      -10.27273 

Paired t-test

See what happens if we fail to account for the pairing of observations!

t.test(Platelet$Before, Platelet$After)

    Welch Two Sample t-test

data:  Platelet$Before and Platelet$After
t = -1.4164, df = 19.516, p-value = 0.1724
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -25.425913   4.880458
sample estimates:
mean of x mean of y 
 42.18182  52.45455 

Two numerical variables

  • A simple way to visualize the relationship between two numerical variables is with a scatterplot.
  • As our first example, we use the bodyFat data: http://lib.stat.cmu.edu/datasets/bodyfat.
  • Suppose that we are interested in examining the relationship between percent body fat (siri) and abdomen circumference (abdomen) among men.

Scatterplot

The plot suggests that the increase in percent body fat tends to coincide with the increase in abdomen circumference.

Scatterplot

Next, we examine the relationship between the annual mortality rate due to malignant melanoma for US states and the latitude of their centers.

Scatterplot

  • Using scatterplots, we could detect possible relationships between two numerical variables.

Scatterplot

  • In both examples, we can see that changes in one variable coincides with substantial systematic changes (increase or decrease) in the other variable.

Scatterplot

  • Since the overall relationship can be presented by a straight line, we say that the two variables have linear relationship.

Scatterplot

  • We say that percent body fat and abdomen circumference have positive linear relationship.

Scatterplot

  • In contrast, we say that annual mortality rate due to malignant melanoma and latitude have negative linear relationship.

Correlation

  • To quantify the strength and direction of linear relationship between two numerical variables, we can use Pearson’s correlation coefficient, \(r\), as a summary statistic.
  • The value of \(r\) is always between \(-1\) and \(+1\) and the relationship is strong when \(r\) approaches \(-1\) or \(+1\).
  • The sign of \(r\) shows the direction (negative or positive) of the linear relationship.
  • For observed pairs of values, \((x_{1}, y_{1}), (x_{1},y_{1}), \ldots, (x_{n}, y_{n})\),

\[\begin{eqnarray*} r_{xy} = \frac{\sum_{i=1}^{n}(x_{i}-\bar{x})(y_{i}- \bar{y})}{(n-1)s_{x}s_{y}} \end{eqnarray*}\]

Correlation

Let’s look at the correlation between height and weight.

Correlation

We can do some fancy math to calculate \(r\).

Correlation

  • We can examine whether the correlation is statistically significant or not using the cor.test() function in R.

Correlation

  • The following code tests whether the correlation coefficient between siri and abdomon is greater than zero.
data(bodyfat, package="mfp")
bodyfat$abdomen = bodyfat$abdomen *.39
cor.test(bodyfat$siri, bodyfat$abdomen, alternative = "greater")

    Pearson's product-moment correlation

data:  bodyfat$siri and bodyfat$abdomen
t = 22.112, df = 250, p-value < 2.2e-16
alternative hypothesis: true correlation is greater than 0
95 percent confidence interval:
 0.77505 1.00000
sample estimates:
      cor 
0.8134323 

Correlation

Later, we will discuss more advanced models for examining such relationships using linear regression models.

Two categorical variables

  • We now discuss techniques for exploring relationships between categorical variables.
  • As an example, we consider the five-year study to investigate whether regular aspirin intake reduces the risk of cardiovascular disease.
  • We usually use contingency tables to summarize such data.

Two categorical variables

  • Each cell shows the frequency of one possible combination of disease status (heart attack or no heart attack) and experiment group (placebo or aspirin).
  • Using these frequencies, we can calculate the sample proportion of people who suffered from heart attack in each experiment group separately.
  • There were 11034 people in the placebo group, of which 189 had heart attack. The proportion of people suffered from a heart attack in the placebo group is therefore \(p_1 = {189}/{11034} = 0.0171\).
  • The proportion of people suffered from heart attack in the aspirin group is \(p_2 = {104}/{11037} = 0.0094\).

Two categorical variables

  • Here, we refer to these proportions as the risk of heart attack for the two groups.
  • Substantial difference between the sample proportion of heart attack between the two experiment groups could lead us to believe that the treatment and disease status are related.
  • A common summary statistic for comparing sample proportions is the relative proportion: \(p_{2}/p_{1}\).

Two categorical variables

  • Since the sample proportions in this case are related to the risk of heart attack, we refer to the relative proportion as the relative risk.
  • Here, the relative risk of suffering from heart attack is \[{p_2}/{p_1} = {0.0094} / {0.0171}= 0.55\]
  • This means that the risk of a heart attack in the aspirin group is 0.55 times of the risk in the placebo group.

Two categorical variables

  • It is more common to compare the sample odds, \[\begin{equation*} o=\frac{p}{1-p}, \end{equation*}\]
  • This is slightly different from the proportions we are used to
  • The odds of a heart attack in the placebo group, \(o_1\), and in the aspirin group, \(o_2\), are \[\begin{eqnarray*} o _1 &=& \frac{0.0171}{(1-0.0171)} = 0.0174, \\ o _2 &=& \frac{0.0094}{(1-0.0094)} = 0.0095. \end{eqnarray*}\]

Two categorical variables

  • We usually compare the sample odds using the sample odds ratio

\[\begin{eqnarray*} \mathit{OR}_{21} = \frac{o_2}{o _1} = \frac{0.0095}{0.0174}= 0.54. \end{eqnarray*}\]

  • Later, we will discuss more advanced models for making statistical inference about odds ratio using logistic regression models.
  • Here, we use a simpler approach for assessing the significance of the relationship between two binary (and in general, two categorical) variables presented as a contingency table.

Pearson’s \(\chi^{2}\) Test of Independence

  • As discussed above, we can use contingency tables to find the observed frequencies for different combinations of categories of the two variables.
  • We denote the observed frequency in row \(i\) and column \(j\) as \(O_{ij}\).
  • Using the independence rule, we can find the expected frequencies under the null hypothesis, which indicates that the two variables are independent.
  • Recall that for two independent random variables, the joint probability is equal to the product of their individual probabilities.

Pearson’s \(\chi^{2}\) Test of Independence

  • Pearson’s \(\chi^{2}\) test uses a test statistic, which we denote as \(Q\), to measure the discrepancy between the observed data and what we expect to observe under the null hypothesis (i.e., assuming the null hypothesis is true).
  • Note that the null hypothesis in this case states that the two variables are independent.

Pearson’s \(\chi^{2}\) Test of Independence

  • We denote the expected frequency in row \(i\) and column \(j\) as \(E_{ij}\).
  • Pearson’s \(\chi^{2}\) test summarizes the differences between the expected frequencies (under the null hypothesis) and the observed frequencies over all cells of the contingency table,

\[\begin{equation*} Q = \sum_{i} \sum_{j} \frac{(O_{ij} - E_{ij})^{2}}{E_{ij}}. \end{equation*}\]

Pearson’s \(\chi^{2}\) Test of Independence

  • In practice, you don’t need to worry about the fancy equations
  • We can use the chisq.test() in R.

asp <- matrix(c(189, 10845, 104, 10933), nrow=2, ncol=2, byrow = TRUE)
asp
     [,1]  [,2]
[1,]  189 10845
[2,]  104 10933
chisq.test(asp)

    Pearson's Chi-squared test with Yates' continuity correction

data:  asp
X-squared = 24.429, df = 1, p-value = 7.71e-07

Smoking and low birthweight babies

As another example, we will examine the association between smoking during pregnancy and having low birth weight babies using the birthwt from the MASS package.

Smoking and low birthweight babies

Let’s code a little bit

library(MASS)
data("birthwt") 
birthwt <- birthwt %>% 
  as_tibble() %>% 
  mutate(across(c(low, race, smoke, ht), as.factor))
head(birthwt)
# A tibble: 6 × 10
  low     age   lwt race  smoke   ptl ht       ui   ftv   bwt
  <fct> <int> <int> <fct> <fct> <int> <fct> <int> <int> <int>
1 0        19   182 2     0         0 0         1     0  2523
2 0        33   155 3     0         0 0         0     3  2551
3 0        20   105 1     1         0 0         0     1  2557
4 0        21   108 1     1         0 0         1     2  2594
5 0        18   107 1     1         0 0         1     0  2600
6 0        21   124 3     0         0 0         0     0  2622

Smoking and low birthweight babies

Running chisq.test()

tab <- table(birthwt$smoke, birthwt$low)
res <- chisq.test(tab)
tab
   
     0  1
  0 86 29
  1 44 30
res

    Pearson's Chi-squared test with Yates' continuity correction

data:  tab
X-squared = 4.2359, df = 1, p-value = 0.03958

Smoking and low birthweight babies

We can check the expected counts under the null hypothesis, and also see the observed accounts from the data.

res$observed
   
     0  1
  0 86 29
  1 44 30
res$expected
   
           0        1
  0 79.10053 35.89947
  1 50.89947 23.10053

Schedule for more coding examples

  • Confidence interval for the population mean
  • One-sample t-test
  • Test for Proportion
  • Two-sample t-test
  • Correlation test
  • \(\chi^2\) test

Simulated Data

Let’s try this on some simulated data like we have before.

set.seed(0) # We set a seed which makes the randomization work the same
norm_size <- 100
norm_random <- rnorm(n = norm_size, mean = 10, sd = 2) # simulate 100 draws from a N(10, 2) distribution
summary(norm_random) # let's see some summary statistics about our generated data
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  5.552   8.861   9.934  10.045  11.251  14.883 

Quick Confidence Interval Review

Now, get a quick confidence interval

# t-test that the mean of the data = 0
# we can add mu = 10 to change the mean we want to test for
t_test_result <- t.test(x = norm_random) 

# in this case we don't really care about the hypothesis 
# we just want a confidence interval for the mean
t_test_result$conf.int 
[1]  9.695063 10.395611
attr(,"conf.level")
[1] 0.95

Adjust the confidence level

We can lower our confidence level, which leads to a narrower interval:

# we adjust the confidence level with the conf.level argument
# if we lower the confidence level it leads to a narrower interval
t_test_result <- t.test(x = norm_random, conf.level = 0.9) 
t_test_result$conf.int
[1]  9.752228 10.338446
attr(,"conf.level")
[1] 0.9

Adjust the confidence level

To have a higher confidence level, we need a broader interval:

# if we increase the confidence level we widen the interval
t_test_result <- 
  t.test(x = norm_random, conf.level = 0.99)
t_test_result$conf.int
[1]  9.581697 10.508976
attr(,"conf.level")
[1] 0.99

Hypothesis Testing

We mentioned hypothesis testing earlier, now let’s try and perform a hypothesis test in R.

summary(norm_random)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  5.552   8.861   9.934  10.045  11.251  14.883 

\(H_0:\mu=10\) vs. \(H_A:\mu\neq10\)

t.test(x = norm_random, mu = 10)$p.value
[1] 0.7978487

Hypothesis Testing (One-sided)

\(H_0:\mu=10\) vs. \(H_A:\mu>10\)

t.test(x = norm_random, mu = 10, 
       alternative = "greater")$p.value
[1] 0.3989244

\(H_0:\mu=9\) vs. \(H_A:\mu>9\)

t.test(x = norm_random, mu = 9, 
       alternative = "greater")$p.value
[1] 2.310052e-08

Back to the Alzheimer Data

Let’s load the packages and data

library(tidyverse)
alzheimer_data <- read.csv("../../data/alzheimer_data.csv") 
# you probably have this and can name it whatever you like! 

Practice 1: Confidence Interval

Try to get a 90 percent confidence interval for the population mean of age among AD subjects.

summary(alzheimer_data$age)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  21.00   64.00   72.00   70.05   78.00  100.00 

Practice 1: Confidence Interval

Try to get a 90 percent confidence interval for the population mean of age among AD subjects.

t_test_age <- t.test(alzheimer_data$age, conf.level = 0.9)
t_test_age$conf.int
[1] 69.68402 70.41524
attr(,"conf.level")
[1] 0.9

Practice 2: One Sample t-test

Test whether the population mean of age is 70 or greater than 70 and get the p-value.

\(H_0:\mu=70\) vs. \(H_A:\mu>70\)

summary(alzheimer_data$age)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  21.00   64.00   72.00   70.05   78.00  100.00 

Practice 2: One Sample t-test

Test whether the population mean of age is 70 or greater than 70 and get the p-value.

\(H_0:\mu=70\) vs. \(H_A:\mu>70\)

t_test_age <- t.test(alzheimer_data$age, mu = 70, alternative = "greater")
round(t_test_age$p.value, 4)
[1] 0.4116

Confidence Interval for Population Proportion

Let’s simulate some more data. This time, we flip a rigged coin 100 times.

set.seed(0)
bern_size <- 100
bern_random <- rbinom(bern_size, 1, 0.3)
head(bern_random, 10)
 [1] 1 0 0 0 1 0 1 1 0 0
success_counts <- sum(bern_random)
success_counts
[1] 33

Confidence Interval for Population Proportion

Now, let’s confidence interval for the chance of landing a heads.

prop_test_result <-
  prop.test(x = success_counts, 
            n = bern_size)
prop_test_result$conf.int
[1] 0.2411558 0.4320901
attr(,"conf.level")
[1] 0.95

Hypothesis Testing for Population Proportion

And we can formulate a hypothesis test for this proportion.

\(H_0:p=0.3\) vs. \(H_A:p\neq0.3\)

prop_test_result <-
  prop.test(x = success_counts, 
            n = bern_size, 
            p = 0.3)
prop_test_result$p.value
[1] 0.5853789

Real Data Practice (brain volume: naccicv)

Now, lets try this on the alzheimer data

success_counts_naccicv <- 
  alzheimer_data %>%
  filter(naccicv > 1300 & naccicv < 1600) %>%  # we want brain volume between 1300 and 1600ml, as a indicator of a healthy human brain size
  nrow()
bern_size_naccicv <- nrow(alzheimer_data)
success_counts_naccicv
[1] 1811
bern_size_naccicv
[1] 2700

Practice

  • Test whether the population proportion of people with normal brain volume is 2/3.

  • Get a 95 percent confidence interval for the population proportion.

prop_test_result_naccicv <-
  prop.test(x = success_counts_naccicv, 
            n = bern_size_naccicv, 
            p = 2/3, 
            conf.level = 0.95)
prop_test_result_naccicv$conf.int
[1] 0.6525956 0.6883957
attr(,"conf.level")
[1] 0.95
prop_test_result_naccicv$p.value
[1] 0.6681702

Comparing Two Samples

Is blood pressure associated with gender?

Two Sample t-test

We can examine whether the average blood pressure is different between male and female? Note that the boxplots above show the medians not the means.

\(H_0:\mu_M=\mu_F\) vs. \(H_A:\mu_M \neq \mu_F\)

t.test(bpsys ~ female, data = alzheimer_data)$p.value
[1] 0.5100128

Two Numerical Variables

Question: Are age and blood pressure correlated?

Correlation Test

\(H_0:\) They are NOT correlated. vs. \(H_A:\) They are correlated.

# here, we are testing for a r = 0
cor.test(alzheimer_data$age, alzheimer_data$bpsys)$p.value
[1] 1.020284e-46

\(H_0:\) They are NOT correlated. vs. \(H_A:\) They are positively correlated.

# here, we do the same but we only test for a positive association using a one-sided test
cor.test(alzheimer_data$age, alzheimer_data$bpsys, alternative = "greater")$p.value
[1] 5.101418e-47

Two Categorical Variables

Question: Are gender and disease status associated with each other?

contingency_table <- table(alzheimer_data$female, alzheimer_data$diagnosis)
contingency_table
   
       0    1    2
  0  529  327  295
  1 1005  286  258

Pearson’s \(\chi^2\) Test of Independence

\(H_0:\) They are independent vs. \(H_A:\) They are NOT independent.

# this is similar to cor.test, but for categorical variables instead of numeric ones
chisq.test(contingency_table)

    Pearson's Chi-squared test

data:  contingency_table
X-squared = 96.346, df = 2, p-value < 2.2e-16

Summary

  • “t.test()” for one/two-sample t-test
  • “prop.test()” for proportion
  • “cor.test()” for correlation test
  • “chisq.test()” for \(\chi^2\) test
  • Useful arguments: “mu”, “conf.level”, “alternative”
  • ?t.test, ?prop.test, ?cor.test and ?chisq.test for more information

More hypothesis testing

Let’s do a little data transformation first

alzheimer_data <- read.csv('../data/alzheimer_data.csv') %>% 
  dplyr::select(id, diagnosis, age, educ, female, height, weight, lhippo, rhippo) %>% 
  mutate(diagnosis = as.factor(diagnosis), 
         female = as.factor(female),
         hippo=lhippo + rhippo)


alzheimer_healthy <- read.csv('../data/alzheimer_data.csv') %>% 
  dplyr::select(id, diagnosis, age, educ, female, height, weight, lhippo, rhippo) %>% 
  mutate(diagnosis = as.factor(diagnosis),
         female = as.factor(female), 
         hippo=lhippo+rhippo) %>% 
  filter(diagnosis==0)

EXAMPLE: Two-Sample t-test

Ex. Do healthy men and women have the same mean left hippocampal volume?

  • Categorical (gender) and Numerical (Left Hippocampus) :

    • Side by Side Boxplot

    • Side by Side Violin

    • 2 Histograms

Example: Visualization – boxplot

Code
ggplot(data= alzheimer_healthy,
       mapping= aes(x= female, 
                    y=lhippo,
                    fill=female)) +
  geom_boxplot()+
  labs(
    title="Boxplot of lhippo by Gender",
    x="Gender",
    y="LHippo volume (cm^3)") +  
  theme_minimal()

Example: Visualization – violin

Code
ggplot(data= alzheimer_healthy,
       mapping= aes(x= female, 
                    y=lhippo,
                    fill=female)) +
  geom_violin()+
  labs(
    title="Violin of lhippo by Gender",
    x="Gender",
    y="LHippo volume (cm^3)") +  
  theme_minimal()

Example: Visualization – histogram

Code
ggplot(data= alzheimer_healthy,
       mapping= aes(x=lhippo,
                    fill=female)) +
  geom_histogram(bins=35)+
  labs(
    title="Histogram of Lhippo by Gender",
    x="LHippo volume (cm^3)",
    y="Count") 

Example: use two-sample t-test

Code
t.test(alzheimer_healthy$lhippo[alzheimer_healthy$female==0], alzheimer_healthy$lhippo[alzheimer_healthy$female==1])

    Welch Two Sample t-test

data:  alzheimer_healthy$lhippo[alzheimer_healthy$female == 0] and alzheimer_healthy$lhippo[alzheimer_healthy$female == 1]
t = 11.756, df = 971.28, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 0.2088777 0.2925882
sample estimates:
mean of x mean of y 
 3.345749  3.095016 

    Welch Two Sample t-test

data:  alzheimer_healthy$lhippo by alzheimer_healthy$female
t = 11.756, df = 971.28, p-value < 2.2e-16
alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
95 percent confidence interval:
 0.2088777 0.2925882
sample estimates:
mean in group 0 mean in group 1 
       3.345749        3.095016 

Example 2

  • Research Question: Is the left hippocampal volume equal to the right hippocampal volume in humans?

  • Null Hypothesis: Human’s left hippocampus volume is the same as the right hippocampus volume.

  • Alternative Hypothesis: Human’s left hippocampus volume is not the same as the right hippocampus volume.

\[H_0: \mu_L = \mu_R \mbox{ vs } \mu_L \not= \mu_R\]

  • Method: paired t-test

Example 2

  • We essentially want to perform a two-sample t-test
  • However, one fundamental assumption in the two-sample problem is that the two samples should be independent, such as the female group vs the male group.
  • Here we are looking at two features (left vs right hippocampus) of the same subjects. The correct test is a paired t-test, which is equivalent to perform a one-sample t-test using the differences

Example 2

Code
t.test(alzheimer_data$rhippo, alzheimer_data$lhippo, paired = T)

    Paired t-test

data:  alzheimer_data$rhippo and alzheimer_data$lhippo
t = 17, df = 2699, p-value < 2.2e-16
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
 0.07008463 0.08836085
sample estimates:
mean difference 
     0.07922274 

    One Sample t-test

data:  alzheimer_data$rhippo - alzheimer_data$lhippo
t = 17, df = 2699, p-value < 2.2e-16
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 0.07008463 0.08836085
sample estimates:
 mean of x 
0.07922274 

Example 3

  • Research Question: Is having a large hippocampus (> 7 cm\(^3\)) associated with gender?

  • This is a category variable vs category variable problem

  • Method: chi-squared test
Code
table(alzheimer_data$hippo>7,
      alzheimer_data$female)
       
           0    1
  FALSE  843 1374
  TRUE   308  175
Code
chisq.test(table(alzheimer_data$rhippo>7,
      alzheimer_data$female))

    Chi-squared test for given probabilities

data:  table(alzheimer_data$rhippo > 7, alzheimer_data$female)
X-squared = 58.668, df = 1, p-value = 1.866e-14

Example 4

  • Research Question: hippocampal volume correlated with age in healthy adults?

  • Null Hypothesis: hippocampal volume and age are not correlated.

  • Alternative Hypothesis: hippocampal volume and age are not correlated.

\[H_0: \rho=0 \mbox{ vs } H_1: \rho\not=0\] . . .

  • Method: correlation test, more general, linear regression.

Example 4: Visualization

Code
#plot(alzheimer_healthy$age, alzheimer_healthy$hippo)
ggplot(data= alzheimer_healthy,
       mapping= aes(x= age, 
                    y= hippo)) +
  geom_point() +
  labs(
    title="Scatter plot of hippocampal volume vs age",
    x="Age (years)",
    y="Hippocampal volume (cm^3)") +  
  theme_minimal()

Discussion: is there a linear trend? are there regions of concerns?

Example 4

Code
cor.test(alzheimer_healthy$age, alzheimer_healthy$hippo)

    Pearson's product-moment correlation

data:  alzheimer_healthy$age and alzheimer_healthy$hippo
t = -15.125, df = 1532, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.4032192 -0.3160971
sample estimates:
      cor 
-0.360444 
Code
#alternatively, we can use linear regression
summary(lm(alzheimer_healthy$hippo ~ alzheimer_healthy$age))

Call:
lm(formula = alzheimer_healthy$hippo ~ alzheimer_healthy$age)

Residuals:
     Min       1Q   Median       3Q      Max 
-2.57311 -0.48368 -0.01818  0.49369  2.80738 

Coefficients:
                      Estimate Std. Error t value Pr(>|t|)    
(Intercept)            8.00629    0.10570   75.75   <2e-16 ***
alzheimer_healthy$age -0.02329    0.00154  -15.12   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.7209 on 1532 degrees of freedom
Multiple R-squared:  0.1299,    Adjusted R-squared:  0.1294 
F-statistic: 228.8 on 1 and 1532 DF,  p-value: < 2.2e-16

Example 5

  • Research Question: Does left hippocampal volume differ across diagnostic groups?

  • Null Hypothesis:

  • Alternative Hypothesis:

\[H_0: \mu_0=\mu_1=\mu_2 \mbox{ vs } H_1: \mbox{ at least two means are different}\]

  • Method: one-way analysis of variance (ANOVA), more generally, linear regression

Example 5

Code
ggplot(data= alzheimer_data,
       mapping= aes(x= diagnosis, 
                    y=lhippo,
                    fill=diagnosis)) +
  geom_boxplot()+
  labs(
    title="Boxplot of lhippo by Diagnosis",
    x="Diagnosis",
    y="LHippo volume (cm^3)") +  
  theme_minimal()

Code
summary(aov(alzheimer_data$lhippo ~ alzheimer_data$diagnosis))
                           Df Sum Sq Mean Sq F value Pr(>F)    
alzheimer_data$diagnosis    2  100.5   50.27   247.1 <2e-16 ***
Residuals                2697  548.6    0.20                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Code
#equivalently
summary(aov(lhippo ~ diagnosis, data=alzheimer_data))
              Df Sum Sq Mean Sq F value Pr(>F)    
diagnosis      2  100.5   50.27   247.1 <2e-16 ***
Residuals   2697  548.6    0.20                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Summary

  • Ex 1. Is the hippocampal volume of healthy males greater than 6 cm\(^3\)? (one-sample t-test)

  • Ex 2. Is the proportion of healthy adults with a right hippocampal volume > 3cm\(^3\) equal to 50%? (one-sample proportion test or z-test)

  • Ex 3. Do healthy men and women have the same mean left hippocampal volume? (two-sample t-test)

  • Ex 4. Is the left hippocampal volume equal to the right hippocampal volume in humans? (paired t-test)

  • Ex 5. Is having a large hippocampus (> 7 cm\(^3\)) associated with gender? (chi-squared test)

  • Ex 6. Is hippocampal volume correlated with age in healthy adults? (Pearson’s correlation or linear regression)

  • Ex 7. Does left hippocampal volume differ across diagnostic groups? (one-way ANOVA or linear regression)