I would like to calculate the parameters of my model with the optim function. I tried to use the most logical values as parameter estimates but they do not work unfortunately as the optim function cannot work with the initial parameter estimates I gave. I thought maybe I can write a loop somehow that tests different combinations of parameter estimates and stores these combinations (or just the first combination) that gives optim$covergence == 0.
However, I do not succeed in writing such a loop. So I was wondering if someone can help me solve this problem. Or perhaps knows a better way to solve the problem
My code looks as follows
FN <- function(par, x, k, N){
a1 <- par[1]
aa <- par[2]
b1 <- par[3]
bb <- par[4]
a2 <- par[5]
aa2 <- par[6]
b2 <- par[7]
bb2 <- par[8]
p <- cbind ((a1+aa*(Data$FDS=="low")) + ((b1+bb*(Data$FDS=="low")) * x),
1 - (((a2+aa2*(Data$FDS=="low")) * (x ^ 2))/(((b2+bb2*(Data$FDS=="low")) ^ 2) + (x ^ 2))))[cbind(1:length(Data$Location_2), Data$Location_2)]
zprob1 <- par [9]
zprob2 <- par [10]
nll <- -sum(dzibinom(x = k, prob = p, size = N,
zprob = c(zprob1,zprob2)[Data$Location_2], log = T))
return(nll)
}
opt1 <- optim(par = c(a1 = 0.38 , aa = 0.0001, b1 = 0.04, bb = 0, a2 = 0.61, aa2 = 0.0001, b2 = 1.6, bb2=0.00001, zprob1 = 0.93, zprob2 = 0.89),
x = Data$Day, k = Data$k,
N = Data$N, fn = FN, hessian = TRUE)
opt2
So first I created a function (FN) and then for this function I would like to calculate the optim. But for this I need to find a way to get the proper parameter estimates. And I think maybe with a loop something is possible. However, I have been struggling the past weeks with writing such a loop. So if someone has another good solution I would also love to hear it.