0

My dataset shows negative binomial distribution, therefore, I want to use negative binomial regression to analyze it.

I followed the instruction described in this web site; https://stats.idre.ucla.edu/r/dae/negative-binomial-regression/

Actually, it worked well, I was able to analyze my data. However, I have many variable to analyze and I do not want to write a script as

linear <- glm(V1 ~ V2 + V3 + V4 + V5 + V6 + V7 + V8 + V9 + V10 ... V100, data = df1)

Let's say if I have 100 variables to analyze, how can I write an efficient code for regression to save my time? Although it works if I simply added everything like + V2 + V3 +V4.... till the end, I really do not want to.

Any comments should be helpful. Thank you.

1 Answers1

2

as.formula and paste to the rescue

> Vmax=10
> as.formula(paste0("V1~",paste0("V",2:(Vmax-1),sep="+",collapse=""),"V",Vmax,collapse=""))

V1 ~ V2 + V3 + V4 + V5 + V6 + V7 + V8 + V9 + V10
user2974951
  • 9,535
  • 1
  • 17
  • 24
  • Thank you so much! I got an exactly the same result. I do memorize the combination of as.formula with paste for future use. – user9690450 Oct 28 '21 at 10:36