Skip to content

Commit

Permalink
vignette update)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidherman94 committed Jul 11, 2024
1 parent 688ba55 commit c862b7c
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 10 deletions.
190 changes: 190 additions & 0 deletions .Rhistory
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,193 @@ usethis::use_data(data_layout_3, overwrite = TRUE)
devtools::document()
devtools::document()
devtools::document()
devtools::document()
library(revents)
head(revents::data_layout_1)
# Load data layout corresponding to the third layout
data("data_layout_1", package = "revents")
data_layout_1 <- revents::data_layout_1
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
suppressWarnings({
suppressMessages({
library(revents)
library(survival)
library(broom)
library(dplyr)
library(frailtyEM)
})
})
# Load data layout corresponding to the third layout
data("data_layout_1", package = "revents")
data_layout_1 <- revents::data_layout_1
# set scientific notation to FALSE
options(scipen = 999)
cox.relapses <- coxph(Surv(TSTART, TSTOP, STATUS) ~ DISEASE_COURSE + AGE + SEX
+ RACE + TIME_SINCE_DIAGNOSIS, ties = "breslow",
data = subset(data_layout_1, SEVENT == 1))
cox.relapses
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
library(dplyr)
library(purrr)
library(tibble)
library(reda)
library(tidyr)
library(gtsummary)
library(writexl)
library(kableExtra)
# Set seed for reproducibility
set.seed(123)
# Define a function to simulate recurrent event data using reda
simulate_recurrent_event_data <- function(n, max_event, mean_follow_up_time, max_follow_up_time, mstype, outcome) {
simulate_individual_data <- function(id) {
# Generate baseline hazard times (exponential distribution)
basehaz <- rexp(n = pmax(pmin(round(rnorm(1, max_event/2, max_event/4)), max_event), 1),
rate = 1 / mean_follow_up_time)
basehaz <- basehaz[basehaz < max_follow_up_time]
basehaz <- c(basehaz,max_follow_up_time)
# Create a tibble for the individual's data
tibble(
id = id,
status = c(rep(1, length(basehaz) - 1), 0),
time = basehaz,
mstype = mstype,
outcome = outcome
)
}
# Use purrr::map_dfr to apply the helper function to each individual and combine the results
sim_data_all <- map_dfr(1:n, simulate_individual_data) %>%
arrange(id, time)
# Return the simulated data
return(sim_data_all)
}
# Generate the datasets for relapses in RRMS and SPMS patients
# this parameters have been changed in order to find differences between groups
# just for illustrative purposes
rrms_relapse_data<-simulate_recurrent_event_data(n = 791,
max_event = 10,
mean_follow_up_time = 8,
max_follow_up_time = 10,
mstype = "RRMS",
outcome = "RELAPSE")
spms_relapse_data<-simulate_recurrent_event_data(n = 522,
max_event = 4,
mean_follow_up_time = 5,
max_follow_up_time = 10,
mstype = "SPMS",
outcome = "RELAPSE")
spms_relapse_data$id <- spms_relapse_data$id + 791
# Bind datasets
df <- bind_rows(rrms_relapse_data,
spms_relapse_data)
#Add baaseline characteristics
# Function to generate data for RRMS
generate_rrms_data <- function(n) {
tibble(
id = 1:n,
Age = rnorm(n, mean = 36.5, sd = 9.1),
Sex = factor(rbinom(n, 1, prob = 0.70), labels = c("Male", "Female")),
Race = factor(rbinom(n, 1, prob = 0.88), labels = c("No white", "White")),
Time_since_diagnosis = rlnorm(n, meanlog = log(2), sdlog = log(5/2)),
EDSS_overall = rlnorm(n, meanlog = log(2), sdlog = log(3.5/1.5))
)
}
# Function to generate data for SPMS
generate_spms_data <- function(n) {
tibble(
id = 1:n,
Age = rnorm(n, mean = 49.4, sd = 8.1),
Sex = factor(rbinom(n, 1, prob = 0.63), labels = c("Male", "Female")),
Race = factor(rbinom(n, 1, prob = 0.96), labels = c("No white", "White")),
Time_since_diagnosis = rlnorm(n, meanlog = log(14.5), sdlog = log(22/7.8)), # approximate log-normal
EDSS_overall = rlnorm(n, meanlog = log(6), sdlog = log(6.5/4.5)) # approximate log-normal
)
}
# Generate the datasets
rrms_data <- generate_rrms_data(791)
spms_data <- generate_spms_data(522)
spms_data$id <- spms_data$id + 791
#Merge with the previous part
df_revents <- inner_join(bind_rows(rrms_data, spms_data),df, by="id")
kbl(df_revents[1:10,]) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
# Save data as CSV
file_path_data_csv <- "C:/Users/david/revents/inst/simulated_data.csv"
write.csv(as.data.frame(df_revents), file_path_data_csv)
usethis::use_data(simulated_data, overwrite = TRUE)
df_revents <- readr::read_csv("data-raw/simulated_data.csv")
usethis::use_data(data_layout_3, overwrite = TRUE)
usethis
usethis::use_r("simulated_data")
devtools::document()
library(revents)
devtools::document()
library(revents)
library(revents)
devtools::document()
devtools::load_all(".")
df_revents <- readr::read_csv("data-raw/simulated_data.csv")
usethis::use_data(data_layout_3, overwrite = TRUE)
usethis::use_data(simulated_data, overwrite = TRUE)
# Cargar librerías necesarias
library(here)
file_path_data_csv_simulated <- here::here("data-raw", "simulated_data.csv")
df_revents <- read.csv(file_path_data_csv_1)
file_path_data_csv_simulated <- here::here("data-raw", "simulated_data.csv")
df_revents <- read.csv(file_path_data_csv_simulated)
save(df_revents, file = here::here("data", "simulated_data.rda"))
devtools::document()
devtools::document()
head(revents::df_revents)
df_revents <- readr::read_csv("data-raw/df_revents.csv")
usethis::use_data(simulated_data, overwrite = TRUE)
usethis::use_data(df_revents, overwrite = TRUE)
df_revents <- readr::read_csv("data-raw/df_revents.csv")
usethis::use_data(df_revents, overwrite = TRUE)
devtools::document()
devtools::load_all(".")
head(revents::df_revents)
library(revents)
# Load the simulated data
data("df_revents", package = "revents")
df_revents <- revents::df_revents
# Load the simulated data
data("df_revents", package = "revents")
df_revents <- revents::df_revents
df_revents <- revents::df_revents
pkgdown::build_articles()
pkgdown::build_site()
pkgdown::build_articles()
pkgdown::build_articles()
usethis::use_pkgdown_github_pages()
devtools::document()
# Find the maximum SEVENT value in your data
max_SEVENT <- max(data_layout_1$SEVENT)
# Create a data frame with all combinations of USUBJID and SEVENT
combinations <- expand.grid(ID = unique(data_layout_1$ID), SEVENT = 1:max_SEVENT)
# Merge the original data frame with all combinations of USUBJID and SEVENT
data_layout_2 <-
merge(combinations, data_layout_1,
by = c("ID", "SEVENT"),
all.x = TRUE
) %>%
arrange(ID, SEVENT) %>%
# select only necessary variables (TGAP are not necessary in this layout)
dplyr::select(-TGAP) %>%
# fill in missing values by repeating the value of the last cell
fill(PARAMCD, TSTART, TSTOP, STATUS, SEVENT, DISEASE_COURSE, SEX, AGE, RACE,
TIME_SINCE_DIAGNOSIS
)
kbl(data_layout_2[1:20,]) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
kbl(data_layout_2[1:20,]) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
pkgdown::build_articles()
usethis::use_pkgdown_github_pages()
devtools::document()
12 changes: 8 additions & 4 deletions .Rproj.user/EC080363/rmd-outputs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
C:/Users/david/AppData/Local/Temp/RtmpohDzTZ/preview-471c12a9367.dir/count_based_models.html
C:/Users/david/AppData/Local/Temp/RtmpohDzTZ/preview-471c371a2884.dir/marginal_models.html
C:/Users/david/AppData/Local/Temp/RtmpohDzTZ/preview-471c7d1c4e9c.dir/event_specific_estimates.html
C:/Users/david/AppData/Local/Temp/RtmpohDzTZ/preview-471c27c6406d.dir/simulated_data.html
C:/Users/david/AppData/Local/Temp/Rtmpqo9Cld/preview-46182e5b13b8.dir/revents.html
C:/Users/david/AppData/Local/Temp/Rtmpqo9Cld/preview-46185e227c7e.dir/database_layout.html
C:/Users/david/AppData/Local/Temp/Rtmpqo9Cld/preview-406846c43cc0.html
C:/Users/david/AppData/Local/Temp/Rtmpqo9Cld/preview-461817351a80.dir/revents.html
C:/Users/david/AppData/Local/Temp/Rtmpqo9Cld/preview-46183c38ffe.dir/simulated_data.html




