2025-03-15

Introduction

Simple linear regression models a graph between a dependent variable, normally y, and an independent variable, normally x.

The equation for a simple linear regression is: \[ y = a + bx \]

where a is the intercept and b is the slope.

Other Concepts of Simple Linear Regression

You use the sum of squared errors, or SSE, to find the best-fitting line for the regression model. The equation for SSE is:

\[ SSE = \sum_{i=1}^{n} (y_i - \hat{y}_i)^2 \]

y_i is the actual value of y, while {y}_i is the predicted value of y from the model. Using those values in the equation above gives the best-fitting line for the regression model.

Simulated Data

A dataset is created manually with 6 points:

x <- c(10, 11, 12, 13, 14, 15)
y <- c(7, 10, 15, 18, 19, 23)

data = data.frame(x,y)

Plotting the Data

There are multiple ways of plotting a simple linear regression in R. For example, you can use ggplot2, which will be the first way shown.

Implement ggplot2.

library(ggplot2)

Then plot.

ggplot Scatter Plot with Regression Line

ggplot(data, aes(x=x, y=y)) +
  geom_point(color="maroon", size=3) +
  geom_smooth(method="lm", color="gold", se=FALSE) +
  labs(title="Simple Linear Regression", x="x", y="y") +theme_minimal()

ggplot Scatter Plot with No Regression Line

ggplot(data, aes(x=x, y=y)) +
  geom_point(color="purple", size=3) +
  labs(title="Scatter Plot", x="x", y="y") +
  theme_minimal()

Plotly Graphs

Another main way of plotting graphs in R is by using plotly.

Implement plotly and make a z variable for 3D plotting.

library(plotly)
data$z <- rep(1, length(data$x))

Then plot.

3D Scatter Plot with plotly

plot_ly(data, x = ~x, y = ~y, z = ~z, type = "scatter3d", mode = "markers") %>%
  layout(title = "3D Scatter Plot",
         scene = list(xaxis = list(title = "x"),
                      yaxis = list(title = "y"),
                      zaxis = list(title = "Constant Z")))

2D Interactive Scatter Plot with plotly

plot_ly(data, x = ~x, y = ~y, type = "scatter", mode = "markers+lines") %>%
  layout(title = "Interactive 2D Scatter Plot",
         xaxis = list(title = "x"),
         yaxis = list(title = "y"))