0

I have a tricky problem. I have a dataframe with more than 1000 variables and want to fit each variable to age using fp smoothing function. I know how to use gamlss() for a specific variable (vari), but that's not practical to repeat this explicitly for more than 1000 times. Moreover, I want to plot the fitting for all 1000 variable in a single figure. What I did is:

variables <- colnames(data)[7:dim(data)[2]]
for(vari in variables) {
   print("ROI is:")
   print(vari)
   model_fem <- gamlss(vari ~ fp(age), family=GG, data=females)

But I got errors:

Error in model.frame.default(formula = vari ~ fp(age), data = females) :
variable lengths differ (found for 'fp(age)')

I think the tricky part is from fp(). I have tried to use as.formula, it didn't work. Also because females$vari return NULL, that's why we got this error. Do you have any solution for this? Thank you

Junhao Wen
  • 31
  • 2
  • 6

1 Answers1

0

Character values are very different from formuals. Formulas contain symbols and you need to properly rebuild them to make them dynamic. There are lots of different ways to do that, but here's one that uses reformulate to turn characters into formulas and update() to modify a base formula.

variables <- colnames(data)[7:dim(data)[2]]
form_resp <- ~ fp(age)
for(vari in variables) {
   print("ROI is:")
   form_model <- update(form, reformulate(".", response=vari))
   print(form_model)
   model_fem <- gamlss(form_model, family=GG, data=females)
}
MrFlick
  • 195,160
  • 17
  • 277
  • 295