###################################3
# Read in the TLC Data
# Note: This is stored for me at data/TLC/TLC.csv, you should update for yourself
TLC <- read.csv("data/TLC/TLC.csv")
head(TLC) # Outputs the first few rows of the data to take a look
# Convert from "wide" to "long" and back again, using reshape.
# If you're interested, you can also use `pivot_wider` and `pivot_longer` from the tidyverse
# (If that doesn't mean anything to you, feel free to ignore it!)
TLC_long <- reshape(data = TLC,
varying = c("W0", "W1", "W4", "W6"),
timevar = "week",
idvar = "ID",
times = c(0, 1, 4, 6),
direction = "long",
sep = "")
TLC_wide <- reshape(data = TLC_long,
timevar = "week",
v.names = "W",
idvar = "ID",
times = c(0, 1, 4, 6),
direction = "wide",
sep = "")
# Create a Basic Boxplot to get a Sense of the Data
boxplot(W ~ week + Treatment, data = TLC_long)
abline(v=4.5) # Abline v=... draws a vertical line at 4.5
# Start with an xyplot
# This requires the package 'lattice'
# You can install using: install.packages("lattice")
lattice::xyplot(W ~ week | Treatment,
data = TLC_long,
groups = ID,
col = 'black',
type = c('l', 'p'))
# The plot is a mess, as-is, so instead we can subset!
plot_num <- 5 # Select a fixed number
# This is Just Randomly Sampling from Each Group
random_samples_P <- sample(unique(TLC_long$ID[which(TLC_long$Treatment == 'P')]),
size = plot_num,
replace = FALSE)
random_samples_A <- sample(unique(TLC_long$ID[which(TLC_long$Treatment == 'A')]),
size = plot_num,
replace = FALSE)
## Actually Draw the Plots
par(mfrow=c(1,2))
plot(W ~ week, data = TLC_long, subset = (Treatment == 'P'))
for (rid in random_samples_P){
# Loop through the Random Points and Draw the Corresponding Lines
lines(W ~ week,
data = TLC_long,
subset = (ID==rid),
type = 'l')
}
# Repeat it for Active Treatment
plot(W ~ week, data = TLC_long, subset = (Treatment == 'A'))
for (rid in random_samples_A){
lines(W ~ week,
data = TLC_long,
subset = (ID==rid),
type = 'l')
}
### Is there Smarter way of plotting?
# What if we ordered by the median observation?
TLC_wide$median <- apply(TLC_wide[c("W0", "W1", "W4", "W6")],
MARGIN=1,
FUN = median) # Generate the Medians
TLC_long <- reshape(data = TLC_wide,
varying = c("W0", "W1", "W4", "W6"),
timevar = "week",
idvar = "ID",
times = c(0, 1, 4, 6),
direction = "long",
sep = "") # Reshape to long again, with the Median
# Sort the Data By The Medians
sorted_medians_P <- sort(TLC_wide$median[which(TLC_wide$Treatment == 'P')])
sorted_medians_A <- sort(TLC_wide$median[which(TLC_wide$Treatment == 'A')])
plot(W ~ week, data = TLC_long, subset = (Treatment == 'P'))
for (row_num in floor(seq(1,50,by=12.25))){
# Here we are looping over a sequence of (1,50) by 12.5
# which selects out every 12.5-th individual from the dataset
# There are 50 in each group so this is essentially grabing the quantiles
rid <- TLC_wide$ID[which(TLC_wide$median == sorted_medians_P[row_num])][1]
lines(W ~ week,
data = TLC_long,
subset = (ID==rid),
type = 'l')
}
plot(W ~ week, data = TLC_long, subset = (Treatment == 'A'))
for (row_num in floor(seq(1,50,by=12.25))){
rid <- TLC_wide$ID[which(TLC_wide$median == sorted_medians_A[row_num])][1]
lines(W ~ week,
data = TLC_long,
subset = (ID==rid),
type = 'l')
}
# This is a basic correlation plot
# It requires the 'corrplot' library, which can be installed with
# install.packages("corrplot")
corrplot::corrplot.mixed(cor(TLC_wide[c("W0","W1","W4","W6")]),
lower = 'number',
upper = 'square')