Quinn
Quinn

Reputation: 429

Plot function error

I am running the leaps package a number of times on different data sets. This produces a number of different figures. Because I am trying to run it quite a few times I wanted to transform this into a plotting function, allowing me to apply it more easily. But I am not great at functions yet and it something rather bizarre has gone wrong - I suspect this is because I have wrote the function incorrectly somewhere along the lines!

FUNCTIONNAME <- function(A, B, C){

plot.new()
vp0 <- viewport(x=0, y = 0.9, just = c("center"))
vp1 <- viewport(x=0,y=0,width=0.5, height=1, just = c("left", "bottom"))
vp2 <- viewport(x=0.5,y=0,width=0.5, height=1, just = c("left", "bottom"))

pushViewport(vp1)
par(new=TRUE, fig=gridFIG())
plot.regsubsetsMOD((A), scale = "bic", main = NULL, cex.main = 0.5, cex.axis = 0.65)
grid.text("BIC", 0.55, 0.825)
upViewport()

pushViewport(vp2)
par(new=TRUE, fig=gridFIG())
plot.regsubsetsMOD((A), scale = "r2", main = NULL, cex.main = 0.5, cex.axis = 0.65)
grid.text("R2", 0.55, 0.825)
upViewport()

pushViewport(vp0)
grid.text("B")

dev.copy(png,"C", height = 400, width = 1000)
dev.off()

}

FUNCTIONNAME(A = "Aut_Q_PCA_Models", B = "'textstring'", C = "'textstring.png'" )

The error I get is:

"Error in lsum$rsq : $ operator is invalid for atomic vectors"

This confuses me because lsum$rsq is in the plot.regsubsetsMOD function, not my plotting one. The plot.regsubsetsMOD function normally works correctly so I believe the error is not in there - it is in my new function for plotting the viewport. Any help would be much appreciated!

#

Here is the full code of the page as it stands:

library(leaps)
library(grid)
library(gridBase)

#####Apply leaps to first data set
setwd("J:/Academic papers/Dissertation journal paper/Version 3/New analysis/R code/Full data/AutumnLIFE-ChalkRiver")

AutumnLIFEChalkRiver <- read.csv("AutumnLIFE-ChalkRiver.csv",header=T)
attach(AutumnLIFEChalkRiver)

Aut_Q_PCA <- AutumnLIFEChalkRiver[,c(3, 6, 7, 10, 12, 16)]

Aut_Q_PCA_Models <-regsubsets(AutumnChalkLife~.,
                          data=Aut_Q_PCA,
                          nbest=5)

#####Generate figure
setwd("J:/R/Leaps")
FUNCTIONNAME(A ="Aut_Q_PCA_Models", B = "'textstring'", C = "'textstring.png'" )
#

The code plot.regsubsetsMOD is a modification of the leaps code, but it is very minor, I simply added ... on a few lines so that I could change the plots to be more legible:

plot.regsubsetsMOD <- function (x, labels = obj$xnames, main = NULL, scale = c("bic", 
                                                                               "Cp", "adjr2", "r2"), col = gray(seq(0, 0.9, length = 10)), 
                                ...) 
{
  obj <- x
  lsum <- summary(obj)
  par(mar = c(7, 5, 6, 3) + 0.1)
  nmodels <- length(lsum$rsq)
  np <- obj$np
  propscale <- FALSE
  sscale <- pmatch(scale[1], c("bic", "Cp", "adjr2", "r2"), 
                   nomatch = 0)
  if (sscale == 0) 
    stop(paste("Unrecognised scale=", scale))
  if (propscale) 
    stop(paste("Proportional scaling only for probabilities"))
  yscale <- switch(sscale, lsum$bic, lsum$cp, lsum$adjr2, lsum$rsq)
  up <- switch(sscale, -1, -1, 1, 1)
  index <- order(yscale * up)
  colorscale <- switch(sscale, yscale, yscale, -log(pmax(yscale, 
                                                         1e-04)), -log(pmax(yscale, 1e-04)))
  image(z = t(ifelse(lsum$which[index, ], colorscale[index], 
                     NA + max(colorscale) * 1.5)), xaxt = "n", yaxt = "n", 
        x = (1:np), y = 1:nmodels, xlab = "", ylab = "", 
        col = col)
  laspar <- par("las")
  on.exit(par(las = laspar))
  par(las = 2)
  axis(1, at = 1:np, labels = labels, ...) #Modified
  axis(2, at = 1:nmodels, labels = signif(yscale[index], 2), ...) #Modified
  if (!is.null(main)) 
    title(main = main, ...) #Modified
  box()
  invisible(NULL)
}

Upvotes: 1

Views: 962

Answers (1)

Qaswed
Qaswed

Reputation: 3879

The main thing to understand is that "A" is a string, containing the letter A. It it remains this even if a variable is named A being a string containing the letters Aut_Q_PCA_Models. In other words "A" and A are different things (as long one defined A <- "A"). The same thing holds for "B" vs. B and "C" vs. C.

Your plotting function, you named FUNCTIONNAME calls the function plot.regsubsetsMOD. As you wrote it starts with

plot.regsubsetsMOD <- function (x, labels = obj$xnames, main = NULL, scale = c("bic", Cp", "adjr2", "r2"), col = gray(seq(0, 0.9, length = 10)), 
                            ...) 
{
obj <- x
lsum <- summary(obj)
par(mar = c(7, 5, 6, 3) + 0.1)
nmodels <- length(lsum$rsq)

Your plotting function parses A = "Aut_Q_PCA_Models" to it. So the problem occurs when basically R tries to run summary("Aut_Q_PCA_Models")$rsq. To run a summary on a single string is apparently senseless. As you named your model from the regsubsets-class Aut_Q_PCA_Models,

FUNCTIONNAME(A = Aut_Q_PCA_Models, B = "textstring", C = "textstring.png" )

should work, if you change line dev.copy(png,"C", height = 400, width = 1000) to dev.copy(png, C, height = 400, width = 1000) and grid.text("B") to grid.text(B).

Upvotes: 1

Related Questions