R nls function and starting values -
i'm wondering how can find/choose starting values nls function i'm getting errors put in. want confirm can use nls function data set.
data [1] 108 128 93 96 107 126 105 137 78 117 111 131 106 123 112 90 79 106 120 [20] 91 100 103 112 138 61 57 97 100 95 92 78 week = (1:31) > data.fit = nls(data~m*(((p+q)^2/p)*exp((p+q)*week)/(1+(q/p)*exp(-(p+q)*week))^2), start=c(m=?, p=?, q=?))
if change function bit , use nls2 starting values can converge. model using is:
log(data) = .lin1 + .lin2 * log((exp((p+q)*week)/(1+(q/p)*exp(-(p+q)*week))^2))) +error
in model .lin1 = log(m*(((p+q)^2/p)) , when .lin2=1 reduces model in question (except multiplicative rather additive error , fact parameterization different when appropriately reduced gives same predictions). 4 parameter rather 3 parameter model.
the linear parameters .lin1 , .lin2. using algorithm = "plinear"
not require starting values these parameters. rhs of plinear formulas specified matrix 1 column each linear parameter specifying coefficient (which may nonlinear function of nonlinear parameters).
the code is:
data <- c(108, 128, 93, 96, 107, 126, 105, 137, 78, 117, 111, 131, 106, 123, 112, 90, 79, 106, 120, 91, 100, 103, 112, 138, 61, 57, 97, 100, 95, 92, 78) week <- 1:31 if (exists("fit2")) rm(fit2) library(nls2) fo <- log(data) ~ cbind(1, log((exp((p+q)*week)/(1+(q/p)*exp(-(p+q)*week))^2))) # try maxiter random starting values set.seed(123) fit2 = nls2(fo, alg = "plinear-random", start = data.frame(p = c(-10, 10), q = c(-10, 10)), control = nls.control(maxiter = 1000)) # use starting value found nls2 fit = nls(fo, alg = "plinear", start = coef(fit2)[1:2]) plot(log(data) ~ week) lines(fitted(fit) ~ week, col = "red")
giving:
> fit nonlinear regression model model: log(data) ~ cbind(1, log((exp((p + q) * week)/(1 + (q/p) * exp(-(p + q) * week))^2))) data: parent.frame() p q .lin1 .lin2 0.05974 -0.02538 5.63199 -0.87963 residual sum-of-squares: 1.069 number of iterations convergence: 16 achieved convergence tolerance: 9.421e-06
Comments
Post a Comment