# Using rstan to estimate Response Times IRT Model parameters with two data (matrices)

**URL:** <https://discourse.mc-stan.org/t/using-rstan-to-estimate-response-times-irt-model-parameters-with-two-data-matrices/8818>\
**Category:** Modeling\
**Tags:** cognitive-science\
**Created:** [May 15, 2019, 12:42am UTC](https://discourse.mc-stan.org/t/using-rstan-to-estimate-response-times-irt-model-parameters-with-two-data-matrices/8818 "2019-05-15T00:42:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lucius](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/lucius/32/4302_2.png) [@Lucius](https://discourse.mc-stan.org/u/Lucius)\
**Post date:** [May 15, 2019, 12:42am UTC](https://discourse.mc-stan.org/t/using-rstan-to-estimate-response-times-irt-model-parameters-with-two-data-matrices/8818/1 "2019-05-15T00:42:43Z")

</div>

Dear Stan users:  
I’m a student and Stan’s beginner.  
Recently, I try to use stan to estimate the Response Times IRT Model (RT-IRTM), and the formula for this model is as follows:

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/1/119a421c120b2db59f2a22bfb744ddb94e15539c.png)  
As we can see, the **tij** in this model is response time, so it is a given fixed variable that obtained during test, just like the response data.  
**I have two matrix data with equal ranks, which are Y[n\_participant,n\_item] and RT[n\_participant,n\_item], and I want to use these two data to estimate the model parameter in rstan.**  
I wrote some code to estimate the item parameters for the model, Here is my Stan code:

> **data** {  
> int\<lower=0\> n\_participant;  
> int\<lower=0\> n\_item;  
> int\<lower=0, upper=1\> Y[n\_participant,n\_item]; // _the response of each student to answer each item_  
> real RT[n\_participant,n\_item]; // _the **response times** of each student to answer each item_  
> }  
> **parameters** {  
> vector\<lower=-3,upper=3\> [n\_participant] theta;  
> vector\<lower=0,upper=2\> [n\_participant] rho;  
> vector\<lower=0,upper=2.5\> [n\_item] alpha;  
> vector\<lower=-3,upper=3\>[n\_item] beta;  
> vector\<lower=0,upper=10\> [n\_item] delta;  
> }  
> **mode** l {  
> theta ~ normal(0,1);  
> rho ~ uniform(0,2);  
> beta ~ normal(0,1);  
> alpha ~ lognormal(0,1);  
> delta ~ uniform(0,10);  
> for(i in 1:n\_participant){  
> for(j in 1:n\_item){  
> real p;  
> p=inv\_logit(-1.7_alpha[j]_(theta[i]-((rho[i]\*delta[j])/RT[i,j])-beta[j]));  
> Y[i,j] ~ bernoulli(p);  
> }}}

However, I get some errors when I try to run the model:  
SAMPLING FOR MODEL ‘4PL’ NOW (CHAIN 1).

> Chain 1: Rejecting initial value:  
> Chain 1: Log probability evaluates to log(0), i.e. negative infinity.  
> Chain 1: Stan can’t start sampling from this initial value.  
> …

And the R code are as follows:

> library(“rstan”)  
> rstan\_options(auto\_write=TRUE)  
> options(mc.cores=parallel::detectCores())  
> #data  
> data\<-read.csv(“C:/Users/Y.Luis/Desktop/4PL.csv”,header=T)  
> I\<-nrow(data)  
> J\<-ncol(data)  
> RT\<-read.csv(“C:/Users/Y.Luis/Desktop/4PLRT.csv”,header=T)  
> data\_4pl\<-list(n\_participant=I,n\_item=J,RT=RT,Y=data)

> irt\_4pl\<-stan(file=“4PL.stan”,data=data\_4pl,iter=1000,chains=1)

4/25  
Today, I try to change the RT[i,j] term to a constant 10, of course I deleted the code related to response time, and I found the model could be fit. So I think it might be a code problem with the response time data.

> p=inv\_logit(-1.7_alpha[j]_(theta[i]-((rho[i]_delta[j])/**RT[i,j]**)-beta[j]));  
> p=inv\_logit(-1.7_alpha[j]\*(theta[i]-((rho[i]\*delta[j])/ **10** )-beta[j]));

**But, I still can not solve this problem. so upset… : (**  
**This problem has been bothering me for a long time.**

5/15  
**So I am wondering if the way I put the response time matrix into the rstan parameter estimated code is correct**

**If you have any suggestions for using two matrices to evaluate model parameters in a model, then please give me some guidance and I’ll be very grateful.**

---

<div class="post-metadata">

**Author:** ![erognli](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/erognli/32/5181_2.png) [@erognli](https://discourse.mc-stan.org/u/erognli)\
**Post date:** [May 15, 2019, 10:46am UTC](https://discourse.mc-stan.org/t/using-rstan-to-estimate-response-times-irt-model-parameters-with-two-data-matrices/8818/2 "2019-05-15T10:46:05Z")

</div>

(I’m by no means an expert, but I’ve fitted some IRT models with Stan, although not response time models).

As far as I can see, the use of two matrices of data is not the problem here, I think you’re doing that right.

But I notice you have a lot of constraints on your parameters - are you sure they are helpful and necessary? I think they could also interact in complicated ways to give you the error message you get (but that’s more of an intuition).

With a std\_normal() prior on theta, you wouldn’t need to constrain it, but I think I’d relax the prior on beta, as these two parameters are quite interdependent?

I’m not familiar with the time-related parameters in your model - what are they meant to represent? Anyhow, is there a reason to not go for less constraints and an hierarchical prior on the rho parameter? And some weakly informative prior on the delta parameter?

Also, your code would be simpler and faster with the bernoulli\_logit in the likelihood.
