# KeepNotes blog

Stay hungry, Stay Foolish.

0%

$\hat{h} = \theta_0 + \theta_1 x^1 + \ ... \ + \theta_{n-1} x^{n-1} + \theta_n x^n$

$\hat{h} = X \cdot \theta$

#### Fitting Polynomial Regression in R

data <- data.frame(x = c(0.50, 1.00, 1.50, 2.00, 3.00, 4.00, 5.00),
y = c(6.24, 14.21, 23.31, 32.62, 49.28, 67.65, 85.59))

fit.plm <- lm(y ~ poly(x, degree = 2), data = data)

fit.plm <- lm(y ~ x + I(x^2), data = data)

If you just want a nudge in the right direction without quite so much judgement: poly() creates orthogonal (not correlated) polynomials, as opposed to I(), which completely ignores correlation between the resultant polynomials. Correlation between predictor variables can be a problem in linear models, so it's probably better (in general) to use poly() instead of I().

fit.plm <- lm(y ~ poly(x, degree = 2, raw = TRUE), data = data)

> summary(fit.plm)

Call:
lm(formula = y ~ x + I(x^2), data = data)

Residuals:
1         2         3         4         5         6         7
0.191786 -0.451418 -0.021044  0.562907 -0.398458  0.124486 -0.008259

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -2.50857    0.53285  -4.708  0.00925 **
x           17.05714    0.48219  35.374 3.81e-06 ***
I(x^2)       0.11284    0.08583   1.315  0.25888
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.4278 on 4 degrees of freedom
Multiple R-squared:  0.9999,    Adjusted R-squared:  0.9998
F-statistic: 1.384e+04 on 2 and 4 DF,  p-value: 2.088e-08

sjPlot::tab_model(fit.plm, show.se = T, show.stat = T, show.df = T, show.fstat = T, show.dev = T)