How to specify the proportion of the transect covered as a data.frame with 2 columns

48 views
Skip to first unread message

Moriah Tanguay

unread,
Mar 26, 2025, 2:40:08 AM3/26/25
to distance-sampling
I would like to run code similar this: https://distancesampling.org/Distance/articles/web-only/CTDS/camera-distill.html, but where the sample fraction in the call to dht2 is a data.frame with 2 columns -- Sample.Label and fraction (because my fractions are different for each transect). I am trying to use this for Camera Trap Distance Sampling, where I would like to use multiple types of cameras, each with a different angle of view. See below for the exact code I ran. When I tried to run the call to dht2, R Studio crashed. I am running Intel(R) Core(TM) i5-10310U CPU @ 1.70GHz, 2208 Mhz, 4 Core(s), 8 Logical Processor(s), with Installed Physical Memory (RAM) 16.0 GB, Total Physical Memory 15.7 GB, Available Physical Memory 4.42 GB, Total Virtual Memory 18.1 GB, Available Virtual Memory 3.48 GB, and Page File Space 2.38 GB. I am assuming the reason for the crash is that the task was too computationally intensive for my computer, but I would like to hear what others think -- whether there's a way I can adjust the code to get around the limitations of my computer.


```# Code mostly copied from: https://distancesampling.org/Distance/articles/web-only/CTDS/camera-distill.html


# Data Input
trigger.events <- read.table(file="VideoStartTimes_FullDays.txt", header=TRUE)

trigger.events$date <- paste("2014",
                             sprintf("%02i", trigger.events$month),
                             sprintf("%02i", trigger.events$day),
                             sep="/")
trigger.events$time <- paste(sprintf("%02i", trigger.events$hour),
                             sprintf("%02i", trigger.events$minute),
                             sep=":")
trigger.events$datetime <- paste(trigger.events$date, trigger.events$time)



# Functions in the Activity Package
library(activity)
trigger.events$rtime <- gettime(trigger.events$datetime,
                                tryFormats = "%Y/%m/%d %H:%M",
                                scale = "radian")

act_result <- fitact(trigger.events$rtime, sample="data", reps=100)



# Adjustment for Temporal Availability
camera.operation.per.day <- 11.5
prop.camera.time <- camera.operation.per.day / 24
avail <- list(creation=data.frame(rate = act_result@act[1]/prop.camera.time,
                                  SE   = act_result@act[2]/prop.camera.time))


### DETECTION DATA ANALYSIS ###

DuikerCameraTraps <- read.csv(file="DaytimeDistances.txt", header=TRUE, sep="\t")
DuikerCameraTraps$Area <- DuikerCameraTraps$Area / (1000*1000)
DuikerCameraTraps$object <- NA
DuikerCameraTraps$object[!is.na(DuikerCameraTraps$distance)] <- 1:sum(!is.na(DuikerCameraTraps$distance))


# Distance Recording
breakpoints <- c(seq(0, 8, 1), 10, 12, 15, 21)
hist(DuikerCameraTraps$distance, breaks=breakpoints, main="Peak activity data set",
     xlab="Radial distance (m)")


# Detection Function Fits
library(Distance)
trunc.list <- list(left = 2, right = 15)
mybreaks <- c(seq(2, 8, 1), 10, 12, 15)
conversion <- convert_units("meter", NULL, "square kilometer")
uni3 <- ds(DuikerCameraTraps, transect = "point", key = "unif", adjustment = "cos",
           nadj = 3, convert_units = conversion,
           cutpoints = mybreaks, truncation = trunc.list)



# Density Estimates

# Original code from website (this runs fairly quickly)
viewangle <- 42 # degrees
samfrac <- viewangle / 360
peak.uni.dens <- dht2(uni3, flatfile=DuikerCameraTraps, strat_formula = ~1,
                     sample_fraction = samfrac, er_est = "P2", multipliers = avail,
                     convert_units = conversion)
print(peak.uni.dens, report="density")

# My adjusted code (this causes R studio to crash)
# Based on pg. 18 of https://cran.r-project.org/web/packages/Distance/Distance.pdf
# where it says this regarding the sample fraction term of the model: proportion
# of the transect covered (e.g., 0.5 for one-sided line transects). May be specified
# as either a single number or a data.frame with 2 columns Sample.Label and fraction
# (if fractions are different for each transect)


# Created that 2-column data.frame to test this out
samfrac_variable <- data.frame(
  Sample.Label = DuikerCameraTraps$Sample.Label,
  fraction = DuikerCameraTraps$multiplier)
samfrac_variable <- edit(samfrac_variable) # In this step I edited ~ 30 of the
# values to be slightly smaller than the others, to mimic what I might be working
# with in my study

peak.uni.dens <- dht2(uni3, flatfile=DuikerCameraTraps, strat_formula = ~1,
                      sample_fraction = samfrac_variable, er_est = "P2", multipliers = avail,
                      convert_units = conversion)
print(peak.uni.dens, report="density")```

Eric Rexstad

unread,
Mar 26, 2025, 9:14:27 AM3/26/25
to Moriah Tanguay, distance-sampling
Moriah

Welcome to the list; I see you also asked this question on StackOverflow. You'll have better luck getting questions to distance sampling related questions on the Distance email list in the future.

The solution to your problem is not related to the power of your computer nor the code.  You simply need to recognise that sampling fraction is a property of the camera station, not of the detection. The two column data frame you need to construct should have as many rows as cameras (in the case of the duikers 21 stations), not the 6277 observations in the DuikerCameraTraps data frame.

Making this change to your definition of samfrac_variable solved the problem for me:

# Created that 2-column data.frame to test this out
samfrac_variable <- data.frame(
  Sample.Label = unique(DuikerCameraTraps$Sample.Label),
  fraction = runif(length(unique(DuikerCameraTraps$Sample.Label)), 
                   min=DuikerCameraTraps$multiplier, max=0.3))

Note the fraction I assigned in the data frame is fictitious, a random variable. The call the dht2 after this runs very quickly (couple of seconds).

One other thing to check, you will want to be running the most current version of the Distance package from Github to prevent an error appearing when you try to print the resulting density estimate.  To get this latest version run these three lines:

library(remotes)
install_github("DistanceDevelopment/Distance")
library(Distance)

Let the list know if this solves your problem.

From: distance...@googlegroups.com <distance...@googlegroups.com> on behalf of Moriah Tanguay <moriah...@gmail.com>
Sent: 25 March 2025 23:25
To: distance-sampling <distance...@googlegroups.com>
Subject: [distance-sampling] How to specify the proportion of the transect covered as a data.frame with 2 columns
 
--
You received this message because you are subscribed to the Google Groups "distance-sampling" group.
To unsubscribe from this group and stop receiving emails from it, send an email to distance-sampl...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/distance-sampling/d090199f-0ace-47cd-86ea-f40786beaeb1n%40googlegroups.com.

Moriah Tanguay

unread,
Mar 26, 2025, 7:18:50 PM3/26/25
to distance-sampling
Hi Eric,

Thanks very much for the quick reply! Your solution solved the problem. Good to know this is the preferred spot to ask Distance related questions.
Reply all
Reply to author
Forward
0 new messages