2015-10-20 13 views
9

Rのデータに負の指数関数を当てようとしていますが、適合線がデータに比べて高すぎるように見えますが、Excelのビルトインパワーフィットより信じられます。誰かが私に理由を教えてくれますか?私はnls()関数とoptim()を使って試してみましたが、両方のメソッドから同様のパラメータが得られましたが、どちらも高く見えます。負の指数関数近似:カーブが高すぎる

x <- c(5.96, 12.86, 8.40, 2.03, 12.84, 21.44, 21.45, 19.97, 8.92, 25.00, 19.90, 20.00, 20.70, 16.68, 14.90, 26.00, 22.00, 22.00, 10.00, 5.70, 5.40, 3.20, 7.60, 0.59, 0.14, 0.85, 9.20, 0.79, 1.40, 2.68, 1.91) 
    y <- c(5.35, 2.38, 1.77, 1.87, 1.47, 3.27, 2.01, 0.52, 2.72, 0.85, 1.60, 1.37, 1.48, 0.39, 2.39, 1.83, 0.71, 1.24, 3.14, 2.16, 2.22, 11.50, 8.32, 38.98, 16.78, 32.66, 3.89, 1.89, 8.71, 9.74, 23.14) 

    xy.frame <- data.frame(x,y) 

    nl.fit <- nls(formula=(y ~ a * x^b), data=xy.frame, start = c(a=10, b=-0.7)) 

    a.est <- coef(nl.fit)[1] 
    b.est <- coef(nl.fit)[2] 

    plot(x=xy.frame$x,y=xy.frame$y) 

    # curve looks too high 
    curve(a.est * x^b.est , add=T) 
    # these parameters from Excel seem to fit better 
    curve(10.495 * x^-0.655, add=T) 

enter image description here

# alternatively use optim() 
    theta.init <- c(1000,-0.5, 50) 

    exp.nll <- function(theta, data){ 
     a <- theta[1] 
     b <- theta[2] 
     sigma <- theta[3] 
     obs.y <- data$y 
     x <- data$x 
     pred.y <- a*x^b 
     nll <- -sum(dnorm(x=obs.y, mean=pred.y , sd=sigma, log=T)) 
     nll 
    } 

    fit.optim <- optim(par=theta.init,fn=exp.nll,method="BFGS",data=xy.frame) 

    plot(x=xy.frame$x,y=xy.frame$y) 

    # still looks too high 
    curve(a.est * x^b.est, add=T) 

enter image description here

答えて

10

あなたが予期しない動作を見ている理由は、「高すぎる」見てカーブが実際にカーブより乗誤差のはるかに低い金額を持っているということですfrom excel:

# Fit from nls 
sum((y - a.est*x^b.est)^2) 
# [1] 1588.313 

# Fit from excel 
sum((y - 10.495*x^ -0.655)^2) 
# [1] 1981.561 

理由nls faより高い曲線は、小さなx値で巨大な誤差を避けるために働いているということです。

mod <- lm(log(y)~log(x)) 
(a.est2 <- exp(coef(mod)["(Intercept)"])) 
# (Intercept) 
# 10.45614 
(b.est2 <- coef(mod)["log(x)"]) 
#  log(x) 
# -0.6529741 

これらは、Excelからの係数に非常に近く、より視覚的に魅力的なフィット感が得られる(サム・オブ上に悪いパフォーマンスにもかかわらず:これは変革を対数 - 対数適用するかもしれません対処する1つの方法二乗エラーメトリック):ExcelはSSEを最小化しようとしていない場合、それはどのような基準を使用している

ただ、好奇心のうち

enter image description here

+0

、? – eipi10

+0

@ eipi10私は肯定的ではありませんが、ログログ変換も使用しています(http://www.real-statistics.com/regression/power-regression/)。したがって、yを予測するときにSSEを最小にする代わりに、 'log(y)'を予測するときにSSEを最小化します。 – josliber

関連する問題