#' @export
rBIRT=function(N,nit,alpha,beta,phi,theta,gamma0=NULL,gamma1=NULL,model=c("beta","samejima","simplex","normal")){
  model=match.arg(model)
  if(length(alpha)!=nit | length(beta)!=nit | length(phi)!=nit) stop("Item parameter vectors need to be of length nit")
  if(length(theta)!=N) stop("Person parameter vector needs to be of length N")
  ln_phi=log(phi)
  if(is.null(gamma0)) gamma0=rep(-Inf,nit)
    else if(length(gamma0)!=nit) stop("Inflation parameter vectors needs to be of length nit")
  if(is.null(gamma1)) gamma1=rep(Inf,nit)
    else if(length(gamma1)!=nit) stop("Inflation parameter vectors needs to be of length nit")

  z=matrix(,N,nit)
  if(model=="beta"){
    for (p in 1:N)
      for (i in 1:nit){{
        #note that ln_phi corresponds to \tau_j in Equation 11-15 from Noel & Dauvier (2007)
        #in addition, phi in the present Package corresponds to phi^2 in Noel & Dauvier (2007) for better alignment with the other models in this package
        C=sample(0:2,1,prob=c(stats::plogis(gamma1[i]-alpha[i]*theta[p])-stats::plogis(gamma0[i]-alpha[i]*theta[p]),stats::plogis(gamma0[i]-alpha[i]*theta[p]),1-stats::plogis(gamma1[i]-alpha[i]*theta[p])))
        if(C==0) z[p,i]=stats::rbeta(1,exp(.5*(alpha[i]*theta[p]+beta[i]+ln_phi[i])),exp(.5*(-alpha[i]*theta[p]-beta[i]+ln_phi[i])))
        else z[p,i]=C-1
      }}
  }
  if(model=="samejima"){
    for (p in 1:N)
      for (i in 1:nit){{
        C=sample(0:2,1,prob=c(stats::plogis(gamma1[i]-alpha[i]*theta[p])-stats::plogis(gamma0[i]-alpha[i]*theta[p]),stats::plogis(gamma0[i]-alpha[i]*theta[p]),1-stats::plogis(gamma1[i]-alpha[i]*theta[p])))
        if(C==0) z[p,i]=stats::plogis(alpha[i]*theta[p]+beta[i]+stats::rnorm(1,0,sqrt(phi[i])))
        else z[p,i]=C-1
      }}
  }
  if(model=="simplex"){
    for (p in 1:N)
      for (i in 1:nit){{
        C=sample(0:2,1,prob=c(stats::plogis(gamma1[i]-alpha[i]*theta[p])-stats::plogis(gamma0[i]-alpha[i]*theta[p]),stats::plogis(gamma0[i]-alpha[i]*theta[p]),1-stats::plogis(gamma1[i]-alpha[i]*theta[p])))
        if(C==0) z[p,i]=rsimpl(1,m=1/(1+exp(-(beta[i]+alpha[i]*theta[p]))),s=phi[i])
        else z[p,i]=C-1
      }}
  }
  if(model=="normal"){
    for (p in 1:N)
      for (i in 1:nit){{
        C=sample(0:2,1,prob=c(stats::plogis(gamma1[i]-alpha[i]*theta[p])-stats::plogis(gamma0[i]-alpha[i]*theta[p]),stats::plogis(gamma0[i]-alpha[i]*theta[p]),1-stats::plogis(gamma1[i]-alpha[i]*theta[p])))
        if(C==0) z[p,i]=alpha[i]*theta[p]+beta[i]+stats::rnorm(1,0,sqrt(phi[i]))
        else z[p,i]=C-1
      }}
  }
return(z)
}





