+
+median_time_to_resolve_window <- function (t, from, to, window, fmt="%b")
+{
+ # find 'type' range of days
+ dates <-seq(as.Date(from), as.Date(to), 'week')
+ months <- format(dates, fmt)
+ hbreaks<-unclass(as.POSIXct(dates))
+
+ xx<-NULL;
+ yy<-NULL;
+ yy_sd_high<-NULL;
+ yy_sd_low<-NULL;
+ date_index <- NULL;
+ q_list <- NULL;
+
+ x<-seq(-20,20,0.01)
+ for ( i in seq(1,length(hbreaks)-window-1) )
+ {
+ print (sprintf("round %s of %s", i, length(hbreaks)-window-1))
+ # get range from t
+ t_sub <- t[which(t$start > hbreaks[i] & t$start<= hbreaks[i+window]),]
+ if ( length(t_sub$start) <= 1 ) { next }
+ # take log, then sn.mle -> h
+ d <- (t_sub$lastreply - t_sub$start)/(60*60) # hours
+ d <- log(d) # log(hours)
+ # sn.mle
+ print (sprintf("length: %s", length(d)))
+ q<-quantile(d)
+ print(q)
+
+ date_index <- c(date_index, round(i+window/2))
+
+ xx<- c(xx, hbreaks[round(i+window/2)])
+ q_list <- rbind(q_list, q)
+
+ }
+ m<- months[date_index]
+ return (cbind(xx,q_list, m))
+}
+mean_time_to_resolve_window <- function (t, from, to, window, fmt="%b")
+{
+ # find 'type' range of days
+ dates <-seq(as.Date(from), as.Date(to), 'week')
+ months <- format(dates, fmt)
+ hbreaks<-unclass(as.POSIXct(dates))
+
+ xx<-NULL;
+ yy<-NULL;
+ yy_sd_high<-NULL;
+ yy_sd_low<-NULL;
+ date_list <- NULL;
+
+ x<-seq(-20,20,0.01)
+ for ( i in seq(1,length(hbreaks)-window-1) )
+ {
+ print (sprintf("round %s of %s", i, length(hbreaks)-window-1))
+ # get range from t
+ t_sub <- t[which(t$start > hbreaks[i] & t$start<= hbreaks[i+window]),]
+ if ( length(t_sub$start) <= 1 ) { next }
+ # take log, then sn.mle -> h
+ d <- (t_sub$lastreply - t_sub$start)/(60*60) # hours
+ d <- log(d) # log(hours)
+ # sn.mle
+ print (sprintf("length: %s", length(d)))
+ avg<-mean(d)
+ s<-sd(d)
+ r<-shapiro.test(d) #, mean=avg, sd=s)
+ if ( r$statistic < 0.9 ){
+ print (r);
+ }
+
+ m<-dnorm(x, mean=avg, sd=s)
+ print(avg)
+ # find max of y
+ y_peak <- x[which(m==max(m))]
+ print(y_peak)
+ # plot point date, max(y)
+ xx<- c(xx, hbreaks[round(i+window/2)])
+ yy<- c(yy, y_peak)
+ yy_sd_high<- y_peak + s
+ yy_sd_low <- y_peak - s
+ date_list <- c(date_list, dates[i])
+ # plot whisker2(x0,y0,y0_hi,y0_lo)
+ }
+ l<-length(months)-window-1
+ m<- months[1:l]
+ return (rbind(xx,yy,yy_sd_high, yy_sd_low, m))
+}
+require(sn)
+sknorm_time_to_resolve_window <- function (t, from, to, window, fmt="%b")
+{
+ # find 'type' range of days
+ dates <-seq(as.Date(from), as.Date(to), 'week')
+ months <- format(dates, fmt)
+ hbreaks<-unclass(as.POSIXct(dates))
+
+ xx<-NULL;
+ yy<-NULL;
+ yy_sd_high<-NULL;
+ yy_sd_low<-NULL;
+ date_list <- NULL;
+
+ x<-seq(-20,20,0.01)
+ for ( i in seq(1,length(hbreaks)-window-1) )
+ {
+ print (sprintf("round %s of %s", i, length(hbreaks)-window-1))
+ # get range from t
+ t_sub <- t[which(t$start > hbreaks[i] & t$start<= hbreaks[i+window]),]
+ if ( length(t_sub$start) <= 1 ) { next }
+ # take log, then sn.mle -> h
+ d <- (t_sub$lastreply - t_sub$start)/(60*60) # hours
+ d <- log(d) # log(hours)
+ # sn.mle
+ print (sprintf("length: %s", length(d)))
+ h<-sn.em(y=d)
+ if ( abs(h$cp['skewness']) > 0.95 )
+ {
+ print(h)
+ next # just skip it
+ }
+
+ # find dsn() using h parameters -> y
+ m<-dsn(x, dp=cp.to.dp(h$cp))
+ # find max of y
+ y_peak <- x[which(m==max(m))]
+ # plot point date, max(y)
+ xx<- c(xx, hbreaks[round(i+window/2)])
+ yy<- c(yy, y_peak)
+ yy_sd_high<- y_peak + h$cp['s.d.']
+ yy_sd_low <- y_peak - h$cp['s.d.']
+ date_list <- c(date_list, dates[i])
+ # plot whisker2(x0,y0,y0_hi,y0_lo)
+ }
+ l<-length(months)-window-1
+ m<- months[1:l]
+ return (rbind(xx,yy,yy_sd_high, yy_sd_low, m))
+}
+
+# NOTE: Try something simpler, like median of the log of ttr.
+# it's going to be a lot of work to explain lsn distributions. something
+# more obvious would be a lot easier.
+
+par(mfrow=c(4,1))
+par(mai=c(.3,0.3,0.3,0.3))
+for ( s in c(7)) #,14,21) )
+{
+ d<- median_time_to_resolve_window(t2, "2004/1/1", "2010/2/28", s, "%b%y")
+ plot(d[,1], exp(as.numeric(d[,5]))/24, type='l', lty=1, xlab="",
+ axes=F, ylim=c(0.01, 15), ylab="Days to Resolve", col='orange')
+ lines(d[,1], exp(as.numeric(d[,4]))/24, lty=1, col='red')
+ lines(d[,1], exp(as.numeric(d[,3]))/24, lty=1, col='black')
+ axis(1, labels=d[,7], at=d[,1])
+ axis(2, las=1)
+ m<-round(max(exp(as.numeric(d[,4]))/24), 2)
+ axis(2, labels=m, at=m, las=1)
+ abline(h=m, lty=2, col='grey40')
+}
+
+# monitor
+ d2<- median_time_to_resolve_window(m2, "2007/02/1", "2010/2/28", s, "%b%y")
+ plot(d[,1], exp(as.numeric(d[,2]))/24, type='l', lty=1, xlab="",
+ axes=F, ylim=c(0.01, 165), ylab="Days to Resolve", col='white')
+ lines(d2[,1], exp(as.numeric(d2[,5]))/24, lty=1, col='red')
+ lines(d2[,1], exp(as.numeric(d2[,4]))/24, lty=1, col='red')
+ lines(d2[,1], exp(as.numeric(d2[,3]))/24, lty=1, col='black')
+ axis(1, labels=d[,7], at=d[,1])
+ axis(2, las=1)
+ m<-round(max(exp(as.numeric(d2[,4]))/24), 2)
+ axis(2, labels=m, at=m, las=1)
+ abline(h=m, lty=2, col='grey40')
+
+
+
+mean_time_to_resolve <- function (t, from, to, type, fmt="%b")
+{
+ # find 'type' range of days
+ dates <-seq(as.Date(from), as.Date(to), type)
+ months <- format(dates, fmt)
+ hbreaks<-unclass(as.POSIXct(dates))
+
+ xx<-NULL;
+ yy<-NULL;
+ yy_sd_high<-NULL;
+ yy_sd_low<-NULL;
+ date_list <- NULL;
+
+ for ( i in seq(1,length(hbreaks)-1) )
+ {
+ # get range from t
+ t_sub <- t[which(t$start > hbreaks[i] & t$start<= hbreaks[i+1]),]
+ if ( length(t_sub$start) == 0 ) { next }
+ # take log, then sn.mle -> h
+ d <- (t_sub$lastreply - t_sub$start)/(60*60) # hours
+ d <- log(d) # log(hours)
+ # sn.mle
+ h<-sn.em(y=d)
+ if ( abs(h$cp['skewness']) > 0.95 )
+ {
+ print(h)
+ }
+
+ # find dsn() using h parameters -> y
+ x<-seq(-8,10,0.01)
+ m<-dsn(x, dp=cp.to.dp(h$cp))
+ # find max of y
+ y_peak <- x[which(m==max(m))]
+ # plot point date, max(y)
+ xx<- c(xx, hbreaks[i])
+ yy<- c(yy, y_peak)
+ yy_sd_high<- y_peak + h$cp['s.d.']
+ yy_sd_low <- y_peak - h$cp['s.d.']
+ date_list <- c(date_list, dates[i])
+ # plot whisker2(x0,y0,y0_hi,y0_lo)
+ }
+ m<- months[1:length(months)-1]
+ return (rbind(xx,yy,yy_sd_high, yy_sd_low, m))
+}
+
+
+par(mfrow=c(5,1))
+par(mai=c(.3,0.3,0.3,0.3))
+for ( s in c("10 days", "2 weeks", "3 weeks", "month", "2 months"))
+#for ( s in c("month") )
+{
+ d<- mean_time_to_resolve(t2, "2004/1/1", "2010/2/28", s, "%b%y")
+ plot(d[1,], exp(as.numeric(d[2,]))/24, type='l', axes=F)
+ points(d[1,], exp(as.numeric(d[2,]))/24, pch=23)
+ axis(1, labels=d[5,], at=d[1,])
+ axis(2)
+}
+
+
+