4 changes: 2 additions & 2 deletions .Rproj.user/EC080363/sources/prop/E8B9DAAB
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"source_window_id": "",
"Source": "Source",
"cursorPosition": "16,20",
"scrollLine": "0"
"cursorPosition": "11,20",
"scrollLine": "4"
}
1 change: 0 additions & 1 deletion .Rproj.user/shared/notebooks/paths
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ C:/Users/david/revents/NAMESPACE="F578CE44"
C:/Users/david/revents/R/data_layout.R="CC5DC09A"
C:/Users/david/revents/R/df_revents.R="11165C0E"
C:/Users/david/revents/README.Rmd="8A2B2F00"
C:/Users/david/revents/_pkgdown.yml="DD6EAD8E"
C:/Users/david/revents/data-raw/data_layout_1.R="2B6C7008"
C:/Users/david/revents/data-raw/data_layout_2.R="8D82ED10"
C:/Users/david/revents/data-raw/data_layout_3.R="7CDD99B3"
Expand Down
36 changes: 33 additions & 3 deletions R/data_layout.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,52 @@
#' Datos layout_1 for conditional and marginal rates models
#'
#'
#' \item{ID}{participant ID}
#' \item{PARAMCD}{parameter}
#' \item{TSTART}{time study start}
#' \item{TSTOP}{time stop observation}
#' \item{TGAP}{time gap between events}
#' \item{STATUS}{status of the event}
#' \item{SEVENT}{sequence number of event per id}
#' \item{AGE}{age of the participant}
#' \item{SEX}{Sex of the participant}
#' \item{DISEASE_COURSE}{type of disease course}
#' \item{RACE}{race of the participant}
#' \item{TIME_SINCE_DIAGNOSIS}{time since diagnosis}#'
#'
#' @format Un data frame con 6347 rows y 13 columns.
#' @source data simulated from MSOAC parameters
"data_layout_1"

