# Creating the data frame
df <- data.frame(
ID = c("046", "149", "096", "064", "050", "210", "082", "121"),
Groupa = c("P", "A", "A", "P", "A", "A", "P", "P"),
Baseline = c(30.8, 26.5, 25.8, 24.7, 20.4, 20.4, 28.6, 33.7),
Week_1 = c(26.9, 14.8, 23.0, 24.5, 2.8, 5.4, 20.8, 31.6),
Week_4 = c(25.8, 19.5, 19.1, 22.0, 3.2, 4.5, 19.2, 28.5),
Week_6 = c(23.8, 21.0, 23.2, 22.5, 9.4, 11.9, 18.4, 25.1)
)
# Displaying the data frame
df
## ID Groupa Baseline Week_1 Week_4 Week_6
## 1 046 P 30.8 26.9 25.8 23.8
## 2 149 A 26.5 14.8 19.5 21.0
## 3 096 A 25.8 23.0 19.1 23.2
## 4 064 P 24.7 24.5 22.0 22.5
## 5 050 A 20.4 2.8 3.2 9.4
## 6 210 A 20.4 5.4 4.5 11.9
## 7 082 P 28.6 20.8 19.2 18.4
## 8 121 P 33.7 31.6 28.5 25.1
#Generate means,covariance and correlation
# Calculate column means based on Groupa
col_means <- aggregate(. ~ df[, 2], data = df[, 3:6], FUN = mean)
print("Column Means:")
## [1] "Column Means:"
print(col_means)
## df[, 2] Baseline Week_1 Week_4 Week_6
## 1 A 23.275 11.50 11.575 16.375
## 2 P 29.450 25.95 23.875 22.450
# Calculate the covariance matrix
cov_mat <- cov(df[, 3:6])
print("Covariance Matrix:")
## [1] "Covariance Matrix:"
print(cov_mat)
## Baseline Week_1 Week_4 Week_6
## Baseline 21.81982 42.46393 39.43107 22.34054
## Week_1 42.46393 105.01357 91.27643 56.33393
## Week_4 39.43107 91.27643 84.66786 51.60393
## Week_6 22.34054 56.33393 51.60393 33.67268
# Calculate the correlation matrix
cov_mat <- cov(df[, 3:6])
print("Correlaton Matrix:")
## [1] "Correlaton Matrix:"
cor_mat<-cov2cor(cov_mat)
print(cor_mat)
## Baseline Week_1 Week_4 Week_6
## Baseline 1.0000000 0.8870985 0.9173894 0.8241934
## Week_1 0.8870985 1.0000000 0.9680029 0.9473457
## Week_4 0.9173894 0.9680029 1.0000000 0.9664617
## Week_6 0.8241934 0.9473457 0.9664617 1.0000000