How to use ols inside a function

hi … I am trying to perform ols on multiple datasets programmatically. Looks like I am unable to set the datadist option within a function. What is the best way to approach this. Below is a MWE

library(rms)
chk_dd <- function(.data){
  mydata = .data
  dd   = datadist(mydata)
  #list2env(dd)
  options(datadist = "dd")
  f1 = ols(conc ~ Wt,  x = T, y = T)
  ggplot(Predict(f1))
}

chk_dd(Theoph)

1 Like

Your example is not reproducible because you didn’t define Theoph. I created a test dataset to identify the problem:

  • You left conc and Wt “open” and should have added data=.data in the ols call
  • datadist must be run in the global (root) environment. You can use assign to put the dd object there is you really need to.

You’ll get an error in Predict(f1) since it is called within a function. That bug will be fixed in rms 6.0.1 which I’ll submit to CRAN tomorrow.

1 Like

ah… sorry. Theoph is an inbuilt dataset in R, I should have been explicit about it.

Yeah, I tried assigning to the global env using assign, but I was still erroring out at Predict.

Thanks for looking into this

1 Like