Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 936
user3036416 Avatar asked Nov 24 '25 19:11

user3036416


1 Answers

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)
like image 142
Zheyuan Li Avatar answered Nov 26 '25 09:11

Zheyuan Li



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!