[R] nls() vs lm() estimates

From: Héctor Villalobos <hvillalo_at_ipn.mx>
Date: Fri, 13 Jun 2008 12:02:59 -0600


Hi,

I'm trying to understand why the coefficients "a" and "b" for the model: W = a*L^b estimated via nls() differs from those obtained for the log transformed model: log(W) = log(a) + b*log(L) estimated via lm(). Also, if I didn't make a mistake, R-squared suggests a "better" adjustment for the model using coefficients estimated by lm() . Perhaps I'm doing something wrong in nls()?

I hope the code below explains this better. Thanks in advance for any hints.

Héctor

L <-

c(8,8.1,8.5,9,9.4,9.4,9.5,9.5,9.5,9.6,9.8,10,10,10,10,10,10,10,10,10,10,10.2,10.3,10.4,10.4,1
0.4,10.4,10.5,10.5,10.5,10.5,10.5,10.5,10.5,10.5,10.7,10.7,10.8,10.9,10.9,10.9,11,11,11,11,1
1,11,11,11,11,11,11,11,11,11,11,11,11,11.1,11.1,11.2,11.2,11.2,11.3,11.3,11.3,11.3,11.3,11.
4,11.4,11.4,11.4,11.5,11.5,11.5,11.5,11.5,11.5,11.5,11.5,11.6,11.6,11.6,11.6,11.6,11.6,11.6,
11.6,11.7,11.7,11.7,11.7,11.7,11.8,11.8,11.8,11.8,11.8,11.9,12,12,12,12,12,12,12,12,12,12,1
2,12,12,12,12,12,12,12,12,12,12,12,12,12,12.1,12.2,12.2,12.2,12.3,12.3,12.3,12.3,12.3,12.3,
12.3,12.3,12.3,12.4,12.4,12.4,12.4,12.4,12.4,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.5,12.6,12
.6,12.7,12.7,12.8,12.8,12.8,12.9,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13, 13,13.2,13.2,13.3,13.5,13.5,13.5,13.5,13.5,13.5,14)

W <-
c(11,13,13.45,21.66,19.5,19.73,19.74,19.42,21.48,20.47,23.02,22.7,20.19,23.3,27.05,19.81, 20.01,26,24,25,20,25,26.29,31.26,23.08,29.85,24.27,27.49,25,26.03,24,26,28.21,24.62,21.6 9,24.68,23.6,25.42,26.7,30.25,30.06,33.62,32,30,32.46,30,30,28.8,30.2,31.44,32.84,33.04,3

5,28,29,33,34,28,28.51,35.67,33.72,33,28.53,34.85,34.5,37.44,37.74,31.36,30.12,36.03,33.4
,33.51,34,33,33.79,34.93,35,34.13,35.65,34,32.77,41.71,31.26,32.4,28.81,35.63,34.96,36.74
,32.38,38.14,34.12,40.26,40.27,36.96,38.35,42.36,40.33,31.59,34.44,38,42.63,40,36.28,37,3
4.4,34,33.64,39.05,40.46,35.45,38.72,35,33,35,33,40,35,37,36,32,43,35,40,33.54,40.06,43.3
8,40.3,44.81,43,46.32,37.45,37.71,45.9,36.1,44.78,43.12,45.5,41.62,38,37,43.08,43.82,47.2
5,43,41.59,43.58,41,44,48,43,45.46,43.5,43.38,47.54,45,46.92,44.75,49.02,43.37,43.44,48,4 3,46,42,48,45,48,43,45,46,43,40,42,40,43,43,50,44,50.65,42.11,50,51.44,53.1,52,56.2,45,49 ,55)

## Using nls() to find "a" and "b" for model: W = a*L^b
 WL.nls <- nls((W ~ a * L^b), start = list(a = 0.02, b = 1),

               trace = TRUE, algorithm = "default", model = TRUE)   summary(WL.nls)

## Scatterplot with fitted model

 plot(L, W)
 lines(L, predict(WL.nls), col = "blue", lwd = 2)

## Finding "log(a)" and "b" for log transformed model: log(W) = log(a)+ b*log(L)
 logWL.lm <- lm(log10(W) ~ log10(L))
  summary(logWL.lm)

## Adding model to plot

 lines(L, 10^coef(logWL.lm)[1]*L^coef(logWL.lm)[2], col="red", lwd=2)

## R-squared for W = a*L^b

 Rsq.nls <- sum((predict(WL.nls) - mean(W))^2) / sum((W - mean(W))^2)

## R-squared for W = a*L^b with coefs from log(W) = log(a)+ b*log(L)
 pred <- 10^coef(logWL.lm )[1]*L^coef(logWL.lm )[2]   Rsq.lm <- sum((pred - mean(W))^2) / sum((W - mean(W))^2)

  text(c(9, 13), c(50, 20), paste("R-squared:", formatC(c(Rsq.nls, Rsq.lm), digits=4)), col=c("blue", "red"))

        [[alternative HTML version deleted]]



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 13 Jun 2008 - 18:20:16 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 13 Jun 2008 - 22:30:42 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive