I run the following loop to print a model for each index as a ns(x, df=j)
library(splines)
library(nlme)
# Generate some data
x <- 1:100
xid <- sort(rep.int(1:10, 10))
y <- x + rnorm(10)
# Loop through the number of knots in the spline
for (j in 1:5) {
# Fit a spline with j knots
fit <- lme(y ~ ns(x, j), random = ~1|xid)
# Print the summary of the fit
print(summary(fit))
}
I get the error message
Error in model.frame.default(formula = ~y + x + j, data = <environment>, : variable lengths differ (found for 'j')
I don't really understand why there's an issue and the error message doesn't show the ns()
function. When doing this with a standard lm
with no xid
grouping structure, it works perfectly fine. There is something vaguely related here but doesn't look to be the same issue, only a similar error message. It's worth noting when I replace j
with actual integers 1:5
it works fine too.