spacetime: creating STIDF objects

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

spacetime: creating STIDF objects

Facundo Muñoz-2
Dear all, I have just posted an issue [1] in the GitHub repository for
the "spacetime" package that I reproduce here:

I have a "long" table with irregular data (observations for some
municipalities at specific months) that I would like to store together
with the administrative map of the region (SpatialPolygons). I tried
unsuccessfully to directly create the STIDF object using both
`stConstruct()` and `STIDF`.

In all the related examples in the package you first create a STFDF
which is later coerced to STIDF. But my dataset is very sparse.

Here is a reproducible example adapted from the examples in the package.
If I use the complete table it leads to a STFDF object as expected and
it works fine. However if the table is not complete it should switch to
a STIDF representation (as far as I understand from the JSS paper).

``` r

library(spacetime)
library(sp)
sp = cbind(x = c(0,0,1), y = c(0,1,1))
row.names(sp) = paste0("point", 1:nrow(sp))
sp = SpatialPoints(sp)
time = as.POSIXct("2010-08-05")+3600*(10:13)
m = c(10,20,30) # means for each of the 3 point locations
mydata = rnorm(length(sp)*length(time),mean=rep(m, 4))

mydata = data.frame(
  space = rep(row.names(sp), 4),
  time  = rep(time, each = 3),
  values = signif(mydata,3)
)

mystfdf <- stConstruct(mydata, "space", "time", sp)  # This works OK

mystidf <- stConstruct(mydata[-1,], "space", "time", sp)
#> Error: nrow(object@time) == length(object@sp) is not TRUE
```


Thank you in advance.

ƒacu.-


[1] https://github.com/edzer/spacetime/issues/30

--
Cirad <https://umr-astre.cirad.fr/>
*Facundo Muñoz*
/Biostatistician/
Bios - UMR ASTRE
Bureau E-203 - TA A-117 / E
Campus international de Baillarguet
34398 Montpellier Cedex 5
France
+33 467 593 868
@famuvie

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: spacetime: creating STIDF objects

Roland Harhoff-2
Hey Facundo,

I'm not sure about the stConstruct issue (yet) ...
but you can create a STIDF object from your example data by

mystidf <- STIDF(rbind(sp,sp,sp,sp), mydata$time, mydata[ , "values", drop = F])

See also the documentation ...

?spacetime::STIDF

HTH!

Best wishes ...

Roland


Facundo Muñoz schrieb am 2017-11-09:
> Dear all, I have just posted an issue [1] in the GitHub repository for
> the "spacetime" package that I reproduce here:

> I have a "long" table with irregular data (observations for some
> municipalities at specific months) that I would like to store together
> with the administrative map of the region (SpatialPolygons). I tried
> unsuccessfully to directly create the STIDF object using both
> `stConstruct()` and `STIDF`.

> In all the related examples in the package you first create a STFDF
> which is later coerced to STIDF. But my dataset is very sparse.

> Here is a reproducible example adapted from the examples in the package.
> If I use the complete table it leads to a STFDF object as expected and
> it works fine. However if the table is not complete it should switch to
> a STIDF representation (as far as I understand from the JSS paper).

> ``` r

> library(spacetime)
> library(sp)
> sp = cbind(x = c(0,0,1), y = c(0,1,1))
> row.names(sp) = paste0("point", 1:nrow(sp))
> sp = SpatialPoints(sp)
> time = as.POSIXct("2010-08-05")+3600*(10:13)
> m = c(10,20,30) # means for each of the 3 point locations
> mydata = rnorm(length(sp)*length(time),mean=rep(m, 4))

> mydata = data.frame(
>   space = rep(row.names(sp), 4),
>   time  = rep(time, each = 3),
>   values = signif(mydata,3)
> )

> mystfdf <- stConstruct(mydata, "space", "time", sp)  # This works OK

> mystidf <- stConstruct(mydata[-1,], "space", "time", sp)
> #> Error: nrow(object@time) == length(object@sp) is not TRUE
> ```


> Thank you in advance.

> ƒacu.-


> [1] https://github.com/edzer/spacetime/issues/30

> --
> Cirad <https://umr-astre.cirad.fr/>
> *Facundo Muñoz*
> /Biostatistician/
> Bios - UMR ASTRE
> Bureau E-203 - TA A-117 / E
> Campus international de Baillarguet
> 34398 Montpellier Cedex 5
> France
> +33 467 593 868
> @famuvie

> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: spacetime: creating STIDF objects

Facundo Muñoz-2
In reply to this post by Facundo Muñoz-2
Hi Roland, thanks for your answer.

However, this solution does not work when sp is a SpatialPolygons (which
is my case, of course):

> rbind(sp, sp)

Error in validObject(res) :
  invalid class “SpatialPolygons” object: non-unique Polygons ID slot values


I'm sorry that my toy example didn't reproduce exactly my situation.

Thanks in any case.

ƒacu.-


On 11/09/2017 03:10 PM, Roland Harhoff wrote:

> Hey Facundo,
>
> I'm not sure about the stConstruct issue (yet) ...
> but you can create a STIDF object from your example data by
>
> mystidf <- STIDF(rbind(sp,sp,sp,sp), mydata$time, mydata[ , "values", drop = F])
>
> See also the documentation ...
>
> ?spacetime::STIDF
>
> HTH so far ...!
>
> Best wishes ...
>
> Roland
>
>
> Facundo Muñoz schrieb am 2017-11-09:
>> Dear all, I have just posted an issue [1] in the GitHub repository for
>> the "spacetime" package that I reproduce here:
>> I have a "long" table with irregular data (observations for some
>> municipalities at specific months) that I would like to store together
>> with the administrative map of the region (SpatialPolygons). I tried
>> unsuccessfully to directly create the STIDF object using both
>> `stConstruct()` and `STIDF`.
>> In all the related examples in the package you first create a STFDF
>> which is later coerced to STIDF. But my dataset is very sparse.
>> Here is a reproducible example adapted from the examples in the package.
>> If I use the complete table it leads to a STFDF object as expected and
>> it works fine. However if the table is not complete it should switch to
>> a STIDF representation (as far as I understand from the JSS paper).
>> ``` r
>> library(spacetime)
>> library(sp)
>> sp = cbind(x = c(0,0,1), y = c(0,1,1))
>> row.names(sp) = paste0("point", 1:nrow(sp))
>> sp = SpatialPoints(sp)
>> time = as.POSIXct("2010-08-05")+3600*(10:13)
>> m = c(10,20,30) # means for each of the 3 point locations
>> mydata = rnorm(length(sp)*length(time),mean=rep(m, 4))
>> mydata = data.frame(
>>   space = rep(row.names(sp), 4),
>>   time  = rep(time, each = 3),
>>   values = signif(mydata,3)
>> )
>> mystfdf <- stConstruct(mydata, "space", "time", sp)  # This works OK
>> mystidf <- stConstruct(mydata[-1,], "space", "time", sp)
>> #> Error: nrow(object@time) == length(object@sp) is not TRUE
>> ```
>
>> Thank you in advance.
>> ƒacu.-
>
>> [1] https://github.com/edzer/spacetime/issues/30
>> --
>> Cirad <https://umr-astre.cirad.fr/>
>> *Facundo Muñoz*
>> /Biostatistician/
>> Bios - UMR ASTRE
>> Bureau E-203 - TA A-117 / E
>> Campus international de Baillarguet
>> 34398 Montpellier Cedex 5
>> France
>> +33 467 593 868
>> @famuvie
>> _______________________________________________
>> R-sig-Geo mailing list
>> [hidden email]
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo


        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: spacetime: creating STIDF objects

edzer


On 11/09/2017 03:32 PM, Facundo Muñoz wrote:

> Hi Roland, thanks for your answer.
>
> However, this solution does not work when sp is a SpatialPolygons (which
> is my case, of course):
>
>> rbind(sp, sp)
>
> Error in validObject(res) :
>   invalid class “SpatialPolygons” object: non-unique Polygons ID slot values
>

