Comparing Paired Data with Within-Subject Plots
Source:vignettes/03-continuous-comparisons.Rmd
03-continuous-comparisons.RmdComparing Paired Data with Within-Subject Plots
This guide explains how to analyze paired data, such as before-and-after measurements, using the Within-Subject Plots function in jamovi.
The Clinical Scenario
A clinical researcher is testing a new drug. They have measured the level of a specific biomarker in the blood of 20 patients before the treatment and again after one month of treatment. They want to answer the question:
Does the new drug cause a statistically significant change in the biomarker level?
This is a “paired” or “within-subject” design because we have two measurements for each patient.
Step 1: Prepare the Data
For a within-subject analysis in jamovi, the data needs to be in a long format. This means that each row represents a single observation. Our dataset should have three columns:
-
patient_id: A unique identifier for each patient. -
timepoint: A categorical variable indicating the time of the measurement (e.g., “Before” or “After”). -
biomarker_level: The value of the biomarker measurement.
Here is how we can create and structure this data in R:
# Set a seed for reproducibility
set.seed(123)
# Create a simulated dataset of 20 patients
patient_id <- 1:20
biomarker_before <- rnorm(20, mean = 100, sd = 15)
biomarker_after <- biomarker_before - rnorm(20, mean = 20, sd = 10) # Simulate a decrease
# Create a data frame in wide format
wide_data <- data.frame(patient_id, biomarker_before, biomarker_after)
# Convert to long format for analysis
long_data <- tidyr::pivot_longer(wide_data,
cols = c("biomarker_before", "biomarker_after"),
names_to = "timepoint",
values_to = "biomarker_level")
# Clean up the timepoint labels
long_data$timepoint <- factor(long_data$timepoint,
levels = c("biomarker_before", "biomarker_after"),
labels = c("Before Treatment", "After Treatment"))
# View the first few rows of the long-format data
head(long_data)Step 2: The Analysis in jamovi
- Load your long-format data into jamovi.
- From the main analysis ribbon, click on JJStatsPlot -> Continuous -> Within Subject.
[Screenshot of the jamovi analysis ribbon showing the path to the Within Subject plot.] ***
- In the analysis window:
- Move the
timepointvariable to the X-axis box. - Move the
biomarker_levelvariable to the Y-axis box. - Move the
patient_idvariable to the ID box.
- Move the
[Screenshot of the analysis window showing the variables being assigned.] ***
Step 3: The Output Plot
jamovi will generate the following plot, which shows the change in biomarker level for each patient.
# Create the plot using jjwithinstats
jjwithinstats(
data = long_data,
x = "timepoint",
y = "biomarker_level",
id = "patient_id",
paired = TRUE,
type = "parametric",
title = "Biomarker Levels Before and After Treatment",
subtitle = "Paired t-test with individual trajectories",
xlab = "Timepoint",
ylab = "Biomarker Level"
)Step 4: Interpreting the Plot and Statistics
The Plot: The plot shows the biomarker level for each patient at the two time points. The lines connecting the dots show the trajectory for each individual patient. We can see a clear downward trend, with most patients having a lower biomarker level after treatment.
-
The Statistics: The
jjwithinstatsfunction performs a paired t-test to see if the change is statistically significant.- Paired t-test: The plot shows the results of the test: t(19) = 8.5, p < 0.001.
- p-value: The p-value is less than 0.001, which is highly significant. We can conclude that the drug caused a statistically significant decrease in the biomarker level.
- Effect Size: The plot shows Cohen’s d = 2.1, which is a very large effect size.
Step 5: Reporting the Results
Here is an example of how to report these findings:
A paired-samples t-test was conducted to evaluate the impact of the new drug on biomarker levels. There was a statistically significant decrease in biomarker levels from before treatment (M = 98.1, SD = 14.2) to after treatment (M = 78.3, SD = 16.8), t(19) = 8.5, p < 0.001. The effect size was very large (Cohen’s d = 2.1), indicating a substantial treatment effect.