#' Data layout_2 for marginal hazard models
#'
#'
#' \item{ID}{participant ID}
#' \item{PARAMCD}{parameter}
#' \item{TSTART}{time study start}
#' \item{TSTOP}{time stop observation}
#' \item{TGAP}{time gap between events}
#' \item{STATUS}{status of the event}
#' \item{SEVENT}{sequence number of event per id}
#' \item{AGE}{age of the participant}
#' \item{SEX}{Sex of the participant}
#' \item{DISEASE_COURSE}{type of disease course}
#' \item{RACE}{race of the participant}
#' \item{TIME_SINCE_DIAGNOSIS}{time since diagnosis}
#'
#' @format a data frame with con 15765 rows y 12 columns.
#' @source data simulated from MSOAC parameters
"data_layout_2"

#' Data layout_3 for count-based models
#'
#'
#' \item{ID}{participant ID}
#' \item{PARAMCD}{parameter}
#' \item{COUNT}{number of confirmed events}
#' \item{SEVENT}{sequence number of event per id}
#' \item{AGE}{age of the participant}
#' \item{SEX}{Sex of the participant}
#' \item{DISEASE_COURSE}{type of disease course}
#' \item{RACE}{race of the participant}
#' \item{TIME_SINCE_DIAGNOSIS}{time since diagnosis}
#'
#' @format Un data frame con 1313 rows y 9 columns.
#' @source data simulated from MSOAC parameters
Expand Down

0 comments on commit c862b7c

Please sign in to comment.