- Given input term(s), we want to be able to predict an output/response using a linear model
- The simplest case: 1 input
- Longley’s Economic Regression Data, showing the relationship between the number of people employed and gross national product.
2024-06-09
lm()lm_econ <- lm(GNP ~ Employed, data=longley)
lm()(linear model) function accepts the inputs in the order of response ~ terms, plus the dataset in question.summary() function on lm_econ, we can see useful facts about the regression model. (See next slides.)lm() continuedThe first column of numbers under “Coefficients” shows the y-intercept and the slope, which is, in this case, the weight assigned to the variable “Employed”. The R-squared values are very close to 1, indicating a good fit.
lm() continuedWe can also see the p-values in the column Pr(>|t|). In this instance, the p-value is much smaller than the common threshold of 0.05, again indicating that our model is a good predictor.
y = fitted(lm_econ) as an argument to plotly’s add_lines() to graph our line of regression.geom_smooth(method = "lm") without using the lm_econ we previously calculated. By default, ggplot2 shows the confidence interval. lm(), we can add multiple inputs using +:lm_econ_multi <- lm(GNP ~ Employed + Armed.Forces, data=longley)