Question

I have this function to take an object returned by the IRT package sirt and plot item response functions for a set of items that the user can specify:

plotRaschIRF <- function(x,items=NULL,thl=-5,thu=5,thi=.01,D=1.7) {
  if (!class(x)=="rasch.mml") stop("Object must be of class rasch.mml")
  thetas <- seq(thl,thu,thi)
  N <- length(thetas)
  n <- length(x$item$b)
  tmp <- data.frame(item=rep(1:n,each=N),theta=rep(thetas,times=n),b=rep(x$item$b,each=N))
  probs <- exp(D*(tmp[,2]-tmp[,3]))/(1+exp(D*(tmp[,2]-tmp[,3])))
  dat <- data.frame(item=rep(1:n,each=N),theta=rep(thetas,times=n),b=rep(x$item$b,each=N),p=probs)
  #dat$item <- factor(dat$item,levels=1:n,labels=paste0("Item",1:n))
  if (is.null(items)) {
    m <- min(10,n)
    items <- 1:m
    if (10<n) warning("By default, this function will plot only the first 10 items")
  }
  if (length(items)==1) {
    title="Item Response Function"
  } else {
    title="Item Response Functions"
  }
  dat2 <- subset(dat,eval(quote(eval(item,dat) %in% items)))
  dat2$item <- factor(dat2$item,levels=unique(dat2$item),labels=paste0("Item",unique(dat2$item)))
  out <- ggplot(dat2,aes(x=theta,y=p,group=item)) +
   geom_line(aes(color=dat2$item),lwd=1) + guides(col=guide_legend(title="Items")) +
   theme_bw() + ggtitle(title) + xlab(expression(theta)) +
   ylab("Probability") + scale_x_continuous(breaks=seq(thl,thu,1))
  print(out)
}

But it seems to be getting stuck at either the line just before I start using ggplot2 (where I convert one column of dat2 to a factor) or at the ggplotting itself -- not really sure which. I get the error message "Error in eval(expr, envir, enclos) : object 'dat2' not found".

I tried reading through this as was suggested here but either this is a different problem or I'm just not getting it. The function works fine when I step through it line by line. Any help is greatly appreciated!

Was it helpful?

Solution

Based on your comments, the error is almost certainly in geom_line(aes(color=dat2$item)). Get rid of dat2$ and it should work fine (i.e. geom_line(aes(color=item))). Stuff in aes is evaluated in the data argument (dat2 here), with the global environment as the enclosure. Notably this means stuff in the function environment is not available for use by aes unless it is part of the data (dat2 here). Since dat2 doesn't exist inside dat2, and dat2 doesn't exist in the global environment, you get that error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top