There is a question with the same title, however, I hope this question is also of interest [and personally I'd like to know the answer].
First, I will specify a dataset with one continuous and one factor covariate:
set.seed(1)
n <- 50
u1 <- sample(c(1,2), n, replace = TRUE)
u1 <- factor(u1)
u2 <- runif(n)
data <- data.frame(u1, u2)
I don't want to run a gam
model, but only to create the design matrix. I have considered 2 ways.
First,
a<-mgcv::s(u2,k=5,bs="ps",by=u1)
b<- mgcv::smoothCon(a,data=data,absorb.cons=TRUE)
However,
b[[1]]$X[u1==2,]
consists of only 0's.
Second,
a<- mgcv::s(u1,u2,k=5,bs="ad")
b<- mgcv::smoothCon(a,data=data,absorb.cons=TRUE)
However, it gives me an error message. Error in Summary.factor(c(1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, :
‘min’ not meaningful for factors
How can this problem be resolved? I'd like to have the design matrix that models a smooth term separately for each level of factor u1
[binary, as here, or otherwise].