Linear functions

To describe and predict how variables are associated, a key mathematical tool is the function.

A function is a mathematical expression that

In DAPR2, we focus on linear functions, which have the form \(y = c + mx\).

Take, for example, the function \(y = 2 + 5x\). This function describes a line with a slope of 5 and a y-intercept (in stats we just call it an “intercept”) of 2. That function looks like this:

Code
len_out <- 10

p_line <- tibble(
  x = seq(0, 10, length.out = len_out),
  y = seq(0, 40, length.out = len_out)
) |>
  ggplot(aes(x = x, y = y)) +
  geom_blank() +
  geom_abline(intercept = 2, slope = 5) +
  scale_y_continuous(expand = c(0,0)) +
  scale_x_continuous(breaks = seq(0, 10, 2), expand = c(0,0)) +
  NULL

p_line

Here’s how we use this linear function to predict \(y\) based on \(x\):

Example 1: \(x = 2\)

\[ \begin{align} y &= 2 + 5(2) \\ &= 2 + 10 \\ &= 12 \end{align} \]

Code
p_line +
  # arrow from x to line
  geom_segment(x = 2, xend = 2, y = 0, yend = 12, colour = dapr2red, arrow = arrow(length = unit(0.5,"cm"))) +
  geom_text(x = 2 + 0.1, y = 6, label = 'if x = 2 ...', colour = dapr2red, hjust = 0) +
  # arrow from line to y
  geom_segment(x = 2, xend = 0, y = 12, yend = 12, colour = dapr2red, arrow = arrow(length = unit(0.5,"cm"))) +
  geom_text(x = 1, y = 12 + 3, label = '... then y = 12', colour = dapr2red, hjust = 0.5) + 
  NULL

Example 2: \(x = 6\)

\[ \begin{align} y &= 2 + 5(6) \\ &= 2 + 30 \\ &= 32 \end{align} \]

Code
p_line +
  # arrow from x to line
  geom_segment(x = 6, xend = 6, y = 0, yend = 32, colour = dapr2red, arrow = arrow(length = unit(0.5,"cm"))) +
  geom_text(x = 6 + 0.1, y = 16, label = 'if x = 6 ...', colour = dapr2red, hjust = 0) +
  # arrow from line to y
  geom_segment(x = 6, xend = 0, y = 32, yend = 32, colour = dapr2red, arrow = arrow(length = unit(0.5,"cm"))) +
  geom_text(x = 3, y = 32 + 3, label = '... then y = 32', colour = dapr2red, hjust = 0.5) + 
  NULL

Because you always get the same output every time you give the function the same input, a linear function like this is an example of a deterministic function.