raster time series statistics

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

raster time series statistics

herry
Hi List,

The following code returns an "Error in as.POSIXlt.character(x, tz, ...) :   character string is not in a standard unambiguous format"

require(raster)
require(rts)
require(stringi)
r <- raster(ncol=100, nrow=100)
values(r) <- runif(ncell(r))
list(ID=seq(1:24),month=rep(str_pad(1:12, pad = 0,width = 2 , "left"),2),year=sort(rep(2016:2017,12)))->dt
stack(r)->s
r->rs
for(i in 1:23){
rs[]<-r[]*i
  addLayer(s,rs)->s
print(nlayers(s))
}
timelst<-paste0(unlist(dt['year']),'-',unlist(dt['month']))
rts(s,time=as.yearmon(timelst))->rsts
str(rsts@time)
apply.monthly(rsts,mean)

I was expecting that the statistics accept the yearmonth format of the timeslot, as it allows subsetting.
Any suggestions?
Thanks
Herry

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: raster time series statistics

David Winsemius

> On Mar 5, 2018, at 3:28 PM, <[hidden email]> <[hidden email]> wrote:
>
> Hi List,
>
> The following code returns an "Error in as.POSIXlt.character(x, tz, ...) :   character string is not in a standard unambiguous format"

I'm unable to produce that error. Which function was being evaluated to produce the error? I don't see where as.POSIXlt would have been called. You don't have any Date or POSIXt-classed variables. (I did need to also load the stringr package to get str_pad into my workspace.)

>
> require(raster)
> require(rts)
> require(stringi)
> r <- raster(ncol=100, nrow=100)
> values(r) <- runif(ncell(r))
> list(ID=seq(1:24),month=rep(str_pad(1:12, pad = 0,width = 2 , "left"),2),year=sort(rep(2016:2017,12)))->dt
> stack(r)->s
> r->rs
> for(i in 1:23){
> rs[]<-r[]*i
>  addLayer(s,rs)->s
> print(nlayers(s))
> }
> timelst<-paste0(unlist(dt['year']),'-',unlist(dt['month']))
> rts(s,time=as.yearmon(timelst))->rsts
> str(rsts@time)
> apply.monthly(rsts,mean)
>
> I was expecting that the statistics accept the yearmonth format of the timeslot, as it allows subsetting.
> Any suggestions?
> Thanks
> Herry
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: raster time series statistics

Jim Lemon-4
In reply to this post by herry
Hi Herry,
This is probably due to a call to strptime (or similar). No, it
doesn't accept %Y-%m as a valid format. Maybe add a constant day to
all the dates as that will work:

dt<-list(ID=seq(1:24),month=rep(formatC(1:12,flag=0,width=2),2),
 year=sort(rep(2016:2017,12)))
timelst<-paste(unlist(dt['year']),unlist(dt['month']),"01",sep="-")
strptime(timelst,format="%Y-%m-%d")

Jim



On Tue, Mar 6, 2018 at 10:28 AM,  <[hidden email]> wrote:

> Hi List,
>
> The following code returns an "Error in as.POSIXlt.character(x, tz, ...) :   character string is not in a standard unambiguous format"
>
> require(raster)
> require(rts)
> require(stringi)
> r <- raster(ncol=100, nrow=100)
> values(r) <- runif(ncell(r))
> list(ID=seq(1:24),month=rep(str_pad(1:12, pad = 0,width = 2 , "left"),2),year=sort(rep(2016:2017,12)))->dt
> stack(r)->s
> r->rs
> for(i in 1:23){
> rs[]<-r[]*i
>   addLayer(s,rs)->s
> print(nlayers(s))
> }
> timelst<-paste0(unlist(dt['year']),'-',unlist(dt['month']))
> rts(s,time=as.yearmon(timelst))->rsts
> str(rsts@time)
> apply.monthly(rsts,mean)
>
> I was expecting that the statistics accept the yearmonth format of the timeslot, as it allows subsetting.
> Any suggestions?
> Thanks
> Herry
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: raster time series statistics

