# The order of the data cause different fitting result

**URL:** <https://discourse.mc-stan.org/t/the-order-of-the-data-cause-different-fitting-result/35356>\
**Category:** Modeling\
**Tags:** rstan, fitting-issues\
**Created:** [June 4, 2024, 9:56am UTC](https://discourse.mc-stan.org/t/the-order-of-the-data-cause-different-fitting-result/35356 "2024-06-04T09:56:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![joyjoy](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@joyjoy](https://discourse.mc-stan.org/u/joyjoy)\
**Post date:** [June 4, 2024, 9:56am UTC](https://discourse.mc-stan.org/t/the-order-of-the-data-cause-different-fitting-result/35356/1 "2024-06-04T09:56:23Z")

</div>

I changed the order of the data, sorted it by ID from smallest to largest, input it into the model after the same preprocessing, and got a completely different fitting result from before sorting. Why?

```stan
fit_m01_1 <- stan(file=paste0(getwd(),'/stan_model/m01_RW.stan'), data=data_list,
                             pars = pars_m01_1,
                             chains = 3,
                             iter = 4000,
                             warmup = 2000,
                             thin = 1,
                             control = list(adapt_delta = 0.95,
                                            stepsize = 1,
                                            max_treedepth = 10))

```

 ![微信图片_20240604175516](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/9/1/9139a6f1c4d924a8d3a4894e71a2f6770613b6dc.png)  
 ![微信图片_20240604175536](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/1/1/11eac7cdd32352298b28d076423e79875ec40578.png)

---

<div class="post-metadata">

**Author:** ![mhollanders](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mhollanders/32/17623_2.png) [@mhollanders](https://discourse.mc-stan.org/u/mhollanders)\
**Post date:** [June 4, 2024, 11:59am UTC](https://discourse.mc-stan.org/t/the-order-of-the-data-cause-different-fitting-result/35356/2 "2024-06-04T11:59:00Z")

</div>

Can you share the code for how you processed and sorted the data? It’s almost certainly occurring due to an error here.

---

<div class="post-metadata">

**Author:** ![joyjoy](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@joyjoy](https://discourse.mc-stan.org/u/joyjoy)\
**Post date:** [June 7, 2024, 3:02pm UTC](https://discourse.mc-stan.org/t/the-order-of-the-data-cause-different-fitting-result/35356/3 "2024-06-07T15:02:43Z")

</div>

Thank you very much.  
The original data diagram is as shown in the figure. I sorted the data in Excel based on the subid in ascending order (starting from 1), and then performed the same processing in R. The code used in R is not convenient to show, but after sorting the original data 1, the fitting results are the same as data 2. However, the unsorted original data 1 does not fit the results.

 ![1111](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/2/3/23da3ec3612bbf069fbba2e8fe46020c516e1b82.png)

---

<div class="post-metadata">

**Author:** ![mhollanders](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mhollanders/32/17623_2.png) [@mhollanders](https://discourse.mc-stan.org/u/mhollanders)\
**Post date:** [June 9, 2024, 4:11am UTC](https://discourse.mc-stan.org/t/the-order-of-the-data-cause-different-fitting-result/35356/4 "2024-06-09T04:11:38Z")

</div>

I’m sorry, but I’d have to see the R code of reading in the data and sorting it before I can help diagnose the issue.

---

<div class="post-metadata">

**Author:** ![joyjoy](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@joyjoy](https://discourse.mc-stan.org/u/joyjoy)\
**Post date:** [June 12, 2024, 2:47pm UTC](https://discourse.mc-stan.org/t/the-order-of-the-data-cause-different-fitting-result/35356/5 "2024-06-12T14:47:33Z")

</div>

it‘s the code

 ![图片1](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/2/1/2111aecd8d1f500c6399b239238ef57eb90ba562.png)  
 ![图片2](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/e/7/e790dc5a15731e3325ef237acaf3280fea895530.png)

---

<div class="post-metadata">

**Author:** ![joyjoy](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@joyjoy](https://discourse.mc-stan.org/u/joyjoy)\
**Post date:** [July 6, 2024, 8:37am UTC](https://discourse.mc-stan.org/t/the-order-of-the-data-cause-different-fitting-result/35356/6 "2024-07-06T08:37:19Z")

</div>

Here is the code, can you provide some help?
