[R-sig-Geo] Mann-Kendall test on raster data

Tiffany Smith smithtiffanyt at gmail.com
Fri Aug 3 16:58:11 CEST 2012


Rafi,

This is exactly what I needed, thanks so much! Obviously I was over thinking this one…

Best,
Tiffany
On Aug 3, 2012, at 3:46 AM, Rafael Wüest wrote:

> Hi Tiffany,
> 
> If I got your initial question right, you want to apply the MannKendall test to the whole raster. If so, you may use the calc() command.
> 
> Something like
> 
> myraster.mk <- calc(myraster,function(x){MannKendall(rev(x))$sl})
> 
> may give you what you need.
> 
> HTH, Rafi
> 
> On 03.08.2012, at 03:37, Thiago Veloso wrote:
> 
>>  Tiffany,
>> 
>>  You can use raster's "rasterfromXYZ" function.
>> 
>>  And it's not necessary to write out and read in the file, unless it's convenient to you.
>> 
>>  Hph,
>>  Thiago.
>> 
>> 
>> ----- Original Message -----
>> From: Tiffany Smith <smithtiffanyt at gmail.com>
>> To: r-sig-geo at r-project.org
>> Cc: 
>> Sent: Thursday, August 2, 2012 3:26 PM
>> Subject: Re: [R-sig-Geo] Mann-Kendall test on raster data
>> 
>> An update, I have been able to automate the MannKendall test and capture the output into a .txt file:
>> 
>> lat<-seq(-125,-67,0.5)
>> lon<-seq(25,53,0.5)
>> 
>> fun.mk<-function(x){
>>    for (i in 1:length(lat)){
>>        for (j in 1:length(lon)){
>>            v<-rev(extract(x,SpatialPoints(cbind(lat[i],lon[j]))))
>>            mk<-MannKendall(v)
>>            pv<-mk$sl
>>            out<-data.frame(cbind(lat[i],lon[j],pv[1]))
>>            write.table(out,"myraster.mk.txt",append=TRUE,col.names=FALSE,row.names=FALSE,quote=FALSE)
>>            }}}
>> myraster.mk<-fun.mk(myraster)
>> 
>> Now, I would like to read that file back into R as a RasterLayer. So far, I can read it in as a table to get:
>> 
>> myraster.table<-read.table("myraster.mk.txt",header=FALSE) 
>> myraster.table
>>        V1     V2          V3
>> 1  -86.000 35.000 0.071539655
>> 2  -86.000 35.125 0.004216323
>> 3  -86.000 35.250 0.449751556
>> 4  -86.000 35.375 0.124644011
>> 5  -86.000 35.500 0.128179774
>>>> 
>> In the Raster package, there is "as.data.frame" to go from raster->data.frame but I can't find anything to go in the opposite direction, data.frame->raster. I tried using "as.raster", but that didn't work:
>> 
>> myraster.raster<-as.raster(myraster.table)
>> Error in UseMethod("as.raster") : 
>>  no applicable method for 'as.raster' applied to an object of class "data.frame"
>> 
>> Thanks,
>> Tiffany
>> On Aug 1, 2012, at 6:24 PM, Tiffany Smith wrote:
>> 
>>> Hello,
>>> 
>>> I'm trying to do a Mann-Kendall test on my RasterBrick data. So far, I've been able to complete the Mann-Kendall test (via the "Kendall" package) by extracting a series like so:
>>> 
>>> onelocation<-rev(extract(myraster,SpatialPoints(cbind(-86,33))))
>>> onelocation.mk<-MannKendall(onelocation)
>>> 
>>> I need help either figuring out a way to do the MK test with raster data, or if I loop through all points and use the above commands, how can I return that data into a Raster so I can visualize the results by making maps?
>>> 
>>> Thanks,
>>> Tiffany
>> 
>> 
>>    [[alternative HTML version deleted]]
>> 
>> 
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> 
>> 
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 
> 
> 
> --
> Rafael Wüest
> Swiss Federal Research Institute WSL
> Zürcherstrasse 111
> 8903 Birmensdorf
> Switzerland
> 
> +41 44 7392126
> rafael.wueest at wsl.ch
> http://www.wsl.ch/info/mitarbeitende/wueest/index_EN
> 
> 
> 



More information about the R-sig-Geo mailing list