# Run a generalized linear regression
glm(
# Model no. of visits vs. gender, income, travel
n_visits ~ gender + income + travel,
# Use the snake_river_visits dataset
data = snake_river_visits,
# Make it a Poisson regression
family = poisson
)
# From previous step
run_poisson_regression <- function(data, formula) {
glm(formula, data, family = poisson)
}
# Re-run the Poisson regression, using your function
model <- snake_river_visits %>%
run_poisson_regression(n_visits ~ gender + income + travel)
I don't understand. Before we create the function, I know glm used the form of y~x1+x2+x3. But in the function we create, the 2 arguments don't have such y and x. How can I use this function to do glm?
If I asked you to draw a graph of y=x^2 you would know what to do.
And the same, I suppose, for the case a=b^2 .
The formula in glm goes in exactly the same way:
a dependent variable before the ~ and the dependent variables after the ~ .
How these variables are called is not important; only the role they play : dependent of independent.
model <- snake_river_visits %>%
run_poisson_regression(n_visits ~ gender + income + travel)
uses the magrittr package but means
model <- run_poisson_regression(snake_river_visits,
n_visits ~ gender + income + travel)
where snake_river_visits plays the role of data and n_visits ~ gender + income + travel plays the role of formula in the function run_poisson_regression .