2025-03-11

Simple Linear regression

Shows the relationship between an independent variable X and the dependent variable Y

This relationship can be model by the following equation:

\[Y = B_0 + B_1 \cdot X\]

Residual Standard Error

The residual can be used to determine how well a linear regression fits the model

The residual of one point can be represented by the following expression:

\[\widehat{y} = B_0 + B_1 \cdot i\]

\[e_i = y_i - \widehat{y}\]

This is the error of the model, but since the \(e_i\) can be positive or negative, to prevent the error from canceling itself out. The sum Squared of the residual is used instead to show how well a linear regression model represent the data.

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

How to plot a Linear Regression graph with Plotly

R code to graph a linear regression:

x = iris$Petal.Width
y = iris$Petal.Length
mod = lm(Petal.Length~Petal.Width, data = iris)
xaxis = list(
  title = "Pedal Width"
)
yaxis = list(
  title = "Pedal Length"
)
graph = plot_ly(x = x,y = y, type = "scatter", 
    mode = "markers", name = "data") %>%
  add_lines(x = x, y = fitted(mod), name = "fitted")%>%
  layout(xaxis = xaxis, yaxis = yaxis)
#to display the graph add: graph

Resulting Graph from the R code

Linear Regression model using ggplot

The same data can be graph using ggplot(less interactive)

Residual plot using ggplot

Conclusion

A simple linear regression model is a powerful tool. Combining it with ggplot and ploty, can make it especially useful to get a visual understanding of linear regression model.