The observed response variable Y takes on one of K(=21) ordered categories.

Here is a summary of my response data (count data: the number of hospital admission in each day), y has observations across all the levels enter image description here The second line is about the number of the oservations that take the value of the first line.

I tried GAM with the following code

m<-gam(sum ~ s(Time,k=20)+s(RSK, k=10),data = mydata, method = "REML",family = ocat(R =21))

But I always get the following Error:

Error in eval(family$initialize) : Values ​​out of range

I was confused about the count data in using ocat GAM ordered categorical family, any help would be much appreciated!

how to fix this problem?

  • $\begingroup$ Maybe I missing out on something here, but you have 21 categories but you set R equal to 20? $\endgroup$
    – usεr11852
    Jan 23, 2022 at 19:22
  • $\begingroup$ that was a Typing error, I actually tried with 21, and even bigger number $\endgroup$
    – Thomas
    Jan 23, 2022 at 20:09

2 Answers 2


For smoothing functions in gamlss I usually use
P-splines, e.g.
where the smoothing parameter is estimated automatically using a local maximum likelihood estimation.

Alternatively a local GAIC can be used, e.g.
pb(Time, method="GAIC", k= 4),
for a Generalised AIC, with penalty 4 for each degree of freedom used.

Alternatively a local GCV can be used, e.g. pb(Time, method="GCV").

Alternatively the user can fix the degrees of freedom, e.g.
pb(Time, df=5).

However to use an explanatory variable, the data would need to be individual cases, e.g. (count of hospital admissions, Time), not frequency data as you give above.

  • $\begingroup$ Maybe I put the y in a wrong way. I do have the count of hospital admissions as my y variable. The frequency was just to show which value y takes and, in that period, how often did y takes each value. for example, in the whole period (9 years) there are 333 days that y takes the value of 5. $\endgroup$
    – Thomas
    Jan 25, 2022 at 18:16

It seems to me that the number of hospital admissions (say y) is a discrete count variable, and so a discrete distribution can be fitted, e.g. a negative binomial distribution.

The second line of data is the frequencies (say f) which can be used as weights, e.g

m1 <- gamlss(y ~ 1, weights=f, family=NBI)

(assuming there are no explanatory variables).

There are many other discrete distributions in gamlss which have heavier tails or are more flexible, e.g. PIG, SICHEL, DEL, BNB.

There are also zero-inflated and zero-adjusted distributions, e.g. ZINBI and ZANBI.

  • $\begingroup$ Hi Robert, thanks a lot for the input! For one thing, because I do need a spline function of time to control the slow changes in baseline risks (due for instance to population changes). For the choosing of smoothing parameter, is there a prefered method in GAMLSS? like GCV or REML(Restricted Maximum Likelihood ) to avoid Overfitting? $\endgroup$
    – Thomas
    Jan 25, 2022 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.