# NOT RUN {
##########
# Example 1: Coding trans, post and time.a in data where
# every event occurs at the same time for each person
##########
# Read data from library
data(tankdat)
# Add a marker (1 or 0) indicating an event
tankdat$taskchange<-0
tankdat$taskchange[tankdat$TIME==6]<-1
# Run function with defaults
OUT<-with(tankdat,dgm.code(ID,TIME,taskchange))
names(OUT)
names(tankdat)
# Merge original data and dgm codes and reorder
tankdat.dgm<-merge(tankdat,OUT,by.x=c("ID","TIME"),by.y=c("grp","time"))
tankdat.dgm<-tankdat.dgm[order(tankdat.dgm$ID,tankdat.dgm$TIME),]
# Examine data
tankdat.dgm[1:12,]
##########
# Example 2: Coding trans, post and time.a in data where every transition
# event occurs at the different times for each person
##########
# Read data from library
data(tankdat)
# Add a marker (1 or 0) indicating an event at random
set.seed(343227)
tankdat$taskchange<-rbinom(nrow(tankdat),1,prob=.1)
tankdat[1:24,] #ID 1 had one event at TIME 10. ID 2 had 3 events
# Run function with defaults
# }
# NOT RUN {
OUT<-with(tankdat,dgm.code(ID,TIME,taskchange))
# }
# NOT RUN {
# returns an error showing the 24 groups that started with an event.
# Either drop these groups or change the first.obs option to TRUE
# which changes these first events to 0 (non-events)
OUT<-with(tankdat,dgm.code(ID,TIME,taskchange,first.obs=TRUE))
OUT[1:24,]
OUT[OUT$grp==9,]
#Notice the event.first value of 1 for group 9 indicating that the
#first value was present and recoded.
# In the default setting, one ID had 4 events. It may be preferable
# to restrict the number of events to 3 or more and code accordingly
OUT<-with(tankdat,dgm.code(ID,TIME,taskchange,n.events=3,first.obs=TRUE))
OUT[1:24,]
# }
Run the code above in your browser using DataLab