문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top