

showIntervals <- function(n =5, st=0, en=1)  {
  interval <- (en-st)/n

  s <- seq(st,en,by=interval)
  interval/2 + head(s,-1) -> s_intervals

  dimnames <- list(NULL,c("quntl", "midp"))
  pretty <- matrix(rep(NA,(length(s)-0.5) * 4), ncol=2, dimnames=dimnames)

  rows <- 2*(1:length(s))-1
  rows_intervals <- head(rows,-1) + 1

  pretty[rows,1] <- s
  pretty[rows_intervals,2] <- s_intervals
   
  pretty
}

# showIntervals(4)

  n=500
  samp <- rnorm(n)
  theor <- ppoints(n)
  xlab <- "theoretical quantile"
  ylab <- "sample quantile"
  qqplot(x=theor, y=samp, xlab=xlab, ylab=ylab)
  qqline(samp)
  qqline(theor)


  library('MASS')
  x=USArrests$Murder
  x
  p=fitdistr(x,'normal')
  p
  mean=coef(p)[1];  sd=coef(p)[2]

  q=ppoints(x)
  y=qnorm(q,mean,sd)
  qqplot(y,x)

  -----------------------------------



  y=qnorm(q,mean,sd);   ylab = "mn=8;sd=4"
  y0 = qnorm(q);  y0lab = "mn=0;sd=1"
  tt <- cbind("mn=8;sd=4" = y,"mn=0;sd=1" = y0, x=sort(x)); rownames(tt) <- q; rbind(head(tt,3), tail(tt,3))



par(mfrow=c(1,3))
qqnorm(x); qqline(x, col="red");
qqplot(y,x, main = ylab); qqline(y0, col="red")
qqplot(y0,x, main = y0lab); qqline(y, col="red")

qqplot(qnorm(q),x)

qqplot(qnorm(x),y)