herry
Thanks Jim

Still getting the same error for apply.montly
Updated code:

require(raster)
require(rts)
require(stringr)
r <- raster(ncol=100, nrow=100)
values(r) <- runif(ncell(r))
stack(r)->s
r->rs
for(i in 1:23){
 rs[]<-r[]*i
  addLayer(s,rs)->s
 print(nlayers(s))
}
dt<-list(ID=seq(1:24),month=rep(formatC(1:12,flag=0,width=2),2),
 year=sort(rep(2016:2017,12)))
 timelst<-paste0(unlist(dt['year']),'-',unlist(dt['month']),"-01")
strptime(timelst,format="%Y-%m-%d")->t1

rts(s,time=as.yearmon(t1))->rsts
subset(rsts,'2017')->r2017
class(r2017@time)
class(rsts@time)

apply.monthly(rsts,mean)



-----Original Message-----
From: Jim Lemon [mailto:[hidden email]]
Sent: Tuesday, 6 March 2018 11:14 AM
To: Herr, Alexander (L&W, Black Mountain) <[hidden email]>
Cc: r-help mailing list <[hidden email]>
Subject: Re: [R] raster time series statistics

Hi Herry,
This is probably due to a call to strptime (or similar). No, it doesn't accept %Y-%m as a valid format. Maybe add a constant day to all the dates as that will work:

dt<-list(ID=seq(1:24),month=rep(formatC(1:12,flag=0,width=2),2),
 year=sort(rep(2016:2017,12)))
timelst<-paste(unlist(dt['year']),unlist(dt['month']),"01",sep="-")
strptime(timelst,format="%Y-%m-%d")

Jim



On Tue, Mar 6, 2018 at 10:28 AM,  <[hidden email]> wrote:

> Hi List,
>
> The following code returns an "Error in as.POSIXlt.character(x, tz, ...) :   character string is not in a standard unambiguous format"
>
> require(raster)
> require(rts)
> require(stringi)
> r <- raster(ncol=100, nrow=100)
> values(r) <- runif(ncell(r))
> list(ID=seq(1:24),month=rep(str_pad(1:12, pad = 0,width = 2 ,
> "left"),2),year=sort(rep(2016:2017,12)))->dt
> stack(r)->s
> r->rs
> for(i in 1:23){
> rs[]<-r[]*i
>   addLayer(s,rs)->s
> print(nlayers(s))
> }
> timelst<-paste0(unlist(dt['year']),'-',unlist(dt['month']))
> rts(s,time=as.yearmon(timelst))->rsts
> str(rsts@time)
> apply.monthly(rsts,mean)
>
> I was expecting that the statistics accept the yearmonth format of the timeslot, as it allows subsetting.
> Any suggestions?
> Thanks
> Herry
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: raster time series statistics

herry
In reply to this post by David Winsemius
Last line in the following (updated) code produces the error
require(raster)
require(rts)
require(stringr)
r <- raster(ncol=100, nrow=100)
values(r) <- runif(ncell(r))
stack(r)->s
r->rs
for(i in 1:23){
 rs[]<-r[]*i
  addLayer(s,rs)->s
 print(nlayers(s))
}
dt<-list(ID=seq(1:24),month=rep(formatC(1:12,flag=0,width=2),2),
 year=sort(rep(2016:2017,12)))
 timelst<-paste0(unlist(dt['year']),'-',unlist(dt['month']),"-01")
strptime(timelst,format="%Y-%m-%d")->t1

rts(s,time=as.yearmon(t1))->rsts
subset(rsts,'2017')->r2017
class(r2017@time)
class(rsts@time)

apply.monthly(rsts,mean)



