0

In R, the lm function allows you to fit multiple, separate linear models simultaneously - from the documentation for lm:

If response is a matrix a linear model is fitted separately by least-squares to each column of the matrix.

But how can you fit multiple separate models simultaneously for a GLM, e.g. for logistic regression?

glm.fit requires the response y to be a vector. If you give it a matrix in the y argument, then it just uses the first column of the y matrix and builds a single GLM model.

cmo
  • 3,762
  • 4
  • 36
  • 64
  • You'll need to just create a loop or use some sort of map/apply function. 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. – MrFlick Feb 04 '19 at 22:15
  • 1
    Perhaps you are overemphasizing the "simultaneously" part. When `y` is a matrix, `lm` doesn't estimate those separate models as a single, related system; that's just a shortcut ("*fitted separately*"). Clearly `glm` doesn't have this shortcut. Then if you want to implement one, that's fine (after following MrFlick's suggestions). – Julius Vainora Feb 04 '19 at 22:26
  • 3
    Actually, it does look `lm.fit()` might [do all of the fits simultaneously](https://github.com/wch/r-source/blob/trunk/src/library/stats/R/lm.R#L105-L117); it calls `dqrls` ([here](https://github.com/wch/r-source/blob/trunk/src/appl/dqrls.f)), which can solve the whole linear system simultaneously. But I agree that this isn't feasible (or at least not easy) for GLMs. – Ben Bolker Feb 04 '19 at 22:41
  • Can you clarify why you want to do this (computational efficiency or convenience or ... ?) – Ben Bolker Feb 04 '19 at 22:42
  • i need to fit several thousand logistic regressions. looping can get slow – cmo Feb 06 '19 at 03:17
  • so, yes, for computational efficiency – cmo Feb 06 '19 at 15:28

0 Answers0