EstPrevInf= function(P,I,Pos,Tested) {
LL=log(P* dbinom(Pos, Tested, prob=I)
+(1-P) * dbinom(Pos, Tested, prob=0))
LL[is.infinite(LL)] = -99999
return(LL)
}
PDF.wrapper=function(predicted) {
return(predicted)
}
pars= list(P=0.5, I=0.5)
vars = list(Pos= "Pos", Tested= "Tested", predicted="predicted")
par_lo= list(P=0, I=0)
par_hi= list(P=1, I=1)
ExampleData = data.frame(cbind(
Tested = c(3, 10, 25, 16, 15, 12, 7, 7, 3, 3, 2),
Pos =c(3, 0, 13, 13, 8, 10, 7 , 5, 0, 0, 0)
))
Results = anneal(model=EstPrevInf,par=pars, var = vars,
source_data=ExampleData, pdf=PDF.wrapper,
par_lo, par_hi, dep_var="Pos",
initial_temp=1)
EstPrevInfSpec =
function(T1, P1, T2, P2, T3, P3, Spec, Pos, Tested, Spp) {
LL = numeric(length(Pos))
LL[which(Spp == "One")] = log(
P1*dbinom(Pos[which(Spp == "One")], Tested[which(Spp == "One")],
prob = (T1 + (1 - T1)*(1 - Spec)) )
+ (1-P1)*dbinom(Pos[which(Spp == "One")],
Tested[which(Spp == "One")], prob = (1-Spec) ) )
LL[which(Spp == "Two")] = log(
P2*dbinom(Pos[which(Spp == "Two")], Tested[which(Spp == "Two")],
prob = (T2 + (1 - T2)*(1 - Spec)) )
+ (1-P2)*dbinom(Pos[which(Spp == "Two")],
Tested[which(Spp == "Two")], prob = (1-Spec) ) )
LL[which(Spp == "Three")] = log(
P3*dbinom(Pos[which(Spp == "Three")], Tested[which(Spp == "Three")],
prob = (T3 + (1 - T3)*(1 - Spec)) )
+ (1-P3)*dbinom(Pos[which(Spp == "Three")],
Tested[which(Spp == "Three")], prob = (1-Spec) ) )
LL[is.na(LL)] = -99999
LL[is.infinite(LL)] = -99999
return(LL)
}
pars = list(T1 = 0.5, P1 = 0.5, T2 = 0.5, P2 = 0.5,
T3 = 0.5, P3 = 0.5, Spec = 0.5)
vars = list(Pos = "Pos", Tested = "Tested", Spp = "Spp",
predicted = "predicted")
par_lo = list(T1 = 0, P1 = 0, T2 = 0, P2 = 0,
T3 = 0, P3 = 0, Spec = 0)
par_hi = list(T1 = 1, P1 = 1, T2 = 1, P2 = 1,
T3 = 1, P3 = 1, Spec = 1)
MultiSppData = data.frame( cbind(
Tested = c(3, 10, 25, 16, 15, 12, 7, 7, 3, 3, 2,
15, 16, 22, 25, 18, 18, 16, 21, 23, 5, 9, 10,
12, 13, 12, 8, 9, 17, 13, 6, 5, 16, 3, 1, 1),
Pos = c(3, 0, 13, 13, 8, 10, 7, 5, 0, 0, 1,
5, 1, 6, 0, 2, 4, 3, 0, 0, 0, 1, 2,
1, 1, 7, 6, 6, 7, 4, 3, 4, 7, 2, 0, 1) ) )
MultiSppData$Spp = c(rep("One", 11), rep("Two", 12),
rep("Three", 13) )
MultiSppResults = anneal(model = EstPrevInfSpec, par = pars, var = vars,
source_data = MultiSppData, pdf = PDF.wrapper,
par_lo, par_hi, dep_var = "Pos",
initial_temp = 1)