We have one predictor \(x\) but the plot of \(y\) vs \(x\) exhibits a quadratic pattern. See simulated data below:
x1 <- runif(100)
y <- 0.2 + 2 * x1 + 5 * x1^2 + rnorm(100, 0, 0.3)
plot(x1, y)
Then we can fit a multiple regression model:
\[\mathbb{E}[y] = \beta_0 + \beta_1 x + \beta_2 x^2. \]
This is also called a quadratic regression model or, more generally, a polynomial regression model. To fit in R:
fit <- lm(y ~x1 + I(x1^2))
coef(fit)
## (Intercept) x1 I(x1^2)
## 0.2315906 1.5892734 5.3925940
Visualize this fit:
#make a grid
xnew <- seq(0,1, by = 0.1)
xnewsq <- xnew^2
f <- function(x,xsq) { r <- coef(fit)[1] + coef(fit)[2] * x + coef(fit)[3] * xsq }
z <- outer(xnew, xnewsq, f)
persp3d(xnew, xnewsq, z, col = "lightblue", xlab = "X1", ylab = "X1^2", zlab = "Y")
points3d(x1,x1^2,y,col="red")
You must enable Javascript to view this page properly.