adehabitatMA/adehabitatHS count.points / mimage(cp) Errors

classic Classic list List threaded Threaded
3 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

adehabitatMA/adehabitatHS count.points / mimage(cp) Errors

R.J.Ward
Hi,

I have been working with the adehabitatMA, HS, HR and LT packages, and have become stuck due to an error whilst using the count.points and subsequent mimage() functions during analyses of habitat selection of 12 radiotracked snakes. I wasn't sure if this was more relevant to R-sig-geo or here so I hope this is a suitable place to post.

I would greatly appreciate any help as I've not managed to solve it despite checking forums, the vignette and other R Documentation. I have produced the errors below with some shortened code to show the process.
'Locs' contains our relocations for 12 snakes, and 'map' is a SpatialPixelsDataFrame consisting of stacked rasters of vegetation and other environmental variables such as slope and aspect.


>class(locs) # Check class
[1] "SpatialPointsDataFrame"
>class(map) # Check class
[1] "SpatialPixelsDataFrame"
attr(,"package")
[1] "sp"
>proj4string(locs)==proj4string(map) # Check projections match
[1] TRUE
>cp<-count.points(locs,map) # Count points
Error in count.points(SpatialPoints(x), w) :
different proj4string in w and xy
# I have seen in a forum <http://stackoverflow.com/questions/16519639/why-does-r-think-the-projection-data-are-different> that it may be worth trying it as a SpatialPoints format instead so I ran:
>locs_sp<-as(locs, "SpatialPoints")
> class(locs_sp)
[1] "SpatialPoints"
attr(,"package")
[1] "sp"
## This works, but when I run the following there is another error using mimage:
>cp.sp<-count.points(locs_sp, map)
>mimage(cp.sp)
Error in checkSlotAssignment(object, name, value) :
assignment of an object of class "numeric" is not valid for slot 'data' in an object of class "SpatialPixelsDataFrame"; is(value, "data.frame") is not TRUE
I am unable to proceed past this point and run the associated K-select analysis in section 5.1 of the vignette without solving this issue. I've noticed that in the example data (lynxjura) there is no associated projection, and am wondering if perhaps the projection is the cause of the issue?

If it is useful, below is the structure of my relocation data. I am assuming one key issue with converting it to SpatialPoints is that the unique ID codes for each animal are lost.

> str(locs)
Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
..@ data :'data.frame': 970 obs. of 1 variable:
.. ..$ Code: Factor w/ 12 levels "LMNN003X","LMNN005X",..: 1 1 1 1 1 1 1 1 1 1 ...
..@ coords.nrs : num(0)
..@ coords : num [1:970, 1:2] 33176 33175 33175 33175 33175 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "X" "Y"
..@ bbox : num [1:2, 1:2] 33081 69752 33617 70571
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "X" "Y"
.. .. ..$ : chr [1:2] "min" "max"
..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..@ projargs: chr "+init=epsg:3109 +proj=tmerc +lat_0=49.225 +lon_0=-2.135 +k=0.9999999000000001 +x_0=40000 +y_0=70000 +ellps=GRS80 +towgs84=0,0,0"| __truncated__

Kind regards,

Rob


________________________________
Robert J. Ward |PhD Candidate - Biodiversity Management| DICE, University of Kent School of Anthropology and Conservation, Marlowe Building, University of Kent, Canterbury, CT2 7NR | Tel: +44(0)7747751180 / (0)1227824076 | E-mail: [hidden email]<https://owa.connect.kent.ac.uk/ecp/Customize/rjw53@...> | Skype: rob.j.ward | [https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcThgccJHtviidhvODxhoa7UnIr6l25te5OLkN7ikQyroURmqH5w2A] <http://www.linkedin.com/pub/rob-ward/45/42a/bb4> | Twitter: @reptile_rob<https://twitter.com/reptile_rob> / @ThinkGrassSnake<https://twitter.com/ThinkGrassSnake> | Web: www.ThinkGrassSnake.Je<http://www.thinkgrasssnake.je/>


        [[alternative HTML version deleted]]

_______________________________________________
R-sig-ecology mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: adehabitatMA/adehabitatHS count.points / mimage(cp) Errors

Mathieu Basille-3
Hi Rob,

This is actually an identified bug of count.points... which has already
been resolved, and should hopefully make it to the next release. In the
meantime, please try the function below, I believe it should solve your
problem.

Best,
Mathieu.


======================================================================

