Skip to Content
Course content
Click on the "Edit" button in the top corner of the screen to edit your slide content.

A few years ago, I was working on a performance audit for a legacy API. I had a strong hunch that the response time was scaling linearly with the size of the JSON payload we were returning, but my lead didn't want "hunches"—he wanted a number. He wanted to know exactly how many milliseconds we were adding for every additional kilobyte of data. I spent an afternoon gathering logs, and instead of just eyeballing a scatter plot, I used a linear model to quantify the relationship. It turned out the "cost" per KB was much higher than we expected, which gave us the leverage we needed to prioritize a pagination refactor. That's the power of linear models: they turn a visual trend into a mathematical certainty.

The Anatomy of the lm() Formula

In R, the heavy lifting for linear regression is handled by the lm() function. The most important thing to wrap your head around is the formula syntax: y ~ x. In this expression, y is your dependent variable (the thing you're trying to predict) and x is your independent variable (the predictor). If you think of it as "y is a function of x," it usually clicks.

# A simple example: predicting response time based on payload size
payload_size <- c(10, 20, 30, 40, 50) # in KB
response_time <- c(105, 210, 290, 410, 505) # in ms

# Fit the linear model
perf_model <- lm(response_time ~ payload_size)

I've noticed that beginners often try to pass the variables as simple vectors without the formula, but R expects that tilde (~). It's a shorthand that allows the function to handle complex data frames and multiple predictors effortlessly.

Decoding the Model Summary

Running lm() creates a model object, but if you print that object directly, it only gives you the coefficients. To actually understand what's happening under the hood, you need to wrap it in the summary() function. This is where the real engineering happens.

summary(perf_model)

When you look at the output, focus on the Coefficients table. The (Intercept) is where the line hits the y-axis (essentially, the baseline response time when the payload is zero). The slope—the value next to payload_size—is the "cost" we were looking for. If that coefficient is 10, it means for every 1 KB increase in payload, the response time increases by 10 ms. Also, keep an eye on the Pr(>|t|) column; if that value is very small (typically < 0.05), you can be confident that the relationship isn't just a fluke of your sample data.

Visualizing the Fit

While the numbers are great for reports, I always recommend plotting the model. A linear model is essentially just drawing the "best" straight line through a cloud of points. You can use the abline() function to overlay your model's predictions onto a standard scatter plot.

# Plot the raw data
plot(payload_size, response_time, pch = 16, col = "blue", 
     xlab = "Payload Size (KB)", ylab = "Response Time (ms)")

# Add the regression line
abline(perf_model, col = "red", lwd = 2)

Seeing the line visually helps you spot "outliers"—those weird data points that might be skewing your results. If one point is miles away from the line, it might be a network glitch or a cold-start issue rather than a trend in your code's performance.




📋 Practical Task

Analyzing Bug Density vs. Code Complexity

You've been handed a dataset from a QA lead who suspects that modules with higher "Cyclomatic Complexity" (a measure of how many paths there are through the code) have a higher number of reported bugs. Your task is to quantify this relationship.

Use the following data to build a linear model and determine the impact of complexity on bug counts:

complexity <- c(2, 5, 8, 12, 15, 18, 22, 25, 30, 35)
bugs <- c(1, 3, 4, 7, 8, 10, 12, 15, 17, 21)

Requirements:

  • Create a linear model named bug_model where bugs is the dependent variable.
  • Use summary() to find the coefficient for complexity.
  • Create a scatter plot of the data and overlay the regression line using abline().
  • Print the specific slope value to the console (e.g., "Each unit of complexity adds X bugs").
Rating
0 0

There are no comments for now.

to be the first to leave a comment.