CP-LEADS t-Test Example

Example for Conducting t-Test from Coladarci and Cobb’s Chapter 14

Load Libraries

Remember you first have to install these libraries, but you only need to install them once. I am sure you already have tidyverse installed.

For skimr, car, and report copy the code below then paste it and run in on the console pane.

install.packages(“skimr”)

install.packages(“car”)

install.packages(“report”)

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(skimr) # for quick descriptives
library(car) # for testing equal variances assumption
Loading required package: carData

Attaching package: 'car'

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

    recode

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

    some
library(report) # for reporting results in APA

Create the dataframe from Chapter 14

df <- data.frame(
  group = rep(c("Scent", "No Scent"), each = 9),
  score = c(25, 23, 30, 14, 22, 28, 18, 21, 26,  # Scent scores
            20, 10, 25, 13, 21, 15, 19, 22, 17)  # No Scent scores
)

Get descriptives using the skimr package

# Overall
skim(df$score)
Data summary
Name df$score
Number of rows 18
Number of columns 1
_______________________
Column type frequency:
numeric 1
________________________
Group variables None

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
data 0 1 20.5 5.36 10 17.25 21 24.5 30 ▃▃▇▅▂
# By Group
df |> 
  group_by(group) |> 
  skim(score)
Data summary
Name group_by(df, group)
Number of rows 18
Number of columns 2
_______________________
Column type frequency:
numeric 1
________________________
Group variables group

Variable type: numeric

skim_variable group n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
score No Scent 0 1 18 4.72 10 15 19 21 25 ▅▂▅▇▂
score Scent 0 1 23 4.97 14 21 23 26 30 ▂▂▇▅▅

Test for equal variances assumption

Levene’s test

If it is not significant, then variances are equal between both groups. Stated another way, the equal variances assumption has been met.

leveneTest(score ~ group, data = df)
Warning in leveneTest.default(y = y, group = group, ...): group coerced to
factor.
Levene's Test for Homogeneity of Variance (center = median)
      Df F value Pr(>F)
group  1  0.0066 0.9365
      16               

Perform the t-Test

formula notation: score ~ group

If Levene’s test was non-significant, then var.equal = TRUE. If Levene’s test was significant, then var.equal = FALSE.

For one-sided tests, if we are predicting Scent will have greater scores, then use alternative = "greater". But the book example uses a two-sided test, so we don’t need to put any additional arguments in the t.test function.

# Equal variances set to TRUE
results <- t.test(score ~ group, data = df, var.equal = TRUE)

# Print results
print(results)

    Two Sample t-test

data:  score by group
t = -2.188, df = 16, p-value = 0.04386
alternative hypothesis: true difference in means between group No Scent and group Scent is not equal to 0
95 percent confidence interval:
 -9.8444462 -0.1555538
sample estimates:
mean in group No Scent    mean in group Scent 
                    18                     23 

Use the report package as a helper for reporting results

# Table report of the results
report_table(results)
In the report() function, for htest objects, you can try providing the
  data argument manually, e.g., report(x, data = data).
Two Sample t-test

Parameter | Group | Mean_Group1 | Mean_Group2 | Difference |         95% CI
---------------------------------------------------------------------------
score     | group |          18 |          23 |         -5 | [-9.84, -0.16]

Parameter | t(16) |     p |     d |          d  CI
--------------------------------------------------
score     | -2.19 | 0.044 | -1.09 | [-2.13, -0.03]

Alternative hypothesis: two.sided
# Descriptive report of the results
report(results)
In the report() function, for htest objects, you can try providing the
  data argument manually, e.g., report(x, data = data).
Effect sizes were labelled following Cohen's (1988) recommendations.

The Two Sample t-test testing the difference of score by group (mean in group
No Scent = 18.00, mean in group Scent = 23.00) suggests that the effect is
negative, statistically significant, and large (difference = -5.00, 95% CI
[-9.84, -0.16], t(16) = -2.19, p = 0.044; Cohen's d = -1.09, 95% CI [-2.13,
-0.03])