fit a glm model in r

To fit a glm model in R, you can use the built-in glm() function. The basic syntax of glm() is:

glm(formula, family, data)
27 chars
2 lines

where:

  • formula is a symbolic description of the model.
  • family is a description of the error distribution and link function to be used in the model.
  • data is the data set containing the variables used in the model.

For example, to fit a linear regression model with normal errors, you could use:

fit <- glm(y ~ x1 + x2, family = gaussian, data = mydata)
58 chars
2 lines

This model assumes a normal distribution of errors and uses the default identity link function.

You can also fit models with other error distributions and link functions, such as logistic regression or Poisson regression, by specifying the appropriate family argument.

fit2 <- glm(y ~ x1 + x2, family = binomial, data = mydata) # logistic regression
fit3 <- glm(y ~ x1 + x2, family = poisson, data = mydata) # Poisson regression
160 chars
3 lines

Once you have fitted the model, you can use the summary() function to obtain a summary of the model and the predict() function to obtain predicted values for new data.

gistlibby LogSnag