liud4 / rVMAP

Data Management code for VMAC-MAP study
Other
3 stars 0 forks source link

derive/harmonize etco2 variables based on Hudson's code #69

Closed liud4 closed 7 months ago

liud4 commented 7 months ago

https://github.com/liud4/rVMAP/blob/1e8aa09d639dcd810d04a8702fa163864245635a/inst/rmarkdown/templates/data_merge/skeleton/skeleton.Rmd#L1863

packages <- as.character(expression( knitr, rms, Hmisc, dplyr, magrittr, plotly, broom, purrr, formattable, DT, nlme, rVMAP, MuMIn, beepr, Formula, longCombat, cowplot ))

suppressMessages(rVMAP::load_pkg(packages))

df <- readRDS("~/Jefferson/resources/MAP_fm_f20230906_m20230906.rds")

changes in etco2 capturing over time--3 different ways

three_obs_bl_cols <- c("asl.3t.bl.etco2.1","asl.3t.bl.etco2.2","asl.3t.bl.etco2.3") three_obs_hyper_cols <- c("asl.3t.hyper.etco2.1","asl.3t.hyper.etco2.2","asl.3t.hyper.etco2.3")

ten_obs_bl_cols <- c("asl.3t.bl.etco2a","asl.3t.bl.etco2b","asl.3t.bl.etco2.1", "asl.3t.bl.etco2c","asl.3t.bl.etco2d","asl.3t.bl.etco2.2", "asl.3t.bl.etco2e","asl.3t.bl.etco2.3","asl.3t.bl.etco2f", "asl.3t.bl.etco2g")

ten_obs_hyper_cols <- c("asl.3t.hyper.etco2a","asl.3t.hyper.etco2b","asl.3t.hyper.etco2.1", "asl.3t.hyper.etco2c","asl.3t.hyper.etco2d","asl.3t.hyper.etco2.2", "asl.3t.hyper.etco2e","asl.3t.hyper.etco2.3","asl.3t.hyper.etco2f", "asl.3t.hyper.etco2g")

physLog_bl <- c("asl.rest.mean.etco2") physLog_hyper <- c("asl.chall.mean.etco2")

df$three_obs_bl_mean <- rowMeans(df[, three_obs_bl_cols], na.rm = TRUE) df$three_obs_hyper_mean <- rowMeans(df[, three_obs_hyper_cols], na.rm = TRUE) df$ten_obs_bl_mean <- rowMeans(df[, ten_obs_bl_cols], na.rm = TRUE) df$ten_obs_hyper_mean <- rowMeans(df[, ten_obs_hyper_cols], na.rm = TRUE)

all_cols <- c(three_obs_bl_cols, three_obs_hyper_cols, ten_obs_bl_cols, ten_obs_hyper_cols, physLog_bl, physLog_hyper)

df <- dplyr::select(df,map.id,epoch,all_of(all_cols))

Determine the unique columns for 10 obs by removing the overlapping 3 obs columns

unique_ten_obs_bl_cols <- setdiff(ten_obs_bl_cols, three_obs_bl_cols) unique_ten_obs_hyper_cols <- setdiff(ten_obs_hyper_cols, three_obs_hyper_cols)

Check if any of the unique 10 obs variables are not NA (i.e., they have a value)

any_unique_ten_obs_value <- rowSums(!is.na(df[, unique_ten_obs_bl_cols])) + rowSums(!is.na(df[, unique_ten_obs_hyper_cols])) >0

This creates a logical vector for each row indicating if all 10 obs baseline columns are NA

all_ten_obs_bl_na <- rowSums(is.na(df[, ten_obs_bl_cols])) == length(ten_obs_bl_cols)

This creates a logical vector for each row indicating if all 10 obs hyper columns are NA

all_ten_obs_hyper_na <- rowSums(is.na(df[, ten_obs_hyper_cols])) == length(ten_obs_hyper_cols)

Now we check both baseline and hyper conditions for 10 obs being NA

all_ten_obs_na <- all_ten_obs_bl_na & all_ten_obs_hyper_na

df$etco2_batch <- dplyr::case_when( !is.na(df$asl.rest.mean.etco2) ~ "physlog", any_unique_ten_obs_value ~ "10obs", all_ten_obs_na & is.na(df$asl.rest.mean.etco2) ~ NAcharacter, TRUE ~ "3obs" )

Compute the mean for each set of columns and create new columns in df for these means

df$three_obs_bl_mean <- rowMeans(df[, three_obs_bl_cols], na.rm = TRUE) df$three_obs_hyper_mean <- rowMeans(df[, three_obs_hyper_cols], na.rm = TRUE) df$ten_obs_bl_mean <- rowMeans(df[, ten_obs_bl_cols], na.rm = TRUE) df$ten_obs_hyper_mean <- rowMeans(df[, ten_obs_hyper_cols], na.rm = TRUE)

get the delta etco2 for these

df$three_obs_delta_etco2 <- df$three_obs_hyper_mean - df$three_obs_bl_mean df$ten_obs_delta_etco2 <- df$ten_obs_hyper_mean - df$ten_obs_bl_mean df$asl_mean_delta_etco2 <- df$asl.chall.mean.etco2 - df$asl.rest.mean.etco2

Convert the labelled vector to a numeric vector before using coalesce()

df <- df %>% mutate( asl_mean_delta_etco2 = as.numeric(asl_mean_delta_etco2), # Convert labelled to numeric combined_cols_etco2 = coalesce(ten_obs_delta_etco2, asl_mean_delta_etco2) )

panpanzhang99299 commented 7 months ago

Function "derive_automated_3T" has been updated.