[R-sig-Geo] Moving Windows Correlation----???

Zia Ahmed zua3 at cornell.edu
Wed Dec 9 18:48:13 CET 2009


Dear Garcia,

Thank you so much! Before running my data, I have tested your code using 
"meuse" data. It works fine. But it produce following output like below 
from V1 to V3103. I do not understand what does the values in rows mean 
(1 to 4). Here is  the code that I used.  I have questions How do I plot 
r-values with coordinates. Is it possible to get p-value for each estimate?

Thanks again
Zia

*# R code:*
#----------
library(gstat)
library(sp)
library(spdep)

data(meuse)
data(meuse.grid)
coordinates(meuse) <- ~x + y
coordinates(meuse.grid) <- ~x + y
str(meuse.grid)

# GRID 1:
v1 <- variogram(log(zinc) ~ 1, meuse)
x1<-fit.variogram(v1, vgm(1, "Sph", 800, 1))
G1 <- krige(zinc ~ 1, meuse, meuse.grid, x1, nmax = 30)
gridded(G1)<-TRUE
# GRID-2:
v2 <- variogram(log(lead) ~ 1, meuse)
x2<-fit.variogram(v2, vgm(.1, "Sph", 1000, .6))
G2 <- krige(zinc ~ 1, meuse, meuse.grid, x2, nmax = 30)
gridded(G2)<-TRUE
# Mooving Windows Correlation
# Define the size of the moving window,
dist = sqrt(2*((40*3)^2))
# Create a list of neighbours for the distance criteria
nb <- dnearneigh(coordinates(G1),0, dist)
v = sapply(nb, function(i) cor(G1 at data[i,], G2 at data[i,]))

When I run following codes it shows error.

coordinates(v) = coordinates(G1)

Error in function (classes, fdef, mtable)  :
  unable to find an inherited method for function "coordinates<-", for 
signature "matrix"

gridded(v) = TRUE

Error in `gridded<-`(`*tmp*`, value = TRUE) :
  gridded<- only works for SpatialPoints[DataFrame] or 
SpatialGrid[DataFrame]

*
# Output #--------
*
       V1         V2         V3         V4         V5         V6          V7
1  0.9996752  0.9996896  0.9997172 0.99971074  0.9996085  0.9996784  
0.99972521
2 -0.3807374 -0.3576051 -0.2260482 0.05467783 -0.2882067 -0.2124191 
-0.08551635
3 -0.3943537 -0.3750882 -0.2435564 0.03511977 -0.3081105 -0.2300869 
-0.10426607
4  0.9999324  0.9999349  0.9999442 0.99994815  0.9999047  0.9999104  
0.99992347



Jaime R. Garcia M. wrote:
> On Tue, 2009-12-08 at 21:54 +0100, Jaime R. Garcia M. wrote:
>   
>> On Tue, 2009-12-08 at 14:32 -0500, Zia Ahmed wrote:
>>     
>>> I have two grid maps (100 m cell size). I want to a create another  
>>> raster gird  of  correlation  coefficient ( r value) of  two maps using  
>>> moving windows approach.  Has someone  any idea how to do it in R?
>>>
>>> Thanks
>>>
>>> Zia
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at stat.math.ethz.ch
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>       
>> Dear Zia,
>>
>> Some time ago I did exactly that and the script was based on a earlier
>> post by Prof. Roger Bivand in this list. (Sorry I couldn't find the link
>> anymore)
>>
>> Suppose "G1" and "G2" are the two grid maps.
>> I assume they are "SpatialGridDataFrames" and have exactly the same
>> extent and number of columns and rows. Also that you have libraries (sp)
>> and (spdep) loaded
>>
>> # Define the size of the moving window, for example (7*7) so you have
>> enough neighbors for reliable correlation estimation:
>>
>> dist = sqrt(2*((100*3)^2))
>>
>> # Create a list of neighbours for the distance criteria
>> nb <- dnearneigh(coordinates(G1),0, dist) 
>>
>> # And then calculate correlations
>> v <- sapply(nb, function(i) cor(G1 at data, G2 at data))
>>     
>
> # Small correction
>
> v = sapply(nb, function(i) cor(G1 at data[i,], G2 at data[i,]))
>
> #  And in this case the result should be a numeric vector
>
> coordinates(v) = coordinates(G1)
> gridded(v) = TRUE
> fullgrid(v) = TRUE
> _______________________________________________
>   
>> R-sig-Geo mailing list
>> R-sig-Geo at stat.math.ethz.ch
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>     
>
>
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20091209/e6df660a/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: zua3.vcf
Type: text/x-vcard
Size: 281 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20091209/e6df660a/attachment.vcf>


More information about the R-sig-Geo mailing list