linear model for starting paramerters in nls fit

2019-03-06 10:13发布

I read about using the log as an alternative for creating a linear equation so that I can extract starting values for an nls fit from the linear equation, in R; Accordingly,

For equation: Y=q/(1+bDX)^(1/b) where Y and X are my data; q,b,D are my parameters to be estimated. I created the linear model:

  X<-1:45
  Y <- c(35326L, 30339L, 23379L, 21877L, 18629L, 17627L, 15691L, 15435L, 
   14205L, 11732L, 10560L, 10592L, 9939L, 7491L, 4928L, 3427L, 8123L, 
   9027L, 8733L, 9599L, 8737L, 9135L, 8548L, 7279L, 8940L, 8459L, 
   8460L, 7700L, 6817L, 7167L, 7089L, 7091L, 7538L, 9206L, 9680L, 
   5876L, 7799L, 8384L, 10586L, 8623L, 7848L, 5534L, 6610L, 6539L, 
   6650L)
lmodel <- coef(lm(log(Y)~X+I(X^2)))   
q0 <- exp(lmodel[1])
D0 <- -lmodel[2]
b0 <- lmodel[3]*2/D0^2 
Start1=list(q=q0,b=b0,D=D0)
theta_hat2 <- nls(y~q*(1+b*D*x)^(-1/b),start=Start1) 

This provides me the required results everytime. However, I want to fit nls to some other equations mentioned below which are rather difficult for me. If someone can help me make a linear model to fit these, I would greatly appreciate.

Equation 2:

Y=q*(-D+(b/n)*X^n here q,b,D,n are to be estimated.

Equation 3:

Y=q*exp⁡(-(X/D)^b here q,b,D are to be estimated.

Equation 4:

Y=q*X^((-b) )*exp⁡(D/((1-b) )*(X^(1-b)-1) here q,b,D are to be estimated.

I am attaching example dataset for this problem here:

1条回答
甜甜的少女心
2楼-- · 2019-03-06 11:12

The main problem is that equations (2) and (4) are overparameterized so regardless of optimization algorithm used there will be problems. In the case of (3) there is a syntax error and we need better starting values. Test input should be contained within the question and we have provided it in the Note at the end.

Equation (1)

x and y should be X and Y. We can use the plinear algorithm to avoid having to specify starting values for the linear parameter q in which case starting values of 1 for the other parameters seems sufficient.

fo1 <- Y ~ (1+b*D*X)^(-1/b)
fm1 <- nls(fo1, start = list(D = 1, b = 1), alg = "plinear")

Equation (2)

Equation (2) is overparameterized because if you multiply q by an arbitrary number a and at the same time divide D and b by a the right hand side is unchanged. Removing q we note that D and b enter linearly and only n is actually nonlinear so we can use the plinear algorithm to avoid initial values for the linear parameters:

fo2 <- Y ~ cbind(-1,(1/n)*X^n)
fm2 <- nls(fo2, start = list(n = 1), alg = "plinear")

Equation (3)

There is a missing right parenthesis in the formula given in the question. If we fix that then the problme is that we need better starting valus. First try it with b fixed at 1 and then use the result as starting values for the full equation. Again use plinear to avoid having to supply starting values for the linear parameter.

fo3 <- Y ~ exp(-(X/D)^b)
b <- 1
fm <- nls(fo3, start = list(D = 1), alg = "plinear")
fm3 <- nls(fo3, start = list(D = coef(fm)[["D"]], b = 1), alg = "plinear")

Equation (4)

Equation (4) is also overparameterized so set D to 1-b in which case exp(D/(1-b)) is the constant exp(1) so:

fo4 <- Y ~ X^((-b))* exp(1) * (X^(1-b)-1)
fm4 <- nls(fo4, start = list(b = .5), alg = "plinear")

Compare

We can plot the various solutions:

plot(Y ~ X, pch = 20)
lines(fitted(fm1) ~ X)
lines(fitted(fm2) ~ X, col = "red")
lines(fitted(fm3) ~ X, col = "blue")
lines(fitted(fm4) ~ X, col = "green")
legend("topright", legend = 1:4, lty = 1,  col = c("black", "red", "blue", "green"))

screenshot

Note

Test data should be self contained in the question and since it was not provided in that form we will provide it here this time:

X <- 1:45
Y <- c(35326L, 30339L, 23379L, 21877L, 18629L, 17627L, 15691L, 15435L, 
14205L, 11732L, 10560L, 10592L, 9939L, 7491L, 4928L, 3427L, 8123L, 
9027L, 8733L, 9599L, 8737L, 9135L, 8548L, 7279L, 8940L, 8459L, 
8460L, 7700L, 6817L, 7167L, 7089L, 7091L, 7538L, 9206L, 9680L, 
5876L, 7799L, 8384L, 10586L, 8623L, 7848L, 5534L, 6610L, 6539L, 
6650L)
查看更多
登录 后发表回答