user33484
user33484

Reputation: 568

Fitting function fails on custom function

structure of data:

fit_df <- structure(list(y = c(0.436390336, 0.677811903, 0.853362031, 1, 
                               1.221809295, 1.410483913, 1.576608222, 1.725742785, 1.860931456, 
                               1.98807088, 2.107010491, 2.217417081, 2.4207773, 2.607191671, 
                               2.79015353, 2.973115389, 3.156077248, 3.339039107, 3.522000965, 
                               3.693017862, 3.830239257, 3.967460651, 4.310514136, 4.653567622, 
                               4.996621107, 5.339674593, 5.613352612, 5.870642726, 6.127932841, 
                               6.385222955, 6.899803183, 7.414383411, 7.92896364, 8.339480669, 
                               8.72541584, 9.111351011, 9.497286182, 9.883221354, 10.26915652, 
                               10.6550917, 11.04102687, 11.42696204, 11.76127531, 13.29702689, 
                               14.74428378, 16.19154067, 17.56136471, 19.86499208, 22.03587742, 
                               24.20676276, 26.33893167, 28.08877587, 29.71693988, 31.34510388, 
                               32.97326788, 34.60143189, 36.22959589, 37.85775989, 39.47347933, 
                               40.83149256, 42.05261556, 43.27373856, 44.49486156), x = c(250000L, 
                                                                                          500000L, 750000L, 1000000L, 1500000L, 2000000L, 2500000L, 3000000L, 
                                                                                          3500000L, 4000000L, 4500000L, 5000000L, 6000000L, 7000000L, 8000000L, 
                                                                                          9000000L, 10000000L, 11000000L, 12000000L, 13000000L, 14000000L, 
                                                                                          15000000L, 17500000L, 20000000L, 22500000L, 25000000L, 27500000L, 
                                                                                          30000000L, 32500000L, 35000000L, 40000000L, 45000000L, 50000000L, 
                                                                                          55000000L, 60000000L, 65000000L, 70000000L, 75000000L, 80000000L, 
                                                                                          85000000L, 90000000L, 95000000L, 100000000L, 125000000L, 150000000L, 
                                                                                          175000000L, 200000000L, 250000000L, 300000000L, 350000000L, 400000000L, 
                                                                                          450000000L, 500000000L, 550000000L, 600000000L, 650000000L, 700000000L, 
                                                                                          750000000L, 800000000L, 850000000L, 900000000L, 950000000L, 1000000000L
                               )), class = "data.frame", row.names = c(NA, -63L))

I am trying to fit a custom function to this data. The function is as follows:

LEV_par <- function(x, alpha,lambda){
  (lambda - lambda^alpha*(lambda+x)^(1-alpha)) / (alpha - 1)
}

A_par <- function(x,base,alpha,lambda){
  
  LEV_par(x=x,alpha=alpha,lambda=lambda) / LEV_par(x=base,alpha=alpha,lambda=lambda)
  
}

when I try to fit using the following code I get an error

base <- 1e6
a_init <- 0.5
l_init <- 1e6
m <- nls2(y ~ A_par(x,1e6,alpha,lambda),
         data = fit_df, start = list(alpha=0.5,lambda=1e6))

Error in numericDeriv(form[[3L]], names(ind), env, central = nDcentral) : 
  Missing value or an infinity produced when evaluating the model

and when I try to use the "brute-force" algorithm the parameters do not seem to change.

Is there something I'm missing?

Upvotes: 0

Views: 61

Answers (1)

Stefano Barbi
Stefano Barbi

Reputation: 3194

LEV_par returns NaN when lambda is negative and alpha is rational, because of lambda^alpha term. See this.

Upvotes: 1

Related Questions