StatsCalculators.com

Quadratic Regression

Created:April 6, 2025

This Quadratic Regression Calculator helps you analyze data that follows a parabolic pattern. It fits data to the modely=ax2+bx+cy=ax^2+bx+c, providing comprehensive analysis including model coefficients, goodness-of-fit statistics, extremum point detection, and diagnostic plots. This is useful for modeling data with U-shaped or inverted U-shaped relationships, such as optimal pricing, ballistic trajectories, and many physical processes. To learn about the data format required and test this calculator, click here to populate the sample data.

Calculator

1. Load Your Data

Note: Column names will be converted to snake_case (e.g., "Product ID" → "product_id") for processing.

2. Select Columns & Options

Related Calculators

Learn More

Quadratic Regression

Definition

Quadratic Regression models the relationship between a predictor variable (X) and a response variable (Y) as a parabola. The model has the form y=ax2+bx+cy=ax^2+bx+c, where aa, bb, and cc are constants determined by the regression. When a>0a > 0, the parabola opens upward; when a<0a < 0, it opens downward.

Key Formulas

Quadratic Model:

y=ax2+bx+cy = ax^2 + bx + c

Vertex Formula (Minimum or Maximum Point):

x=b2ax = -\frac{b}{2a}

Y-value at Vertex:

y=c+b(b2a)+a(b2a)2y = c + b\left(-\frac{b}{2a}\right) + a\left(-\frac{b}{2a}\right)^2

Derivative of the Quadratic Function:

dydx=2ax+b\frac{dy}{dx} = 2ax + b

Key Assumptions

Quadratic relationship: The relationship between X and Y follows a parabolic pattern
Independence: Observations are independent of each other
Homoscedasticity: Variance of residuals is constant across all levels of X
Normality: Residuals are approximately normally distributed

Practical Example

Step 1: Data
Time (hours)Distance (km)
00
125
240
345
440
525
Step 2: Fit Quadratic Model

After regression analysis, we get the equation:

y=5x2+30x+0y = -5x^2 + 30x + 0
Step 3: Calculate the Vertex

The x-coordinate of the vertex is:

x=b2a=302(5)=3x = -\frac{b}{2a} = -\frac{30}{2 \cdot (-5)} = 3

The y-coordinate of the vertex is:

y=0+303+(5)32=9045=45y = 0 + 30 \cdot 3 + (-5) \cdot 3^2 = 90 - 45 = 45
Step 4: Interpret Results

The quadratic equation models the distance traveled by an object that initially accelerates and then decelerates.

The vertex at (3, 45) indicates that the maximum distance of 45 km is reached after 3 hours.

The negative coefficient of x² (-5) indicates a downward opening parabola, consistent with a maximum point.

Code Examples

R
library(tidyverse)

set.seed(42)
data <- tibble(
  x = seq(-5, 5, by = 0.5),
  y = 2*x^2 - 5*x + 3 + rnorm(21, 0, 5) 
)

# Fit quadratic model
quad_model <- lm(y ~ x + I(x^2), data = data)
summary(quad_model)

# Compare with linear model
linear_model <- lm(y ~ x, data = data)
anova(linear_model, quad_model)

# Extract coefficients
a <- coef(quad_model)[3]  # x^2 term
b <- coef(quad_model)[2]  # x term
c <- coef(quad_model)[1]  # Intercept

# Calculate vertex (minimum/maximum point)
x_vertex <- -b/(2*a)
y_vertex <- c + b*x_vertex + a*x_vertex^2

str_glue("Quadratic function: y = {a} x^2 + {b} x + {c}")
str_glue("Vertex point: ({x_vertex}, {y_vertex})")
str_glue("This is a {ifelse(a > 0, 'minimum', 'maximum')}")

# Plot the results
ggplot(data, aes(x = x, y = y)) +
  geom_point() +
  geom_smooth(method = "lm", formula = y ~ x + I(x^2), color = "red") +
  geom_vline(xintercept = x_vertex, linetype = "dashed", color = "green") +
  annotate("point", x = x_vertex, y = y_vertex, color = "green", size = 3) +
  labs(title = "Quadratic Regression",
       subtitle = str_glue("y = {round(a, 3)}x² + {round(b, 3)}x + {round(c, 3)}"),
       x = "x",
       y = "y") +
  theme_minimal()
Python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
import statsmodels.api as sm

np.random.seed(42)
x = np.linspace(-5, 5, 40)
y = 2*x**2 - 5*x + 3 + np.random.normal(0, 5, size=len(x))
data = pd.DataFrame({'x': x, 'y': y})

# Fit quadratic model
X = sm.add_constant(np.column_stack((x, x**2)))
model = sm.OLS(y, X).fit()
print(model.summary())

# Extract coefficients
c = model.params[0]  # Intercept
b = model.params[1]  # x coefficient
a = model.params[2]  # x^2 coefficient

print(f"Quadratic equation: y = {a:.4f}x² + {b:.4f}x + {c:.4f}")

# Calculate vertex (minimum/maximum)
x_vertex = -b / (2*a)
y_vertex = c + b*x_vertex + a*x_vertex**2

print(f"Vertex point: ({x_vertex:.4f}, {y_vertex:.4f})")
print(f"This is a {'minimum' if a > 0 else 'maximum'}")


# Plot the results
plt.figure(figsize=(10, 6))
plt.scatter(x, y, alpha=0.7, label='Data')

# Plot regression line
x_sorted = np.sort(x)
X_sorted = sm.add_constant(np.column_stack((x_sorted, x_sorted**2)))
y_pred = model.predict(X_sorted)
plt.plot(x_sorted, y_pred, 'r-', label='Quadratic Fit')

# Mark vertex
plt.axvline(x=x_vertex, color='green', linestyle='--', alpha=0.5)
plt.plot(x_vertex, y_vertex, 'go', markersize=8, label=f"{'Minimum' if a > 0 else 'Maximum'} Point")

plt.title(f"Quadratic Regression: y = {a:.2f}x² + {b:.2f}x + {c:.2f}")
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()

Verification