Hello R expert,
the following is 2PL model to generate dichotomous data. I want correlation of a and b is 0.8. How should I incorporate this correlation information into the twopl.sim function so that I can generate data that I wanted?
item difficulty parameter
item_b <- rnorm(200, mean=0, 2)
transpose item difficulty parameter
item_b1<-t(item_p)
item discrimination parameter
item_a <- rlnorm(200, mean=0, 0.1225)
#transpose item discrimination parameter
item_a1<-t(item_a)
twopl.sim <- function( nitem = 200, npers = 2688 ) {
i.loc <- item_b1
p.loc <- rnorm( npers )
i.slp <- item_a1
temp <- matrix( rep( p.loc, length( i.loc ) ), ncol = length( i.loc ) )
logits <- t( apply( temp , 1, '-', i.loc) )
logits <- t( apply( logits, 1, '*', i.slp) )
probabilities <- 1 / ( 1 + exp( -logits ) )
resp.prob <- matrix( probabilities, ncol = nitem)
obs.resp <- matrix( sapply( c(resp.prob), rbinom, n = 1, size = 1), ncol = length(i.loc) )
output <- list()
output$i.loc <- i.loc
output$i.slp <- i.slp
output$p.loc <- p.loc
output$resp <- obs.resp
output
}
Thanks much for any help!