[This article was first published on R Snippets for IRT, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The function:
This is a very simple data simulator for a 2PL Model. This is just to get you started, from here is easy to add function parameters for indicating item locations and slopes or person distribution characteristics.
- The function accepts only two parameters:
- The number of items
- The number of persons
- The function creates a list containing four objects:
- A vector of item locations
- A vector of item slopes
- A vector of person locations
- A matrix of simulated responses
The code:
twopl.sim <- function( nitem = 20, npers = 100 ) { i.loc <- rnorm( nitem ) p.loc <- rnorm( npers ) i.slp <- rlnorm( nitem, sdlog = .4 ) 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 }
Example:
This is a simple example that uses the IRToys (irtoys) package to estimate the model parameters after simulating the data. Do try this at home too!
#install.packages('irtoys') # Note: 'irtoys' might not load right away in OSX. You can try the following sequence in that case: # install.packages('mvtnorm') # install.packages('msm') # install.packages('ltm',type='source') # install.packages('irtoys',type='source') library(irtoys) ###### Running Simulation ###### data2pl <- twopl.sim(nitem=20,npers=10000) sim.loc.param <- data2pl$i.loc sim.slp.param <- data2pl$i.slp ###### Estimation Using irtoys ###### analysis.2pl <- est(data2pl$resp, model = '2PL', engine = 'ltm') est.slp.param <- analysis.2pl[,1] est.loc.param <- analysis.2pl[,2] layout(matrix(c(1,2),nrow=1)) plot(est.slp.param,sim.slp.param) plot(est.loc.param,sim.loc.param)
To leave a comment for the author, please follow the link and comment on their blog: R Snippets for IRT.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.