0

I am trying to write a macro that can repeat over many independent variables. The dependent variable, outcome, is the same. I don't want to type independent variables over and over again. For example, I have variable age and gender, and I want to use it in my regression.

However, I am getting errors.

Here is my function:

lapply(c("age", "gender"),
       function (var) {
         formula <- as.formula(paste("cov1 =", var))
         shr_fit <-
           crr(
             ftime = followuptime,
             fstatus = followupstatus,
             formula, # this is where I want to apply the macro #
             cencode = 0,
             failcode = 1,
             data=data1
           )
         summary(shr_fit)
       }
)
Jinny
  • 1
  • 1
  • 2
    Can you provide some more information on what errors you are getting? Note that in R, formula notation normally uses a `~` not an `=`, so you may want to change that in the formula line (line 3). – nrennie Feb 22 '23 at 20:37
  • error: Error in class(ff) <- "formula" : cannot set attribute on a symbol. if I dont use the macro, where it is the formula I would write 'cov1=age', so on and so forth. – Jinny Feb 22 '23 at 20:40
  • What errors are you getting? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. R doesn't really have "macros"; things tend to be implemented as function. Macros typically generate code in text form while functions manipulate native language objects. Please also include the non-function form so we can run and test. – MrFlick Feb 22 '23 at 20:43
  • Please provide enough code so others can better understand or reproduce the problem. – Community Feb 22 '23 at 20:54

0 Answers0