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. 

Similar Reads

Visualization of Dataframe:

To fit a curve to some data frame in the R Language we first visualize the data with the help of a basic scatter plot. In the R language, we can create a basic scatter plot by using the plot() function....

Create Several Curves to fit on data

...

Best fit curve with adjusted r squared value

Then we create linear regression models to the required degree and plot them on top of the scatter plot to see which one fits the data better. We use the lm() function to create a linear model. And then use lines() function to plot a line plot on top of scatter plot using these linear models....

Visualize Best fit curve with data frame:

...

Contact Us