Original link:tecdat.cn/?p=22438 

Original source:Tuo End number according to the tribe public number

 

Polynomial regression is a nonlinear relationship between independent X variables and causal Y variables.

Fitting this type of regression is critical when we analyze volatility data with some bending. In this article, we will learn how to fit and plot polynomial regression data in R. We use lm() function in this regression model. Although it is a linear regression model function, lm() also works for polynomial models by changing the target formula type. This tutorial includes

  1. To prepare data
  2. Fitting model
  3. Look for the best fit
  4. The source code

To prepare data

We first need to prepare the test data, as shown below.

function(x) x^3+2*x^2+5

 
df = data.frame(x = x, y = y)
head(df)
Copy the code



We can visualize the ‘DF’ data and check it visually in the graph. Our task is to fit this data with the best curve.

plot(df$x, df$y
Copy the code

 

Fitting model

We use lm() to build a model with a formula. I of x squared is x2 in a formula. We can also use the poly(x,2) function, which is expressed in the same way as I(x^2).



Next, we will use the trained model to predict the data.

pred = predict(model,data=df)
Copy the code

Look for the best fit

It’s important to find the curve that best fits. We examine the model with all possible functions. Here, we apply four types of functions to fit and examine their performance. The orange line (linear regression) and yellow curve are the wrong choices for this data. The pink curve is close, but the blue curve is the one that best matches the trend in our data. Therefore, I use the y~x3+x2 formula to build our polynomial regression model. You can find the formula that works best by visualizing your data.

The source code is listed below.

Lines (df $x, predict (lm (~ x, y data = df)), type = "l" LWD = 2) legend (" topleft ", legend = c (" ~ x, y - linear ", "y ~ x ^ 2", "y ~ ^ 3 x", "y~x^3+x^2"),Copy the code

Draw the result

1. Plot with the plot() function.

 

2. Plot with ggplot(). Polynomial regression data can be fitted and plotted using ggplot().

ggplot(data=df ) +
       geom_smooth(  y~I(x^3)+I(x^2))
Copy the code

 

In this tutorial, we briefly looked at how to fit polynomial regression data and plot the results using the plot() and ggplot() functions in R. The complete source code is below.


Most welcome insight

1.R language multiple Logistic Logistic regression application case

2. Panel smooth transfer regression (PSTR) analysis case implementation

3. Partial least squares regression (PLSR) and principal component regression (PCR) in MATLAB

4.R language Poisson regression model analysis cases

5. Hosmer-lemeshow goodness of fit test in R language regression

6. Implementation of LASSO regression, Ridge regression and Elastic Net model in R language

7. Realize Logistic Logistic regression in R language

8. Python predicts stock prices using linear regression

9. How to calculate IDI and NRI indices for R language in survival analysis and Cox regression