Please see Jackson et al 2019 for the format of data that is accepted by MSM package. This section details various steps taken to acheive the format. ### IP DATASET
cols.required <- c(1:6,10,11,24:26,32:35,38:40,46:49,52:54,59:62,65:67,73:76,
80:82,88:91,95:97,103:106)
dat <- read.csv(
file = file.path(data.path,"PTC.csv"), header = TRUE,
stringsAsFactors = FALSE,
na.strings = c(""," ")
)[,cols.required]
dat <- dat[-1,]
cols.names <- c(
"name",
"age.current",
"gender",
"occupation",
"background",
"marital",
"age.first.use",
"age.ads",
"age.problem.1",
"type.problem.1",
"type.contact.1",
"advise.contact.1",
"motivation.contact.1",
"abstinence.1",
"dur.abstinence.1",
"age.problem.2",
"type.problem.2",
"type.contact.2",
"advise.contact.2",
"motivation.contact.2",
"abstinence.2",
"dur.abstinence.2",
"age.problem.3",
"type.problem.3",
"type.contact.3",
"advise.contact.3",
"motivation.contact.3",
"abstinence.3",
"dur.abstinence.3",
"age.problem.4",
"type.problem.4",
"type.contact.4",
"advise.contact.4",
"motivation.contact.4",
"abstinence.4",
"dur.abstinence.4",
"age.problem.5",
"type.problem.5",
"type.contact.5",
"advise.contact.5",
"motivation.contact.5",
"abstinence.5",
"dur.abstinence.5",
"age.problem.6",
"type.problem.6",
"type.contact.6",
"advise.contact.6",
"motivation.contact.6",
"abstinence.6",
"dur.abstinence.6"
)
names(dat) <- cols.names
We need a dataset in the following format:
tbl <- data.frame(id = rep(1, times = 5), year = c(0, 18, 25, 26, 26.02), state = c(1,
2, 3, 4, 3))
kable(tbl)
| id | year | state |
|---|---|---|
| 1 | 0.00 | 1 |
| 1 | 18.00 | 2 |
| 1 | 25.00 | 3 |
| 1 | 26.00 | 4 |
| 1 | 26.02 | 3 |
The id “1” started drinking at age 18 so was in state 1 till age 18, entered state 2 i.e. drinking without any problems at 18 and remained in that state till 25. At 25 he entered state 3 i.e. drinking with problems and remained in that till 26 when he acheived abstinence and thus entered state 4. He was in state 4 of abstinence between age 26 and 26.02 following which he again entered state 3 i.e drinking with problems.
dat[,3:6] <- lapply(dat[,3:6],tolower)
dat[,10:14] <- lapply(dat[,10:14],tolower)
dat[,17:21] <- lapply(dat[,17:21],tolower)
dat[,24:28] <- lapply(dat[,24:28],tolower) ## All to lowercase to avoid errors ##
dat[,31:35] <- lapply(dat[,31:35],tolower)
dat[,38:42] <- lapply(dat[,38:42],tolower)
dat[,45:49] <- lapply(dat[,45:49],tolower)
dat$gender <- factor(dat$gender, levels = c("male","female"),ordered = TRUE)
dat$occupation <- factor(dat$occupation,
levels = c("unemployed","unskilled","semiskilled" ,"skilled","professional"),
ordered = TRUE)
dat$background <- factor(dat$background,
levels = c("rural","urban"),ordered = TRUE)
dat$marital <- factor(dat$marital)
dat$type.problem.1 <- factor(dat$type.problem.1,
levels = c("physical","psychological","family","legal","social","other"),
ordered = TRUE)
dat$type.problem.2 <- factor(dat$type.problem.2,
levels = c("physical","psychological","family","legal","social","other"),
ordered = TRUE)
dat$type.problem.3 <- factor(dat$type.problem.3,
levels = c("physical","psychological","family","legal","social","other"),
ordered = TRUE)
dat$type.problem.4 <- factor(dat$type.problem.4,
levels = c("physical","psychological","family","legal","social","other"), ## Making all problems an ordered factor
ordered = TRUE)
dat$type.problem.5 <- factor(dat$type.problem.5,
levels = c("physical","psychological","family","legal","social","other"),
ordered = TRUE)
dat$type.problem.6 <- factor(dat$type.problem.6,
levels = c("physical","psychological","family","legal","social","other"),
ordered = TRUE)
## Decreasing the number of levels of contact to 3##
lev1 <- c("non-medical",
"medical","psychiatric")
lev <- c("general hospital", "medical practitioner" ,
"psychiatric services" ,
"other","native/religious healer","police","social worker","local pharmacist")
dat$type.contact.1[dat$type.contact.1 == "none"] <- NA
dat$type.contact.1[dat$type.contact.1 %in% lev[1:2]] <- lev1[2]
dat$type.contact.1[dat$type.contact.1 %in% lev[3]] <- lev1[3]
dat$type.contact.1[dat$type.contact.1 %in% lev[4:8]] <- lev1[1]
dat$type.contact.1 <- factor(dat$type.contact.1, levels = lev1, ordered = TRUE)
dat$type.contact.2[dat$type.contact.2 == "none"] <- NA
dat$type.contact.2[dat$type.contact.2 %in% lev[1:2]] <- lev1[2]
dat$type.contact.2[dat$type.contact.2 %in% lev[3]] <- lev1[3]
dat$type.contact.2[dat$type.contact.2 %in% lev[4:8]] <- lev1[1]
dat$type.contact.2 <- factor(dat$type.contact.2, levels = lev1, ordered = TRUE)
dat$type.contact.3[dat$type.contact.3 == "none"] <- NA
dat$type.contact.3[dat$type.contact.3 %in% lev[1:2]] <- lev1[2]
dat$type.contact.3[dat$type.contact.3 %in% lev[3]] <- lev1[3]
dat$type.contact.3[dat$type.contact.3 %in% lev[4:8]] <- lev1[1]
dat$type.contact.3 <- factor(dat$type.contact.3, levels = lev1, ordered = TRUE)
dat$type.contact.4[dat$type.contact.4 == "none"] <- NA
dat$type.contact.4[dat$type.contact.4 %in% lev[1:2]] <- lev1[2]
dat$type.contact.4[dat$type.contact.4 %in% lev[3]] <- lev1[3]
dat$type.contact.4[dat$type.contact.4 %in% lev[4:8]] <- lev1[1]
dat$type.contact.4 <- factor(dat$type.contact.4, levels = lev1, ordered = TRUE)
dat$type.contact.5[dat$type.contact.5 == "none"] <- NA
dat$type.contact.5[dat$type.contact.5 %in% lev[1:2]] <- lev1[2]
dat$type.contact.5[dat$type.contact.5 %in% lev[3]] <- lev1[3]
dat$type.contact.5[dat$type.contact.5 %in% lev[4:8]] <- lev1[1]
dat$type.contact.5 <- factor(dat$type.contact.5, levels = lev1, ordered = TRUE)
dat$type.contact.6[dat$type.contact.6 == "none"] <- NA
dat$type.contact.6[dat$type.contact.6 %in% lev[1:2]] <- lev1[2]
dat$type.contact.6[dat$type.contact.6 %in% lev[3]] <- lev1[3]
dat$type.contact.6[dat$type.contact.6 %in% lev[4:8]] <- lev1[1]
dat$type.contact.6 <- factor(dat$type.contact.6, levels = lev1, ordered = TRUE)
## Ordered Factor
dat$advise.contact.1 <- factor(dat$advise.contact.1,levels = c("no","yes"),ordered = TRUE)
dat$advise.contact.2 <- factor(dat$advise.contact.2,levels = c("no","yes"),ordered = TRUE)
dat$advise.contact.3 <- factor(dat$advise.contact.3,levels = c("no","yes"),ordered = TRUE)
dat$advise.contact.4 <- factor(dat$advise.contact.4,levels = c("no","yes"),ordered = TRUE)
dat$advise.contact.5 <- factor(dat$advise.contact.5,levels = c("no","yes"),ordered = TRUE)
dat$advise.contact.6 <- factor(dat$advise.contact.6,levels = c("no","yes"),ordered = TRUE)
## Ordered Factor
dat$motivation.contact.1 <- factor(dat$motivation.contact.1,levels = c("no","yes"),ordered = TRUE)
dat$motivation.contact.2 <- factor(dat$motivation.contact.2,levels = c("no","yes"),ordered = TRUE)
dat$motivation.contact.3 <- factor(dat$motivation.contact.3,levels = c("no","yes"),ordered = TRUE)
dat$motivation.contact.4 <- factor(dat$motivation.contact.4,levels = c("no","yes"),ordered = TRUE)
dat$motivation.contact.5 <- factor(dat$motivation.contact.5,levels = c("no","yes"),ordered = TRUE)
dat$motivation.contact.6 <- factor(dat$motivation.contact.6,levels = c("no","yes"),ordered = TRUE)
## Ordered Factor
dat$abstinence.1 <- factor(dat$abstinence.1)
dat$abstinence.2 <- factor(dat$abstinence.2)
dat$abstinence.3 <- factor(dat$abstinence.3)
dat$abstinence.4 <- factor(dat$abstinence.4)
dat$abstinence.5 <- factor(dat$abstinence.5)
dat$abstinence.6 <- factor(dat$abstinence.6)
## Converting to years
dat$dur.abstinence.1 <- dat$dur.abstinence.1/12
dat$dur.abstinence.2 <- dat$dur.abstinence.2/12
dat$dur.abstinence.3 <- dat$dur.abstinence.3/12
dat$dur.abstinence.4 <- dat$dur.abstinence.4/12
dat$dur.abstinence.5 <- dat$dur.abstinence.5/12
dat$dur.abstinence.6 <- dat$dur.abstinence.6/12
## Dur.abstinenece for all admitted cases is 0
dat$dur.abstinence.1[dat$dur.abstinence.1 == 83.25] <- NA
dat$dur.abstinence.2[dat$dur.abstinence.2 == 83.25] <- NA
dat$dur.abstinence.3[dat$dur.abstinence.3 == 83.25] <- NA
dat$dur.abstinence.4[dat$dur.abstinence.4 == 83.25] <- NA
dat$dur.abstinence.5[dat$dur.abstinence.5 == 83.25] <- NA
dat$dur.abstinence.6[dat$dur.abstinence.6 == 83.25] <- NA
# Non Transition Related
n <- nrow(dat)
dat <- dat %>% mutate(id = 1:n, trans.0 = rep(1, times = n), age.trans.0 = rep(0,
times = n))
# Transition 1, 1 ->2 Nondrinking to drinking
dat <- dat %>% mutate(trans.1 = rep(2, times = n), age.trans.1 = age.first.use) %>%
select(-age.first.use)
# Transition 2, 2 ->3 Drinking to drinking with problems (using problem.1)
dat <- dat %>% mutate(trans.2 = rep(3, times = n), age.trans.2 = age.problem.1)
# Transition 3, 3 ->4 First Abstinence
dat <- dat %>% rename(type.prb.trans.3 = type.problem.1, type.cont.trans.3 = type.contact.1,
adv.cont.trans.3 = advise.contact.1, motiv.cont.trans.3 = motivation.contact.1)
dat <- dat %>% mutate(trans.3 = ifelse(abstinence.1 == "yes", 4, NA), age.trans.3 = ifelse(is.na(trans.3),
NA, age.problem.1 + 0.019)) %>% mutate(age.trans.3 = ifelse((age.current -
age.trans.3) > 0, age.trans.3, NA), trans.3 = ifelse((age.current - age.trans.3) >
0, trans.3, NA))
# Transition 4, 4 ->3 or 4 ->2 or nil, First Relapse
dat <- dat %>% mutate(age.trans.4 = ifelse(abstinence.1 == "no", NA, age.trans.3 +
dur.abstinence.1), trans.4 = ifelse((abstinence.1 == "no" | is.na(age.trans.4)),
NA, ifelse(age.problem.2 == age.trans.4, 3, 2)))
# Transition 5: NA, 4,. Second Abstinence
dat <- dat %>% rename(type.prb.trans.5 = type.problem.2, type.cont.trans.5 = type.contact.2,
adv.cont.trans.5 = advise.contact.2, motiv.cont.trans.5 = motivation.contact.2)
dat <- dat %>% mutate(trans.5 = ifelse((is.na(trans.4) | abstinence.2 == "no"),
NA, 4), age.trans.5 = ifelse(is.na(trans.5), NA, age.problem.2 + 0.019)) %>%
mutate(age.trans.5 = ifelse((age.current - age.trans.5) > 0, age.trans.5,
NA), trans.5 = ifelse((age.current - age.trans.5) > 0, trans.5, NA))
# Transition 6: Second Relapse, NA|3|2
dat <- dat %>% mutate(age.trans.6 = ifelse(abstinence.2 == "no", NA, age.trans.5 +
dur.abstinence.2), trans.6 = (ifelse((is.na(trans.5) | is.na(age.problem.3)),
NA, ifelse(age.problem.3 == age.trans.6, 3, 2))))
# Transition 7: Third Abstinence, NA|4
dat <- dat %>% rename(type.prb.trans.7 = type.problem.3, type.cont.trans.7 = type.contact.3,
adv.cont.trans.7 = advise.contact.3, motiv.cont.trans.7 = motivation.contact.3)
dat$age.problem.3 <- as.numeric(dat$age.problem.3)
dat <- dat %>% mutate(trans.7 = ifelse((is.na(trans.6) | abstinence.3 == "no"),
NA, 4), age.trans.7 = ifelse(is.na(trans.7), NA, age.problem.3 + 0.019)) %>%
mutate(age.trans.7 = ifelse((age.current - age.trans.7) > 0, age.trans.7,
NA), trans.7 = ifelse((age.current - age.trans.7) > 0, trans.7, NA))
# Transition 8: Third Relapse, NA|3|2
dat <- dat %>% mutate(age.trans.8 = ifelse(abstinence.3 == "no", NA, age.trans.7 +
dur.abstinence.3), trans.8 = (ifelse((is.na(trans.7) | is.na(age.problem.4)),
NA, ifelse(age.problem.4 == age.trans.8, 3, 2))))
# Transition 9: Fourth Abstinence, NA|4
dat <- dat %>% rename(type.prb.trans.9 = type.problem.4, type.cont.trans.9 = type.contact.4,
adv.cont.trans.9 = advise.contact.4, motiv.cont.trans.9 = motivation.contact.4)
dat <- dat %>% mutate(trans.9 = ifelse((is.na(trans.8) | abstinence.4 == "no"),
NA, 4), age.trans.9 = ifelse(is.na(trans.9), NA, age.problem.4 + 0.019)) %>%
mutate(age.trans.9 = ifelse((age.current - age.trans.9) > 0, age.trans.9,
NA), trans.9 = ifelse((age.current - age.trans.9) > 0, trans.9, NA))
# Transition 10: Fourth Relapse, NA|3|2
dat <- dat %>% mutate(age.trans.10 = ifelse(abstinence.4 == "no", NA, age.trans.9 +
dur.abstinence.4), trans.10 = (ifelse((is.na(trans.9) | is.na(age.problem.5)),
NA, ifelse(age.problem.5 == age.trans.10, 3, 2))))
# Transition 11: Fifth Abstinence, NA|4
dat <- dat %>% rename(type.prb.trans.11 = type.problem.5, type.cont.trans.11 = type.contact.5,
adv.cont.trans.11 = advise.contact.5, motiv.cont.trans.11 = motivation.contact.5)
dat <- dat %>% mutate(trans.11 = ifelse((is.na(trans.10) | abstinence.5 == "no"),
NA, 4), age.trans.11 = ifelse(is.na(trans.10), NA, age.problem.5 + 0.019)) %>%
mutate(age.trans.11 = ifelse((age.current - age.trans.11) > 0, age.trans.11,
NA), trans.11 = ifelse((age.current - age.trans.11) > 0, trans.11, NA))
# Transition 12: Fifth Relapse, NA|3|2
dat <- dat %>% mutate(age.trans.12 = ifelse(abstinence.5 == "no", NA, age.trans.11 +
dur.abstinence.5), trans.12 = (ifelse((is.na(trans.11) | is.na(age.problem.6)),
NA, ifelse(age.problem.6 == age.trans.12, 3, 2))))
# Transition 13: Sixth Abstinence, NA|4
dat <- dat %>% rename(type.prb.trans.13 = type.problem.6, type.cont.trans.13 = type.contact.6,
adv.cont.trans.13 = advise.contact.6, motiv.cont.trans.13 = motivation.contact.6)
dat <- dat %>% mutate(trans.13 = ifelse((is.na(trans.12) | abstinence.6 == "no"),
NA, 4), age.trans.13 = ifelse(is.na(trans.13), NA, age.problem.6 + 0.019)) %>%
mutate(age.trans.13 = ifelse((age.current - age.trans.13) > 0, age.trans.13,
NA), trans.13 = ifelse((age.current - age.trans.13) > 0, trans.13, NA))
# CLOSING THE LOOP USING CURRENT AGE AT ADMISSION
dat <- dat %>% mutate(trans.14 = rep(5, n), age.trans.14 = age.current)
saveRDS(dat, file = file.path(data.path, "Broad_IP.Rdata"))
trans_var <- paste("trans", ".", 0:14, sep = "")
age_var <- paste("age", ".", trans_var, sep = "")
moti_var <- paste("motiv.cont.trans.", c(3, 5, 7, 9, 11, 13), sep = "")
adv_var <- paste("adv.cont.trans.", c(3, 5, 7, 9, 11, 13), sep = "")
prb_var <- paste("type.prb.trans.", c(3, 5, 7, 9, 11, 13), sep = "")
cont_var <- paste("type.cont.trans.", c(3, 5, 7, 9, 11, 13), sep = "")
dat1 <- dat %>% select(id, name, age.current, age.ads, trans_var, age_var, moti_var,
adv_var, prb_var, cont_var)
dat2<- dat1 %>% pivot_longer(cols = starts_with("trans."),
names_to = c("transition.number"),
values_to = c("state"),
values_drop_na = TRUE, # WORKS AND WORKS FOR ALL SCENARIOS
names_prefix = "trans." )%>% select(id,name,state,transition.number,everything()) %>%
arrange(id,transition.number)
dat2$state.max <- rep(5, times = nrow(dat2))
dat3 <- dat1 %>% pivot_longer(cols = starts_with("age.trans."),
names_to = "transition.number.1",
values_to = "age",
values_drop_na = TRUE,
names_prefix = "age.trans.") %>%
select(id, name, age,transition.number.1,everything()) %>%
arrange(id,transition.number.1)
dat2$age <- dat3$age # FINAL DATASET ACHEIVED
saveRDS(dat2,file = file.path(data.path,"Final_MSM_IP.Rdata"))
We can see that we have achieved the long format of process-counting data for the IP Dataset.
tbl <- dat2 %>% select(id, name, state, age)
kable(tbl)
| id | name | state | age |
|---|---|---|---|
| 1 | govinda raju | 1 | 0.00000 |
| 1 | govinda raju | 2 | 18.00000 |
| 1 | govinda raju | 5 | 40.00000 |
| 1 | govinda raju | 3 | 22.00000 |
| 2 | Chengappa | 1 | 0.00000 |
| 2 | Chengappa | 2 | 24.00000 |
| 2 | Chengappa | 5 | 34.00000 |
| 2 | Chengappa | 3 | 29.00000 |
| 3 | Panduranga | 1 | 0.00000 |
| 3 | Panduranga | 2 | 21.00000 |
| 3 | Panduranga | 5 | 41.00000 |
| 3 | Panduranga | 3 | 40.00000 |
| 3 | Panduranga | 4 | 40.01900 |
| 3 | Panduranga | 2 | 40.10233 |
| 4 | Chandrasekhar | 1 | 0.00000 |
| 4 | Chandrasekhar | 2 | 25.00000 |
| 4 | Chandrasekhar | 5 | 33.00000 |
| 4 | Chandrasekhar | 3 | 33.00000 |
| 5 | Manjunath | 1 | 0.00000 |
| 5 | Manjunath | 2 | 18.00000 |
| 5 | Manjunath | 5 | 42.00000 |
| 5 | Manjunath | 3 | 33.00000 |
| 5 | Manjunath | 4 | 33.01900 |
| 5 | Manjunath | 2 | 33.18567 |
| 5 | Manjunath | 4 | 39.01900 |
| 5 | Manjunath | 2 | 39.10233 |
| 5 | Manjunath | 4 | 41.01900 |
| 5 | Manjunath | 2 | 41.10233 |
| 6 | ganesh babu | 1 | 0.00000 |
| 6 | ganesh babu | 2 | 17.00000 |
| 6 | ganesh babu | 5 | 47.00000 |
| 6 | ganesh babu | 3 | 29.00000 |
| 6 | ganesh babu | 4 | 29.01900 |
| 6 | ganesh babu | 2 | 29.10233 |
| 6 | ganesh babu | 4 | 29.01900 |
| 6 | ganesh babu | 2 | 29.10233 |
| 6 | ganesh babu | 4 | 44.01900 |
| 6 | ganesh babu | 2 | 44.03983 |
| 7 | Srinivas | 1 | 0.00000 |
| 7 | Srinivas | 2 | 14.00000 |
| 7 | Srinivas | 5 | 40.00000 |
| 7 | Srinivas | 3 | 39.00000 |
| 8 | Shankar | 1 | 0.00000 |
| 8 | Shankar | 2 | 18.00000 |
| 8 | Shankar | 5 | 25.00000 |
| 8 | Shankar | 3 | 24.00000 |
| 8 | Shankar | 4 | 24.01900 |
| 8 | Shankar | 2 | 24.10233 |
| 9 | Raju | 1 | 0.00000 |
| 9 | Raju | 2 | 29.00000 |
| 9 | Raju | 5 | 44.00000 |
| 9 | Raju | 3 | 37.00000 |
| 9 | Raju | 4 | 37.01900 |
| 9 | Raju | 2 | 37.03983 |
| 10 | Chiranjeevi | 1 | 0.00000 |
| 10 | Chiranjeevi | 2 | 15.00000 |
| 10 | Chiranjeevi | 5 | 25.00000 |
| 10 | Chiranjeevi | 3 | 22.00000 |
| 11 | Gopi | 1 | 0.00000 |
| 11 | Gopi | 2 | 20.00000 |
| 11 | Gopi | 5 | 33.00000 |
| 11 | Gopi | 3 | 23.00000 |
| 12 | Gangadhar | 1 | 0.00000 |
| 12 | Gangadhar | 2 | 28.00000 |
| 12 | Gangadhar | 5 | 40.00000 |
| 12 | Gangadhar | 3 | 39.00000 |
| 13 | bairappa | 1 | 0.00000 |
| 13 | bairappa | 2 | 18.00000 |
| 13 | bairappa | 5 | 43.00000 |
| 13 | bairappa | 3 | 33.00000 |
| 13 | bairappa | 4 | 33.01900 |
| 13 | bairappa | 2 | 33.03983 |
| 14 | Venkatesh | 1 | 0.00000 |
| 14 | Venkatesh | 2 | 26.00000 |
| 14 | Venkatesh | 5 | 32.00000 |
| 14 | Venkatesh | 3 | 27.00000 |
| 15 | Saravana | 1 | 0.00000 |
| 15 | Saravana | 2 | 23.00000 |
| 15 | Saravana | 5 | 33.00000 |
| 15 | Saravana | 3 | 26.00000 |
| 16 | Naga | 1 | 0.00000 |
| 16 | Naga | 2 | 18.00000 |
| 16 | Naga | 5 | 38.00000 |
| 16 | Naga | 3 | 36.00000 |
| 16 | Naga | 4 | 36.01900 |
| 16 | Naga | 2 | 36.10233 |
| 17 | shamakka | 1 | 0.00000 |
| 17 | shamakka | 2 | 29.00000 |
| 17 | shamakka | 5 | 58.00000 |
| 17 | shamakka | 3 | 57.00000 |
| 18 | mani | 1 | 0.00000 |
| 18 | mani | 2 | 18.00000 |
| 18 | mani | 5 | 35.00000 |
| 18 | mani | 3 | 29.00000 |
| 18 | mani | 4 | 29.01900 |
| 18 | mani | 2 | 29.06067 |
| 18 | mani | 4 | 31.01900 |
| 18 | mani | 2 | 31.18567 |
| 19 | anjaneyulu | 1 | 0.00000 |
| 19 | anjaneyulu | 2 | 25.00000 |
| 19 | anjaneyulu | 5 | 45.00000 |
| 19 | anjaneyulu | 3 | 42.00000 |
| 20 | shanthamma | 1 | 0.00000 |
| 20 | shanthamma | 2 | 45.00000 |
| 20 | shanthamma | 5 | 50.00000 |
| 20 | shanthamma | 3 | 46.00000 |
| 20 | shanthamma | 4 | 46.01900 |
| 20 | shanthamma | 2 | 46.18567 |
| 20 | shanthamma | 4 | 47.01900 |
| 20 | shanthamma | 2 | 47.18567 |
| 21 | shivaraju | 1 | 0.00000 |
| 21 | shivaraju | 2 | 40.00000 |
| 21 | shivaraju | 5 | 48.00000 |
| 21 | shivaraju | 3 | 45.00000 |
| 22 | srikanth | 1 | 0.00000 |
| 22 | srikanth | 2 | 26.00000 |
| 22 | srikanth | 5 | 30.00000 |
| 22 | srikanth | 3 | 30.00000 |
| 23 | Narasimhulu | 1 | 0.00000 |
| 23 | Narasimhulu | 2 | 23.00000 |
| 23 | Narasimhulu | 5 | 59.00000 |
| 23 | Narasimhulu | 3 | 45.00000 |
| 24 | khalid | 1 | 0.00000 |
| 24 | khalid | 2 | 35.00000 |
| 24 | khalid | 5 | 62.00000 |
| 24 | khalid | 3 | 58.00000 |
| 25 | Chandrasekar | 1 | 0.00000 |
| 25 | Chandrasekar | 2 | 15.00000 |
| 25 | Chandrasekar | 5 | 32.00000 |
| 25 | Chandrasekar | 3 | 26.00000 |
| 26 | Shivaram | 1 | 0.00000 |
| 26 | Shivaram | 2 | 22.00000 |
| 26 | Shivaram | 5 | 37.00000 |
| 26 | Shivaram | 3 | 37.00000 |
| 27 | Zakir | 1 | 0.00000 |
| 27 | Zakir | 2 | 22.00000 |
| 27 | Zakir | 5 | 30.00000 |
| 27 | Zakir | 3 | 26.00000 |
| 27 | Zakir | 4 | 26.01900 |
| 27 | Zakir | 2 | 26.18567 |
| 27 | Zakir | 4 | 27.01900 |
| 27 | Zakir | 2 | 27.43567 |
| 27 | Zakir | 4 | 29.01900 |
| 27 | Zakir | 2 | 29.03983 |
| 28 | Balaraj | 1 | 0.00000 |
| 28 | Balaraj | 2 | 18.00000 |
| 28 | Balaraj | 5 | 41.00000 |
| 28 | Balaraj | 3 | 29.00000 |
| 29 | Infant | 1 | 0.00000 |
| 29 | Infant | 2 | 26.00000 |
| 29 | Infant | 5 | 34.00000 |
| 29 | Infant | 3 | 28.00000 |
| 30 | Manjunatha k | 1 | 0.00000 |
| 30 | Manjunatha k | 2 | 12.00000 |
| 30 | Manjunatha k | 5 | 34.00000 |
| 30 | Manjunatha k | 3 | 24.00000 |
| 31 | Harish | 1 | 0.00000 |
| 31 | Harish | 2 | 25.00000 |
| 31 | Harish | 5 | 49.00000 |
| 31 | Harish | 3 | 45.00000 |
| 31 | Harish | 4 | 45.01900 |
| 31 | Harish | 2 | 47.51900 |
| 31 | Harish | 4 | 48.01900 |
| 31 | Harish | 2 | 48.18567 |
| 32 | prakash | 1 | 0.00000 |
| 32 | prakash | 2 | 23.00000 |
| 32 | prakash | 5 | 49.00000 |
| 32 | prakash | 3 | 33.00000 |
| 32 | prakash | 4 | 33.01900 |
| 32 | prakash | 2 | 34.01900 |
| 32 | prakash | 4 | 36.01900 |
| 32 | prakash | 2 | 36.68567 |
| 32 | prakash | 4 | 44.01900 |
| 32 | prakash | 2 | 48.01900 |
| 33 | kodappa 419625 | 1 | 0.00000 |
| 33 | kodappa 419625 | 2 | 32.00000 |
| 33 | kodappa 419625 | 5 | 40.00000 |
| 33 | kodappa 419625 | 3 | 38.00000 |
| 34 | thirumala | 1 | 0.00000 |
| 34 | thirumala | 2 | 17.00000 |
| 34 | thirumala | 5 | 34.00000 |
| 34 | thirumala | 3 | 18.00000 |
| 34 | thirumala | 4 | 18.01900 |
| 34 | thirumala | 2 | 22.01900 |
| 35 | anirudha p419421 | 1 | 0.00000 |
| 35 | anirudha p419421 | 2 | 16.00000 |
| 35 | anirudha p419421 | 5 | 46.00000 |
| 35 | anirudha p419421 | 3 | 27.00000 |
| 35 | anirudha p419421 | 4 | 27.01900 |
| 35 | anirudha p419421 | 2 | 29.01900 |
| 35 | anirudha p419421 | 4 | 34.01900 |
| 35 | anirudha p419421 | 2 | 36.01900 |
| 36 | hulichandra | 1 | 0.00000 |
| 36 | hulichandra | 2 | 12.00000 |
| 36 | hulichandra | 5 | 45.00000 |
| 36 | hulichandra | 3 | 23.00000 |
| 37 | shivshankar 418615 | 1 | 0.00000 |
| 37 | shivshankar 418615 | 2 | 20.00000 |
| 37 | shivshankar 418615 | 5 | 45.00000 |
| 37 | shivshankar 418615 | 3 | 30.00000 |
| 37 | shivshankar 418615 | 4 | 30.01900 |
| 37 | shivshankar 418615 | 2 | 31.01900 |
| 37 | shivshankar 418615 | 4 | 38.01900 |
| 37 | shivshankar 418615 | 2 | 38.06067 |
| 37 | shivshankar 418615 | 4 | 39.01900 |
| 37 | shivshankar 418615 | 2 | 39.06067 |
| 38 | mahadevu 419869 | 1 | 0.00000 |
| 38 | mahadevu 419869 | 2 | 18.00000 |
| 38 | mahadevu 419869 | 5 | 55.00000 |
| 38 | mahadevu 419869 | 3 | 24.00000 |
| 39 | Krishhnappa | 1 | 0.00000 |
| 39 | Krishhnappa | 2 | 8.00000 |
| 39 | Krishhnappa | 5 | 60.00000 |
| 39 | Krishhnappa | 3 | 60.00000 |
| 40 | antony | 1 | 0.00000 |
| 40 | antony | 2 | 10.00000 |
| 40 | antony | 5 | 36.00000 |
| 40 | antony | 3 | 11.00000 |
| 41 | vaniah | 1 | 0.00000 |
| 41 | vaniah | 2 | 18.00000 |
| 41 | vaniah | 5 | 35.00000 |
| 41 | vaniah | 3 | 28.00000 |
| 42 | Yuvraj | 1 | 0.00000 |
| 42 | Yuvraj | 2 | 24.00000 |
| 42 | Yuvraj | 5 | 34.00000 |
| 42 | Yuvraj | 3 | 25.00000 |
| 43 | Naveen | 1 | 0.00000 |
| 43 | Naveen | 2 | 18.00000 |
| 43 | Naveen | 5 | 31.00000 |
| 43 | Naveen | 3 | 22.00000 |
| 44 | Ashok | 1 | 0.00000 |
| 44 | Ashok | 2 | 15.00000 |
| 44 | Ashok | 5 | 26.00000 |
| 44 | Ashok | 3 | 21.00000 |
| 44 | Ashok | 4 | 21.01900 |
| 44 | Ashok | 2 | 21.03983 |
| 45 | Krishna | 1 | 0.00000 |
| 45 | Krishna | 2 | 22.00000 |
| 45 | Krishna | 5 | 32.00000 |
| 45 | Krishna | 3 | 25.00000 |
| 45 | Krishna | 4 | 25.01900 |
| 45 | Krishna | 2 | 27.01900 |
| 45 | Krishna | 4 | 30.01900 |
| 45 | Krishna | 2 | 30.26900 |
| 46 | Radha | 1 | 0.00000 |
| 46 | Radha | 2 | 42.00000 |
| 46 | Radha | 5 | 45.00000 |
| 46 | Radha | 3 | 44.00000 |
| 46 | Radha | 4 | 44.01900 |
| 46 | Radha | 2 | 44.04400 |
| 47 | Narayanamma | 1 | 0.00000 |
| 47 | Narayanamma | 2 | 38.00000 |
| 47 | Narayanamma | 5 | 53.00000 |
| 47 | Narayanamma | 3 | 52.00000 |
| 48 | Shantha bai | 1 | 0.00000 |
| 48 | Shantha bai | 2 | 30.00000 |
| 48 | Shantha bai | 5 | 34.00000 |
| 48 | Shantha bai | 3 | 33.00000 |
| 49 | Shantha bai2 | 1 | 0.00000 |
| 49 | Shantha bai2 | 2 | 30.00000 |
| 49 | Shantha bai2 | 5 | 34.00000 |
| 49 | Shantha bai2 | 3 | 33.00000 |
| 50 | Manjula | 1 | 0.00000 |
| 50 | Manjula | 2 | 17.00000 |
| 50 | Manjula | 5 | 41.00000 |
| 50 | Manjula | 3 | 26.00000 |
| 50 | Manjula | 4 | 26.01900 |
| 50 | Manjula | 2 | 26.51900 |
| 51 | Obulesu | 1 | 0.00000 |
| 51 | Obulesu | 2 | 18.00000 |
| 51 | Obulesu | 5 | 63.00000 |
| 51 | Obulesu | 3 | 50.00000 |
| 52 | Mohammad munazir | 1 | 0.00000 |
| 52 | Mohammad munazir | 2 | 23.00000 |
| 52 | Mohammad munazir | 5 | 32.00000 |
| 52 | Mohammad munazir | 3 | 31.00000 |
| 52 | Mohammad munazir | 4 | 31.01900 |
| 52 | Mohammad munazir | 2 | 31.51900 |
| 53 | Arun kumar | 1 | 0.00000 |
| 53 | Arun kumar | 2 | 18.00000 |
| 53 | Arun kumar | 5 | 30.00000 |
| 53 | Arun kumar | 3 | 28.00000 |
| 54 | Muthuraj | 1 | 0.00000 |
| 54 | Muthuraj | 2 | 17.00000 |
| 54 | Muthuraj | 5 | 48.00000 |
| 54 | Muthuraj | 3 | 45.00000 |
| 54 | Muthuraj | 4 | 45.01900 |
| 54 | Muthuraj | 2 | 45.26900 |
| 54 | Muthuraj | 4 | 47.01900 |
| 54 | Muthuraj | 2 | 47.26900 |
| 55 | X | 1 | 0.00000 |
| 55 | X | 2 | 20.00000 |
| 55 | X | 5 | 52.00000 |
| 55 | X | 3 | 50.00000 |
| 56 | Sumitra | 1 | 0.00000 |
| 56 | Sumitra | 2 | 27.00000 |
| 56 | Sumitra | 5 | 35.00000 |
| 56 | Sumitra | 3 | 34.00000 |
| 57 | Roshan | 1 | 0.00000 |
| 57 | Roshan | 2 | 16.00000 |
| 57 | Roshan | 5 | 21.00000 |
| 57 | Roshan | 3 | 17.00000 |
| 57 | Roshan | 4 | 17.01900 |
| 57 | Roshan | 2 | 17.26900 |
| 58 | Nagesh | 1 | 0.00000 |
| 58 | Nagesh | 2 | 20.00000 |
| 58 | Nagesh | 5 | 38.00000 |
| 58 | Nagesh | 3 | 37.00000 |
| 59 | Bala chandra | 1 | 0.00000 |
| 59 | Bala chandra | 2 | 16.00000 |
| 59 | Bala chandra | 5 | 35.00000 |
| 59 | Bala chandra | 3 | 27.00000 |
| 60 | Pandy | 1 | 0.00000 |
| 60 | Pandy | 2 | 24.00000 |
| 60 | Pandy | 5 | 41.00000 |
| 60 | Pandy | 3 | 32.00000 |
| 60 | Pandy | 4 | 32.01900 |
| 60 | Pandy | 2 | 32.26900 |
| 61 | Chaitanya | 1 | 0.00000 |
| 61 | Chaitanya | 2 | 18.00000 |
| 61 | Chaitanya | 5 | 28.00000 |
| 61 | Chaitanya | 3 | 19.00000 |
| 61 | Chaitanya | 4 | 19.01900 |
| 61 | Chaitanya | 2 | 20.01900 |
| 62 | Masthan babu | 1 | 0.00000 |
| 62 | Masthan babu | 2 | 17.00000 |
| 62 | Masthan babu | 5 | 44.00000 |
| 62 | Masthan babu | 3 | 38.00000 |
| 62 | Masthan babu | 4 | 38.01900 |
| 62 | Masthan babu | 2 | 38.51900 |
| 62 | Masthan babu | 4 | 40.01900 |
| 62 | Masthan babu | 2 | 42.01900 |
| 62 | Masthan babu | 4 | 43.01900 |
| 62 | Masthan babu | 2 | 43.18567 |
| 63 | Madhu | 1 | 0.00000 |
| 63 | Madhu | 2 | 14.00000 |
| 63 | Madhu | 5 | 33.00000 |
| 63 | Madhu | 3 | 19.00000 |
| 64 | Ramesh | 1 | 0.00000 |
| 64 | Ramesh | 2 | 19.00000 |
| 64 | Ramesh | 5 | 39.00000 |
| 64 | Ramesh | 3 | 38.00000 |
| 65 | Santosh | 1 | 0.00000 |
| 65 | Santosh | 2 | 24.00000 |
| 65 | Santosh | 5 | 34.00000 |
| 65 | Santosh | 3 | 28.00000 |
| 66 | Stalin | 1 | 0.00000 |
| 66 | Stalin | 2 | 20.00000 |
| 66 | Stalin | 5 | 52.00000 |
| 66 | Stalin | 3 | 37.00000 |
| 67 | Ammu | 1 | 0.00000 |
| 67 | Ammu | 2 | 35.00000 |
| 67 | Ammu | 5 | 48.00000 |
| 67 | Ammu | 3 | 45.00000 |
| 67 | Ammu | 4 | 45.01900 |
| 67 | Ammu | 2 | 45.51900 |
| 67 | Ammu | 4 | 47.01900 |
| 67 | Ammu | 2 | 47.26900 |
| 68 | Aruna | 1 | 0.00000 |
| 68 | Aruna | 2 | 25.00000 |
| 68 | Aruna | 5 | 31.00000 |
| 68 | Aruna | 3 | 31.00000 |
| 69 | Ramesh | 1 | 0.00000 |
| 69 | Ramesh | 2 | 14.00000 |
| 69 | Ramesh | 5 | 36.00000 |
| 69 | Ramesh | 3 | 30.00000 |
| 70 | Munna | 1 | 0.00000 |
| 70 | Munna | 2 | 15.00000 |
| 70 | Munna | 5 | 35.00000 |
| 70 | Munna | 3 | 20.00000 |
| 71 | Rajasekhar | 1 | 0.00000 |
| 71 | Rajasekhar | 2 | 16.00000 |
| 71 | Rajasekhar | 5 | 39.00000 |
| 71 | Rajasekhar | 3 | 26.00000 |
| 71 | Rajasekhar | 4 | 26.01900 |
| 71 | Rajasekhar | 2 | 26.03983 |
| 72 | Loknath | 1 | 0.00000 |
| 72 | Loknath | 2 | 15.00000 |
| 72 | Loknath | 5 | 36.00000 |
| 72 | Loknath | 3 | 27.00000 |
| 72 | Loknath | 4 | 27.01900 |
| 72 | Loknath | 2 | 27.01900 |
| 73 | Venkatesh | 1 | 0.00000 |
| 73 | Venkatesh | 2 | 10.00000 |
| 73 | Venkatesh | 5 | 35.00000 |
| 73 | Venkatesh | 3 | 25.00000 |
| 74 | Prasanna | 1 | 0.00000 |
| 74 | Prasanna | 2 | 17.00000 |
| 74 | Prasanna | 5 | 34.00000 |
| 74 | Prasanna | 3 | 33.00000 |
| 75 | Sharavana | 1 | 0.00000 |
| 75 | Sharavana | 2 | 18.00000 |
| 75 | Sharavana | 5 | 33.00000 |
| 75 | Sharavana | 3 | 30.00000 |
| 76 | Balaraj | 1 | 0.00000 |
| 76 | Balaraj | 2 | 20.00000 |
| 76 | Balaraj | 5 | 60.00000 |
| 76 | Balaraj | 3 | 40.00000 |
| 76 | Balaraj | 4 | 40.01900 |
| 76 | Balaraj | 2 | 40.26900 |
| 76 | Balaraj | 4 | 59.01900 |
| 76 | Balaraj | 2 | 59.03983 |
| 77 | Shivakumar | 1 | 0.00000 |
| 77 | Shivakumar | 2 | 15.00000 |
| 77 | Shivakumar | 5 | 35.00000 |
| 77 | Shivakumar | 3 | 21.00000 |
| 77 | Shivakumar | 4 | 21.01900 |
| 77 | Shivakumar | 2 | 21.26900 |
| 78 | Satya | 1 | 0.00000 |
| 78 | Satya | 2 | 15.00000 |
| 78 | Satya | 5 | 30.00000 |
| 78 | Satya | 3 | 17.00000 |
| 79 | Francis | 1 | 0.00000 |
| 79 | Francis | 2 | 16.00000 |
| 79 | Francis | 5 | 28.00000 |
| 79 | Francis | 3 | 25.00000 |
| 79 | Francis | 4 | 25.01900 |
| 79 | Francis | 2 | 25.06067 |
| 80 | Raghavendra | 1 | 0.00000 |
| 80 | Raghavendra | 2 | 19.00000 |
| 80 | Raghavendra | 5 | 45.00000 |
| 80 | Raghavendra | 3 | 35.00000 |
| 80 | Raghavendra | 4 | 35.01900 |
| 80 | Raghavendra | 2 | 35.03983 |
| 80 | Raghavendra | 4 | 38.01900 |
| 80 | Raghavendra | 2 | 38.03983 |
| 81 | Dilip | 1 | 0.00000 |
| 81 | Dilip | 2 | 25.00000 |
| 81 | Dilip | 5 | 39.00000 |
| 81 | Dilip | 3 | 32.00000 |
| 82 | Narayana | 1 | 0.00000 |
| 82 | Narayana | 2 | 13.00000 |
| 82 | Narayana | 5 | 43.00000 |
| 82 | Narayana | 3 | 35.00000 |
| 82 | Narayana | 4 | 35.01900 |
| 82 | Narayana | 2 | 35.26900 |
| 83 | Mehaboob pasha | 1 | 0.00000 |
| 83 | Mehaboob pasha | 2 | 18.00000 |
| 83 | Mehaboob pasha | 5 | 42.00000 |
| 83 | Mehaboob pasha | 3 | 35.00000 |
| 83 | Mehaboob pasha | 4 | 35.01900 |
| 83 | Mehaboob pasha | 2 | 35.06067 |
| 84 | Vinod babu | 1 | 0.00000 |
| 84 | Vinod babu | 2 | 13.00000 |
| 84 | Vinod babu | 5 | 36.00000 |
| 84 | Vinod babu | 3 | 31.00000 |
| 85 | Muniraju | 1 | 0.00000 |
| 85 | Muniraju | 2 | 15.00000 |
| 85 | Muniraju | 5 | 35.00000 |
| 85 | Muniraju | 3 | 34.00000 |
| 85 | Muniraju | 4 | 34.01900 |
| 85 | Muniraju | 2 | 34.10233 |
| 85 | Muniraju | 4 | 34.01900 |
| 85 | Muniraju | 2 | 34.10233 |
| 86 | Parthiban | 1 | 0.00000 |
| 86 | Parthiban | 2 | 10.00000 |
| 86 | Parthiban | 5 | 22.00000 |
| 86 | Parthiban | 3 | 13.00000 |
| 87 | Rajendra | 1 | 0.00000 |
| 87 | Rajendra | 2 | 33.00000 |
| 87 | Rajendra | 5 | 53.00000 |
| 87 | Rajendra | 3 | 40.00000 |
| 88 | Manjunatha s | 1 | 0.00000 |
| 88 | Manjunatha s | 2 | 19.00000 |
| 88 | Manjunatha s | 5 | 29.00000 |
| 88 | Manjunatha s | 3 | 21.00000 |
| 89 | deepa | 1 | 0.00000 |
| 89 | deepa | 2 | 32.00000 |
| 89 | deepa | 5 | 40.00000 |
| 89 | deepa | 3 | 38.00000 |
| 90 | Natraj | 1 | 0.00000 |
| 90 | Natraj | 2 | 20.00000 |
| 90 | Natraj | 5 | 31.00000 |
| 90 | Natraj | 3 | 22.00000 |
| 90 | Natraj | 4 | 22.01900 |
| 90 | Natraj | 2 | 22.51900 |
| 90 | Natraj | 4 | 28.01900 |
| 90 | Natraj | 2 | 28.03983 |
| 90 | Natraj | 4 | 30.01900 |
| 90 | Natraj | 2 | 30.06067 |
dat <- read.csv(file = file.path(data.path, "community.csv"), header = TRUE,
stringsAsFactors = FALSE, na.strings = c("", " ", "NA"))[1:100, ]
dat[, 3:6] <- lapply(dat[, 3:6], tolower)
dat[, 10:14] <- lapply(dat[, 10:14], tolower)
dat[, 17:21] <- lapply(dat[, 17:21], tolower)
dat[, 24:28] <- lapply(dat[, 24:28], tolower)
dat[, 31:35] <- lapply(dat[, 31:35], tolower) # All lower case
dat[, 38:42] <- lapply(dat[, 38:42], tolower)
dat[, 45:49] <- lapply(dat[, 45:49], tolower)
dat$gender <- factor(dat$gender, levels = c("male", "female"), ordered = TRUE)
dat$occupation <- factor(dat$occupation, levels = c("unemployed", "unskilled",
"semiskilled", "skilled", "professional"), ordered = TRUE)
dat$background <- factor(dat$background, levels = c("rural", "urban"), ordered = TRUE)
dat$marital <- factor(dat$marital)
dat$type.problem.1 <- factor(dat$type.problem.1, levels = c("physical", "psychological",
"family", "legal", "social", "other"), ordered = TRUE)
dat$type.problem.2 <- factor(dat$type.problem.2, levels = c("physical", "psychological",
"family", "legal", "social", "other"), ordered = TRUE)
dat$type.problem.3 <- factor(dat$type.problem.3, levels = c("physical", "psychological",
"family", "legal", "social", "other"), ordered = TRUE)
dat$type.problem.4 <- factor(dat$type.problem.4, levels = c("physical", "psychological",
"family", "legal", "social", "other"), ordered = TRUE)
dat$type.problem.5 <- factor(dat$type.problem.5, levels = c("physical", "psychological",
"family", "legal", "social", "other"), ordered = TRUE)
dat$type.problem.6 <- factor(dat$type.problem.6, levels = c("physical", "psychological",
"family", "legal", "social", "other"), ordered = TRUE)
lev1 <- c("non-medical", "medical", "psychiatric")
dat$type.contact.1 <- factor(dat$type.contact.1, levels = lev1, ordered = TRUE)
dat$type.contact.2 <- factor(dat$type.contact.2, levels = lev1, ordered = TRUE)
dat$type.contact.3 <- factor(dat$type.contact.3, levels = lev1, ordered = TRUE)
dat$type.contact.4 <- factor(dat$type.contact.4, levels = lev1, ordered = TRUE)
dat$type.contact.5 <- factor(dat$type.contact.5, levels = lev1, ordered = TRUE)
dat$type.contact.6 <- factor(dat$type.contact.6, levels = lev1, ordered = TRUE)
dat$advise.contact.1 <- factor(dat$advise.contact.1, levels = c("no", "yes"),
ordered = TRUE)
dat$advise.contact.2 <- factor(dat$advise.contact.2, levels = c("no", "yes"),
ordered = TRUE)
dat$advise.contact.3 <- factor(dat$advise.contact.3, levels = c("no", "yes"),
ordered = TRUE)
dat$advise.contact.4 <- factor(dat$advise.contact.4, levels = c("no", "yes"),
ordered = TRUE)
dat$advise.contact.5 <- factor(dat$advise.contact.5, levels = c("no", "yes"),
ordered = TRUE)
dat$advise.contact.6 <- factor(dat$advise.contact.6, levels = c("no", "yes"),
ordered = TRUE)
dat$motivation.contact.1 <- factor(dat$motivation.contact.1, levels = c("no",
"yes"), ordered = TRUE)
dat$motivation.contact.2 <- factor(dat$motivation.contact.2, levels = c("no",
"yes"), ordered = TRUE)
dat$motivation.contact.3 <- factor(dat$motivation.contact.3, levels = c("no",
"yes"), ordered = TRUE)
dat$motivation.contact.4 <- factor(dat$motivation.contact.4, levels = c("no",
"yes"), ordered = TRUE)
dat$motivation.contact.5 <- factor(dat$motivation.contact.5, levels = c("no",
"yes"), ordered = TRUE)
dat$motivation.contact.6 <- factor(dat$motivation.contact.6, levels = c("no",
"yes"), ordered = TRUE)
dat$abstinence.1 <- factor(dat$abstinence.1)
dat$abstinence.2 <- factor(dat$abstinence.2)
dat$abstinence.3 <- factor(dat$abstinence.3)
dat$abstinence.4 <- factor(dat$abstinence.4)
dat$abstinence.5 <- factor(dat$abstinence.5)
dat$abstinence.6 <- factor(dat$abstinence.6)
dat$dur.abstinence.1 <- dat$dur.abstinence.1/12
dat$dur.abstinence.2 <- dat$dur.abstinence.2/12
dat$dur.abstinence.3 <- dat$dur.abstinence.3/12
dat$dur.abstinence.4 <- dat$dur.abstinence.4/12
dat$dur.abstinence.5 <- dat$dur.abstinence.5/12
dat$dur.abstinence.6 <- dat$dur.abstinence.6/12
n <- nrow(dat)
dat <- dat %>% mutate(id = 91:91 + n, trans.0 = rep(1, times = n), age.trans.0 = rep(0,
times = n))
# Transition 1, 1 ->2 Nondrinking to drinking
dat <- dat %>% mutate(trans.1 = rep(2, times = n), age.trans.1 = age.first.use) %>%
select(-age.first.use)
# Transition 2, 2 ->3 Drinking to drinking with problems (using problem.1)
dat <- dat %>% mutate(trans.2 = rep(3, times = n), age.trans.2 = age.problem.1,
trans.2 = ifelse(is.na(age.problem.1), NA, 3))
# Transition 3, 3 ->4 First Abstinence
dat <- dat %>% rename(type.prb.trans.3 = type.problem.1, type.cont.trans.3 = type.contact.1,
adv.cont.trans.3 = advise.contact.1, motiv.cont.trans.3 = motivation.contact.1)
dat <- dat %>% mutate(trans.3 = ifelse(abstinence.1 == "yes", 4, NA), age.trans.3 = ifelse(is.na(trans.3),
NA, age.problem.1 + 0.019)) %>% mutate(age.trans.3 = ifelse((age.current -
age.trans.3) > 0, age.trans.3, NA), trans.3 = ifelse((age.current - age.trans.3) >
0, trans.3, NA))
# Transition 4, 4 ->3 or 4 ->2 or nil, First Relapse
dat <- dat %>% mutate(age.trans.4 = ifelse(abstinence.1 == "no", NA, age.trans.3 +
dur.abstinence.1), trans.4 = ifelse(is.na(age.trans.4), NA, 3))
# Transition 5: NA, 4,. Second Abstinence
dat <- dat %>% rename(type.prb.trans.5 = type.problem.2, type.cont.trans.5 = type.contact.2,
adv.cont.trans.5 = advise.contact.2, motiv.cont.trans.5 = motivation.contact.2)
dat <- dat %>% mutate(trans.5 = ifelse((is.na(trans.4) | abstinence.2 == "no"),
NA, 4), age.trans.5 = ifelse(is.na(trans.5), NA, age.problem.2 + 0.019)) %>%
mutate(age.trans.5 = ifelse((age.current - age.trans.5) > 0, age.trans.5,
NA), trans.5 = ifelse((age.current - age.trans.5) > 0, trans.5, NA))
# Transition 6: Second Relapse, NA|3|2
dat <- dat %>% mutate(age.trans.6 = ifelse(abstinence.2 == "no", NA, age.trans.5 +
dur.abstinence.2), trans.6 = (ifelse(is.na(age.trans.6), NA, 3)))
# Transition 7: Third Abstinence, NA|4
dat <- dat %>% rename(type.prb.trans.7 = type.problem.3, type.cont.trans.7 = type.contact.3,
adv.cont.trans.7 = advise.contact.3, motiv.cont.trans.7 = motivation.contact.3)
dat$age.problem.3 <- as.numeric(dat$age.problem.3)
dat <- dat %>% mutate(trans.7 = ifelse((is.na(trans.6) | abstinence.3 == "no"),
NA, 4), age.trans.7 = ifelse(is.na(trans.7), NA, age.problem.3 + 0.019)) %>%
mutate(age.trans.7 = ifelse((age.current - age.trans.7) > 0, age.trans.7,
NA), trans.7 = ifelse((age.current - age.trans.7) > 0, trans.7, NA))
# Transition 8: Third Relapse, NA|3|2
dat <- dat %>% mutate(age.trans.8 = ifelse(abstinence.3 == "no", NA, age.trans.7 +
dur.abstinence.3), trans.8 = (ifelse(is.na(age.trans.8), NA, 3)))
# Transition 9: Fourth Abstinence, NA|4
dat <- dat %>% rename(type.prb.trans.9 = type.problem.4, type.cont.trans.9 = type.contact.4,
adv.cont.trans.9 = advise.contact.4, motiv.cont.trans.9 = motivation.contact.4)
dat <- dat %>% mutate(trans.9 = ifelse((is.na(trans.8) | abstinence.4 == "no"),
NA, 4), age.trans.9 = ifelse(is.na(trans.9), NA, age.problem.4 + 0.019)) %>%
mutate(age.trans.9 = ifelse((age.current - age.trans.9) > 0, age.trans.9,
NA), trans.9 = ifelse((age.current - age.trans.9) > 0, trans.9, NA))
# Transition 10: Fourth Relapse, NA|3|2
dat <- dat %>% mutate(age.trans.10 = ifelse(abstinence.4 == "no", NA, age.trans.9 +
dur.abstinence.4), trans.10 = (ifelse(is.na(age.trans.10), NA, 3)))
# Transition 11: Fifth Abstinence, NA|4
dat <- dat %>% rename(type.prb.trans.11 = type.problem.5, type.cont.trans.11 = type.contact.5,
adv.cont.trans.11 = advise.contact.5, motiv.cont.trans.11 = motivation.contact.5)
dat <- dat %>% mutate(trans.11 = ifelse((is.na(trans.10) | abstinence.5 == "no"),
NA, 4), age.trans.11 = ifelse(is.na(trans.10), NA, age.problem.5 + 0.019)) %>%
mutate(age.trans.11 = ifelse((age.current - age.trans.11) > 0, age.trans.11,
NA), trans.11 = ifelse((age.current - age.trans.11) > 0, trans.11, NA))
# Transition 12: Fifth Relapse, NA|3|2
dat <- dat %>% mutate(age.trans.12 = ifelse(abstinence.4 == "no", NA, age.trans.10 +
dur.abstinence.4), trans.12 = (ifelse(is.na(age.trans.12), NA, 3)))
# Transition 13: Sixth Abstinence, NA|4
dat <- dat %>% rename(type.prb.trans.13 = type.problem.6, type.cont.trans.13 = type.contact.6,
adv.cont.trans.13 = advise.contact.6, motiv.cont.trans.13 = motivation.contact.6)
dat <- dat %>% mutate(trans.13 = ifelse((is.na(trans.12) | abstinence.6 == "no"),
NA, 4), age.trans.13 = ifelse(is.na(trans.13), NA, age.problem.6 + 0.019)) %>%
mutate(age.trans.13 = ifelse((age.current - age.trans.13) > 0, age.trans.13,
NA), trans.13 = ifelse((age.current - age.trans.13) > 0, trans.13, NA))
nm <- names(dat)
trans_var <- grep("trans.",nm)
age_var <- grep("age.trans.",nm)
moti_var <- grep("motiv.cont.trans.",nm)
adv_var <- grep("adv.cont.trans.",nm)
prb_var <- grep("type.prb.trans.",nm)
cont_var <- grep("type.cont.trans.",nm)
dat1 <- dat %>% select(id,
name,
age.current,
age.ads,
trans_var,
age_var,
moti_var,
adv_var,
prb_var,
cont_var)
dat2<- dat1 %>% pivot_longer(cols = starts_with("trans."),
names_to = c("transition.number"),
values_to = c("state"),
values_drop_na = TRUE, # WORKS AND WORKS FOR ALL SCENARIOS
names_prefix = "trans." )%>% select(id,name,state,transition.number,everything()) %>%
arrange(id,transition.number)
dat3 <- dat1 %>% pivot_longer(cols = starts_with("age.trans."),
names_to = "transition.number.1",
values_to = "age",
values_drop_na = TRUE,
names_prefix = "age.trans.") %>%
select(id, name, age,transition.number.1,everything()) %>%
arrange(id,transition.number.1)
dat2$age <- dat3$age # Age added
dat2 <- dat2 %>% select(id,name,age,everything())
## Adding the last transition with current age.
trans.14 <- dat2 %>% group_by(id) %>%
summarise(N = which.max(age),"trans.n" = state[N]) %>%
select(trans.n)
trans.14 <- as.numeric(trans.14$trans.n)
dat1$trans.14 <- trans.14
dat1$age.trans.14 <- dat1$age.current
dat$trans.14 <- trans.14
dat$age.trans.14 <- dat1$age.current
saveRDS(dat,file = file.path(data.path,"Broad_OP.Rdata"))
dat1$state.max <- trans.14 # The max state varies by patient
dat2<- dat1 %>% pivot_longer(cols = starts_with("trans."),
names_to = c("transition.number"),
values_to = c("state"),
values_drop_na = TRUE, # Converting to long format again
names_prefix = "trans." )%>% select(id,name,state,transition.number,everything()) %>%
arrange(id,transition.number)
dat3 <- dat1 %>% pivot_longer(cols = starts_with("age.trans."),
names_to = "transition.number.1",
values_to = "age",
values_drop_na = TRUE,
names_prefix = "age.trans.") %>%
select(id, name, age,transition.number.1,everything()) %>%
arrange(id,transition.number.1)
dat2$age <- dat3$age # Age added
dat2 <- dat2 %>% select(id,name,age,everything())
saveRDS(dat2,file = file.path(data.path,"Final_MSM_OP.RData"))
remove(list = c("dat1","dat2","dat3","dat","tbl"))
long_IP <- readRDS(file = file.path(data.path, "Final_MSM_IP.Rdata"))
long_OP <- readRDS(file = file.path(data.path, "Final_MSM_OP.Rdata"))
dat_long <- rbind(long_IP, long_OP)
dat_long <- dat_long %>% mutate(firstobs = ifelse(transition.number == 0, 1,
0))
saveRDS(dat_long, file = file.path(data.path, "Long.Rdata"))
broad_IP <- readRDS(file = file.path(data.path, "Broad_IP.Rdata"))
broad_OP <- readRDS(file = file.path(data.path, "Broad_OP.Rdata"))
dat_broad <- rbind(broad_IP, broad_OP)
saveRDS(dat_broad, file = file.path(data.path, "Broad.Rdata"))
remove(list = c("long_IP", "long_OP", "broad_IP", "broad_OP"))