Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

R language Nonlinear least two times


May 12, 2021 R language tutorial


Table of contents


When simulating real-world data for regression analysis, we observe that, in rare cases, the equations of the model are linear equations that give linear graphs. M ost of the time, the equations of real-world data models involve higher degrees of mathematical functions, such as the exponent of 3 or the sin function. I n this case, the diagram of the model gives the curve instead of the line. T he purpose of linear and nonlinear regression is to adjust the values of model parameters to find the line or curve closest to your data. W hen we find these values, we will be able to estimate the response variable with good accuracy.

In the least square regression, we build a regression model in which the sum of the vertical distances from different points of the regression curve is minimized. W e usually start with the defined model and assume some values of the coefficient. T hen we apply the nls() function of the R language to get a more accurate value and confidence interval.

Grammar

The basic syntax for creating nonlinear least-multiplier tests in the R language is -

nls(formula, data, start)

The following is a description of the parameters used -

  • Formula is a nonlinear model formula that includes variables and parameters.

  • data is a data frame used to calculate variables in formulas.

  • Start is a named list or named number vector for a start estimate.

Cases

We will consider a nonlinear model that assumes the initial value of its coefficient. N ext, we'll see what the confidence intervals for these hypothetical values are so that we can determine how good they are in the model.

So let's consider the following equation for this purpose -

a = b1*x^2+b2

Let's assume that the initial coefficients are 1 and 3 and fit these values into the nls() function.

xvalues <- c(1.6,2.1,2,2.23,3.71,3.25,3.4,3.86,1.19,2.21)
yvalues <- c(5.19,7.43,6.94,8.11,18.75,14.88,16.06,19.12,3.21,7.58)

# Give the chart file a name.
png(file = "nls.png")


# Plot these values.
plot(xvalues,yvalues)


# Take the assumed values and fit into the model.
model <- nls(yvalues ~ b1*xvalues^2+b2,start = list(b1 = 1,b2 = 3))

# Plot the chart with new data by fitting it to a prediction from 100 data points.
new.data <- data.frame(xvalues = seq(min(xvalues),max(xvalues),len = 100))
lines(new.data$xvalues,predict(model,newdata = new.data))

# Save the file.
dev.off()

# Get the sum of the squared residuals.
print(sum(resid(model)^2))

# Get the confidence intervals on the chosen values of the coefficients.
print(confint(model))

When we execute the code above, it produces the following results -

[1] 1.081935
Waiting for profiling to be done...
       2.5%    97.5%
b1 1.137708 1.253135
b2 1.497364 2.496484
R language Nonlinear least two times

We can conclude that the value of b1 is closer to 1, while the value of b2 is closer to 2 than 3.