TEXT   35
Partial Scatterplot matrix
Guest on 15th March 2023 02:28:33 AM


  1. ## The partial scatterplot matrix defined in section 4.3.3
  2.  
  3. "partial"<-function(x=matrix(rnorm(120),20,6),...){
  4.  
  5. # For testing purposes only (set to F if you don't need "2 vs. 1" labels):
  6.   showlabels <- F
  7.  
  8. # Make sure x is in matrix form
  9.   x <- as.matrix(x)
  10.   n <- ncol(x)
  11.  
  12. # Make sure we clean up our mess (revert back to original "par" parameters)
  13. # when we're done
  14.   oldpar <- par("pty", "oma", "mar", "cex", "tck",
  15.     "mgp", "mex", "mfrow")
  16.   on.exit({par(oldpar)})
  17.  
  18. # Set layout, size and margin parameters
  19.   # make plots square
  20.   par(pty="s")
  21.   # determine new optimal plotting character size
  22.   CEX <- par("cex") * max(7.7/(2*(n-1)+3), 0.6)*0.5
  23.   # specify grid of (n-1) by (n-1) plots
  24.   par(mfcol = c(n-1,n-1))
  25.   # magically cause plots to pack together nicely
  26.   par(oma = rep(3,4))
  27.   par(mar = rep(0.3,4))
  28.   dif <- diff(par("fin"))/2
  29.   if(dif > 0)
  30.     par(omi = c(dif*(n-1), 0, dif*(n-1), 0) + par("omi"))
  31.   else
  32.     par(omi = c(0, (-dif)*(n-1), 0, (-dif)*(n-1)) + par("omi"))
  33.   # re-specify plotting character size (since par(mfrow) screwed it up)
  34.   par(cex = CEX)
  35.   # specify the points type
  36.   par(pch=16)
  37.  
  38.  
  39.   dat <- x
  40.   m <- n-1
  41.   mat.x <- NULL
  42.   mat.y <- NULL
  43.  
  44.   for (k in (1:m)){
  45.    fres <- sapply(1:(m+1-k), function(i) residuals(lm(dat[,i] ~ dat[,-c(i,i+k)])))
  46.    bres <- sapply(1:(m+1-k), function(i) residuals(lm(dat[,i+k] ~ dat[,-c(i,i+k)])))
  47.    mat.x <- cbind(mat.x,fres)
  48.    mat.y <- cbind(mat.y,bres)
  49.   }
  50.  
  51.   locmat <- matrix(rep(0,m^2),nrow=m)
  52.   count <- 0
  53.   for (i in (1:m)){
  54.     for (j in (1:(m+1-i))){
  55.         count <- count+1
  56.         locmat[i,j] <- count
  57.     }
  58.   }
  59.  
  60.   # Make plots (i from top to bottom, j from left to right)
  61.   for (i in (2:n)){
  62.     for(j in (1:(n-1))){
  63.       if (j < i-1){
  64.         # prepare an empty plot with correct limits and no axes
  65.         plot(range(x[!is.na(x[,j]),j]), range(x[!is.na(x[,i]),i]),
  66.            type = "n", axes = F, ...)
  67.         # create empty plot and put border around it
  68.         box()
  69.         # show points for j'th vs. i'th columns of x
  70.         points(as.vector(x[,j]),as.vector(x[,i]), ...)
  71.         # show which variables are being plotted
  72.         if (showlabels)
  73.         text(mean(range(x[!is.na(x[,j]),j])),
  74.         mean(range(x[!is.na(x[,i]),i])),
  75.         paste(j,"vs.",i),cex=1.5*CEX)
  76.       }
  77.       else{
  78.         # Get the information of which variables to be plotted against each other on this grid
  79.         loc <- locmat[j-i+2,i-1]
  80.         # prepare an empty plot with correct limits and no axes
  81.         plot(range(mat.x[!is.na(mat.x[,loc]),loc]), range(mat.y[!is.na(mat.y[,loc]),loc]),
  82.            type = "n", axes = F, ...)
  83.         # create empty plot and put border around it
  84.         box()
  85.         # show points for j'th vs. i'th columns of x
  86.         points(as.vector(mat.x[,loc]),as.vector(mat.y[,loc]), ...)
  87.  
  88.       }
  89.     }
  90.   }
  91. }

Raw Paste

Login or Register to edit or fork this paste. It's free.