Link to original article:tecdat.cn/?p=22531 

Original source:Tuoduan numbers according to the public account of the tribe

 

For a linear relationship, we can do a simple linear regression. For other relationships, we can try to fit a curve.

Curve fitting is the process of constructing a curve or mathematical function, which has the best fitting effect for a series of data points.

 

Use the sample data set

# We will make Y the dependent variable and X the predictive variable # The dependent variable is usually on the Y-axis plot(X, Y, PCH =19)Copy the code

 

It looks like we can fit a curve.

# Fit a polynomial equation of degree. Fit < - lm (y ~ x) # secondary fit2 < - lm (y ~ poly real (x, 2) # 3 times... Xx < -seq (30,160, length=50) lines(xx, predict(fit, xx)Copy the code

 

We can see how well each curve fits. We can use the summary() function to make more detailed statistics of the fitting results.

 

 

Summary using different polynomials R squared.

1st: 0.5759
2nd: 0.9474
3rd: 0.9924
4th: 0.9943
Copy the code

We can use an “analysis of variance” to compare different models.

 

The value of Pr(>F) is the probability of rejecting an invalid hypothesis, i.e. one model is no better than the other. We have a very significant P-value, so we can reject the invalid hypothesis that FIT2 provides a better fit than FIT.

We can also create a function that reflects a polynomial equation.

 

The values calculated from the cubic polynomial fit well with the original values, as we can see from the values of R-Squared.

conclusion

For nonlinear curve fitting, we can use the LM () and poly() functions, which also provide useful statistics on how well the polynomial function fits the data set. We can also use anOVA tests to assess the degree of contrast between different models. From the model you can define a function that reflects the polynomial function, which can be used to calculate the dependent variable.

yy<-third(xx,fit)

plot(xx,yy)
Copy the code


Most popular insight

1. Application case of R language multiple Logistic Logistic regression

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

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

4. Analysis case of R language Poisson regression model

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

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

7. Implement Logistic Logistic regression in R language

8. Python uses linear regression to predict stock prices

9. How did R language calculate IDI and NRI indicators in survival analysis and Cox regression