count.points <- function (xy, w)
{
    if (is(w, "SpatialGrid"))
        fullgrid(w) = FALSE
    if (!inherits(w, "SpatialPixels"))
        stop("w should inherit the class SpatialPixels")
    if (!inherits(xy, "SpatialPoints"))
        stop("xy should inherit the class SpatialPoints")
    pfsx <- proj4string(w)
    pfsxy <- proj4string(xy)
    if (!identical(pfsx, pfsxy))
        stop("different proj4string in w and xy")
    gr <- gridparameters(w)
    if (nrow(gr) > 2)
        stop("w should be defined in two dimensions")
    if ((gr[1, 2] - gr[2, 2]) > get(".adeoptions", envir =
.adehabitatMAEnv)$epsilon)
        stop("the cellsize should be the same in x and y directions")
    if (ncol(coordinates(xy)) > 2)
        stop("xy should be defined in two dimensions")
    meth <- "one"
    if (inherits(xy, "SpatialPointsDataFrame")) {
        if (ncol(xy) == 1) {
            meth = "sev"
        }
        else {
            meth = "one"
            warning("several columns in the SpatialPointsDataFrame, no id
considered")
        }
    }
    if (meth == "one") {
        ov <- over(xy, geometry(w))
        oo <- table(ov)
        repo <- rep(0, length(w[[1]]))
        repo[as.numeric(names(oo))] <- oo
        repo <- data.frame(x = repo)
        coordinates(repo) <- coordinates(w)
        gridded(repo) <- TRUE
        if (!is.na(pfsx))
            proj4string(repo) <- CRS(pfsx)
        return(repo)
    }
    else {
        id <- factor(xy[[1]])
        xy2 <- as.data.frame(coordinates(xy))
        lixy <- split(xy2, id)
        cp <- lapply(lixy, function(x) {
            count.points(SpatialPoints(x, proj4string =
CRS(proj4string(xy))), w)
        })
        cp <- do.call("data.frame", lapply(cp, function(x) x[[1]]))
        coordinates(cp) <- coordinates(w)
        gridded(cp) <- TRUE
        if (!is.na(pfsx))
            proj4string(cp) <- CRS(pfsx)
        return(cp)
    }
}




Le 04/05/2016 11:17, R.J.Ward a écrit :

> Hi,
>
> I have been working with the adehabitatMA, HS, HR and LT packages, and have become stuck due to an error whilst using the count.points and subsequent mimage() functions during analyses of habitat selection of 12 radiotracked snakes. I wasn't sure if this was more relevant to R-sig-geo or here so I hope this is a suitable place to post.
>
> I would greatly appreciate any help as I've not managed to solve it despite checking forums, the vignette and other R Documentation. I have produced the errors below with some shortened code to show the process.
> 'Locs' contains our relocations for 12 snakes, and 'map' is a SpatialPixelsDataFrame consisting of stacked rasters of vegetation and other environmental variables such as slope and aspect.
>
>
>> class(locs) # Check class
> [1] "SpatialPointsDataFrame"
>> class(map) # Check class
> [1] "SpatialPixelsDataFrame"
> attr(,"package")
> [1] "sp"
>> proj4string(locs)==proj4string(map) # Check projections match
> [1] TRUE
>> cp<-count.points(locs,map) # Count points
> Error in count.points(SpatialPoints(x), w) :
> different proj4string in w and xy
> # I have seen in a forum <http://stackoverflow.com/questions/16519639/why-does-r-think-the-projection-data-are-different> that it may be worth trying it as a SpatialPoints format instead so I ran:
>> locs_sp<-as(locs, "SpatialPoints")
>> class(locs_sp)
> [1] "SpatialPoints"
> attr(,"package")
> [1] "sp"
> ## This works, but when I run the following there is another error using mimage:
>> cp.sp<-count.points(locs_sp, map)
>> mimage(cp.sp)
> Error in checkSlotAssignment(object, name, value) :
> assignment of an object of class "numeric" is not valid for slot 'data' in an object of class "SpatialPixelsDataFrame"; is(value, "data.frame") is not TRUE
> I am unable to proceed past this point and run the associated K-select analysis in section 5.1 of the vignette without solving this issue. I've noticed that in the example data (lynxjura) there is no associated projection, and am wondering if perhaps the projection is the cause of the issue?
>
> If it is useful, below is the structure of my relocation data. I am assuming one key issue with converting it to SpatialPoints is that the unique ID codes for each animal are lost.
>
>> str(locs)
> Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
> ..@ data :'data.frame': 970 obs. of 1 variable:
> .. ..$ Code: Factor w/ 12 levels "LMNN003X","LMNN005X",..: 1 1 1 1 1 1 1 1 1 1 ...
> ..@ coords.nrs : num(0)
> ..@ coords : num [1:970, 1:2] 33176 33175 33175 33175 33175 ...
> .. ..- attr(*, "dimnames")=List of 2
> .. .. ..$ : NULL
> .. .. ..$ : chr [1:2] "X" "Y"
> ..@ bbox : num [1:2, 1:2] 33081 69752 33617 70571
> .. ..- attr(*, "dimnames")=List of 2
> .. .. ..$ : chr [1:2] "X" "Y"
> .. .. ..$ : chr [1:2] "min" "max"
> ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
> .. .. ..@ projargs: chr "+init=epsg:3109 +proj=tmerc +lat_0=49.225 +lon_0=-2.135 +k=0.9999999000000001 +x_0=40000 +y_0=70000 +ellps=GRS80 +towgs84=0,0,0"| __truncated__
>
> Kind regards,
>
> Rob
>
>
> ________________________________
> Robert J. Ward |PhD Candidate - Biodiversity Management| DICE, University of Kent School of Anthropology and Conservation, Marlowe Building, University of Kent, Canterbury, CT2 7NR | Tel: +44(0)7747751180 / (0)1227824076 | E-mail: [hidden email]<https://owa.connect.kent.ac.uk/ecp/Customize/rjw53@...> | Skype: rob.j.ward | [https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcThgccJHtviidhvODxhoa7UnIr6l25te5OLkN7ikQyroURmqH5w2A] <http://www.linkedin.com/pub/rob-ward/45/42a/bb4> | Twitter: @reptile_rob<https://twitter.com/reptile_rob> / @ThinkGrassSnake<https://twitter.com/ThinkGrassSnake> | Web: www.ThinkGrassSnake.Je<http://www.thinkgrasssnake.je/>
>
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-ecology mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>

--

Mathieu Basille

[hidden email] | http://ase-research.org/basille
+1 954-577-6314 | University of Florida FLREC

  « Le tout est de tout dire, et je manque de mots
  Et je manque de temps, et je manque d'audace. »
  — Paul Éluard

This message is signed to guarantee its authenticity.
For a true private correspondence, use my public key
to encrypt your messages:

  http://mathieu.basille.net/pub.asc

Learn more: http://mzl.la/1BsOGiZ

_______________________________________________
R-sig-ecology mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: adehabitatMA/adehabitatHS count.points / mimage(cp) Errors

R.J.Ward
Hi Mathieu,

Thank you very much, that has solved the problem.

Best wishes,

Rob

-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
Sent: 05 May 2016 23:51
To: R.J.Ward <[hidden email]>; [hidden email]
Subject: Re: [R-sig-eco] adehabitatMA/adehabitatHS count.points / mimage(cp) Errors

Hi Rob,

This is actually an identified bug of count.points... which has already been resolved, and should hopefully make it to the next release. In the meantime, please try the function below, I believe it should solve your problem.

Best,
Mathieu.


======================================================================

count.points <- function (xy, w)
{
    if (is(w, "SpatialGrid"))
        fullgrid(w) = FALSE
    if (!inherits(w, "SpatialPixels"))
        stop("w should inherit the class SpatialPixels")
    if (!inherits(xy, "SpatialPoints"))
        stop("xy should inherit the class SpatialPoints")
    pfsx <- proj4string(w)
    pfsxy <- proj4string(xy)
    if (!identical(pfsx, pfsxy))
        stop("different proj4string in w and xy")
    gr <- gridparameters(w)
    if (nrow(gr) > 2)
        stop("w should be defined in two dimensions")
    if ((gr[1, 2] - gr[2, 2]) > get(".adeoptions", envir =
.adehabitatMAEnv)$epsilon)
        stop("the cellsize should be the same in x and y directions")
    if (ncol(coordinates(xy)) > 2)
        stop("xy should be defined in two dimensions")
    meth <- "one"
    if (inherits(xy, "SpatialPointsDataFrame")) {
        if (ncol(xy) == 1) {
            meth = "sev"
        }
        else {
            meth = "one"
            warning("several columns in the SpatialPointsDataFrame, no id
considered")
        }
    }
    if (meth == "one") {
        ov <- over(xy, geometry(w))
        oo <- table(ov)
        repo <- rep(0, length(w[[1]]))
        repo[as.numeric(names(oo))] <- oo
        repo <- data.frame(x = repo)
        coordinates(repo) <- coordinates(w)
        gridded(repo) <- TRUE
        if (!is.na(pfsx))
            proj4string(repo) <- CRS(pfsx)
        return(repo)
    }
    else {
        id <- factor(xy[[1]])
        xy2 <- as.data.frame(coordinates(xy))
        lixy <- split(xy2, id)
        cp <- lapply(lixy, function(x) {
            count.points(SpatialPoints(x, proj4string = CRS(proj4string(xy))), w)
        })
        cp <- do.call("data.frame", lapply(cp, function(x) x[[1]]))
        coordinates(cp) <- coordinates(w)
        gridded(cp) <- TRUE
        if (!is.na(pfsx))
            proj4string(cp) <- CRS(pfsx)
        return(cp)
    }
}




Le 04/05/2016 11:17, R.J.Ward a écrit :

> Hi,
>
> I have been working with the adehabitatMA, HS, HR and LT packages, and have become stuck due to an error whilst using the count.points and subsequent mimage() functions during analyses of habitat selection of 12 radiotracked snakes. I wasn't sure if this was more relevant to R-sig-geo or here so I hope this is a suitable place to post.
>
> I would greatly appreciate any help as I've not managed to solve it despite checking forums, the vignette and other R Documentation. I have produced the errors below with some shortened code to show the process.
> 'Locs' contains our relocations for 12 snakes, and 'map' is a SpatialPixelsDataFrame consisting of stacked rasters of vegetation and other environmental variables such as slope and aspect.
>
>
>> class(locs) # Check class
> [1] "SpatialPointsDataFrame"
>> class(map) # Check class
> [1] "SpatialPixelsDataFrame"
> attr(,"package")
> [1] "sp"
>> proj4string(locs)==proj4string(map) # Check projections match
> [1] TRUE
>> cp<-count.points(locs,map) # Count points
> Error in count.points(SpatialPoints(x), w) :
> different proj4string in w and xy
> # I have seen in a forum <http://stackoverflow.com/questions/16519639/why-does-r-think-the-projection-data-are-different> that it may be worth trying it as a SpatialPoints format instead so I ran:
>> locs_sp<-as(locs, "SpatialPoints")
>> class(locs_sp)
> [1] "SpatialPoints"
> attr(,"package")
> [1] "sp"
> ## This works, but when I run the following there is another error using mimage:
>> cp.sp<-count.points(locs_sp, map)
>> mimage(cp.sp)
> Error in checkSlotAssignment(object, name, value) :
> assignment of an object of class "numeric" is not valid for slot
> 'data' in an object of class "SpatialPixelsDataFrame"; is(value, "data.frame") is not TRUE I am unable to proceed past this point and run the associated K-select analysis in section 5.1 of the vignette without solving this issue. I've noticed that in the example data (lynxjura) there is no associated projection, and am wondering if perhaps the projection is the cause of the issue?
>
> If it is useful, below is the structure of my relocation data. I am assuming one key issue with converting it to SpatialPoints is that the unique ID codes for each animal are lost.
>
>> str(locs)
> Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots ..@
> data :'data.frame': 970 obs. of 1 variable:
> .. ..$ Code: Factor w/ 12 levels "LMNN003X","LMNN005X",..: 1 1 1 1 1 1 1 1 1 1 ...
> ..@ coords.nrs : num(0)
> ..@ coords : num [1:970, 1:2] 33176 33175 33175 33175 33175 ...
> .. ..- attr(*, "dimnames")=List of 2
> .. .. ..$ : NULL
> .. .. ..$ : chr [1:2] "X" "Y"
> ..@ bbox : num [1:2, 1:2] 33081 69752 33617 70571 .. ..- attr(*,
> "dimnames")=List of 2 .. .. ..$ : chr [1:2] "X" "Y"
> .. .. ..$ : chr [1:2] "min" "max"
> ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot .. ..
> ..@ projargs: chr "+init=epsg:3109 +proj=tmerc +lat_0=49.225
> +lon_0=-2.135 +k=0.9999999000000001 +x_0=40000 +y_0=70000 +ellps=GRS80
> +towgs84=0,0,0"| __truncated__
>
> Kind regards,
>
> Rob
>
>
> ________________________________
> Robert J. Ward |PhD Candidate - Biodiversity Management| DICE,
> University of Kent School of Anthropology and Conservation, Marlowe
> Building, University of Kent, Canterbury, CT2 7NR | Tel:
> +44(0)7747751180 / (0)1227824076 | E-mail:
> [hidden email]<https://owa.connect.kent.ac.uk/ecp/Customize/rjw53@ke
> nt.ac.uk> | Skype: rob.j.ward |
> [https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcThgccJHtviidhvO
> Dxhoa7UnIr6l25te5OLkN7ikQyroURmqH5w2A]
> <http://www.linkedin.com/pub/rob-ward/45/42a/bb4> | Twitter:
> @reptile_rob<https://twitter.com/reptile_rob> /
> @ThinkGrassSnake<https://twitter.com/ThinkGrassSnake> | Web:
> www.ThinkGrassSnake.Je<http://www.thinkgrasssnake.je/>
>
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-ecology mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>

--

Mathieu Basille

[hidden email] | http://ase-research.org/basille
+1 954-577-6314 | University of Florida FLREC

  « Le tout est de tout dire, et je manque de mots
  Et je manque de temps, et je manque d'audace. »
  - Paul Éluard

This message is signed to guarantee its authenticity.
For a true private correspondence, use my public key to encrypt your messages:

  http://mathieu.basille.net/pub.asc

Learn more: http://mzl.la/1BsOGiZ

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