Bonjour je souhaite réaliser une interpolation spatiale puis réaliser une étude avec des krigegage et une carte des krigeage. Voici le code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
test.interieur.polygone<-function(gr.x,gr.y,poly.x,poly.y) {
  S<-length(poly.x)
  I<-length(gr.x)
  J<-length(gr.y)
  TEST<-array(NA,c(I,J,S))
  for( s in 1:S) {
    xa<-poly.x[s]
    ya<-poly.y[s]
    xb<-poly.x[s+1-S*(s==S)]
    yb<-poly.y[s+1-S*(s==S)]
    for(i in 1:I) {
      if(xa != xb) test1 <- gr.y < ((xb-gr.x[i])*ya + (gr.x[i]-xa)*yb)/(xb-xa)
      if(xa == xb) test1 <- rep(TRUE,J)
      if(xa != xb) test2 <- min(xa,xb) < gr.x[i] & gr.x[i] <= max(xa,xb)
      if(xa == xb) test2 <- gr.x[i] == xa
      TEST[i,1:J,s]<- test1 & test2
    }}
  NB.intersect<-apply(TEST,c(1,2),sum)
  test.parite<- floor(NB.intersect/2)*2 != NB.intersect
  return(test.parite)
}
 
FRANCE<-  read.table(file="data-contour-France.dat",sep=" ",dec=".")
FR.x<-FRANCE[,1] 
FR.x
FR.y<-FRANCE[,2]
FR.y
TAB<- read.table(file="data-pluvio.dat",sep="\t",dec=",",header=TRUE)
 
x<-TAB[,1]
x
y<-TAB[,2]
y
z<-TAB[,3]
z
n<-length(x)
n
 
V0<- rep(1,n)
V1<-x
V2<-y
V3<-x^2
V4<-x*y
V5<-y^2
V6<-x^3/1000
V7<-x^2*y/1000
V8<-x*y^2/1000
V9<-y^3/1000
 
# question 1 
modele.simplifie<-lm(z~V1+V3+V4+V5+V6+V7+V8+V9)
 
coef<-modele.simplifie$coef
 
# cartographie 
#----------------------
grid.x<-seq(-5.5,8.2,by=0.10)
grid.y<-seq(42,51.2,by=0.10)
 
p<-length(grid.x)
q<-length(grid.y)
 
MAP<-matrix(NA,p,q)
 
for(i in 1:p) for(j in 1:q) 
{
  x0<- grid.x[i]
  y0<- grid.y[j]
  eta<-c(1,x0,x0^2,x0*y0,y0^2,x0^3/1000,x0^2*y0/1000,x0*y0^2/1000,y0^3/1000)
  MAP[i,j]<-sum(coef*eta)
}
 
# application du masque
 
MASK<-test.interieur.polygone(grid.x,grid.y,FR.x,FR.y)
MASK[MASK==FALSE]<-NA
 
MAP<- MAP*MASK
 
niveaux<- seq(40,150,by=5)
palette<-rainbow(length(niveaux)-1,start=0.4,end=0.7)
image(grid.x,grid.y,MAP,breaks=niveaux,col=palette,main="pluviometrie en France")
points(x,y,pch=16,cex=0.5)
contour(grid.x,grid.y,MAP,levels=niveaux,add=TRUE)
polygon(FRANCE) 
v<-length(niveaux)
text<-paste(niveaux[-v],"-",niveaux[-1])
legend(x=-5.0,y=46.5,fill=palette,legend=text,cex=0.5)
 
#residus
 
z.predict<-predict(modele.simplifie)
 
modele.simplifie<-lm(z~V1+V3+V4+V5+V6+V7+V8+V9)
 
residus<-residuals(modele.simplifie)
residus
 
#question 2 analyse variographique
 
res<- residus
DIST<-matrix(NA,n,n)
for(i in 1:n) for(j in 1:n) {
  DIST[i,j]<- sqrt((x[i]-x[j])^2+(y[i]-y[j])^2) 
}
DIST
 
Q<-matrix(NA,n,n)
for(i in 1:n) for(j in 1:n) {
  Q[i,j]<- 0.5*(res[i]-res[j])^2
}
Q
 
H<-as.vector(DIST)
V<-as.vector(Q)
 
plot(x=H,y=V)
 
breaks<-seq(0,14,by=1.0)
CL<-cut(H,breaks)
mH<-tapply(H,CL,mean)
mV<-tapply(V,CL,mean)
 
 
points(mH,mV,col="blue",type="b",pch=16)
abline(v=breaks,lty=2,col="green")
 
variog.curve<-function(tau2,sig2,a,type,...){
  if(type=="lin")   
    curve( sig2*(x/a)*(x<a) + sig2*(x>=a) + tau2*(x>0) ,add=TRUE,...)
  if(type=="spher") 
    curve( sig2*(1.5*x/a - 0.5*(x/a)^3)*(x<a) + sig2*(x>=a) + tau2*(x>0) ,add=TRUE,...)
  if(type=="exp")   
    curve( sig2*(1 -exp(-(x/a)  )) + tau2*(x>0),add=TRUE,...)
  if(type=="gauss") 
    curve( sig2*(1 -exp(-(x/a)^2)) + tau2*(x>0),add=TRUE,...)
}
 
 
tau2<- 100
sig2<- 50
a   <- 0.5
 
