@@ -49,45 +49,62 @@ setValidity("Survey.LT",
4949# ' @rdname run.survey-methods
5050# ' @param region an object of class Region.
5151# ' @export
52+ # ' @importFrom dplyr left_join
5253setMethod(
5354 f = " run.survey" ,
5455 signature = " Survey.LT" ,
5556 definition = function (object , region = NULL ){
56- population <- object @ population
57- line.transect <- object @ transect
58- # Find possible detection distances
59- poss.distances <- calc.perp.dists(population , line.transect )
60- # Store them in the object
61- if (! is.null(poss.distances $ distance )){
62- object @ dists.in.covered <- poss.distances $ distance
63- }
64- # Simulate detections
65- dist.data <- simulate.detections(poss.distances , object @ population @ detectability )
66- # Check if there are any detections
67- if (nrow(dist.data ) == 0 ){
68- return (object )
69- }
70- # Get the covariate names
71- all.col.names <- names(object @ population @ population )
72- cov.param.names <- all.col.names [! all.col.names %in% c(" object" , " x" , " y" , " Region.Label" , " Sample.Label" , " scale.param" , " shape.param" , " individual" )]
73- dist.data <- dist.data [,c(" object" , " individual" , " Region.Label" , " Sample.Label" , " distance" , " x" , " y" , cov.param.names )]
74- # Add in the transect lengths
75- sample.table <- data.frame (Region.Label = line.transect @ samplers $ strata ,
76- Sample.Label = line.transect @ samplers $ transect ,
77- Effort = sf :: st_length(line.transect @ samplers ))
78- # If the region is supplied then add in the survey region Area
79- if (! is.null(region )){
80- region.table <- data.frame (Region.Label = region @ strata.name ,
81- Area = region @ area )
82- sample.table <- dplyr :: left_join(sample.table , region.table , by = " Region.Label" )
83- }
84- dist.data <- dplyr :: full_join(dist.data , sample.table , by = c(" Sample.Label" , " Region.Label" ))
85- # Order by transect id
86- index <- order(dist.data $ Sample.Label )
87- dist.data <- dist.data [index ,]
88- object @ dist.data <- dist.data
57+ # To allow debugging via breakpoints
58+ object <- run.survey.body.LT(object , region )
8959 return (object )
9060 }
9161)
9262
63+ run.survey.body.LT <- function (object , region ){
64+ population <- object @ population
65+ line.transect <- object @ transect
66+ # Find possible detection distances
67+ poss.distances <- calc.perp.dists(population , line.transect )
68+ # Store them in the object
69+ if (! is.null(poss.distances $ distance )){
70+ object @ dists.in.covered <- poss.distances $ distance
71+ }
72+ # Simulate detections
73+ dist.data <- simulate.detections(poss.distances , object @ population @ detectability )
74+ # Check if there are any detections
75+ if (nrow(dist.data ) == 0 ){
76+ return (object )
77+ }
78+ # Get the covariate names
79+ all.col.names <- names(object @ population @ population )
80+ cov.param.names <- all.col.names [! all.col.names %in% c(" object" , " x" , " y" , " Region.Label" , " Sample.Label" , " scale.param" , " shape.param" , " individual" )]
81+ dist.data <- dist.data [,c(" object" , " individual" , " Region.Label" , " Sample.Label" , " distance" , " x" , " y" , cov.param.names )]
82+ # Add in the transect lengths
83+ sample.table <- data.frame (Region.Label = line.transect @ samplers $ strata ,
84+ Sample.Label = line.transect @ samplers $ transect ,
85+ Effort = sf :: st_length(line.transect @ samplers ))
86+ # If the region is supplied then add in the survey region Area
87+ if (! is.null(region )){
88+ region.table <- data.frame (Region.Label = region @ strata.name ,
89+ Area = region @ area )
90+ sample.table <- dplyr :: left_join(sample.table , region.table , by = " Region.Label" )
91+ }
92+ # Rename Region.Label to obs.Region.Label
93+ index <- which(names(dist.data ) == " Region.Label" )
94+ names(dist.data )[index ] <- " obs.Region.Label"
95+ # Only join by sampler ID
96+ dist.data <- dplyr :: full_join(dist.data , sample.table , by = c(" Sample.Label" ))
97+ # Check if any Region.Labels and obs.Region.Label don't match (detections across stratum boundaries)
98+ index <- which(dist.data $ obs.Region.Label != dist.data $ Region.Label )
99+ if (length(index ) > 0 ){
100+ # Remove any detections across stratum boundaries
101+ dist.data <- dist.data [- index ,]
102+ }
103+ # Order by transect id
104+ index <- order(dist.data $ Sample.Label )
105+ dist.data <- dist.data [index ,]
106+ object @ dist.data <- dist.data
107+ return (object )
108+ }
109+
93110
0 commit comments