hello R: we have a two-parameter IRT simulation code. The goal is to generate a response matrix.But the "for" part doesn't run. we don't know what is wrong with it. Thanks so much~~~ I <- 10 J <- 5 response <- matrix(0, 10, 5) pij <- function(a,b,theta) { a <- rnorm(J, 0.8, 0.04) a b <- rnorm(J, 0, 1) b theta <- rnorm(I, 0,1) theta for( i in 1:I ) { for( j in 1:J ) { ptemp <- runif(1) pij <- exp(a[j]*(theta[i]-b[j]))/(1+exp(a[j]*(theta[i]-b[j]))) response[i,j]<-ifelse(pij(b=b[j], a=a[j], theta[i]) < ptemp , 0 ,1) } } } response helena
have a look at function rmvlogis() from package ltm, e.g., library(ltm) p <- 5 n <- 10 a <- rnorm(p, 0.8, 0.04) b <- rnorm(p, 0, 1) ?rmvlogis rmvlogis(n, cbind(b, a)) I hope it helps. Best, Dimitris On 3/11/2010 5:03 AM, Helena wrote:> hello R: > we have a two-parameter IRT simulation code. The goal is to generate a > response matrix.But the "for" part doesn't run. we don't know what is wrong > with it. > > Thanks so much~~~ > > I<- 10 > J<- 5 > response<- matrix(0, 10, 5) > pij<- function(a,b,theta) > { > a<- rnorm(J, 0.8, 0.04) > a > b<- rnorm(J, 0, 1) > b > theta<- rnorm(I, 0,1) > theta > for( i in 1:I ) { > for( j in 1:J ) { > ptemp<- runif(1) > pij<- exp(a[j]*(theta[i]-b[j]))/(1+exp(a[j]*(theta[i]-b[j]))) > response[i,j]<-ifelse(pij(b=b[j], a=a[j], theta[i])< ptemp , 0 ,1) > > } > } > } > response > > > > helena > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
Helen Your program makes no sense. Try the following script I <- 10 J <- 5 response <- matrix(0, 10, 5) #function for probability of response #revised but equivalent version of Helen's formula pij <- function(a,b,theta) {1/(1+exp(-a*(theta-b)))} a <- rnorm(J, 0.8, 0.04) a b <- rnorm(J, 0, 1) b theta <- rnorm(I, 0,1) theta for( i in 1:I ) { for( j in 1:J ) { response[i,j]<-ifelse(pij(a=a[j], b=b[j], theta[i]) < runif(1) , 0 ,1) } } response Helena <helenagucheng_7@hotmail.com> Sent by: r-help-bounces@r-project.org 03/10/2010 11:03 PM To r-help@stat.math.ethz.ch cc Subject [R] about IRT simulation hello R: we have a two-parameter IRT simulation code. The goal is to generate a response matrix.But the "for" part doesn't run. we don't know what is wrong with it. Thanks so much~~~ I <- 10 J <- 5 response <- matrix(0, 10, 5) pij <- function(a,b,theta) { a <- rnorm(J, 0.8, 0.04) a b <- rnorm(J, 0, 1) b theta <- rnorm(I, 0,1) theta for( i in 1:I ) { for( j in 1:J ) { ptemp <- runif(1) pij <- exp(a[j]*(theta[i]-b[j]))/(1+exp(a[j]*(theta[i]-b[j]))) response[i,j]<-ifelse(pij(b=b[j], a=a[j], theta[i]) < ptemp , 0 ,1) } } } response helena ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]