2

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.

phg
  • 536
  • 1
  • 7
  • 19

1 Answers1

1

The issue is j is recognized as a variable instead of values 1 to 5. Therefore, we can use as.formula with paste0 to turn j into numbers:

for (j in 1:5) {
  
  # Fit a spline with j knots
  #fit <- lme(y ~ ns(x, j), random = ~1|xid)
  fit <- lme(as.formula(paste0("y~ns(x,",j,")")), random = ~1|xid)
  
  # Print the summary of the fit
  print(summary(fit))
}
one
  • 3,121
  • 1
  • 4
  • 24