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

If I do this, I will get two ratios (interception 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 only for the response variable? If I use the second formula with the first data frame, I get the wrong answer because "." also includes "year". In the second data frame, I cheat because there is only one column.

+4
source share
1 answer

Here is the spell you are looking for:

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

Using it with the 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 

In addition, as Ben Bollerker mentions, the R Introduction that comes with R includes a well-informative section on the grammar of the formula interface .

+10
source

Source: https://habr.com/ru/post/1393663/


All Articles