-----Original Message-----
From: David Winsemius [mailto:[hidden email]]
Sent: Tuesday, 6 March 2018 11:10 AM
To: Herr, Alexander (L&W, Black Mountain) <[hidden email]>
Cc: [hidden email]
Subject: Re: [R] raster time series statistics


> On Mar 5, 2018, at 3:28 PM, <[hidden email]> <[hidden email]> wrote:
>
> Hi List,
>
> The following code returns an "Error in as.POSIXlt.character(x, tz, ...) :   character string is not in a standard unambiguous format"

I'm unable to produce that error. Which function was being evaluated to produce the error? I don't see where as.POSIXlt would have been called. You don't have any Date or POSIXt-classed variables. (I did need to also load the stringr package to get str_pad into my workspace.)

>
> require(raster)
> require(rts)
> require(stringi)
> r <- raster(ncol=100, nrow=100)
> values(r) <- runif(ncell(r))
> list(ID=seq(1:24),month=rep(str_pad(1:12, pad = 0,width = 2 ,
> "left"),2),year=sort(rep(2016:2017,12)))->dt
> stack(r)->s
> r->rs
> for(i in 1:23){
> rs[]<-r[]*i
>  addLayer(s,rs)->s
> print(nlayers(s))
> }
> timelst<-paste0(unlist(dt['year']),'-',unlist(dt['month']))
> rts(s,time=as.yearmon(timelst))->rsts
> str(rsts@time)
> apply.monthly(rsts,mean)
>
> I was expecting that the statistics accept the yearmonth format of the timeslot, as it allows subsetting.
> Any suggestions?
> Thanks
> Herry
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: raster time series statistics

Jim Lemon-4
I can't test that at the moment as I don't have the libraries. Perhaps later.

Jim

On Tue, Mar 6, 2018 at 11:36 AM,  <[hidden email]> wrote:

