I have a dataframe that I would like to split into subsets and apply a GAM to each one. Ultimately I'd like to output a set of predictions to one large dataframe.
So far the approach I have tried to take:
- Create function to build the model
- Subset data into list of dataframes
- Use lapply to turn list of dataframes into list of models
Is this the right approach or is there a better way?
# Function to create generalised additive model then create predictions
gam.function <- function(x) {
gam.x <- gam(x$switch ~ s(x$input_1) + s(x$input_2), data=x) #%>%
#predict(gam.x, newdata = data.frame(input_1 = 3500, input_2 = 13500))
}
# Create a list of data frames for position
position.split <- split(switch,switch$position)
# Return a list of models
model.list <- lapply(position.split, FUN=gam.function)
An error is returned:
> model.list <- lapply(position.split, FUN=gam.function)
Error in gam(x$switch ~ s(x$input_1) + s(x$input_2), :
Model has more coefficients than data
I know this error seems very clear but I'm struggling to interpret as I can successfully run one subset at a time without lapply.
lapply(position.split, nrow)
$\endgroup$