I've been experiencing an issue when trying to estimate a model using the 'nls' function. I am trying to estimate the parameters (i.e. the aj's for j=0,1,...,9
) in the following equation:
log(y) = log(a0 + a1*x1 + a2*x2 + a3*x3 + a4*(x1)^2 + a5*(x2)^2 +
a6*(x3)^2 + a7*(x1*x2) + a8*(x1*x3) + a9*(x2*x3)) + error
In order to avoid negative values inside the log
function, I set the starting values for the parameters in the non-linear least squares model according to the following code:
ols.model <- lm(y ~ x1 + x2 + x3 + (x1)^2 + (x2)^2 + (x3)^2 + x1*x2 +
x1*x3 + x2*x3)
ols.coefficients <- ols.model$coefficients
fitted <- fitted(ols.model)
fitted <- fitted-ols.coefficients[1]
min.fitted <- min(fitted)
b0.start <- -min.fitted + 0.1
The above ensured that none of the starting fitted values will be negative inside the log
function. My call of the nls
regression looked like this:
nls.model <- nls(log(y) ~ log(b0 + b1*x1 + b2*x2 + b3*x3 + b4*(x1)^2 +
b5*(x2)^2 + b6*(x3)^2 + b7*(x1*x2) + b8*(x1*x3) +
b9*(x2*x3)),
start=list(b0=b0.start, b1=ols.coefficients[2],
b2=ols.coefficients[3], b3=ols.coefficients[4],
b4=ols.coefficients[5],b5=ols.coefficients[6],
b6=ols.coefficients[7], b7=ols.coefficients[8],
b8=ols.coefficients[9], b9=ols.coefficients[10]),
trace=TRUE)
Despite these carefully selected starting parameter values, I keep on getting an error message that reads:
Error in numericDeriv(form[[3L]], names(ind), env) :
Missing value or an infinity produced when evaluating the model
In addition: Warning message:
In log(b0 + b1 * x1 + b2 * x2 + b3 * x3 + b4 * x4 + :
NaNs produced
Does anyone have any idea how I can resolve this issue and estimate the non-linear model without getting an error message? My dataset does not contain any missing or zero values so that is definitely not the problem.