wwblog

Willie Wheeler's personal blog. Mostly tech.

Nonlinear Regression in R

Yesterday I wrote about how to do polynomial regression in R, and noted that it's really a form of linear regression.

This post shows how to do nonlinear regression in R using the nlsLM function from the minpack.lm package. See A better 'nls' (?) for a discussion of nls vs. nlsLM.

library(minpack.lm)
set.seed(16)
x <- 0:600
y <- 5.0*x^0.35 + rnorm(length(x), 0, 1)
plot(x, y)
fit <- nlsLM(y ~ a * x^b, start=list(a=1, b=0.5))
points(x, predict(fit), type="l", col="red", lwd=4)

Nonlinear fit

Here's the summary:

> summary(fit)

Formula: y ~ a * x^b

Parameters:
  Estimate Std. Error t value Pr(>|t|)    
a  5.02255    0.05542   90.62   <2e-16 ***
b  0.34947    0.00189  184.91   <2e-16 ***
---
Signif. codes:  0 *** 0.001 ** 0.01 * 0.05 ‘.’ 0.1   1

Residual standard error: 0.9822 on 599 degrees of freedom

Number of iterations to convergence: 8
Achieved convergence tolerance: 1.49e-08

Comments