Help with dht() Function – “invalid argument type” Error

77 views
Skip to first unread message

Sabi

unread,
Feb 1, 2025, 2:33:50 PM2/1/25
to distance-sampling

Dear Distance Sampling Group,

I am encountering an error when running the dht() function in R using the Distance package. Here is my code:

dht_results <- dht(
  model = ds.site,
  sample.table = sample_table,
  obs.table = obs_table,
  region.table = region_table
)

However, I get the following error message:

Error in !model$meta.data$point : invalid argument type.

summary(ds.site) runs well. It shows:


str(ds.site) shows the structure as

..$ meta.data :List of 7 .. ..$ width : num 1000 .. ..$ point : logi FALSE

But, print(ds.site$meta.data) returns NULL


I tried reworking from start. Distance sampling analysis works well if I don't account effort. But I need to include effort. I am unsure how to resolve it. 

My model (ds.site) was created using the ds() function. R version is R-4.4.1. I wanted to perform density-based effort calculation.


Any guidance on troubleshooting this error, re;iab;e website to study about working on this would be greatly appreciated!


Thank you in advance for your help.

Sincerely

Sabi

Eric Rexstad

unread,
Feb 2, 2025, 2:49:42 AM2/2/25
to Sabi, distance-sampling
Sabi

I'm not sure why you are calling the dht function on a model object created by the ds function. That is normally not necessary.

Using the ds function implicitly calls the dht function, without your intervention.

If you look at summary(ds.site) you will find estimates of abundance and density that were produced by the dht function; hence no need to call dht again.

If you feel you need to call dht yourself, I would set the argument model to ds.site$ddf.

Let me know if I misunderstand what you are trying to do.

From: distance...@googlegroups.com <distance...@googlegroups.com> on behalf of Sabi <sabi...@gmail.com>
Sent: 01 February 2025 19:33
To: distance-sampling <distance...@googlegroups.com>
Subject: {Suspected Spam} [distance-sampling] Help with dht() Function – “invalid argument type” Error
 
--
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/8001bf5b-7929-4076-a216-1cce6022f085n%40googlegroups.com.

Sabita Gurung

unread,
Feb 2, 2025, 8:58:38 AM2/2/25
to Eric Rexstad, distance-sampling

Dear Eric, 

Thank you so much.

I wanted to include the transect length information in the distance sampling analysis as effort-based density estimation. For that I could not find way to include it within the function for distanceSampling. There were errors when included as effort = effort. So, I followed the way which used sample table, observation table and region table (as below).   summary(ds.site) does not show density by below process. I wonder I should be missing or incorrect somewhere.
Following other resources, I am trying all the ways; things work if "dht" is not included in the process.
Alternately, I worked step by step (long process) by fitting detection function with adjustments, then extracting N in covered region from the model, calculating density using area of study site, calculating total effort (total transect length) and then calculating density based on the effort. This results
Density (per km² per km effort), identify model with lowest AIC. Is this correct process?

 

However, I wanted to work on below R work flow which I reviewed in R documents for distance sampling. While working for effort-based density estimation (in distance sampling), I set function as below: I wonder if issue with transect id (Sample.Label) might also cause error in this process, or my process of working with dht function is wrong here. I have tried to include as much as possible below thinking if that would help more to figure out, and suggest anything I could do.
Thank you in advance for your time in guiding me. It means a lot to me to learn this analysis in R.
 

distanceSampling <- function(survey) {

  KEY <- c("hn", "unif", "hr") 

  ADJ <- c("cos", "herm", "poly")

  AICs <- data.frame(ID = 1:9,

                     Key = NA,

                     Adjustment = NA,

                     AIC = NA)

  N <- 0

  for (i in 1:length(KEY)) {

    for (j in 1:length(ADJ)) {

      N <- N + 1

      AICs$Key[N] <- KEY[i]

      AICs$Adjustment[N] <- ADJ[j]

      ds.fit <- ds(survey,

                   key = KEY[i],

                   adjustment = ADJ[j],

                   convert_units = 0.001)

      AICs$AIC[N] <- as.numeric(AIC(ds.fit)[2])

     

      print(paste('Finished ', round(N / 9 * 100, 2), "%", sep = ''))

    }

  }

  AICs <- AICs[order(AICs$AIC), ]

  return(AICs)

}

 

