-
Notifications
You must be signed in to change notification settings - Fork 4
/
MegafaunaValidation(gradient).r
141 lines (109 loc) · 3.06 KB
/
MegafaunaValidation(gradient).r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
solow_euclide.fct <- function(xx=xx,yy=yy,Lon=Lon,Lat=Lat,Age=Age,SdAge=SdAge)
{
etape <- 1
plot(Lon,Lat,pch=19,col=grey((Age-min(Age))/(max(Age)-min(Age))))
paramsig <- lm(log(SdAge)~Age)$coef
plot(Age,log(SdAge))
abline(paramsig,col=2,lwd=2)
distan <- outer(xx,Lon,"-")**2 +outer(yy,Lat,"-")**2
distan <- sqrt(distan)
iappartenance <- foreach (i = 1:ncol(distan),.combine='c') %dopar%
{ order(distan[,i])[1]}
estim.fct <- function(yobs,sdobs,distan,pas)
{
lmv.fct <- function(tmax,yobs=yobs,sdobs=sdobs,pond=pond)
{
if(tmax<0){return(10**6)}
yobs <- yobs/10000
sdobs <- sdobs/10000
lm0 <- pnorm(yobs-tmax,mean=0,sd=sdobs,lower.tail=F,log.p=F)
lm1 <- pnorm(yobs,mean=0,sd=sdobs,lower.tail=F,log.p=F)
lm2 <- lm0-lm1
lm2[lm2<=0] <- 10**(-150)
lm2[is.na(lm2)] <- 10**(-150)
lm2[!is.finite(lm2)] <- 10**(-150)
lm2 <- log(lm2)
lmv <- sum(pond)*log(tmax)-sum(pond*lm2)
lmv[is.na(lmv)] <- 10**6
if(!is.finite(lmv)){lmv <- 10**6}
return(lmv)
}
Test <- NULL
for(i in 1:nrow(distan))
{
pond <- pas*distan[i,]/max(distan)
pond <- exp(-pond*pond)
uu <- nlm(lmv.fct,max(yobs)/10000,yobs=yobs,sdobs=sdobs,pond=pond,
stepmax=0.5,iterlim=1000)
vv <- nlm(lmv.fct,uu$est,yobs=yobs,sdobs=sdobs,pond=pond,iterlim=1000)
Test <- c(Test,vv$est)
}
Test <- Test*10**4
return(Test)
}
simu.fct <- function(Test,iappartenance,paramsig)
{
ysim <- NULL
for(i in 1:length(iappartenance))
{
Tm <- Test[iappartenance[i]]
ysim <- c(ysim,Tm*runif(1))
}
sdsim <- exp(paramsig[1]+paramsig[2]*ysim)
ysim <- ysim+rnorm(length(ysim),rep(0,length(ysim)),sdsim)
return(list(ysim=ysim,sdsim=sdsim))
}
estpond.fct <- function(Test,iappartenance,paramsig,distan,pas,estim.fct)
{
vu <- range(Test)
vu <- vu[1]+(vu[2]-vu[1])*(0:5)/5
leg <- as.character(round(vu,3)*10000)
Tsim <- NULL
for(i in 1:100)
{
cdsim <- simu.fct(Test,iappartenance,paramsig)
Tsim <- cbind(Tsim,estim.fct(cdsim$ysim,cdsim$sdsim,distan,pas))
}
msim <- apply(Tsim,1,mean)
sdsim <- apply(Tsim,1,sd)
biais <- msim-Test
mse <- biais*biais+sdsim*sdsim
imse <- sum(mse)
return(list(Test=Test,biais=biais,sd=sdsim,imse=imse))
}
if(etape==1) {
Test <- estim.fct(Age,SdAge,distan,20)
voir <- foreach(iteration=1:100) %dopar% {
prop <- iteration
print(c(iteration,prop))
estpond.fct(Test,iappartenance,paramsig,distan,prop,estim.fct)
}
} ## fin etape==1
iv <- NULL
for(i in 1:100){iv <- c(iv,voir[[i]]$imse)}
plot(iv,type="l",xlab="pas",ylab="imse")
pas <- order(iv)[1]
Testfinal <- estim.fct(Age,SdAge,distan,pas)
result <- estpond.fct(Testfinal,iappartenance,paramsig,distan,pas,estim.fct)
result$pas <- pas
result$iv <- iv
result$voir <- voir
return(result)
} ##fin solow.fct
########################
########################
library(doParallel)
library(foreach)
source("Genpop(gradient).r")
mat <- humgen.fct(500,1,1)
nx <- 30
xx1 <- rep(1:nx,nx)/(nx+1)
yy1 <- sort(xx1)
Lon <- mat[,1]
Lat <- mat[,2]
Age <- mat[,3]
SdAge <- mat[,4]
cl <- makeCluster(28,outfile="")
registerDoParallel(cl)
est_solow <- solow_euclide.fct(xx=xx1,yy=yy1,Lon=Lon,Lat=Lat,Age=Age,SdAge=SdAge)
stopCluster(cl)