Skip to content

Commit

Permalink
New PAP.adh data, renamed old one to PAP.adh1y (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
niekdt committed Mar 10, 2023
1 parent 6a4c83f commit e199c21
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 72 deletions.
51 changes: 40 additions & 11 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,44 @@ NULL


#' @name PAP.adh
#' @title Biweekly Mean Therapy Adherence of OSA Patients over 1 Year
#' @title Weekly Mean PAP Therapy Usage of OSA Patients in the First 3 Months
#' @description
#' A simulated longitudinal dataset comprising 500 patients with obstructive sleep apnea (OSA) during their
#' first year on CPAP therapy.
#' A simulated longitudinal dataset comprising 301 patients with obstructive sleep apnea (OSA) during their first 91 days (13 weeks) of PAP therapy.
#' The longitudinal patterns were inspired by the adherence patterns reported by Yi et al. (2022), interpolated to weekly hours of usage.
#' @format A `data.frame` comprising longitudinal data of 500 patients, each having 26 observations over a period of 1 year.
#' Each row represents a patient observation interval (two weeks), with columns:
#' \describe{
#' \item{Patient}{`integer`: The patient identifier, where each level represents a simulated patient.}
#' \item{Week}{`integer`: The week number, starting from 1.}
#' \item{UsageHours}{`numeric`: The mean hours of usage in the respective week.
#' Greater than or equal to zero, and typically around 4-6 hours.}
#' \item{Group}{`factor`: The reference group (i.e., adherence pattern) from which this patient was generated.}
#' }
#'
#' \insertRef{yi2022identifying}{latrend}
#' @seealso [latrend-data] [PAP.adh1y]
#' @examples
#' data(PAP.adh)
#'
#' if (require("ggplot2")) {
#' plotTrajectories(PAP.adh, id = "Patient", time = "Week", response = "UsageHours")
#'
#' # plot according to cluster ground truth
#' plotTrajectories(
#' PAP.adh,
#' id = "Patient",
#' time = "Week",
#' response = "UsageHours",
#' cluster = "Group"
#' )
#' }
"PAP.adh"


#' @name PAP.adh1y
#' @title Biweekly Mean PAP Therapy Adherence of OSA Patients over 1 Year
#' @description
#' A simulated longitudinal dataset comprising 500 patients with obstructive sleep apnea (OSA) during their first year on CPAP therapy.
#' The dataset contains the patient usage hours, averaged over 2-week periods.
#'
#' The daily usage data underlying the downsampled dataset was simulated based on 7 different adherence patterns.
Expand All @@ -77,25 +111,20 @@ NULL
#' \insertRef{aloia2008time}{latrend}
#' @seealso [latrend-data]
#' @examples
#' data(PAP.adh)
#' data(PAP.adh1y)
#'
#' if (require("ggplot2")) {
#' plotTrajectories(PAP.adh, id = "Patient", time = "Biweek", response = "UsageHours")
#' plotTrajectories(PAP.adh1y, id = "Patient", time = "Biweek", response = "UsageHours")
#'
#' # plot according to cluster ground truth
#' plotTrajectories(
#' PAP.adh,
#' PAP.adh1y,
#' id = "Patient",
#' time = "Biweek",
#' response = "UsageHours",
#' cluster = "Group"
#' )
#' }
"PAP.adh"

#' @rdname PAP.adh
#' @description The `PAP.adh1y` dataset is a subset of `PAP.adh`, comprising only patients who used therapy for at least 1 year.
#' The subset does not contain the Non-users and Early drop-out groups.
"PAP.adh1y"

#' @export
Expand Down
116 changes: 116 additions & 0 deletions data-raw/PAP.adh.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
library(magrittr)
library(assertthat)
library(data.table)
library(latrend)

# Cluster 1: "adherers"
# y = 385, 326, 281
# Cluster 2: "improvers"
# y = 256, 297, 365
# Cluster 3: "non-adherers"
# y = 175, 137, 107

# convert daily usage-hour variance to weekly usage minutes SD
matrix(rnorm(1e6 * 7L, sd = 1.5 * 60), ncol = 1e6L) %>% colMeans() %>% sd()

# define group trajectories
groupTrajMat = cbind(
Adherers = approx(x = c(1, 4, 13), y = c(385, 328, 281), xout = 1:13, method = 'linear')$y,
Improvers = approx(x = c(1, 4, 13), y = c(256, 293, 365), xout = 1:13, method = 'linear')$y,
`Non-adherers` = approx(x = c(1, 4, 13), y = c(176, 138, 108), xout = 1:13, method = 'linear')$y
)

cluster1Data = generateLongData(
sizes = 162L,
id = 'Patient',
cluster = ~ Week + Adherers,
random = ~ 1,
data = data.frame(Week = 1:13, Adherers = groupTrajMat[, 'Adherers']),
clusterCoefs = cbind(c(0, 0, 1)),
clusterNames = 'Adherer',
randomScales = cbind(62.7),
noiseScales = 23,
seed = 1L
)

plotTrajectories(cluster1Data, time = 'Week', id = 'Patient', cluster = 'Class', response = 'Value') +
expand_limits(y = 0) +
scale_x_continuous(breaks = seq(1, 13, by = 2))

cluster2Data = generateLongData(
sizes = 56L,
id = 'Patient',
cluster = ~ Week + Adherers,
random = ~ 1,
data = data.frame(Week = 1:13, Adherers = groupTrajMat[, 'Improvers']),
clusterCoefs = cbind(c(0, 0, 1)),
clusterNames = 'Improver',
randomScales = cbind(68.9),
noiseScales = 43,
seed = 2L
) %>%
.[, Patient := Patient + max(cluster1Data$Patient)]

cluster3Data = generateLongData(
sizes = 83L,
id = 'Patient',
cluster = ~ Week + Adherers,
random = ~ 1,
data = data.frame(Week = 1:13, Adherers = groupTrajMat[, 'Non-adherers']),
clusterCoefs = cbind(c(0, 0, 1)),
clusterNames = 'Non-adherer',
randomScales = cbind(53.7),
noiseScales = 34,
seed = 3L
) %>%
.[, Patient := Patient + max(cluster2Data$Patient)]

dataset = rbind(cluster1Data, cluster2Data, cluster3Data) %>%
.[, UsageHours := fifelse(Value > 0, Value / 60, 0)] %>%
setnames('Class', 'Group')

plotTrajectories(dataset, time = 'Week', id = 'Patient', cluster = 'Group', response = 'UsageHours') +
expand_limits(y = 0) +
scale_x_continuous(breaks = seq(1, 13, by = 2))

PAP.adh = subset(dataset, select = c('Patient', 'Week', 'UsageHours', 'Group')) %>%
as.data.frame()
head(PAP.adh)
stopifnot(uniqueN(PAP.adh$Patient) == 301)

usethis::use_data(PAP.adh, overwrite = TRUE)


# parallel
nCores <- parallel::detectCores(logical = FALSE)
if (.Platform$OS.type == "windows") {
cl <- parallel::makeCluster(nCores)
doParallel::registerDoParallel(cl)
} else {
doMC::registerDoMC(nCores)
}

# test gbtm
options(latrend.id = "Patient", latrend.time = "Week")
gbtmMethod <- lcMethodLcmmGBTM(
fixed = UsageHours ~ Week,
mixture = ~ Week,
maxiter = 20, #not recommended; used for speed
idiag = TRUE
)
gbtmMethods <- lcMethods(gbtmMethod, nClusters = 1:5)
gbtmList <- latrendBatch(gbtmMethods, data = PAP.adh, parallel = TRUE)

plotMetric(gbtmList, c("Dunn", "WMAE", "BIC", "estimationTime"))

# test gmm
gmmMethod <- lcMethodLcmmGMM(
fixed = UsageHours ~ Week,
mixture = ~ Week,
idiag = TRUE
)
gmmMethods <- lcMethods(gmmMethod, nClusters = 1:5)
system.time({
gmmList <- latrendBatch(gmmMethods, data = PAP.adh, parallel = TRUE)
})
plotMetric(gmmList, c("Dunn", "WMAE", "BIC", "estimationTime"))
10 changes: 1 addition & 9 deletions data-raw/osa-adherence-data.R → data-raw/PAP.adh1y.R
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,5 @@ plotTrajectories(dataset, id = 'Patient', time = 'Biweek', response = 'UsageHour
dataset[, Biweek := as.integer(Biweek)]
dataset[, MaxDay := as.integer(MaxDay)]

PAP.adh = as.data.frame(dataset)
usethis::use_data(PAP.adh, overwrite = TRUE)

# PAP.adh1y
PAP.adh1y = as.data.frame(
dataset[not(Group %in% c('Early drop-outs', 'Non-users'))]
)
PAP.adh1y$Patient = factor(PAP.adh1y$Patient)
PAP.adh1y$Group = factor(PAP.adh1y$Group)
PAP.adh1y = as.data.frame(dataset)
usethis::use_data(PAP.adh1y, overwrite = TRUE)
Binary file modified data/PAP.adh.rda
Binary file not shown.
Binary file modified data/PAP.adh1y.rda
Binary file not shown.
12 changes: 12 additions & 0 deletions inst/REFERENCES.bib
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,16 @@ @Article{denteuling2021comparison
timestamp = {2021-03-25},
}

@Article{yi2022identifying,
author = {Yi, Huijie and Dong, Xiaosong and Shang, Shaomei and Zhang, Chi and Xu, Liyue and Han, Fang},
journal = {Frontiers in Neurology},
title = {Identifying longitudinal patterns of {CPAP} treatment in {OSA} using growth mixture modeling: Disease characteristics and psychological determinants},
year = {2022},
month = nov,
pages = {1063461},
volume = {13},
doi = {10.3389/fneur.2022.1063461},
modificationdate = {2023-03-10T11:31:38},
}

@Comment{jabref-meta: databaseType:bibtex;}
39 changes: 9 additions & 30 deletions man/PAP.adh.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions man/PAP.adh1y.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/plot-lcModel-method.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/plot-lcModels-method.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e199c21

Please sign in to comment.