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:
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.
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
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})\),
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 *.39cor.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
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,
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.
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 samenorm_size <-100norm_random <-rnorm(n = norm_size, mean =10, sd =2) # simulate 100 draws from a N(10, 2) distributionsummary(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 fort_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 meant_test_result$conf.int
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 intervalt_test_result <-t.test(x = norm_random, conf.level =0.9) t_test_result$conf.int
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 sizenrow()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
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 = 0cor.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 testcor.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?
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")
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
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