Best fit curve with adjusted r squared value
Now since we cannot determine the better fitting model just by its visual representation, we have a summary variable r.squared this helps us in determining the best fitting model. The adjusted r squared is the percent of the variance of Y intact after subtracting the error of the model. The more the R Squared value the better the model is for that data frame. To get the adjusted r squared value of the linear model, we use the summary() function which contains the adjusted r square value as variable adj.r.squared.
Syntax:
summary( linear_model )$adj.r.squared
where,
- linear_model: determines the linear model whose summary is to be extracted.
Example:
R
# create sample data sample_data <- data.frame (x=1:10, y= c (25, 22, 13, 10, 5, 9, 12, 16, 34, 44)) # fit polynomial regression models up to degree 5 linear_model1 <- lm (y~x, data=sample_data) linear_model2 <- lm (y~ poly (x,2,raw= TRUE ), data=sample_data) linear_model3 <- lm (y~ poly (x,3,raw= TRUE ), data=sample_data) linear_model4 <- lm (y~ poly (x,4,raw= TRUE ), data=sample_data) linear_model5 <- lm (y~ poly (x,5,raw= TRUE ), data=sample_data) # calculated adjusted R-squared of each model summary (linear_model1)$adj.r.squared summary (linear_model2)$adj.r.squared summary (linear_model3)$adj.r.squared summary (linear_model4)$adj.r.squared summary (linear_model5)$adj.r.squared |
Output:
[1] 0.07066085 [2] 0.9406243 [3] 0.9527703 [4] 0.955868 [5] 0.9448878
Curve Fitting in R
In this article, we will discuss how to fit a curve to a dataframe in the R Programming language.
Curve fitting is one of the basic functions of statistical analysis. It helps us in determining the trends and data and helps us in the prediction of unknown data based on a regression model/function.
Contact Us