# NOT RUN {
# }
# NOT RUN {
#Get a sequence of years from "2000-01-01" to "2005-01-01"
TotalDateRange <- seq(as.Date("2000-01-01"), as.Date("2005-01-01"), "mon")
TotalDateRange_Yr <- seq(as.Date("2000-01-01"), as.Date("2005-01-01"), "years")
#Set parameters
snapshots <- 12 #monthly transition matrices
interval <- 1/12 #1 month transition matrices
#define list to hold initial counts and transition counts
lstCnt <-rep(list(list()), length(TotalDateRange_Yr))
lstInit <-rep(list(list()), length(TotalDateRange_Yr))
#initialize counters
nn <- 1
k <- 1
kk <- 1
#Create transition and initial counts (start vector) used as inputs to the function
for (l in 1:(length(TotalDateRange)-1)){
istartDate = POSIXTomatlab(as.POSIXlt(as.Date(TotalDateRange[l],format = "%Y-%m-%d")))
iendDate = POSIXTomatlab(as.POSIXlt(as.Date(TotalDateRange[l+1],format = "%Y-%m-%d")))
DateRange <- as.Date(matlabToPOSIX(cfdates(istartDate,iendDate,snapshots)))
for(i in 1:(length(DateRange)-1)){
sDate <- DateRange[i]
eDate <- DateRange[i+1]
Sample1<-TransitionProb(data,sDate, eDate, 'cohort', snapshots, interval)
lstCnt[[k]][[kk]] <- Sample1$sampleTotals$totalsMat
lstInit[[k]][[kk]] <- Sample1$sampleTotals$totalsVec
#print(lstCnt)
if(kk>=snapshots){
kk <- 1
k <- k+1
} else {
kk <- kk+1
}
}
}
#average of monthly transition matrices using the monthly initial portfolio
#counts (start vector) and transition counts.
#remove empty elements from the lists (lstCnt and lstInit)
lstInit <- lstInit[lapply(lstInit,length)>0]
lstCnt <- lstCnt[lapply(lstCnt,length)>0]
transtype <- "AverageMonthlyTransitions" #averaging method
gYear <- length(lapply(lstCnt,length)) #Add Error Checking for this
gHorizon <- snapshots #number of months in the horizon (default= 12)
transMatTest <- cohort.TTC(lstCnt,lstInit,gYear,gHorizon,transtype)
# }
Run the code above in your browser using DataLab