user3036416
user3036416

Reputation: 1255

mgcv error: "x has insufficient unique values to support 3 knots: reduce k"

I am trying to fit the following GAM model

mod <- gam(y ~ s(x, bs = "cr", k = 2), family = betar(link = "logit"), data = d)

where x can take only two unique values.

However, when I run the model I get the following error

Error in smooth.construct.cr.smooth.spec(object, dk$data, dk$knots) : 
  x has insufficient unique values to support 3 knots: reduce k.
In addition: Warning message:
In smooth.construct.cr.smooth.spec(object, dk$data, dk$knots) :
  basis dimension, k, increased to minimum possible

As the error message shows, the number of knots is automatically set to 3 (even if I have specified k = 2 in the gam call).

Is there any reason why I cannot have only 2 knots? How can I fix this?

Thanks.

Upvotes: 3

Views: 4430

Answers (1)

Zheyuan Li
Zheyuan Li

Reputation: 73265

How can you define a cubic polynomial (uniquely) on just two points? A cubic polynomial s(x) needs 4 coefficients, that is, as least 4 unique x values. mgcv applies centering constraint on s(x) so one fewer coefficient is needed, but you still need 3.

If your covariate just has two unique values, you can at most fit it with a straight line:

gam(y ~ x, family = betar(link = "logit"), data = d)

Upvotes: 3

Related Questions