I tried to use the following code to extract the coefficients from the gnm object:
library(tidyverse)
library(gnm)
library(broom)
library(haven)
data = read_dta('londondataset2002_2006.dta')
data$ozone10 <- data$ozone/10
# GENERATE MONTH AND YEAR
data$month <- as.factor(months(data$date))
data$year <- as.factor(format(data$date, format="%Y") )
data$dow <- as.factor(weekdays(data$date))
data$stratum <- as.factor(data$year:data$month:data$dow)
data <- data[order(data$date),]
# FIT A CONDITIONAL POISSON MODEL WITH A YEAR X MONTH X DOW STRATA
modelcpr = vector(mode = 'list',length = 12)
for(i in 1:12){
modelcpr1 <- gnm(numdeaths ~ ozone10 +
lag(temperature,i), data=data, family=poisson,
eliminate=factor(stratum))
modelcpr[[i]] = broom::tidy(modelcpr1,conf.int = T, exponentiate = T) %>% slice(n()) %>%
select(estimate, conf.low, conf.high) %>% as.matrix %>% unname
}
#vs
#only for i = 1
modelcpr1 <- gnm(numdeaths ~ ozone10 +
lag(temperature,1), data=data, family=poisson,
eliminate=factor(stratum))
#broom::tidy
broom::tidy(modelcpr1,conf.int = T, exponentiate = T) %>% slice(n()) %>%
select(estimate, conf.low, conf.high) %>% as.matrix %>% unname
The dataset and part of the code are from this paper:
https://bmcmedresmethodol.biomedcentral.com/articles/10.1186/1471-2288-14-122#Sec13
After running the forloop, the following error popped up:
modelcpr = vector(mode = 'list',length = 12)
for(i in 1:12){
modelcpr1 <- gnm(numdeaths ~ ozone10 +
lag(temperature,i), data=data, family=poisson,
eliminate=factor(stratum))
modelcpr[[i]] = broom::tidy(modelcpr1,conf.int = T, exponentiate = T) %>% slice(n()) %>%
select(estimate, conf.low, conf.high) %>% as.matrix %>% unname
}
Error in profile.gnm(object, which = parm, alpha = 1 - level, trace = trace) :
profiling has found a better solution, so original fit had not converged
In addition: Warning message:
In sqrt((deviance(updated) - fittedDev)/disp) : NaNs produced
where i = 1.
However, when I run the for loop one by one:
> modelcpr1 <- gnm(numdeaths ~ ozone10 +
+ lag(temperature,1), data=data, family=poisson,
+ eliminate=factor(stratum))
>
> #broom::tidy
> broom::tidy(modelcpr1,conf.int = T, exponentiate = T) %>% slice(n()) %>%
+ select(estimate, conf.low, conf.high) %>% as.matrix %>% unname
[,1] [,2] [,3]
[1,] 1.000446 0.9988817 1.002013
I can obtain the result without any error. Is there any thing that I missed?
Great question; thanks for including the relevant details. I started working through the source code of
gnm()but couldn't figure out the root cause of the problem. I also tried converting the for-loop into a function and usinglapply(),Map()andpurrr::map(), and got the same result.As a potential workaround, perhaps you could run
for (i in 2:12)in the loop and then add the problematic result fori = 1'manually', e.g.Created on 2023-10-11 with reprex v2.0.2
Would that solve your problem?