R: DirichReg Error in summary.DirichletRegModel Optimization did not converge in ... iterations and exited with code 8

226 Views Asked by At

I have a dataframe (df) with calculated proportions of different behaviours. See head and structure of the df below.

    Model    Cat_id    Day     Active Inactive Maintenance Total propActive propInactive propMaintenance Model2
 1: C.RF1    Cho       1 days  1936   75672    8792        86400 0.02240741    0.8758333      0.10175926  C.RF1
 2: C.RF1    Cho       2 days  1307   78236    6857        86400 0.01512731    0.9055093      0.07936343  C.RF1
 3: C.RF1    Cho       3 days  1360   73784    11256       86400 0.01574074    0.8539815      0.13027778  C.RF1
 4: C.RF1    Cho       4 days  2828   70666    12906       86400 0.03273148    0.8178935      0.14937500  C.RF1
 5: C.RF1    Cho       5 days  2988   74130    9282        86400 0.03458333    0.8579861      0.10743056  C.RF1
 6: C.RF1    Cho       6 days  1809   74477    10114       86400 0.02093750    0.8620023      0.11706019  C.RF1

Classes ‘data.table’ and 'data.frame':  1152 obs. of  11 variables:
$ Model          : Factor w/ 16 levels "C.RF1","C.RF2",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Cat_id         : Factor w/ 12 levels "Cho","George",..: 1 1 1 1 1 1 2 2 2 2 ...
$ Day            : 'difftime' num  1 2 3 4 ...
..- attr(*, "units")= chr "days"
$ Active         : int  1936 1307 1360 2828 2988 1809 2616 2697 2540 3796 ...
$ Inactive       : int  75672 78236 73784 70666 74130 74477 74862 75588 74547 73742 ...
$ Maintenance    : int  8792 6857 11256 12906 9282 10114 8922 8115 9313 8862 ...
$ Total          : int  86400 86400 86400 86400 86400 86400 86400 86400 86400 86400 ...
$ propActive     : num  0.0224 0.0151 0.0157 0.0327 0.0346 ...
$ propInactive   : num  0.876 0.906 0.854 0.818 0.858 ...
$ propMaintenance: num  0.1018 0.0794 0.1303 0.1494 0.1074 ...
$ Model2         : Factor w/ 16 levels "H.RF1","C.RF1",..: 2 2 2 2 2 2 2 2 2 2 ...
- attr(*, ".internal.selfref")=<externalptr> 
- attr(*, "sorted")= chr [1:3] "Model" "Cat_id" "Day"

I want to test for differences in these proportions within each behaviour with a Dirichlet regression, using the code below.

dirig <- DR_data(df2[, c("propActive","propInactive","propMaintenance")], base=1)
m1 <- DirichReg(dirig ~ Model + Day, data = df2, model = "common")
summary(m1)

This code works fine an does its job. The problem with the Dirichlet Regression, though, is that it will always use the first level as a reference category. In this case, it will always compare all the model categories to C.RF1. I have found that you can use "relevel" to change the reference categorie, like below.

df2$Model2 <- relevel(df2$Model, ref="C.RF3")
dirig <- DR_data(df2[, c("propActive","propInactive","propMaintenance")], base=1)
m1 <- DirichReg(dirig ~ Model2 + Day, data = df2, model = "common")
summary(m1)

This works fine for all but one reference category, where I get the following error:

Error in summary.DirichletRegModel(m1) : 
Optimization did not converge in 264 + 2 iterations and exited with code 8

I am using the exact same dataframe and code. The only thing that is different, is the reference category. It only does it with this model set as a reference category. In short: I do not understand why it gives me this error, since nothing changes but the reference category. Is there anyone who can help me with this problem?

Edit: I have tried to change the model from "common" to "alternative". However, the "alternative" model will not only use the first category of the model as a reference category, but also (in my case) the behaviour. If I then run the summary, I only get the comparisons between the reference model and all the other models for inactive and maintenance, while it is not showing it for active, as that is also set as a reference category. If I then change the reference category for behaviour by changing base = 1 to base = 2, the results in significant difference change, so this is not an option. The other thing I found, is that while the alternative model might work in the case where the common model gives the above error, I get the exact same error as above with the alternative model when it worked with the common model.

1

There are 1 best solutions below

0
Dai On

Not sure why, but it has something to do with the convergence criteria for the optimization. Try different tolerance values under the control argument. Setting higher iteration values may help as well. For example,

DirichReg(dirig ~ Model2 + Day, 
          data = df2, 
          model = "common", 
          control = list(interlim = 2000, tol1 = 1e-2, tol2 = 1e-2))