# divergence between nls (simple power equation) on non-transformed data and lm on log transformed data in R

I'm testing different ways to fit a simple power model to a dataset of 5 points.

I don't get same results if I use nls on non-transformed data or lm on log10 transformed data in R whereas Excel gives exactly the same results (it's power fitting might be a hidden linear fitting on log transformed data).

Here is an example of the code I'm using:

# data
r <- c(0.0778,0.0429,0.0320,0.0216,0.0148)
t <- c(4,10,40,100,400)
df <- data.frame(r,t)
df$log.r <- log10(df$r)
df$log.t <- log10(df$t)

# models
m1 <- nls(r ~ m * t^(-n), data=df, start=list(m=1, n=0.5))
m2 <- lm(log.r ~ log.t, data=df)

# simulate data
t.range <- seq(1, 1000, length.out=30000)
r.pred1 <- predict(m1, newdata=data.frame(t=t.range))
p1 <- data.frame(t=t.range, r=r.pred1)

log.t.range <- log10(t.range)
r.pred2 <- predict(m2, newdata=data.frame(log.t=log.t.range))
p2 <- data.frame(log.t=log.t.range, log.r=r.pred2)

# plot results
p1$log.r <- log10(p1$r)
p1$log.t <- log10(p1$t)
library(ggplot2)
ggplot(df, aes(x=log.t, y=log.r))+
geom_line(data=p1, color="red")+
geom_line(data=p2, color="blue", linetype="dashed")+
geom_point()+
ggtitle("red: nls on non-transformed data\ndashed blue: lm on log10 transformed data")+
theme_bw()


Thus, some questions:

• Which method (nls vs lm) would be the "best" in this case using R?
• Am I missing something in the use of nls?
• Could this be due to the small number of points (only 5) I'm trying to fit?
• See here and here. – Glen_b Jul 31 '14 at 13:23

$y=ax^{-n}+\epsilon$

Note how I include the error term $\epsilon$. Usually you make some assumptions about the error term (for statistical inference).

Now the log-transformed model:

$\log(y)=\log(ax^{-n}+\epsilon)$

There is no way to simplify this.

With lm you use this model:

$\log(y)=\log(ax^{-n})+\epsilon=\log(a)-n\log(x)+\epsilon$

This is not a transformation of the original model and will give different parameter estimates.

So, what should you use? It depends on your data, i.e., the expected error structure.