Untitled
unknown
plain_text
2 years ago
3.8 kB
10
Indexable
calculate_adherence = function(users = NULL, adherence_duration=100000, adherence_start=0){
filtered=baseline_adherence
if (!is.null(users)) {
filtered = filtered[id %in% users]
}
filtered = filtered[t1 < adherence_duration]
adherence_dt = filtered[, .(id, t1=pmax(t1, adherence_start), t2=pmin(t2, adherence_duration), ace, bb, statin, age, gender, region, diabetes,
baseline.condition,
ace_adherence = (pmin(t2, adherence_duration) - pmax(t1, adherence_start))*ace,
bb_adherence = (pmin(t2, adherence_duration) - pmax(t1, adherence_start))*bb,
statin_adherence = (pmin(t2, adherence_duration) - pmax(t1, adherence_start))*statin,
overall_adherence = ifelse(ace == 1 & bb == 1 & statin == 1, pmin(t2, adherence_duration) - pmax(t1, adherence_start), 0))]
max_t2_by_id = adherence_dt[, .(max_t2 = max(t2-adherence_start)), by = id]
summed_adherence = adherence_dt[, .(sum_ace_adherence = sum(ace_adherence),
sum_bb_adherence = sum(bb_adherence),
sum_statin_adherence = sum(statin_adherence),
sum_overall_adherence = sum(overall_adherence)),
by = .(id,age, gender, region, diabetes, baseline.condition)]
joined_adherence = summed_adherence[max_t2_by_id, on = "id"]
calculate_adherence = joined_adherence[, .(id, age, gender, region, diabetes, baseline.condition,
ace_adherence_pct = sum_ace_adherence / max_t2,
bb_adherence_pct = sum_bb_adherence / max_t2,
statin_adherence_pct = sum_statin_adherence / max_t2,
overall_adherence_pct = sum_overall_adherence / max_t2)]
return(calculate_adherence)
}
#Data Creation
statin_initiated <- dat[statin == 1 & t1 < two_weeks, unique(id)]
bb_initiated <- dat[bb == 1 & t1 < two_weeks, unique(id)]
ace_initiated <- dat[ace == 1 & t1 < two_weeks, unique(id)]
q5_patients = dat[, ace_initiated_14 := ifelse(id %in% ace_initiated, 1, 0)]
q5_patients = dat[, bb_initiated_14 := ifelse(id %in% bb_initiated, 1, 0)]
q5_patients= dat[, statin_initiated_14 := ifelse(id %in% statin_initiated, 1, 0)]
q6_patients = merge(q5_patients, baseline, by="id")
q6_patients <- q6_patients[,.SD[1], id]
q9_initiated = q6_patients
q9_patients = dat[t2 >= followup_days, unique(id)]
q9_total_adherence = calculate_adherence(users=q9_patients, adherence_duration = followup_days, adherence_start=14)
# Left join based on 'id'
q9_data <- merge(q9_total_adherence, q9_initiated, by = "id", all.x = TRUE,suffixes = c("", "_y"))
# Drop redundant columns from the result
q9_data <- q9_data[, .(id,
age,
gender,
region,
diabetes,
t1,
t2,
ace,
bb,
statin,
ace_initiated_14,
bb_initiated_14,
statin_initiated_14,
baseline.condition,
ace_adherence_pct,
bb_adherence_pct,
statin_adherence_pct,
overall_adherence_pct)]
model_ace <- lm(ace_adherence_pct ~ age + gender + region + diabetes + baseline.condition + ace_initiated_14, data = q9_data)
coef_ace <- summary(model_ace)$coefficients
coef_ace_rounded <- apply(coef_ace, 2, round, digits = 2)
coef_ace_df <- as.data.frame(coef_ace_rounded )
datatable(coef_ace_df)Editor is loading...