patients <- data.frame(
pid = sprintf("P%02d", 1:12),
age = c(84, 79, 91, 76, 93, 82, 80, 88, 74, 86, 90, 78), # years at admission
sex = c("f", "f", "f", "m", "f", "f", "m", "f", "f", "m", "f", "f"),
fracture = c("neck", "pertroch", "pertroch", "neck", "pertroch", "neck",
"neck", "pertroch", "neck", "pertroch", "neck", "neck"),
end_day = c(91, 88, 20, 90, 11, 90, 93, 33, 90, 90, 52, 89), # days since surgery
end_reason = c("follow-up complete", "follow-up complete", "transferred to nursing home",
"follow-up complete", "died", "day-90 visit missed", "follow-up complete",
"transferred to acute hospital", "follow-up complete", "follow-up complete",
"died", "follow-up complete")
)
windows <- c("V1", "V2", "V3", "V4", "V5")
visit_rows <- function(pid, day, gait_ms, status) { # five planned visits of one patient
data.frame(pid = pid, window = windows, day = day, gait_ms = gait_ms, status = status)
}
m <- "measured"
visits <- rbind(
visit_rows("P01", c(2, 6, 15, 27, 91), c(.31, .42, .55, .63, .78), rep(m, 5)),
visit_rows("P02", c(1, 5, 13, 29, 88), c(.28, .35, .47, .58, .70), rep(m, 5)),
visit_rows("P03", c(2, 6, 14, NA, NA), c(NA, .31, .38, NA, NA),
c("unable", m, m, "transferred", "transferred")),
visit_rows("P04", c(3, 8, NA, NA, 90), c(.40, .52, NA, NA, .85),
c(m, m, "at home", "at home", m)),
visit_rows("P05", c(1, 4, NA, NA, NA), c(NA, NA, NA, NA, NA),
c("unable", "unable", "died", "died", "died")),
visit_rows("P06", c(2, 7, 16, 30, NA), c(.33, .41, .50, .57, NA),
c(m, m, m, m, "visit missed")),
visit_rows("P07", c(2, 6, 12, 26, 93), c(.36, .47, .58, .66, .81), rep(m, 5)),
visit_rows("P08", c(3, 8, 17, 31, NA), c(NA, .30, .29, NA, NA),
c("unable", m, m, "unable", "transferred")),
visit_rows("P09", c(2, 5, NA, NA, 90), c(.38, .49, NA, NA, .88),
c(m, m, "at home", "at home", m)),
visit_rows("P10", c(2, 9, 18, 28, 90), c(.27, .33, .41, .46, .52), rep(m, 5)),
visit_rows("P11", c(1, 6, 15, 24, NA), c(.30, .37, .45, .44, NA), c(m, m, m, m, "died")),
visit_rows("P12", c(3, 7, 14, 28, 89), c(.35, .44, NA, .61, .74),
c(m, m, "device fault", m, m))
)
visits$method <- ifelse(visits$window %in% c("V3", "V4"), "sensor mat", "stopwatch")
visits$method[is.na(visits$day)] <- NA # no visit took place, so no method
head(visits, 7)