variog.curve(tau2,sig2,a,type="gauss",col="red",lwd=2)
 
# 4. modele variographique le plus vraisemblable 
 
type<-"gauss"
 
gr.tau2<-seq(0.01,100,length=25)  ; p<-length(gr.tau2)
gr.sig2<-seq(0.10,50,length=25)  ; q<-length(gr.sig2)
gr.a<-seq(0.10,0.50,length=25)    ; r<-length(gr.a)
 
 
calcul.cov<-function(D,tau2,sig2,a,type){
  if(type=="lin") COV  <- sig2 * (1- D/a )*(D<a) + diag(rep(tau2,n))
  if(type=="spher") COV  <- sig2 * (1- 1.5*D/a + 0.5*(D/a)^3)*(D<a) + diag(rep(tau2,n))
  if(type=="exp") COV  <- sig2 * exp (-(D/a)  ) + diag(rep(tau2,n))
  if(type=="gauss") COV  <- sig2 * exp (-(D/a)^2) + diag(rep(tau2,n))
  return(COV)
}
 
loglik <- function(tau2,sig2,a,type,res,DIST){
  COV<-calcul.cov(DIST,tau2,sig2,a,type)
  ll<- -0.5*log(det(COV)) - 0.5*t(res)%*%solve(COV)%*%res 
  return(ll)
}
 
CUBE<-array(NA,dim=c(p,q,r))
for(i in 1:p) for(j in 1:q) for(k in 1:r) 
  CUBE[i,j,k]<-loglik(gr.tau2[i],gr.sig2[j],gr.a[k],type,res,DIST)
 
i.opt<-which(CUBE==max(CUBE),arr.ind=TRUE) ; i.opt
 
tau2.optimal<-gr.tau2[i.opt[1]] ; tau2.optimal
sig2.optimal<-gr.sig2[i.opt[2]] ; sig2.optimal
a.optimal   <-gr.a[i.opt[3]]    ; a.optimal
 
loglik.max  <- loglik(tau2.optimal,sig2.optimal,a.optimal,type,res,DIST) ; loglik.max
 
 
# question 3
COV<-calcul.cov(DIST,tau2,sig2,a,type)
COV
INVCOV<-solve(COV)
INVCOV
 
gr.x<-seq(0,1,length=200)
gr.y<-seq(0,1,length=200)
 
MAP<-matrix(NA,length(gr.x),length(gr.y))
for(i in 1:length(gr.x))
  for(j in 1:length(gr.y)){
    x0<-gr.x[i]
    y0<-gr.y[j]
    dist<- sqrt((x-x0)^2+(y-y0)^2)
    co<- diag(calcul.cov(diag(dist),tau2,sig2,a,type))
    lambda<- INVCOV %*% co
    MAP[i,j]<-sum(lambda*res)
  }
 
#question 4 
#cartographie du krigeage
#~~~~~~~~~~~~~~~~~~~~~~~~
niveaux<-seq(-2,2,length=20)
palette<-topo.colors(length(niveaux)-1)
image(MAP,breaks=niveaux,col=palette,main="krigeage")
contour(MAP,levels=niveaux,add=TRUE)
points(x[res>0],y[res>0],col="red",pch=16)
points(x[res<0],y[res<0],col="black",pch=16)
 
#cartographie idw des résidus 
 
interp.inv.dist <-function(x,y,z,gr.x, gr.y,p) {
  I<-length(gr.x)
  J<-length(gr.y)
  MAP<-matrix(NA,I,J)
  for(i in 1:I) for(j in 1:J) {
    x0<- gr.x[i]
    y0<- gr.y[j]
    d<-sqrt((x - x0)^2 + (y - y0)^2)
    w<- 1 / (d+0.000001)^p
    MAP[i,j]<- sum(w*z)/sum(w)
  }
  return(MAP)
}
par(mfrow=c(1,2))
 
MAP2<-interp.inv.dist(x,y,res,gr.x,gr.y,2)
 
niveaux<-seq(-2,2,length=20)
palette<-topo.colors(length(niveaux)-1)
image(MAP2,breaks=niveaux,col=palette, main="IDW")
contour(MAP2,levels=niveaux,add=TRUE)
points(x[res>0],y[res>0],col="red",pch=16)
points(x[res<0],y[res<0],col="black",pch=16)
 
# 5d/ cartographie du krigeage
#~~~~~~~~~~~~~~~~~~~~~~~~
niveaux<-seq(-2,2,length=20)
palette<-topo.colors(length(niveaux)-1)
image(MAP,breaks=niveaux,col=palette,main="krigeage")
contour(MAP,levels=niveaux,add=TRUE)
points(x[res>0],y[res>0],col="red",pch=16)
points(x[res<0],y[res<0],col="black",pch=16)

Toutefois avec ce code j'obtiens cette erreur. Que je n'arrive pas à résoudre :

Warning message:
In contour.default(MAP, levels = niveaux, add = TRUE) :
toutes les valeurs en z sont égales
Si jamais une personne peut me proposer une solution je suis preneur


D'avance merci à vous