rbind(sp, sp, makeUniqueIDs = TRUE)

>
> I'm sorry that my toy example didn't reproduce exactly my situation.
>
> Thanks in any case.
>
> ƒacu.-
>
>
> On 11/09/2017 03:10 PM, Roland Harhoff wrote:
>> Hey Facundo,
>>
>> I'm not sure about the stConstruct issue (yet) ...
>> but you can create a STIDF object from your example data by
>>
>> mystidf <- STIDF(rbind(sp,sp,sp,sp), mydata$time, mydata[ , "values", drop = F])
>>
>> See also the documentation ...
>>
>> ?spacetime::STIDF
>>
>> HTH so far ...!
>>
>> Best wishes ...
>>
>> Roland
>>
>>
>> Facundo Muñoz schrieb am 2017-11-09:
>>> Dear all, I have just posted an issue [1] in the GitHub repository for
>>> the "spacetime" package that I reproduce here:
>>> I have a "long" table with irregular data (observations for some
>>> municipalities at specific months) that I would like to store together
>>> with the administrative map of the region (SpatialPolygons). I tried
>>> unsuccessfully to directly create the STIDF object using both
>>> `stConstruct()` and `STIDF`.
>>> In all the related examples in the package you first create a STFDF
>>> which is later coerced to STIDF. But my dataset is very sparse.
>>> Here is a reproducible example adapted from the examples in the package.
>>> If I use the complete table it leads to a STFDF object as expected and
>>> it works fine. However if the table is not complete it should switch to
>>> a STIDF representation (as far as I understand from the JSS paper).
>>> ``` r
>>> library(spacetime)
>>> library(sp)
>>> sp = cbind(x = c(0,0,1), y = c(0,1,1))
>>> row.names(sp) = paste0("point", 1:nrow(sp))
>>> sp = SpatialPoints(sp)
>>> time = as.POSIXct("2010-08-05")+3600*(10:13)
>>> m = c(10,20,30) # means for each of the 3 point locations
>>> mydata = rnorm(length(sp)*length(time),mean=rep(m, 4))
>>> mydata = data.frame(
>>>   space = rep(row.names(sp), 4),
>>>   time  = rep(time, each = 3),
>>>   values = signif(mydata,3)
>>> )
>>> mystfdf <- stConstruct(mydata, "space", "time", sp)  # This works OK
>>> mystidf <- stConstruct(mydata[-1,], "space", "time", sp)
>>> #> Error: nrow(object@time) == length(object@sp) is not TRUE
>>> ```
>>
>>> Thank you in advance.
>>> ƒacu.-
>>
>>> [1] https://github.com/edzer/spacetime/issues/30
>>> --
>>> Cirad <https://umr-astre.cirad.fr/>
>>> *Facundo Muñoz*
>>> /Biostatistician/
>>> Bios - UMR ASTRE
>>> Bureau E-203 - TA A-117 / E
>>> Campus international de Baillarguet
>>> 34398 Montpellier Cedex 5
>>> France
>>> +33 467 593 868
>>> @famuvie
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> [hidden email]
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

--
Edzer Pebesma
Institute for Geoinformatics
Heisenbergstrasse 2, 48151 Muenster, Germany
Phone: +49 251 8333081

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: spacetime: creating STIDF objects

Facundo Muñoz-2
Dear Edzer,

thanks for your response. That's cool, but still it does not solve the
problem.

rbind can be useful for a full grid of observations in space-time, but
mine is sparse. So I really need the subset method `[` for
SpatialPolygons, which does not have this argument makeUniqueIDs.

However, you pointed me to the right direction, and from the code of
rbind I could manage to manually perform the subset and building a
replicated spatial object with unique IDs. The code reproducing the
example is in the GitHub issue [1].

In conclusion:

- It would be good to allow for makeUniqueIDs in the method `[`
for the class SpatialPolygons. That would avoid the manual crafting
of the replicated Spatial object

