Introduction

This analysis explores the JohnsonJohnson dataset, which contains quarterly earnings per share from 1960 to 1980. We’ll examine the data using summary statistics with gtsummary.

Loading Packages and Data

# Load required packages
library(gtsummary)
library(dplyr)

# Load dataset
data("JohnsonJohnson")

# Convert time series to data frame
jj_df <- data.frame(
  Year = rep(1960:1980, each = 1),
  Qtr1 = JohnsonJohnson[seq(1, length(JohnsonJohnson), 4)],
  Qtr2 = JohnsonJohnson[seq(2, length(JohnsonJohnson), 4)],
  Qtr3 = JohnsonJohnson[seq(3, length(JohnsonJohnson), 4)],
  Qtr4 = JohnsonJohnson[seq(4, length(JohnsonJohnson), 4)]
)

# Create year groups
jj_df$Year_group <- cut(jj_df$Year, breaks = 3, labels = c("Early Period", "Mid Period", "Late Period"))

# Display first few rows
head(jj_df)
##   Year Qtr1 Qtr2 Qtr3 Qtr4   Year_group
## 1 1960 0.71 0.63 0.85 0.44 Early Period
## 2 1961 0.61 0.69 0.92 0.55 Early Period
## 3 1962 0.72 0.77 0.92 0.60 Early Period
## 4 1963 0.83 0.80 1.00 0.77 Early Period
## 5 1964 0.92 1.00 1.24 1.00 Early Period
## 6 1965 1.16 1.30 1.45 1.25 Early Period

Summary Statistics Without Grouping

jj_df %>% select(Qtr1, Qtr2, Qtr3, Qtr4) %>% tbl_summary()
Characteristic N = 211
Qtr1 2.8 (1.2, 6.9)
Qtr2 3.4 (1.3, 7.7)
Qtr3 3.7 (1.5, 7.8)
Qtr4 3.60 (1.25, 6.12)
1 Median (Q1, Q3)

Summary Statistics by Year Group

jj_df %>% select(Qtr1, Qtr2, Qtr3, Qtr4,Year_group) %>% tbl_summary(by=Year_group)
Characteristic Early Period
N = 7
1
Mid Period
N = 7
1
Late Period
N = 7
1
Qtr1 0.8 (0.7, 1.2) 2.8 (1.5, 4.9) 9.5 (6.9, 14.0)
Qtr2 0.8 (0.7, 1.3) 3.4 (2.1, 5.0) 10.3 (7.7, 13.0)
Qtr3 1.0 (0.9, 1.5) 3.7 (2.3, 5.0) 9.5 (7.8, 14.9)
Qtr4 0.77 (0.55, 1.25) 3.60 (2.25, 4.41) 8.73 (6.12, 9.99)
1 Median (Q1, Q3)

Summary with P-values

jj_df %>% select(Qtr1, Qtr2, Qtr3, Qtr4,Year_group) %>% tbl_summary(by=Year_group) %>% add_p()
Characteristic Early Period
N = 7
1
Mid Period
N = 7
1
Late Period
N = 7
1
p-value2
Qtr1 0.8 (0.7, 1.2) 2.8 (1.5, 4.9) 9.5 (6.9, 14.0) <0.001
Qtr2 0.8 (0.7, 1.3) 3.4 (2.1, 5.0) 10.3 (7.7, 13.0) <0.001
Qtr3 1.0 (0.9, 1.5) 3.7 (2.3, 5.0) 9.5 (7.8, 14.9) <0.001
Qtr4 0.77 (0.55, 1.25) 3.60 (2.25, 4.41) 8.73 (6.12, 9.99) <0.001
1 Median (Q1, Q3)
2 Kruskal-Wallis rank sum test