avehtari / ROS-Examples

Regression and other stories R examples
https://avehtari.github.io/ROS-Examples/
323 stars 257 forks source link

Issue with binned_resids() - `Error in cut.default(x, breaks) : 'breaks' are not unique #93

Closed fsdias closed 11 months ago

fsdias commented 3 years ago

Hi I'm working with the code from the binned_resids() function and came across the following issue:

x=data_list$OM_per
y=data_list$germ-pred
nclass=sqrt(length(x))

breaks.index <- floor(length(x)*(1:(nclass-1))/nclass)
breaks <- c(-Inf, sort(x)[breaks.index], Inf)
output <- NULL
xbreaks <- NULL
x.binned <- as.numeric (cut (x, breaks))

For this particular covariate the code returns returns:

Error in cut.default(x, breaks) : 'breaks' are not unique

I think the problem is caused by repeated values in the vector "breaks":

breaks
 [1] -Inf 1.09 1.23 1.34 1.43 1.50 1.51 1.51 1.62 1.63 1.66 1.76 1.77 1.77 1.86 1.92 1.94 2.02
[19] 2.12 2.17 2.19 2.30 2.54 2.73 2.77 2.91 2.93 2.95 3.20 3.40  Inf

and it seems that I need to change this line:

breaks.index <- floor(length(x)*(1:(nclass-1))/nclass)

but I'm not sure how.

Any advice? I can share data if necessary.

Thanks

andrewgelman commented 2 years ago

Hi, thanks for sharing. I guess the function should be rewritten. I don't see an easy general solution because the x's could in theory all take on the same values, which would really destroy everything . . . I guess that's the problem with having amateur coders like me do coding! I do think the principle of the binned residual plot is useful.

fsdias commented 2 years ago

I was able to solve this issue by using binnedplot() from library arm.