AICs <- distanceSampling(distance_sampling_data)

 

My distance_sampling_data has study_site_name, year, transect_id, species, perpendicular_distance( in meter), number_of_animal observed, and transect_length (in km).

 

# Calculated total effort as below

total_effort <- distance_sampling_data %>%

  group_by(transect_id, study_site, year) %>%

  summarise(Effort = sum(transect_length, na.rm = TRUE)) %>%

  ungroup()

 

# Created sample table

sample_table <- data.frame(

  Region.Label = total_effort$study_site,

  Sample.Label = total_effort$transect_id,

  Effort = total_effort$Effort

)

 

# Created observation table

obs_table <- data.frame(

  Sample.Label = distance_sampling_data$transect_id,

  Region.Label = distance_sampling_data$study_site,

  distance = distance_sampling_data$perpendicular_distance

)

 

# Created region table

region_table <- data.frame(

  Region.Label = unique(distance_sampling_data$study_site),

  Area = 336 

)

 

# Fitted the best model

ds.site <- ds(

  distance_sampling_data,

  key = AICs$Key[1],

  adjustment = AICs$Adjustment[1],

  truncation = 1000

)

summary(ds.site)

# Summary for distance analysis

# Number of observations :  182

# Distance range         :  0  -  1000

#

# Model       : Hazard-rate key function

# AIC         :  2447.217

# Optimisation:  mrds (nlminb)

#

# Detection function parameters

# Scale coefficient(s): 

#   estimate       se

# (Intercept) 6.010397 0.142116

#

# Shape coefficient(s): 

#   estimate        se

# (Intercept) 0.8206267 0.2156811

#

# Estimate          SE         CV

# Average p             0.5477412  0.04640994 0.08472968

# N in covered region 332.2737007 32.66447626 0.09830593

 

# Perform effort based density estimation

dht_results <- dht(

  model = ds.site,

  sample.table = sample_table,

  obs.table = obs_table,

  region.table = region_table

)

Error in !model$meta.data$point : invalid argument type

 

 

Sincerely
Sabi

Sabita Gurung

unread,
Feb 2, 2025, 9:13:18 AM2/2/25
to Eric Rexstad, distance-sampling
I realised, while extrating N or SE or CV from detection function models, it does not work using below code, may be due to this path with dht is not there in model summary structure and this might be the cause of the issue?
abundance.hn.cos <- summary(ds.fit.hn.cos)$dht$individuals$N$Estimate


Eric Rexstad

unread,
Feb 2, 2025, 10:09:02 AM2/2/25
to Sabita Gurung, distance-sampling
Sabita

Before we get involved in complex workflow and functions to loop over a candidate model set, let us start with the basics.

The fields you describe in your distance_sampling_data data frame does not contain a field Area (but I see you have added it in your region_table), which is the size of the study area over which you are making inference. You should not need to build a sample, region and observation tables if your analysis is straightforward.

I suggest we correspond off list to understand the organisation of your data and the questions you are trying to answer. Perhaps sending me the distance_sampling_data data frame as a starting point. When we determine how to proceed, you can report this back to the list for others who may be addressing similar questions.

From: Sabita Gurung <sabi...@gmail.com>
Sent: 02 February 2025 13:57
To: Eric Rexstad <Eric.R...@st-andrews.ac.uk>
Cc: distance-sampling <distance...@googlegroups.com>
Subject: Re: {Suspected Spam} [distance-sampling] Help with dht() Function – “invalid argument type” Error
 

Sabi

unread,
Feb 5, 2025, 2:58:06 AM2/5/25
to distance-sampling
Thank you so much dear Eric for prompt response with the solution.

My issue was caused by column names not matching exactly in the format needed while using Distance package. (please refer ?flatfile in R). Once the field names were corrected, the analysis accounted the transect length difference automatically, and using dht was unnecessary. 
Following the required data format if very important. That was so helpful.
Reply all
Reply to author
Forward
0 new messages