Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to fit a linear model with only a response variable?

Tags:

r

glm

lm

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.

like image 769
nachocab Avatar asked Dec 22 '22 02:12

nachocab


1 Answers

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.

like image 156
Josh O'Brien Avatar answered Dec 24 '22 01:12

Josh O'Brien