multipanel plot with labels extracted from htest R objects

56 views Asked by At

ks.test returns a list with class "htests", but I do not find the way to store those lists with the proper class into a vector. The code I am using is:

random.sim <- read.delim("ABC_searStatsForModelFit_model0_RandomValidation.txt")
labels <- names(random.sim)
par(mfrow=c(4,3), oma=c(0.5, 0.75, 2, 0.25), mar=c(4, 4, 4, 4))
pdf("posterior_bias_random.pdf",width=9,height=13)
ks = vector("list",12)
i=1
for (n in c(6,11,16,21,26,31,36,41,46,51,56,61)) {
  ks[i]<-ks.test(random.sim[,n], "qunif")
  i=i+1
  hist(random.sim[,n], main="", xlab=labels[n], ylab="Frequency")
  add_label(0.4, 0.07, paste("K-S test = ", ks[i], sep=""))
     }
title("2CAB+CJAfg", outer=T)
dev.off()

I know I'm doing something wrong because each ks[i] has not class htest

Please note that "add_label" is a small function that I borrowed from somewhere else (sorry I do not recall where, but possibly from Stackoverflow) to align the labels within the plots

add_label <- function(xfrac, yfrac, label, pos = 4, ...) {
  u <- par("usr")
  x <- u[1] + xfrac * (u[2] - u[1])
  y <- u[4] - yfrac * (u[4] - u[3])
  text(x, y, label, pos = pos, ...)
}

Thanks for any help. Pablo

I could solve the plot part by labeling on the fly

pdf("posterior_bias_random.pdf",width=9,height=13)
par(mfrow=c(4,3), oma=c(0.5, 0.75, 2, 0.25), mar=c(4, 4, 4, 4))
for (n in c(6,11,16,21,26,31,36,41,46,51,56,61)) {
  ks<-ks.test(random.sim[,n], "qunif")
  hist(random.sim[,n], main="", xlab=labels[n], ylab="Frequency")
  add_label(0.4, 0.07, paste("K-S test = ", signif(ks$statistic, digits=3), sep=""))
}
  title("2CAB+CJAfg", outer=T)
dev.off()

However, I still wonder how to store such list. I have read Q:"How to store htest list into a matrix", but can not use the solutions. Josh's only keeps the last test list, whereas Bruno's own answer do not clarify how to store the non-numeric info.
Tanks anyway for maintaining this great forum. Probably my preferred source for solving R code questions.
Pablo

0

There are 0 answers