> Last line in the following (updated) code produces the error
> require(raster)
> require(rts)
> require(stringr)
> r <- raster(ncol=100, nrow=100)
> values(r) <- runif(ncell(r))
> stack(r)->s
> r->rs
> for(i in 1:23){
>  rs[]<-r[]*i
>   addLayer(s,rs)->s
>  print(nlayers(s))
> }
> dt<-list(ID=seq(1:24),month=rep(formatC(1:12,flag=0,width=2),2),
>  year=sort(rep(2016:2017,12)))
>  timelst<-paste0(unlist(dt['year']),'-',unlist(dt['month']),"-01")
> strptime(timelst,format="%Y-%m-%d")->t1
>
> rts(s,time=as.yearmon(t1))->rsts
> subset(rsts,'2017')->r2017
> class(r2017@time)
> class(rsts@time)
>
> apply.monthly(rsts,mean)
>
>
>
> -----Original Message-----
> From: David Winsemius [mailto:[hidden email]]
> Sent: Tuesday, 6 March 2018 11:10 AM
> To: Herr, Alexander (L&W, Black Mountain) <[hidden email]>
> Cc: [hidden email]
> Subject: Re: [R] raster time series statistics
>
>
>> On Mar 5, 2018, at 3:28 PM, <[hidden email]> <[hidden email]> wrote:
>>
>> Hi List,
>>
>> The following code returns an "Error in as.POSIXlt.character(x, tz, ...) :   character string is not in a standard unambiguous format"
>
> I'm unable to produce that error. Which function was being evaluated to produce the error? I don't see where as.POSIXlt would have been called. You don't have any Date or POSIXt-classed variables. (I did need to also load the stringr package to get str_pad into my workspace.)
>
>>
>> require(raster)
>> require(rts)
>> require(stringi)
>> r <- raster(ncol=100, nrow=100)
>> values(r) <- runif(ncell(r))
>> list(ID=seq(1:24),month=rep(str_pad(1:12, pad = 0,width = 2 ,
>> "left"),2),year=sort(rep(2016:2017,12)))->dt
>> stack(r)->s
>> r->rs
>> for(i in 1:23){
>> rs[]<-r[]*i
>>  addLayer(s,rs)->s
>> print(nlayers(s))
>> }
>> timelst<-paste0(unlist(dt['year']),'-',unlist(dt['month']))
>> rts(s,time=as.yearmon(timelst))->rsts
>> str(rsts@time)
>> apply.monthly(rsts,mean)
>>
>> I was expecting that the statistics accept the yearmonth format of the timeslot, as it allows subsetting.
>> Any suggestions?
>> Thanks
>> Herry
>>
>>       [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> David Winsemius
> Alameda, CA, USA
>
> 'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: raster time series statistics

herry
It works if you use as.Date. But this defeates the purpose for the yearmon notion...

require(raster)
require(rts)
require(stringr)
r <- raster(ncol=100, nrow=100)
values(r) <- runif(ncell(r))
stack(r)->s
r->rs
for(i in 1:23){
 rs[]<-r[]*i
  addLayer(s,rs)->s
 print(nlayers(s))
}
dt<-list(ID=seq(1:24),month=rep(formatC(1:12,flag=0,width=2),2), year=sort(rep(2016:2017,12)))
 timelst<-paste0(unlist(dt['year']),'-',unlist(dt['month']),"-01")
strptime(timelst,format="%Y-%m-%d")->t1


rts(s,time=as.yearmon(t1))->rsts
subset(rsts,'2017')->r2017
class(r2017@time)
class(rsts@time)
apply.monthly(rsts,mean) # this creates error

rts(s,time=as.Date(t1))->rsts1
apply.monthly(rsts1,mean) # this creates output


-----Original Message-----
From: Jim Lemon [mailto:[hidden email]]
Sent: Tuesday, 6 March 2018 11:40 AM
To: Herr, Alexander (L&W, Black Mountain) <[hidden email]>
Cc: David Winsemius <[hidden email]>; r-help mailing list <[hidden email]>
Subject: Re: [R] raster time series statistics

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: raster time series statistics

David Winsemius

> On Mar 5, 2018, at 9:07 PM, <[hidden email]> <[hidden email]> wrote:
>
> It works if you use as.Date. But this defeates the purpose for the yearmon notion...
>
> require(raster)
> require(rts)
> require(stringr)
> r <- raster(ncol=100, nrow=100)
> values(r) <- runif(ncell(r))
> stack(r)->s
> r->rs
> for(i in 1:23){
> rs[]<-r[]*i
>  addLayer(s,rs)->s
> print(nlayers(s))
> }
> dt<-list(ID=seq(1:24),month=rep(formatC(1:12,flag=0,width=2),2), year=sort(rep(2016:2017,12)))
> timelst<-paste0(unlist(dt['year']),'-',unlist(dt['month']),"-01")
> strptime(timelst,format="%Y-%m-%d")->t1
>
>
> rts(s,time=as.yearmon(t1))->rsts
> subset(rsts,'2017')->r2017
> class(r2017@time)
> class(rsts@time)
> apply.monthly(rsts,mean) # this creates error
>
> rts(s,time=as.Date(t1))->rsts1
> apply.monthly(rsts1,mean) # this creates output

So it appears that you have shown that apply.monthly doesn't work with 'yearmon'-classed vectors in the endpoints of a RasterStackTS. It's not documented as doing so after all. So this is really a feature request for the maintainer. You should contact the responsible individual.

maintainer('rts')
[1] "Babak Naimi <[hidden email]>"

>
>
> -----Original Message-----
> From: Jim Lemon [mailto:[hidden email]]
> Sent: Tuesday, 6 March 2018 11:40 AM
> To: Herr, Alexander (L&W, Black Mountain) <[hidden email]>
> Cc: David Winsemius <[hidden email]>; r-help mailing list <[hidden email]>
> Subject: Re: [R] raster time series statistics
>

David Winsemius
Alameda, CA, USA

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.