Question

If I do this, I get two coefficients (intercept and year)

data <- data.frame(accidents=c(3,1,5,0,2,3,4), year=1:7)
glm(accidents ~ year, family=poisson(link = log), data)

Coefficients:
(Intercept)         year  
     0.7155       0.0557

But the correct answer is 0.944

data <-data.frame(accidents=c(3,1,5,0,2,3,4))
glm(accidents ~ ., family=poisson(link=log), data)

Coefficients:
(Intercept)  
  0.944 

Is there a way to specify the glm formula for just the response variable? If I use the second formula with the first data frame I get the wrong answer because the "." also includes the "year". In the second data frame I'm cheating because there is only one column.

Was it helpful?

Solution

Here's the incantation that you are looking for:

glm(accidents ~ 1, family=poisson(link = log), data)

Using it with your original data frame:

data <- data.frame(accidents=c(3,1,5,0,2,3,4), year=1:7)
coef(glm(accidents ~ 1, family=poisson(link = log), data))
(Intercept) 
  0.9444616 

Also, as Ben Bolker mentions, the R Introduction document that ships with R includes a nicely informative section on the grammar of the formula interface.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top