R/add.ps.R

Defines functions add.ps

Documented in add.ps

add.ps <- function(d, col.id, col.offset, col.x, col.d, fun.aggregate = "mean", ref.offset = 1, prefixes,  alternative = c("two.sided", "less", "greater"), mu = 0, paired = FALSE, var.equal = FALSE, lty = 0, ... ) {
  round.ps <- function(x) {
		as.character(ifelse(x < 0.001, "<.001", ifelse(x < 0.01, paste("=", substr(round(x, 3),2,5), sep =""),ifelse((x < 0.05) & (round(x, 2) == 0.05), "<.05", ifelse(round(x,2) == 1, ">.99", paste("=", substr(round(x, 2), 2, 4), sep = ""))))))
	}
  if(!is.data.frame(d)) stop("d must be a data.frame")
  columns <- c(col.id, col.offset, col.x, col.d)
  if (any(!(columns %in% colnames(d)))) stop("column not matching the data")
  formula.agg <- as.formula(paste(col.d, "~", col.id, "+", col.offset, "+", col.x))
  d.new <- aggregate(formula.agg, data = d, FUN = fun.aggregate)
  #so far same as in rm.plot2
  l.offset <- levels(factor(d.new[,col.offset]))
  l.offset.test <- l.offset[-ref.offset]
  if (missing(prefixes)) prefixes <- paste("p(", l.offset.test, ")", sep = "")
  l.x <- levels(factor(d.new[,col.x]))

  for (c.offset in seq_along(l.offset.test)) {
    tmp.ps <- vector("numeric", length(l.x))
    for (c.x in seq_along(l.x)) {
      tmpx <- d.new[d.new[,col.offset] == l.offset[ref.offset] & d.new[,col.x] == l.x[c.x], ]
	  tmpx <- tmpx[order(tmpx[,col.id]),]
	  tmpy <- d.new[d.new[,col.offset] == l.offset.test[c.offset] & d.new[,col.x] == l.x[c.x], ]
	  tmpy <- tmpy[order(tmpy[,col.id]),]
      tmp.ps[c.x] <- t.test(tmpx[,col.d], tmpy[,col.d], alternative = alternative, mu = mu, paired = paired, var.equal = var.equal)$p.value
    }
    tmp.labels <- paste(prefixes[c.offset], round.ps(tmp.ps), sep = "")
    axis(1,seq_along(l.x), labels = tmp.labels, line = c.offset, lty = lty,  ...)
  }      
}

Try the rm.plot package in your browser

Any scripts or data that you put into this service are public.

rm.plot documentation built on May 2, 2019, 5 p.m.