The "Model" chapter of R4DS walks through some examples of setting up a regression and exploring it.
http://r4ds.had.co.nz/model.html
For your particular problem, I'd suggest two steps:
- Transform your data set to get the variables you want. In this case, you may want variables that show a few of the prior years' values.
fake_lynx_data <-
data.frame(year = 1900:2017,
lynx = runif(118, 500, 1000)) # 118 years of random fake counts
# New data frame with more variables we can refer to later
fake_lynx_data_addl_vars <-
fake_lynx_data %>%
mutate(lynx_1yr_prior = lag(lynx, 1),
lynx_2yr_prior = lag(lynx, 2),
lynx_3yr_prior = lag(lynx, 3))
- Fit a model to those variables. Since I used totally random data, this model doesn't predict well:
# This uses the 'lm' function to make a linear model to predict 'lynx' based on
# independent relationships to the 3 prior years' counts.
lm(lynx ~ lynx_1yr_prior + lynx_2yr_prior + lynx_3yr_prior,
data = fake_lynx_data_addl_vars) %>%
summary()
# Given that the counts are partly dependent on the counts in prior years, you
# could also try a model that also uses the interactions between those prior counts.
lm(lynx ~ lynx_1yr_prior * lynx_2yr_prior * lynx_3yr_prior,
data = fake_lynx_data_addl_vars) %>%
summary()