Skip to content
Prev 273066 / 398506 Next

Problem with logarithmic nonlinear model using nls() from the `stats' package

On Sat, Oct 1, 2011 at 5:28 AM, Casper Ti. Vector
<caspervector at gmail.com> wrote:
Its linear given c so calculate the residual sum of squares using lm
(or lm.fit which is faster) given c and optimize over c:

set.seed(123) # for reproducibility

# test data
x <- 1:10
y <- 1 + 2 * log(1 + 3 * x) + rnorm(1, sd = 0.5)

# calculate residual sum of squares for best fit given c
fitc <- function(c) lm.fit(cbind(1, log(1 + c * x)), y)
rssvals <- function(c) sum(resid(fitc(c))^2)

out <- optimize(rssvals, c(0.01, 10))

which gives:
a         b         c
0.7197666 2.0000007 2.9999899