PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/myfunctions.R

https://bitbucket.org/adni/adni.bitbucket.org
R | 1350 lines | 932 code | 260 blank | 158 comment | 152 complexity | 5f09ad7f08b55b4024ae6d6f0a5260f7 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. summary.default <- function (object, ..., digits = max(3, getOption("digits") -
  2. 3), useNA = c("ifany", "no", "always"))
  3. {
  4. useNA <- match.arg(useNA)
  5. if (is.factor(object)) {
  6. return(summary.factor(object, ...))
  7. }
  8. else if (is.matrix(object)) {
  9. return(summary.matrix(object, digits = digits, ...))
  10. }
  11. value <- if (is.logical(object)) {
  12. c(Mode = "logical", {
  13. tb <- table(object, exclude = NULL)
  14. if (!is.null(n <- dimnames(tb)[[1]]) && any(iN <- is.na(n))) dimnames(tb)[[1]][iN] <- "NA's"
  15. tb
  16. })
  17. }
  18. else if (is.numeric(object)) {
  19. nas <- is.na(object)
  20. object <- object[!nas]
  21. N <- length(object)
  22. qq <- stats::quantile(object)
  23. qq <- signif(c(N = N, qq[1:3], mean(object), qq[4:5],
  24. SD = sd(object), SE = sd(object)/sqrt(N)), digits)
  25. names(qq) <- c("N", "Min.", "Q.1", "Median", "Mean",
  26. "Q.3", "Max.", "SD", "SE")
  27. if ((any(nas) & useNA != "no") | useNA == "always")
  28. c(qq, "NA's" = sum(nas))
  29. else qq
  30. }
  31. else if (is.recursive(object) && !is.language(object) &&
  32. (n <- length(object))) {
  33. sumry <- array("", c(n, 3), list(names(object), c("Length",
  34. "Class", "Mode")))
  35. ll <- numeric(n)
  36. for (i in 1:n) {
  37. ii <- object[[i]]
  38. ll[i] <- length(ii)
  39. cls <- oldClass(ii)
  40. sumry[i, 2] <- if (length(cls) > 0)
  41. cls[1]
  42. else "-none-"
  43. sumry[i, 3] <- mode(ii)
  44. }
  45. sumry[, 1] <- format(as.integer(ll))
  46. sumry
  47. }
  48. else c(Length = length(object), Class = class(object), Mode = mode(object))
  49. class(value) <- "table"
  50. value
  51. }
  52. summary2matrix <- function(x, hsep="|", header.vsep = FALSE, ...){
  53. tf <- tempfile()
  54. trash <- capture.output(print(x, hsep = hsep, header.vsep = FALSE,
  55. top.border = FALSE, left.border = FALSE, vsep = '', csep = '',
  56. file = tf))
  57. tab <- read.table(tf, sep = hsep, as.is = TRUE)
  58. colnames(tab) <- tab[1, ]
  59. tab <- tab[-1, ]
  60. tab <- as.matrix(tab)
  61. rownames(tab)=rep("", nrow(tab))
  62. tab[1, ] <- paste(colnames(tab), tab[1, ], sep="\n")
  63. tab
  64. }
  65. longsums <- function(Data, oc = "ADAS13")
  66. {
  67. cat("\n\\clearpage\n")
  68. cat(paste("\\subsection{", label(Data[, oc]), "change from baseline}\n"))
  69. dd1 <- merge(subset(dd, RID > 2000 & VISCODE == "bl", c("RID", "DX")),
  70. Data[Data$RID > 2000, c("RID", "VISCODE", oc)], by = "RID", all.y = TRUE)
  71. dd1$Month <- ifelse(dd1$VISCODE %in% c("sc", "bl"), 0, as.numeric(gsub("m", "", dd1$VISCODE)))
  72. dd1$Y <- dd1[, oc]
  73. dd1 <- merge(dd1, subset(dd1, Month == 0, c("RID", "Y")), by = "RID",
  74. all.x = TRUE, suffixes = c("", ".0"))
  75. dd1$Y.change <- with(dd1, Y - Y.0)
  76. s <- with(dd1, Hmisc::summarize(Y.change,
  77. by = llist(Month, DX),
  78. stat.name = 'Change',
  79. function(x) c(smean.cl.normal(x), N = sum(!is.na(x)))))
  80. s <- s[order(s$DX, s$Month), ]
  81. s <- subset(s, !is.na(DX) & N > 4)
  82. m <- length(unique(s$Month))
  83. n <- length(unique(s$DX))
  84. Palette <- brewer.pal(n, "Set1")
  85. blank.pic <- ggplot(s, aes(Month, Change)) +
  86. geom_blank() + theme_bw() +
  87. theme(axis.text.x = element_blank(),axis.text.y = element_blank(),
  88. axis.title.x = element_blank(),axis.title.y = element_blank(),
  89. axis.ticks = element_blank(),
  90. panel.grid.major = element_blank(),panel.border = element_blank())
  91. p <- ggplot(s, aes(Month, Change, group = DX, color = DX)) +
  92. geom_line() +
  93. geom_smooth(aes(ymax = Upper, ymin = Lower),
  94. size = 1.5, stat = 'identity') +
  95. ylab(paste(label(Data[, oc]), "change")) +
  96. scale_x_continuous(breaks = unique(dd1$Month)) +
  97. scale_colour_manual(values=Palette) +
  98. ggtitle("Mean and 95% CI") + theme(legend.position = "none")
  99. data.table <- ggplot(s, aes(x = Month, y = DX, label = format(N, nsmall = 0), color = DX)) +
  100. geom_text() + theme_bw() +
  101. scale_colour_manual(values=Palette) +
  102. scale_y_discrete(breaks = levels(s$DX),
  103. labels = levels(s$DX)) +
  104. theme(axis.title.x = element_text(vjust = 1),
  105. panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
  106. panel.border = element_blank(),axis.text.x = element_blank(),
  107. axis.ticks = element_blank(),
  108. axis.text.y = element_text(hjust = 1, face = "bold", color = Palette)) +
  109. theme(legend.position = "none") + xlab("") + ylab(NULL) +
  110. theme(plot.margin = unit(c(-1.5, 1, 0.1, ifelse(m < 10, 1.5, 2.5) - 0.28 * m), "lines"))
  111. # ADJUST POSITION OF TABLE FOR AT RISK
  112. grid.arrange(p, blank.pic, data.table, clip = FALSE, nrow = 3,
  113. ncol = 1, heights = unit(c(2, .1, .5),c("null", "null", "null")))
  114. s <- with(dd1, Hmisc::summarize(Y.change,
  115. by = llist(Month, DX),
  116. stat.name = 'N',
  117. function(x) summary(x)[c('N', 'Min.', 'Q.1', 'Median', 'Mean', 'Q.3', 'Max.', 'SD')]))
  118. tab <- s[order(s$DX, s$Month), ]
  119. tab <- subset(tab, !is.na(DX) & Month != 0)
  120. rcmd <- rep("", nrow(tab))
  121. rcmd[seq(1, length(rcmd), by = 2)] <- "rowcolor[gray]{.9}"
  122. tab[, 2] <- as.character(tab[, 2])
  123. latex(tab[, -(1:2)], file = "", rowlabel = "Month", rowname = tab[, 1],
  124. rgroup = unique(tab[, 2]), n.rgroup = table(tab[, 2]),
  125. digits = 3, where = 'h!', rownamesTexCmd = rcmd, label = paste("long", oc, sep=""))
  126. }
  127. make_first_to_final_trans <- function(RID, stage, Year){
  128. dd <- data.frame(RID = RID, stage = stage, Year = Year)
  129. ddf <- c()
  130. for(rid in unique(dd$RID)){
  131. subdd <- subset(dd, RID == rid)
  132. nr <- nrow(subdd)
  133. ddf <- rbind(ddf, c(id = rid, start = subdd$Year[1], stop = subdd$Year[nr],
  134. start.stage = subdd$stage[1],
  135. end.stage = subdd$stage[nr]))
  136. }
  137. data.frame(ddf)
  138. }
  139. make_all_intermediate_trans <- function(RID, stage, Year){
  140. dd <- data.frame(RID = RID, stage = stage, Year = Year)
  141. dd2 <- c()
  142. # id start stop start.stage end.stage
  143. for(rid in unique(dd$RID)){
  144. subdd <- subset(dd, RID == rid)
  145. if(nrow(subdd) > 1){
  146. start <- subdd$Year[1]
  147. start.stage <- subdd$stage[1]
  148. for(j in 1:(nrow(subdd)-1)){
  149. if(subdd$stage[j] != start.stage){
  150. dd2 <- rbind(dd2, c(id = rid, start = start, stop = subdd$Year[j],
  151. start.stage = start.stage, end.stage = subdd$stage[j]))
  152. start.stage <- subdd$stage[j]
  153. }
  154. start <- subdd$Year[j]
  155. }
  156. dd2 <- rbind(dd2, c(id = rid, start = start, stop = subdd$Year[j+1],
  157. start.stage = start.stage,
  158. end.stage = ifelse(start.stage == subdd$stage[j+1], 0, subdd$stage[j+1])))
  159. }
  160. }
  161. data.frame(dd2)
  162. }
  163. obj_memory <- function(x){
  164. EDU <- x[1]
  165. LM <- x[2]
  166. if(is.na(EDU) | is.na(LM)) return(NA)
  167. if(EDU >= 16){
  168. if(LM <= 8) return("LMCI")
  169. if(LM <= 11) return("EMCI")
  170. if(LM > 11) return("NL")
  171. }
  172. if(EDU <= 15 & EDU >= 8){
  173. if(LM <= 4) return("LMCI")
  174. if(LM <= 9) return("EMCI")
  175. if(LM > 9) return("NL")
  176. }
  177. if(EDU <= 7){
  178. if(LM <= 2) return("LMCI")
  179. if(LM <= 6) return("EMCI")
  180. if(LM > 6) return("NL")
  181. }
  182. }
  183. ####################################################################
  184. ## My Plot Method ##
  185. ####################################################################
  186. my_msSurv_plot <- function (x, states="ALL", trans="ALL", plot.type="stateocc",
  187. CI=TRUE, ci.level=0.95, ci.trans="linear",
  188. state.names=NULL, timelab="Event Times",
  189. legend.title = "Stage", ...) {
  190. require(ggplot2)
  191. plot.type <- match.arg(plot.type, c("stateocc", "transprob","entry.sub",
  192. "entry.norm","exit.sub","exit.norm"))
  193. ####################################################################
  194. ## State occupation probabilities plot
  195. ####################################################################
  196. if (plot.type=="stateocc") {
  197. CIs <- MSM.CIs(x, ci.level=0.95) ## Calling CIs
  198. if (states[1]=="ALL") states <- nodes(tree(x))
  199. if(is.null(state.names)){
  200. labels <- unique(sort(f.st))
  201. }else{
  202. labels <- state.names
  203. }
  204. f.st <- factor(states, labels = labels)
  205. ls <- length(states)
  206. sl <- which(nodes(tree(x))%in%as.numeric(states)) ## location of states in the matrix
  207. if (CI==TRUE & !is.null(var.sop(x))) {
  208. rd <- CIs$CI.p
  209. dimnames(rd)$state <- gsub("p", "State", dimnames(rd)$state)
  210. y <- as.vector(rd[,1,sl])
  211. y2 <- as.vector(rd[,2,sl]) ## lower limit
  212. y3 <- as.vector(rd[,3,sl]) ## upper limit
  213. xvals <- rep(et(x), length(states))
  214. f.st <- factor(rep(dimnames(rd)$state[sl], each=dim(rd)[1]), labels = labels)
  215. # st.plot <- xyplot(y + y2 + y3 ~ xvals | f.st, allow.multiple=TRUE,
  216. # type="s", lty=c(1,2,2), col=c(1,2,2), ...)
  217. # st.plot <- update(st.plot, main="Plot of State Occupation Probabilites",
  218. # xlab="Event Times", ylab="State Occupation Probabilities",
  219. # key = list(lines=list(col=c(1, 2, 2), lty=c(1, 2, 2)),
  220. # text=list(c("Est", "Lower CI", "Upper CI")),
  221. # columns=3))
  222. st.plot <- ggplot(data.frame(xvals, y, y2, y3, f.st),
  223. aes(xvals, y, color = f.st))+
  224. geom_line() +
  225. geom_smooth(aes(ymax = y3, ymin = y2), stat = 'identity')
  226. st.plot <- st.plot +
  227. ggtitle("Plot of State Occupation Probabilites") +
  228. xlab(timelab) + ylab("State Occupation Probabilities") +
  229. labs(color = legend.title)
  230. print(st.plot)
  231. } else {
  232. if (CI==TRUE)
  233. cat("Warning: 'var.sop' is NULL and therefore CIs not plotted. \n")
  234. rd <- CIs$CI.p
  235. dimnames(rd)$state <- gsub("p", "State", dimnames(rd)$state)
  236. y <- as.vector(rd[,1,sl])
  237. xvals <- rep(et(x), length(states))
  238. f.st <- as.factor(rep(dimnames(rd)$state[sl], each=dim(rd)[1]))
  239. st.plot <- xyplot(y ~ xvals | f.st, type="s",col=1, ...)
  240. st.plot <- update(st.plot, main="Plot of State Occupation Probabilites",
  241. xlab="Event Times", ylab="State Occupation Probabilities",
  242. key = list(lines=list(col=c(1), lty=c(1)), text=list(c("Est"))))
  243. print(st.plot)
  244. } ## end of no CIs
  245. } ## end of state occ plot
  246. ####################################################################
  247. ## Transition probabilities plot
  248. ####################################################################
  249. if (plot.type=="transprob") {
  250. CIs <- MSM.CIs(x, ci.level, ci.trans, sop=FALSE) ## Calling CIs
  251. all.trans <- pos.trans(x)
  252. if (trans[1] =="ALL") trans <- all.trans
  253. rd <- CIs$CI.trans
  254. dimnames(rd)$trans <- gsub(" ", " to ", dimnames(rd)$trans)
  255. if(!is.null(state.names)){
  256. for(st in length(state.names):1){
  257. dimnames(rd)$trans <- gsub(st, state.names[st], dimnames(rd)$trans)
  258. }
  259. }
  260. tr <- which(all.trans%in%trans) ## location of states in the matrix
  261. if (CI==TRUE & !is.null(cov.AJs(x))) {
  262. y <- as.vector(rd[,1,tr])
  263. y2 <- as.vector(rd[,2,tr]) ## lower limit
  264. y3 <- as.vector(rd[,3,tr]) ## upper limit
  265. xvals <- rep(et(x), length(tr))
  266. f.tp <- as.factor(rep(dimnames(rd)$trans[tr], each=dim(rd)[1]))
  267. # tr.plot <- xyplot(y + y2 + y3 ~ xvals | f.tp, allow.multiple=TRUE,
  268. # type="s", lty=c(1,2,2), col=c(1,2,2),...)
  269. # tr.plot <- update(tr.plot, main="Plot of Transition Probabilites",
  270. # xlab="Event Times", ylab="Transition Probabilites",
  271. # key = list(lines=list(col=c(1, 2, 2), lty=c(1, 2, 2)),
  272. # text=list(c("Est", "Lower CI", "Upper CI")),
  273. # columns=3))
  274. tr.plot <- ggplot(data.frame(xvals, y, y2, y3, f.tp),
  275. aes(xvals, y, color = f.tp))+
  276. geom_line() +
  277. geom_smooth(aes(ymax = y3, ymin = y2), stat = 'identity')
  278. tr.plot <- tr.plot +
  279. ggtitle("Plot of Transition Probabilites") +
  280. xlab(timelab) + ylab("Transition Probabilites") +
  281. labs(color = legend.title)
  282. print(tr.plot)
  283. } else {
  284. if (CI==TRUE)
  285. cat("Warning: 'cov.AJs' is NULL and therefore CIs not plotted. \n")
  286. y <- as.vector(rd[,1,tr])
  287. xvals <- rep(et(x), length(tr))
  288. f.tp <- as.factor(rep(dimnames(rd)$trans[tr], each=dim(rd)[1]))
  289. tr.plot <- xyplot(y ~ xvals | f.tp, type="s", lty=1, col=1, ...)
  290. tr.plot <- update(tr.plot, main="Plot of Transition Probabilites",
  291. xlab="Event Times", ylab="Transition Probabilities",
  292. key = list(lines=list(col=1, lty=1), text=list("Est"), columns=1))
  293. print(tr.plot)
  294. }
  295. } ## end of 'transprob' plot
  296. ####################################################################
  297. ## Entry subdistribution function
  298. ####################################################################
  299. if (plot.type=="entry.sub") {
  300. enter <- names(which(!(sapply(inEdges(tree(x)), function(x) length(x) == 0))))
  301. if (states[1]=="ALL") states <- enter
  302. f.st <- factor(states)
  303. ls <- length(states)
  304. sl <- which(nodes(tree(x))%in%states) ## location of states in the matrix
  305. if (CI==TRUE & !is.null(Fsub.var(x))) {
  306. CIs <- Dist.CIs(x, ci.level, ci.trans, norm=FALSE) ## Calling CIs for subdistribution
  307. rd <- CIs$CI.Fs
  308. dimnames(rd)$state=gsub("F", "State", dimnames(rd)$state)
  309. y <- as.vector(rd[,1,sl])
  310. y2 <- as.vector(rd[,2,sl]) ## lower limit
  311. y3 <- as.vector(rd[,3,sl]) ## upper limit
  312. xvals <- rep(et(x), length(states))
  313. f.st <- as.factor(rep(dimnames(rd)$state[sl], each=dim(rd)[1]))
  314. ent.plot <- xyplot(y + y2 + y3 ~ xvals | f.st, allow.multiple=TRUE,
  315. type="s", lty=c(1,2,2), col=c(1,2,2), ...)
  316. ent.plot <- update(ent.plot, main="Plot of State Entry Time Subdistributions",
  317. xlab="Event Times", ylab="State Entry Time Subdistributions",
  318. key = list(lines=list(col=c(1, 2, 2), lty=c(1, 2, 2)),
  319. text=list(c("Est", "Lower CI", "Upper CI")),
  320. columns=3))
  321. print(ent.plot)
  322. } else {
  323. if (CI==TRUE)
  324. cat("Warning: 'Fsub.var' is NULL and therefore CIs not plotted. \n")
  325. rd <- Fsub(x)
  326. dimnames(rd)[[2]]=gsub("F", "State", dimnames(rd)[[2]])
  327. y <- as.vector(rd[,sl])
  328. xvals <- rep(et(x), length(states))
  329. f.st <- as.factor(rep(dimnames(rd)[[2]][sl], each=dim(rd)[1]))
  330. ent.plot <- xyplot(y ~ xvals | f.st, type="s", col=1, ...)
  331. ent.plot <- update(ent.plot, main="Plot of State Entry Time Subdistributions",
  332. xlab="Event Times", ylab="State Entry Time Subdistributions",
  333. key = list(lines=list(col=c(1), lty=c(1)), text=list(c("Est"))))
  334. print(ent.plot)
  335. }
  336. } ## end of entry subdistribution plot
  337. ####################################################################
  338. ## State entry distribution (normalized)
  339. ####################################################################
  340. if (plot.type=="entry.norm") {
  341. enter <- names(which(!(sapply(inEdges(tree(x)), function(x) length(x) == 0))))
  342. if (states[1]=="ALL") states <- enter
  343. f.st <- factor(states)
  344. ls <- length(states)
  345. sl <- which(nodes(tree(x))%in%states) ## location of states in the matrix
  346. if (CI==TRUE & !is.null(Fnorm.var(x))) {
  347. CIs <- Dist.CIs(x,ci.level,ci.trans,norm=TRUE) ## Calling CIs for normalized distribution
  348. rd <- CIs$CI.Fs
  349. dimnames(rd)$state=gsub("F", "State", dimnames(rd)$state)
  350. y <- as.vector(rd[,1,sl])
  351. y2 <- as.vector(rd[,2,sl]) ## lower limit
  352. y3 <- as.vector(rd[,3,sl]) ## upper limit
  353. xvals <- rep(et(x), length(states))
  354. f.st <- as.factor(rep(dimnames(rd)$state[sl], each=dim(rd)[1]))
  355. ent.plot <- xyplot(y + y2 + y3 ~ xvals | f.st, allow.multiple=TRUE,
  356. type="s",lty=c(1,2,2),col=c(1,2,2), ...)
  357. ent.plot <- update(ent.plot, main="Plot of Normalized State Entry Time Distributions",
  358. xlab="Event Times", ylab="Normalized State Entry Time Distributions",
  359. key = list(lines=list(col=c(1, 2, 2), lty=c(1, 2, 2)),
  360. text=list(c("Est", "Lower CI", "Upper CI")),
  361. columns=3))
  362. print(ent.plot)
  363. } else {
  364. if (CI==TRUE)
  365. cat("Warning: 'Fnorm.var' is NULL and therefore CIs not plotted. \n")
  366. rd <- Fnorm(x)
  367. dimnames(rd)[[2]]=gsub("F", "State", dimnames(rd)[[2]])
  368. y <- as.vector(rd[,sl])
  369. xvals <- rep(et(x), length(states))
  370. f.st <- as.factor(rep(dimnames(rd)[[2]][sl], each=dim(rd)[1]))
  371. ent.plot <- xyplot(y ~ xvals | f.st, type="s", col=1, ...)
  372. ent.plot <- update(ent.plot, main="Plot of Normalized State Entry Time Distributions",
  373. xlab="Event Times", ylab="Normalized State Entry Time Distributions",
  374. key = list(lines=list(col=c(1), lty=c(1)), text=list(c("Est"))))
  375. print(ent.plot)
  376. }
  377. } ## end of normalized entry distribution plot
  378. ####################################################################
  379. ## State exit subdistribution
  380. ####################################################################
  381. if (plot.type=="exit.sub") {
  382. transient <- names(which(sapply(edges(tree(x)), function(x) length(x) > 0)))
  383. if (states[1]=="ALL") states <- transient
  384. f.st <- factor(states)
  385. ls <- length(states)
  386. sl <- which(nodes(tree(x))%in%as.numeric(states)) ## location of states in the matrix
  387. if (CI==TRUE & !is.null(Gsub.var(x))) {
  388. CIs <- Dist.CIs(x,ci.level,ci.trans,norm=FALSE)
  389. ## Calling CIs, adding norm arg for subdistribution CIs
  390. rd <- CIs$CI.Gs
  391. dimnames(rd)$state=gsub("G", "State", dimnames(rd)$state)
  392. y <- as.vector(rd[,1,sl])
  393. y2 <- as.vector(rd[,2,sl]) ## lower limit
  394. y3 <- as.vector(rd[,3,sl]) ## upper limit
  395. xvals <- rep(et(x), length(states))
  396. f.st <- as.factor(rep(dimnames(rd)$state[sl], each=dim(rd)[1]))
  397. exit.plot <- xyplot(y + y2 + y3 ~ xvals | f.st, allow.multiple=TRUE,
  398. type="s", lty=c(1,2,2), col=c(1,2,2),...)
  399. exit.plot <- update(exit.plot, main="Plot of State Exit Time Distributions",
  400. xlab="Event Times", ylab="State Exit Time Distributions",
  401. key = list(lines=list(col=c(1, 2, 2), lty=c(1, 2, 2)),
  402. text=list(c("Est", "Lower CI", "Upper CI")),
  403. columns=3))
  404. print(exit.plot)
  405. } else {
  406. if (CI==TRUE)
  407. cat("Warning: 'Gsub.var' is NULL and therefore CIs not plotted. \n")
  408. rd <- Gsub(x)
  409. dimnames(rd)[[2]]=gsub("G", "State", dimnames(rd)[[2]])
  410. y <- as.vector(rd[,sl])
  411. xvals <- rep(et(x), length(states))
  412. f.st <- as.factor(rep(dimnames(rd)[[2]][sl], each=dim(rd)[1]))
  413. exit.plot <- xyplot(y ~ xvals | f.st, type="s", col=1)
  414. exit.plot <- update(exit.plot, main="Plot of State Exit Time Distributions",
  415. xlab="Event Times",ylab="State Exit Time Distributions",
  416. key = list(lines=list(col=c(1), lty=c(1)), text=list(c("Est"))))
  417. print(exit.plot)
  418. }
  419. } ## end of exit.sub
  420. ####################################################################
  421. ## State exit distribution (normalized)
  422. ####################################################################
  423. if (plot.type=="exit.norm") {
  424. transient <- names(which(sapply(edges(tree(x)), function(x) length(x) > 0)))
  425. if (states[1]=="ALL") states<-transient
  426. f.st <- factor(states)
  427. ls <- length(states)
  428. sl <- which(nodes(tree(x))%in%as.numeric(states)) ## location of states in the matrix
  429. if (CI==TRUE & !is.null(Gnorm.var(x))) {
  430. CIs <- Dist.CIs(x,ci.level,ci.trans,norm=TRUE) ## Calling CIs
  431. rd <- CIs$CI.Gs
  432. dimnames(rd)$state=gsub("G", "State", dimnames(rd)$state)
  433. y <- as.vector(rd[,1,sl])
  434. y2 <- as.vector(rd[,2,sl]) ## lower limit
  435. y3 <- as.vector(rd[,3,sl]) ## upper limit
  436. xvals <- rep(et(x), length(states))
  437. f.st <- as.factor(rep(dimnames(rd)$state[sl], each=dim(rd)[1]))
  438. exit.plot <- xyplot(y + y2 + y3 ~ xvals | f.st, allow.multiple=TRUE,
  439. type="s",lty=c(1,2,2), col=c(1,2,2), ...)
  440. exit.plot <- update(exit.plot, main="Plot of State Exit Time Distributions",
  441. xlab="Event Times", ylab="State Exit Time Distributions",
  442. key = list(lines=list(col=c(1, 2, 2), lty=c(1, 2, 2)),
  443. text=list(c("Est", "Lower CI", "Upper CI")),
  444. columns=3))
  445. print(exit.plot)
  446. } else {
  447. if (CI==TRUE)
  448. cat("Warning: 'Gnorm.var' is NULL and therefore CIs not plotted. \n")
  449. rd <- Gnorm(x)
  450. dimnames(rd)[[2]]=gsub("G", "State", dimnames(rd)[[2]])
  451. y <- as.vector(rd[,sl])
  452. xvals <- rep(et(x), length(states))
  453. f.st <- as.factor(rep(dimnames(rd)[[2]][sl], each=dim(rd)[1]))
  454. exit.plot <- xyplot(y ~ xvals | f.st, type="s",col=1, ...)
  455. exit.plot <- update(exit.plot, main="Plot of State Exit Time Distributions",
  456. xlab="Event Times", ylab="State Exit Time Distributions",
  457. key = list(lines=list(col=c(1), lty=c(1)), text=list(c("Est"))))
  458. print(exit.plot)
  459. }
  460. } ## end of exit.norm
  461. }
  462. ############################################################
  463. ## Adding Start Times ##
  464. ############################################################
  465. ## Adds starting time to Data
  466. Add.start <- function(Data) {
  467. Data$start <- 0
  468. idx <- which(table(Data$id)>1)
  469. for (i in names(idx)) {
  470. ab <- Data[which(Data$id==i), ]
  471. ab <- with(ab, ab[order(ab$stop), ])
  472. ab2 <- which(Data$id==i) ## row numbers in Data
  473. start2 <- vector(length=length(ab2))
  474. start2[1] <- 0
  475. start2[2:length(ab2)] <- ab$stop[1:length(ab2)-1]
  476. Data$start[ab2] <- start2
  477. } ## end of for loop
  478. new.data <- data.frame(id=Data$id, start=Data$start, stop=Data$stop,
  479. start.stage=Data$start.stage, end.stage=Data$end.stage)
  480. res <- new.data
  481. }
  482. ####################################################################
  483. ## Adding 'Dummy' States for Censoring and Left Truncation
  484. ####################################################################
  485. Add.States <- function(tree, LT) {
  486. ## Adding censoring state to Nodes & Edges
  487. Nodes <- c(nodes(tree), "0")
  488. Edges <- edges(tree)
  489. Edges[["0"]] <- character(0)
  490. nt.states <- names(which(sapply(Edges, function(x) length(x)>0))) ## nontermewinal states
  491. for (stage in nt.states) {
  492. Edges[[stage]] <- c("0", Edges[[stage]])
  493. }
  494. ## tree for censored data
  495. tree0 <- new("graphNEL", nodes=Nodes, edgeL=Edges, edgemode="directed")
  496. ## Adding "Left Truncated" State
  497. if (LT) {
  498. Nodes <- c(nodes(tree0), "LT")
  499. Edges[["LT"]] <- nt.states
  500. nt.states.LT <- names(which(sapply(Edges, function(x) length(x)>0))) ## nonterminal states
  501. treeLT <- new("graphNEL", nodes=Nodes, edgeL=Edges, edgemode="directed")
  502. }
  503. if (LT) {
  504. return(list(tree0=tree0, nt.states=nt.states, nt.states.LT=nt.states.LT, treeLT=treeLT))
  505. } else {
  506. return(list(tree0=tree0, nt.states=nt.states))
  507. }
  508. }
  509. ############################################################
  510. ## Adding Dummy "LT" obs to Data set ##
  511. ############################################################
  512. LT.Data <- function(Data) {
  513. Data <- Data[order(Data$id), ] ## make sure id's line up below
  514. ids <- unique(Data$id)
  515. stop.time <- with(Data, tapply(start, id, min))
  516. enter.st <- by(Data, Data$id, function(x) x$start.stage[which.min(x$start)])
  517. ## dummy initial stage
  518. dummy <- data.frame(id = ids, start = -1, stop = stop.time, start.stage="LT",
  519. end.stage=as.character(enter.st))
  520. Data <- rbind(Data, dummy)
  521. Data <- with(Data, Data[order(id, stop), ])
  522. return(Data=Data)
  523. }
  524. ############################################################
  525. ## Counting Process & At Risk ##
  526. ############################################################
  527. ## Assuming stage 0 is censored and stage 1 is the initial state
  528. ## state 'LT' for left truncated data
  529. CP <- function(tree, tree0, Data, nt.states) {
  530. ## tree0=tree for uncens, tree0=tree0 for cens, tree0=treeLT for LT
  531. ## NOTE - nt.states includes 'LT' for LT data
  532. ## unique stop times in entire data set
  533. ## Exclude stop times of zero (for LT data)
  534. times <- sort(unique(Data$stop[!Data$stop==0]))
  535. ## names for dNs transitions
  536. lng <- sapply(edges(tree0)[nodes(tree0)%in%nt.states], length)
  537. ds <- paste("dN", rep(nodes(tree0)[nodes(tree0)%in%nt.states], lng),
  538. unlist(edges(tree0)[nodes(tree0)%in%nt.states]))
  539. ## names for at-risk calcs
  540. nt.states2 <- nt.states[!nt.states=="LT"]
  541. ys <- paste("y", nt.states2) ## used for Ys
  542. ## matrix of # of transitions, initialize to zeros
  543. dNs <- matrix(0, nrow=length(times), ncol=length(ds))
  544. ## matrix of total # of transitions from a state, initialize to zeros
  545. sum_dNs <- matrix(0, nrow=length(times), ncol=length(nt.states))
  546. ## matrix of at-risk sets for each stage at each time
  547. Ys <- matrix(NA, nrow=length(times), ncol=length(ys))
  548. ## names of rows/columns for vectors/matrices
  549. rownames(dNs) <- rownames(sum_dNs) <- rownames(Ys) <- times
  550. colnames(dNs) <- ds
  551. colnames(Ys) <- ys
  552. colnames(sum_dNs) <- paste("dN", nt.states, ".")
  553. ## Calculations for transitions 'dNs'
  554. ## Outer loop = all nodes w/transitions into them
  555. nodes.into <- nodes(tree0)[sapply(inEdges(tree0), function(x) length(x) > 0)]
  556. for (i in nodes.into) {
  557. ## Inner loop = all nodes which transition into node i
  558. nodes.from <- inEdges(tree0)[[i]]
  559. for (j in nodes.from) {
  560. nam2 <- paste("dN", j, i)
  561. idx <- which(Data$end.stage==i & Data$start.stage==j)
  562. tmp.tab <- table(Data$stop[idx][!Data$stop[idx]==0]) ## no. trans at each trans time
  563. dNs[names(tmp.tab), nam2] <- tmp.tab
  564. }
  565. }
  566. ## start counts at time == 0 for below
  567. start.stages <- Data$start.stage[Data$start==0]
  568. start.stages <- factor(start.stages, levels=nt.states2, labels=nt.states2)
  569. start.cnts <- table(start.stages)
  570. ## Calculations for at-risk sets 'Ys'
  571. ## only need for non-terminal nodes - use 'nt.states2' to exclude 'LT' state
  572. for (i in nt.states2) {
  573. n <- start.cnts[i]
  574. nam <-paste("y", i)
  575. if (length(inEdges(tree0)[[i]]) > 0)
  576. into.node <- paste("dN", inEdges(tree0)[[i]], i) else into.node <- NULL
  577. if (length(edges(tree0)[[i]])>0)
  578. from.node <- paste("dN", i, edges(tree0)[[i]]) else from.node <- NULL
  579. Ys[, nam] <- c(n, n + cumsum(rowSums(dNs[, into.node, drop=FALSE])) -
  580. cumsum(rowSums(dNs[, from.node, drop=FALSE])))[-(nrow(Ys)+1)]
  581. } ## end of loop for Ys
  582. ## Counting transitions from different states (ie: state sums)
  583. sum_dNs <- matrix(nrow=nrow(dNs), ncol=length(nt.states))
  584. rownames(sum_dNs) <- rownames(dNs) ##
  585. colnames(sum_dNs) <- paste("dN", nt.states, ".")
  586. a <- strsplit(colnames(sum_dNs), " ")
  587. a2 <- strsplit(colnames(dNs), " ")
  588. uni <- unique(sapply(a, function(x) x[2]))## gives the unique states exiting
  589. for (i in uni) { ## calculating the dNi.s
  590. b <- which(sapply(a, function(x) x[2]==i))
  591. b2 <- which(sapply(a2, function(x) x[2]==i))
  592. sum_dNs[, b] <- rowSums(dNs[, b2, drop=FALSE])
  593. } ## end of for loop for calculating dNi.s
  594. list(dNs=dNs, Ys=Ys, sum_dNs=sum_dNs)
  595. } ## end of function
  596. ############################################################
  597. ## Datta-Satten Estimation ##
  598. ############################################################
  599. DS <- function(nt.states, dNs, sum_dNs, Ys, Cens="0", cens.type) {
  600. ## Calculating dNs, sum_dNs, and Y from D-S(2001) paper
  601. ## Dividing dNs*, sum_dNs*, & Y* by K to get dNs^, sum_dNs^, & Ys^
  602. ## Make sure nt.states is from the non-LT
  603. res <- strsplit(colnames(dNs), " ") ## string splits names
  604. res2 <- strsplit(colnames(Ys), " ") ## string split names of Ys
  605. res3 <- strsplit(colnames(sum_dNs), " ") ## string splits names of dNs
  606. ## looks at censored columns, needed for D-S est
  607. DS.col.idx <- which(sapply(res, function(x) x[3]==Cens))
  608. ## looks at censored columns, needed for D-S est
  609. DS2.col.idx <- which(sapply(res2, function(x) x[2]%in%nt.states))
  610. ## looks at censored columns, needed for D-S est
  611. DS3.col.idx <- which(sapply(res3, function(x) x[2]%in%nt.states))
  612. ## for INDEPENDENT censoring
  613. if (cens.type=="ind") {
  614. K <- vector(length=nrow(dNs))
  615. dN0 <- rowSums(dNs[, DS.col.idx, drop=FALSE])
  616. Y0 <- rowSums(Ys[, DS2.col.idx, drop=FALSE]) ## those at risk of being censored
  617. N.Y <- ifelse(dN0/Y0=="NaN", 0, dN0/Y0)
  618. colnames(N.Y) <- NULL
  619. H.t <- cumsum(N.Y) ## calculating the hazard
  620. k <- exp(-H.t)
  621. K <- c(1, k[-length(k)])
  622. dNs.K <- dNs/K ## D-S dNs
  623. Ys.K <- Ys/K ## D-S Ys
  624. sum_dNs.K <- sum_dNs/K
  625. } ## end of independent censoring
  626. ## for DEPENDENT censoring
  627. if (cens.type=="dep") {
  628. dN0 <- dNs[, DS.col.idx]
  629. Y0 <- Ys[, DS2.col.idx] ## those at risk of being censored
  630. N.Y <- ifelse(dN0/Y0=="NaN", 0, dN0/Y0)
  631. colnames(N.Y) <- paste(colnames(dN0), "/", colnames(Y0))
  632. H.t <- apply(N.Y, 2, function(x) cumsum(x))
  633. K <- exp(-H.t)
  634. ## K <- apply(k, 2, function(x) c(1, x[-length(x)])) ## maybe don't need
  635. dNs.K <- dNs; Ys.K <- Ys; sum_dNs.K <- sum_dNs
  636. for (i in nt.states) {
  637. K.idx <- which(sapply(strsplit(colnames(N.Y), " "), function(x) x[2]==i))
  638. dN.idx <- which(sapply(res, function(x) x[2]==i))
  639. sum_dNs.idx <- which(sapply(res3, function(x) x[2]==i))
  640. Ys.idx <- which(sapply(res2, function(x) x[2]==i))
  641. dNs.K[, dN.idx] <- dNs[, dN.idx]/K[, K.idx]
  642. sum_dNs.K[, sum_dNs.idx] <- sum_dNs[, sum_dNs.idx]/K[, K.idx]
  643. Ys.K[, Ys.idx] <- Ys[, Ys.idx]/K[, K.idx]
  644. }
  645. } ## end of dependent censoring
  646. res <- list(dNs.K=dNs.K, Ys.K=Ys.K, sum_dNs.K=sum_dNs.K)
  647. return(res)
  648. } ## end of D-S function
  649. ############################################################
  650. ## Reducing dNs & Ys to event times ##
  651. ############################################################
  652. Red <- function(tree, dNs, Ys, sum_dNs, dNs.K, Ys.K, sum_dNs.K) {
  653. ## tree is original tree currently inputted by user
  654. ## dNs, sum.dNS, & Ys come from CP function
  655. ## K comes from DS
  656. ## reducing dNs & Ys to just event times & noncens/nontruncated states
  657. res <- strsplit(colnames(dNs), " ") ## string splits names
  658. ## looks at noncensored columns
  659. col.idx <- which(sapply(res, function(x) x[2]%in%nodes(tree) & x[3]%in%nodes(tree)))
  660. ## identifies times where transitions occur
  661. row.idx <- which(apply(dNs[, col.idx, drop=FALSE], 1, function(x) any(x>0)))
  662. dNs.et <- dNs[row.idx, col.idx, drop=FALSE] ## reduces dNs
  663. res2 <- strsplit(colnames(Ys), " ") ## string split names of Ys
  664. nt.states.f <- names(which(sapply(edges(tree), function(x) length(x)>0))) ## nonterminal states
  665. col2.idx <- which(sapply(res2, function(x) x[2]%in%nt.states.f)) ## ids nonterminal columns
  666. Ys.et <- Ys[row.idx, col2.idx, drop=FALSE] ## reduces Ys
  667. col3.idx <- which(sapply(strsplit(colnames(sum_dNs), " "), function(x) x[2]%in%nodes(tree)))
  668. sum_dNs.et <- sum_dNs[row.idx, col3.idx, drop=FALSE]
  669. dNs.K.et <- dNs.K[row.idx, col.idx, drop=FALSE]
  670. Ys.K.et <- Ys.K[row.idx, col2.idx, drop=FALSE]
  671. sum_dNs.K.et <- sum_dNs.K[row.idx, col3.idx, drop=FALSE]
  672. ans <- list(dNs=dNs.et, Ys=Ys.et, sum_dNs=sum_dNs.et, dNs.K=dNs.K.et, Ys.K=Ys.K.et, sum_dNs.K=sum_dNs.K.et)
  673. return(ans)
  674. }
  675. ############################################################
  676. ## AJ Estimates and State Occupation Probabilities ##
  677. ############################################################
  678. AJ.estimator <- function(ns, tree, dNs.et, Ys.et, start.probs) {
  679. ## currently ns is defined in main function
  680. ## tree needs to be uncensored tree
  681. cum.tm <- diag(ns)
  682. colnames(cum.tm) <- rownames(cum.tm) <- nodes(tree)
  683. ps <- matrix(NA, nrow=nrow(dNs.et), ncol=length(nodes(tree)))
  684. rownames(ps) <- rownames(dNs.et); colnames(ps) <- paste("p", nodes(tree))
  685. all.dA <- all.I.dA <- all.AJs <- array(dim=c(ns, ns, nrow(dNs.et)),
  686. dimnames=list(rows=nodes(tree),
  687. cols=nodes(tree), time=rownames(dNs.et)))
  688. for (i in 1:nrow(dNs.et)) { ##loop through times
  689. I.dA <- diag(ns) ## creates trans matrix for current time
  690. dA <- matrix(0, nrow=ns, ncol=ns)
  691. colnames(I.dA) <- rownames(I.dA) <- colnames(dA) <- rownames(dA) <- nodes(tree)
  692. idx <- which(dNs.et[i, , drop=FALSE]>0) ## transition time i
  693. t.nam <- colnames(dNs.et)[idx] ## gets names of transitions (ie: dN##)
  694. tmp <- strsplit(t.nam, " ") ## splits title of dN##
  695. start <- sapply(tmp, function(x) x[2])
  696. end <- sapply(tmp, function(x) x[3]) ## pulls start & stop states as character strings
  697. idxs <- matrix(as.character(c(start, end)), ncol=2)
  698. idxs2 <- matrix(as.character(c(start, start)), ncol=2)
  699. dA[idxs] <- dNs.et[i, idx]/Ys.et[i, paste("y", start)]
  700. if (length(idx)==1) {
  701. dA[start, start] <- -dNs.et[i, idx]/Ys.et[i, paste("y", start)]
  702. } else {
  703. dA[idxs2] <- -rowSums(dA[start, ])
  704. }
  705. I.dA <- I.dA + dA ## I+dA (transition) matrix
  706. all.dA[, , i] <- dA ## stores all dA matrices
  707. all.I.dA[, , i] <- I.dA ## array for storing all tran matrices
  708. cum.tm <- cum.tm %*% I.dA ## Multiply current.tm and cum.tm to get matrix for current time
  709. all.AJs[, , i] <- cum.tm ## A-J estimates, stored in array
  710. ## multiply by start.probs to allow for starting states other than '1'
  711. ps[i, ] <- start.probs%*%all.AJs[, , i] ## state occupation probabilities
  712. } ## end of loop
  713. list(ps=ps, AJs=all.AJs, I.dA=all.I.dA)
  714. } ## end of function
  715. ############################################################
  716. ## State Entry/Exit Distributions ##
  717. ############################################################
  718. Dist <- function(ps, ns, tree) {
  719. ## ps from AJ.estimator function
  720. ## tree needs to be uncensored tree
  721. initial <- which(sapply(inEdges(tree), function(x) !length(x)>0)) ## initial states, no Fs
  722. terminal <- which(sapply(edges(tree), function(x) !length(x)>0)) ## terminal states, no Gs
  723. Fnorm <- Fsub <- matrix(0, nrow=nrow(ps), ncol=ns) ## entry distn
  724. rownames(Fnorm) <- rownames(Fsub) <- rownames(ps)
  725. colnames(Fnorm) <- colnames(Fsub) <- paste("F", nodes(tree))
  726. Gsub <- Gnorm <- matrix(0, nrow=nrow(ps), ncol=ns) ## exit distn
  727. rownames(Gnorm) <- rownames(Gsub) <- rownames(ps)
  728. colnames(Gnorm) <- colnames(Gsub) <- paste("G", nodes(tree))
  729. ## looping through nodes
  730. for (i in 1:ns) {
  731. node <- nodes(tree)[i]
  732. later.stages <- names(acc(tree, node)[[1]])
  733. stages <- c(node, later.stages)
  734. Fsub[, i] <- f.numer <- rowSums(ps[, paste("p", stages), drop=FALSE])
  735. Fnorm[, i] <- f.numer/f.numer[length(f.numer)]
  736. if (length(stages)==1) next
  737. Gsub[, i] <- g.numer <- rowSums(ps[, paste("p", later.stages), drop=FALSE])
  738. Gnorm[, i] <- g.numer/f.numer[length(f.numer)]
  739. } ## end of for loop
  740. Fr <- strsplit(colnames(Fnorm), " ")
  741. Fs.idx <- which(sapply(Fr, function(x) x[2]%in%names(initial)))
  742. Fnorm[, Fs.idx] <- Fsub[, Fs.idx] <- NA
  743. Gr <- strsplit(colnames(Gnorm), " ")
  744. Gs.idx <- which(sapply(Gr, function(x) x[2]%in%names(terminal)))
  745. Gnorm[, Gs.idx]<- Gsub[, Gs.idx] <- NA
  746. list(Fnorm=Fnorm, Gsub=Gsub, Fsub=Fsub, Gnorm=Gnorm)
  747. } ## end of function
  748. ############################################################
  749. ### Variance of AJ estimates ####
  750. ############################################################
  751. var.fn <- function(tree, ns, nt.states, dNs.et, Ys.et, sum_dNs, AJs, I.dA, ps) {
  752. ## covariance matrices
  753. state.names <- nodes(tree)
  754. trans.names <- paste(rep(state.names, ns), rep(state.names, each=ns))
  755. cov.dA <- cov.AJs <- array(0, dim = c(ns^2, ns^2, nrow(dNs.et)))
  756. dimnames(cov.dA) <- dimnames(cov.AJs) <- list(trans.names, trans.names, rownames(dNs.et))
  757. res.array <- array(0, dim=c(ns^2, ns^2))
  758. colnames(res.array) <- rownames(res.array) <- trans.names
  759. ## Ident matrix for Kronecker products
  760. bl.Id <- diag(1, (ns)^2)
  761. Id <- diag(1, ns)
  762. ## matrix for var matrix of state occup prob
  763. var.sop <- matrix(0, nrow=nrow(dNs.et), ncol=ns)
  764. colnames(var.sop) <- paste("Var", "p", state.names)
  765. rownames(var.sop) <- rownames(ps)
  766. ## NOTE - see note below for use of v.p ...
  767. v.p <- matrix(0, ns, ns)
  768. for (i in 1:nrow(dNs.et)) { ## loop through times
  769. ## VARIANCE OF A-J ( TRANS PROB MATRIX P(0, t) )
  770. ## Equation 4.4.20 in Anderson 1993
  771. ## loop on the blocks (g) - only needed for non-terminal states
  772. for (outer in nt.states) {
  773. ## find positioning of outer in state.names
  774. idx.outer <- which(state.names==outer)
  775. ## tmp matrix for calculating variance of transitions in each block (g)
  776. tm <- matrix(0, nrow=ns, ncol=ns)
  777. colnames(tm) <- rownames(tm) <- state.names
  778. ## loop in the blocks
  779. for (j in 1:ns) {
  780. ## this just fills in upper diagonal matrix
  781. ## use symmetry to fill in rest
  782. for (k in j:ns) {
  783. statej <- state.names[j]
  784. statek <- state.names[k]
  785. outer.Ys <- paste("y", outer)
  786. outer.sum_dNs <- paste("dN", outer, ".")
  787. if (Ys.et[i, outer.Ys]==0) { ## if Y_g = 0 then covariance = 0
  788. tm[j, k] <- 0
  789. next
  790. }
  791. if (statej == outer & statek == outer) { ## 3rd formula
  792. tm[j, k] <- (Ys.et[i, outer.Ys] - sum_dNs[i, outer.sum_dNs])*sum_dNs[i, outer.sum_dNs] / Ys.et[i, outer.Ys]^3
  793. } else if (statej == outer & statek != outer) { ## 2nd formula
  794. name <- paste("dN", outer, statek)
  795. if (!name%in%colnames(dNs.et)) next
  796. tm[j, k] <- -(Ys.et[i, outer.Ys] - sum_dNs[i, outer.sum_dNs])*dNs.et[i, name] / Ys.et[i, outer.Ys]^3
  797. } else if (statej != outer & statek == outer) { ## 2nd formula pt 2, for recurrent
  798. name <- paste("dN", outer, statej)
  799. if (!name%in%colnames(dNs.et)) next
  800. tm[j, k] <- -(Ys.et[i, outer.Ys] - sum_dNs[i, outer.sum_dNs])*dNs.et[i, name]/Ys.et[i, outer.Ys]^3
  801. } else { ## 1st formula
  802. namek <- paste("dN", outer, statek)
  803. namej <- paste("dN", outer, statej)
  804. if (!(namej%in%colnames(dNs.et) & namek%in%colnames(dNs.et))) next
  805. tm[j, k] <- (ifelse(j==k, 1, 0)*Ys.et[i, outer.Ys] - dNs.et[i, namej])*dNs.et[i, namek]/Ys.et[i, outer.Ys]^3
  806. } ## end of if/else statements
  807. } ## end of k loop
  808. } ## end of j loop
  809. tm[lower.tri(tm)] <- t(tm)[lower.tri(tm)]
  810. res.array[(seq(1, ns*(ns-1)+1, by=ns) + idx.outer - 1), (seq(1, ns*(ns-1)+1, by=ns) + idx.outer - 1)] <- tm
  811. }## end of outer loop
  812. ## array holding var-cov matrix for I+dA matrix at each time (differential of NA estim)
  813. cov.dA[, , i] <- res.array
  814. if (i==1) {
  815. cov.AJs[, , i] <- bl.Id%*% cov.dA[, , i] %*% bl.Id
  816. } else {
  817. cov.AJs[, , i] <- (t(I.dA[, , i]) %x% Id) %*% cov.AJs[, , i-1] %*%((I.dA[, , i]) %x% Id) +
  818. (Id %x% AJs[, , i-1]) %*% cov.dA[, , i] %*% (Id%x% t(AJs[, , i-1]))
  819. }
  820. ## note: AJs[, , i-1] corresponds to P(0, t-)
  821. ## cov.AJs is the var/cov est of P(0, t)
  822. ## calculating the variance of state occupation prob
  823. for (j in state.names) { ## loop through states
  824. name <- paste(state.names[1], j)
  825. part1 <- var.pkj0t <- cov.AJs[name, name, i]
  826. res <- strsplit(colnames(ps), " ")
  827. col.idx <- which(sapply(res, function(x) x[2]== j)) ## looks at state transitioned to
  828. b.t <- AJs[, col.idx, i] ## creating vector of col for current state from trans prob
  829. ## NOTE - This is ZERO when all indiv start from single state ...
  830. part2 <- t(b.t)%*%v.p%*%b.t ## should be 0 when P(0, t)
  831. ## right now forced to be 0 by the way v.p defined outside of time loop
  832. res.varp <- part1 + part2 ## calculating var/cov matrix for time i, state j
  833. var.sop[i, col.idx] <- res.varp ## storing var/cov calc for time i,n state j
  834. } ## closes states loop
  835. } ## end of time loop
  836. list(cov.AJs=cov.AJs, cov.dA=cov.dA, var.sop=var.sop)
  837. }## end of function
  838. #########################################################
  839. ## BS Variance ##
  840. #########################################################
  841. ## Needed For:
  842. ## 1. Dependent Censoring
  843. ## 2. Entry / Exit functions
  844. ## 3. SOPs when > 1 starting state
  845. BS.var <- function(Data, tree, ns, et, cens.type, B, LT) {
  846. n <- length(unique(Data$id)) ## sample size
  847. ids <- unique(Data$id)
  848. ## storage for bootstrap estimates of transition probability matrices
  849. bs.est <- array(dim=c(length(nodes(tree)), length(nodes(tree)), length(et), B),
  850. dimnames=list(rows=nodes(tree), cols=nodes(tree), time=et))
  851. bs.ps <- array(dim=c(length(et), ns, B))
  852. rownames(bs.ps) <- et
  853. colnames(bs.ps) <- paste("p", nodes(tree))
  854. ## For entry / exit distributions
  855. bs.Fnorm <- bs.Fsub <- bs.ps; bs.Gnorm <- bs.Gsub <- bs.ps ## storage for BS entry/exit
  856. colnames(bs.Fnorm) <- colnames(bs.Fsub) <- paste("F", nodes(tree))
  857. colnames(bs.Gnorm) <- colnames(bs.Gsub) <- paste("G", nodes(tree))
  858. initial <- which(sapply(inEdges(tree), function(x) !length(x) > 0)) ## initial states, no Fs (entry)
  859. terminal <- which(sapply(edges(tree), function(x) !length(x) > 0)) ## terminal states, no Gs (exit)
  860. bs.var.sop <- matrix(0, nrow=length(et), ncol=ns) ## matrix for var matrix of state occup prob
  861. colnames(bs.var.sop) <- paste("Var", "p", nodes(tree))
  862. rownames(bs.var.sop) <- et
  863. res.array <- array(0, dim=c(ns^2, ns^2, length(et)),
  864. dimnames=list(rows=paste(rep(nodes(tree), ns), sort(rep(nodes(tree), ns))),
  865. cols=paste(rep(nodes(tree), ns), sort(rep(nodes(tree), ns))), time=et))
  866. for (b in 1:B) { ## randomly selects the indices
  867. ## Find the bootstrap sample, pull bs sample info from original data & put into data set
  868. bs <- sample(ids, n, replace=TRUE)
  869. bs <- factor(bs, levels=ids)
  870. bs.tab <- data.frame(table(bs)) ### table with the frequencies
  871. Data.bs <- merge(Data, bs.tab, by.x="id", by.y="bs") ## merging original data with bs freq table
  872. bs.id <- as.vector(unlist(apply(Data.bs[Data.bs$Freq>0, , drop=FALSE], 1, function(x) paste(x["id"], 1:x["Freq"], sep=".")))) ## creating bs id
  873. idx <- rep(1:nrow(Data.bs), Data.bs$Freq) ## indexing the bs sample
  874. Data.bs <- Data.bs[idx, ] ## creating a bs dataset
  875. Data.bs.originalID <- Data.bs$id
  876. Data.bs$id <- bs.id ## changing id column to bs.id to use functions
  877. Data.bs <- Data.bs[order(Data.bs$stop), ] ## ordered bs dataset
  878. ## Calling functions using bs dataset
  879. Cens <- Add.States(tree, LT)
  880. ## Here calculate start probs for BS data ...
  881. ## Data may be LT so need to account for that
  882. ## Put check here for whether have LT or not ...
  883. if (LT) {
  884. min.tran <- min(Data.bs$stop[Data.bs$end.stage!=0 & Data.bs$start.stage!="LT"])
  885. idx1 <- which(Data.bs$start.stage=="LT" & Data.bs$stop < min.tran)
  886. idx2

Large files files are truncated, but you can click here to view the full file