- I think stConstruct() should perform the subsetting internally for
consistency. So that the argument SpatialObj doesn't have a different
meaning dependind of whether x is a full grid or not.

- There is something else going on with |stplot()|, but the object is
correctly created.



[1] https://github.com/edzer/spacetime/issues/30


On 11/09/2017 03:52 PM, Edzer Pebesma wrote:

>
> On 11/09/2017 03:32 PM, Facundo Muñoz wrote:
>> Hi Roland, thanks for your answer.
>>
>> However, this solution does not work when sp is a SpatialPolygons (which
>> is my case, of course):
>>
>>> rbind(sp, sp)
>> Error in validObject(res) :
>>   invalid class “SpatialPolygons” object: non-unique Polygons ID slot values
>>
> rbind(sp, sp, makeUniqueIDs = TRUE)
>
>> I'm sorry that my toy example didn't reproduce exactly my situation.
>>
>> Thanks in any case.
>>
>> ƒacu.-
>>
>>
>> On 11/09/2017 03:10 PM, Roland Harhoff wrote:
>>> Hey Facundo,
>>>
>>> I'm not sure about the stConstruct issue (yet) ...
>>> but you can create a STIDF object from your example data by
>>>
>>> mystidf <- STIDF(rbind(sp,sp,sp,sp), mydata$time, mydata[ , "values", drop = F])
>>>
>>> See also the documentation ...
>>>
>>> ?spacetime::STIDF
>>>
>>> HTH so far ...!
>>>
>>> Best wishes ...
>>>
>>> Roland
>>>
>>>
>>> Facundo Muñoz schrieb am 2017-11-09:
>>>> Dear all, I have just posted an issue [1] in the GitHub repository for
>>>> the "spacetime" package that I reproduce here:
>>>> I have a "long" table with irregular data (observations for some
>>>> municipalities at specific months) that I would like to store together
>>>> with the administrative map of the region (SpatialPolygons). I tried
>>>> unsuccessfully to directly create the STIDF object using both
>>>> `stConstruct()` and `STIDF`.
>>>> In all the related examples in the package you first create a STFDF
>>>> which is later coerced to STIDF. But my dataset is very sparse.
>>>> Here is a reproducible example adapted from the examples in the package.
>>>> If I use the complete table it leads to a STFDF object as expected and
>>>> it works fine. However if the table is not complete it should switch to
>>>> a STIDF representation (as far as I understand from the JSS paper).
>>>> ``` r
>>>> library(spacetime)
>>>> library(sp)
>>>> sp = cbind(x = c(0,0,1), y = c(0,1,1))
>>>> row.names(sp) = paste0("point", 1:nrow(sp))
>>>> sp = SpatialPoints(sp)
>>>> time = as.POSIXct("2010-08-05")+3600*(10:13)
>>>> m = c(10,20,30) # means for each of the 3 point locations
>>>> mydata = rnorm(length(sp)*length(time),mean=rep(m, 4))
>>>> mydata = data.frame(
>>>>   space = rep(row.names(sp), 4),
>>>>   time  = rep(time, each = 3),
>>>>   values = signif(mydata,3)
>>>> )
>>>> mystfdf <- stConstruct(mydata, "space", "time", sp)  # This works OK
>>>> mystidf <- stConstruct(mydata[-1,], "space", "time", sp)
>>>> #> Error: nrow(object@time) == length(object@sp) is not TRUE
>>>> ```
>>>> Thank you in advance.
>>>> ƒacu.-
>>>> [1] https://github.com/edzer/spacetime/issues/30
>>>> --
>>>> Cirad <https://umr-astre.cirad.fr/>
>>>> *Facundo Muñoz*
>>>> /Biostatistician/
>>>> Bios - UMR ASTRE
>>>> Bureau E-203 - TA A-117 / E
>>>> Campus international de Baillarguet
>>>> 34398 Montpellier Cedex 5
>>>> France
>>>> +33 467 593 868
>>>> @famuvie
>>>> _______________________________________________
>>>> R-sig-Geo mailing list
>>>> [hidden email]
>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>> [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> [hidden email]
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo