Building a data frame from a function using ggplot2

I have this function to take the object returned by the sirt functions of the IRT package and the response functions of the plot object for a set of elements 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 looks like he was stuck on one line just before starting to use ggplot2 (where I convert one dat2 column to a coefficient) or ggplotting itself is not quite sure what. I get an error"Error in eval(expr, envir, enclos) : object 'dat2' not found".

I tried reading this one as suggested here , but either this is a different problem, or I just don't understand. The function works fine when I go through it line by line. Any help is much appreciated!

0
1

, geom_line(aes(color=dat2$item)). dat2$, (.. geom_line(aes(color=item))). aes data (dat2 ), - . , , aes, data (dat2 ). dat2 dat2, dat2 , .

+2

Source: https://habr.com/ru/post/1673671/


All Articles