0

Suppose the dataset, my_data, looks like this

Count Covariate offset_a offset_b
... ... ... ...

while using offset_a as my offset, the below statement would work:

glm(my_formula, data = my_data, family = 'quasipoisson', offset = offset_a)

However, if I want to write a loop that passes offset_a and offset_b sequentially into glm(), I wouldn't be able to simply pass a character value into the offset argument. Like this:

offset_list <- c("offset_a", "offset_b")

for (k in offset_list){
  ...
  fit = glm(my_formula, data = my_data, family = 'quasipoisson', offset = k)
  ...
}

I tried applying noquote() to the offset, as

fit = glm(my_formula, data = my_data, family = 'quasipoisson', offset = noquote(k))

but it doesn't work either.

Does anyone know how to achieve this?

Furtherly,

if we have a character variable,

statement = "glm(my_formula, data = my_data, family = 'quasipoisson', offset = offset_a)"

Is there any way to pass variable, statement, into glm() directly and get the output successfully?

Much Appreciated!

XiapingZ
  • 11
  • 2
  • You are asking multiple questions, all of which have been asked and answered on SO before. Read [tour], [ask], and [MCVE] – IRTFM Jul 09 '22 at 23:18
  • Hi @IRTFM, thanks for your comment! I am sorry if my question is duplicated. I asked because I have searched for a long time but still couldn't find the answer, probably due to unsuitable key words. I will keep working on it. :) – XiapingZ Jul 09 '22 at 23:26
  • Narrow your question and search on the use of `as.formula` for converting from text to language objects in R. If you really did a diligent search then posting the search strategy and the results that were unhelpful would save time for your audience. (and the reason I'm not offering any hints on your second part is my desire to not encourage the posting of multi-part questions. – IRTFM Jul 09 '22 at 23:33
  • Nominations for duplicate: https://stackoverflow.com/questions/33098644/how-to-create-a-loop-that-will-add-new-variables-to-a-pre-define-glm-model/33099169#33099169 .. https://stackoverflow.com/questions/27318944/r-multiple-friedman-tests/27319403#27319403 I'm not able to revise my close action to use my dupe-hammer. – IRTFM Jul 09 '22 at 23:44

3 Answers3

2

Probably the easiest way to do this is to put the offset term directly into the formula with reformulate:

resp_var <- "y"
form0 <- "var1 + var2 + var3*var4"
offset_list <- c("offset_a", "offset_b")
for (k in offset_list){
  ...
  form <- reformulate(response = resp_var,
                      c(form0, sprintf("offset(%s)", k)))
  fit <- glm(form, data = my_data, family = 'quasipoisson')
  ...
}

offset = get(k) would probably also work.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
1

You can use this

offset_list <- c("offset_a", "offset_b")
fit <- list()
for (k in 1:length(offset_list)){
    fit[[k]] = glm(my_formula, data = my_data, family = 'quasipoisson',
    offset = get(offset_list[k]))
}
Mohamed Desouky
  • 4,340
  • 2
  • 4
  • 19
0

Just found another solution myself.

According to the documentation of glm, the argument 'offset' could be

NULL or a numeric vector of length equal to the number of cases.

So we could directly put the column 'offset_x' as vector into the offset argument.

Like this:

offset_list <- c("offset_a", "offset_b")

for (offset in offset_list){
  ...
  fit = glm(my_formula, data = my_data, family = 'quasipoisson', offset = my_data[[offset]])
  ...
}

I hope this helps.

XiapingZ
  • 11
  • 2