How to fix error message "Error in rep$pdHess : $ operator is invalid for atomic vectors" when using aniMotum package

79 Views Asked by At

I am having trouble with fit an ssm using a move persistence model. The current issue is: Error in rep$pdHess : $ operator is invalid for atomic vectors

As all my columns are atomic vectors, I am not to sure how to fix this issue.

Also, bonus question as this model only allows me to run through one animal at a time do you know an easy way for me to run through them all such as in a loop - I have over 60 animals.

Below is a snipet of data I am working with for one of my animals: My code is:

fit <- fit_ssm(Shark3,
               vmax = 3,
               model = "mp",
               time.step = 24,
               control = ssm_control(verbose = 0))

id  date                    lc     lat          lon
3   03/16/2016 00:36:39     A   -31.50391   106.90178
3   03/16/2016 00:36:39     A   -31.50391   106.90178
3   03/16/2016 00:36:39     A   -31.50391   106.90178
3   03/16/2016 08:48:20     B   -39.82062   146.76394
3   03/16/2016 15:58:14     B   -39.93783   146.81515
3   03/17/2016 06:44:00     B   -40.0844    146.89076
3   03/19/2016 18:53:24     B   -41.0345    148.13549
3   08/11/2016 03:17:00 1   -25.46735   153.41662
3   08/11/2016 03:17:00 1   -25.46735   153.41662
3   08/11/2016 03:17:00 1   -25.46735   153.41662
3   04/18/2016 22:50:43     B   -40.23962   147.57244
3   04/19/2016 00:37:50     B   -40.20943   147.56543
3   04/19/2016 00:37:50     B   -40.20943   147.56543
3   04/24/2016 03:46:23     3   -40.43101   147.94571
3   04/24/2016 03:46:23     3   -40.43101   147.94571
3   04/24/2016 03:46:23     3   -40.43101   147.94571
3   04/24/2016 03:46:23     3   -40.43101   147.94571
3   04/10/2016 06:26:00 2   -40.57273   148.41413
3   04/10/2016 06:26:00 2   -40.57273   148.41413
3   04/10/2016 06:26:00 2   -40.57273   148.41413
3   04/10/2016 06:26:00 2   -40.57273   148.41413
3   04/10/2016 06:26:00 2   -40.57273   148.41413
3   04/10/2016 06:26:00 2   -40.57273   148.41413
3   04/10/2016 06:26:00 2   -40.57273   148.41413
3   04/10/2016 08:52:00 B   -40.58898   148.4267
3   04/11/2016 23:41:00 A   -40.60004   148.32712
3   04/11/2016 23:41:00 A   -40.60004   148.32712
3   04/11/2016 23:41:00 A   -40.60004   148.32712
3   04/12/2016 13:33:00 A   -40.53807   148.3033
3   04/12/2016 13:33:00 A   -40.53807   148.3033
3   04/12/2016 13:33:00 A   -40.53807   148.3033
3   04/14/2016 22:11:31     B   -40.5596    148.37321
3   04/14/2016 22:11:31     B   -40.5596    148.37321
3   04/16/2016 21:13:08     0   -40.30304   147.74462
3   04/16/2016 21:13:08     0   -40.30304   147.74462
3   04/16/2016 21:13:08     0   -40.30304   147.74462
3   04/16/2016 21:13:08     0   -40.30304   147.74462
3   04/17/2016 05:13:25     B   -40.30302   147.72642
3   04/18/2016 03:13:06     B   -40.35905   147.66374
3   04/18/2016 21:50:10     3   -40.2418    147.57622
3   04/18/2016 21:50:10     3   -40.2418    147.57622
3   04/18/2016 21:50:10     3   -40.2418    147.57622
3   04/18/2016 21:50:10     3   -40.2418    147.57622
3   04/18/2016 21:50:10     3   -40.2418    147.57622
3   04/18/2016 21:50:10     3   -40.2418    147.57622
3   04/18/2016 21:50:10     3   -40.2418    147.57622
3   04/18/2016 21:50:10     3   -40.2418    147.57622
3   04/18/2016 21:50:10     3   -40.2418    147.57622
3   04/18/2016 21:50:10     3   -40.2418    147.57622
3   04/24/2016 03:46:23     3   -40.43101   147.94571
3   04/24/2016 03:46:23     3   -40.43101   147.94571
3   04/24/2016 03:46:23     3   -40.43101   147.94571
3   04/24/2016 03:46:23     3   -40.43101   147.94571
3   04/24/2016 03:46:23     3   -40.43101   147.94571
3   04/24/2016 03:46:23     3   -40.43101   147.94571
3   04/24/2016 03:46:23     3   -40.43101   147.94571
3   04/24/2016 03:46:23     3   -40.43101   147.94571
3   04/24/2016 03:46:23     3   -40.43101   147.94571
3   05/11/2016 15:19:00 B   -40.58412   148.49763
3   05/11/2016 07:34:00 1   -40.5991    148.50535
3   05/11/2016 07:34:00 1   -40.5991    148.50535
3   05/11/2016 07:34:00 1   -40.5991    148.50535
3   05/11/2016 07:34:00 1   -40.5991    148.50535
1

There are 1 best solutions below

0
L Tyrone On

The issue lies with the TMB package, which is a dependency of aniMotum. For aniMotum to work, TMB needs the Matrix package, you need to have Matrix_1.6-5 installed, with R 4.3.2. So in this exact order first run:

remove.packages("aniMotum")
remove.packages("TMB")

then quit R/RStudio. Also, update R to the latest version if necessary. Second, start R/RStudio and run:

install.packages("Matrix")

install.packages("aniMotum", 
                 repos = c("https://cloud.r-project.org",
                           "https://ianjonsen.r-universe.dev"),
                 dependencies = TRUE)

and now try again:

library(aniMotum)
library(dplyr)

# Your sample data
df <- read.table(text = "id;date;lc;lat;lon
3;03/16/2016 00:36:39;A;-31.50391;106.90178
3;03/16/2016 00:36:39;A;-31.50391;106.90178
3;03/16/2016 00:36:39;A;-31.50391;106.90178
3;03/16/2016 08:48:20;B;-39.82062;146.76394
3;03/16/2016 15:58:14;B;-39.93783;146.81515
3;03/17/2016 06:44:00;B;-40.0844;146.89076
3;03/19/2016 18:53:24;B;-41.0345;148.13549
3;08/11/2016 03:17:00;1;-25.46735;153.41662
3;08/11/2016 03:17:00;1;-25.46735;153.41662
3;08/11/2016 03:17:00;1;-25.46735;153.41662
3;04/18/2016 22:50:43;B;-40.23962;147.57244
3;04/19/2016 00:37:50;B;-40.20943;147.56543
3;04/19/2016 00:37:50;B;-40.20943;147.56543
3;04/24/2016 03:46:23;3;-40.43101;147.94571
3;04/24/2016 03:46:23;3;-40.43101;147.94571
3;04/24/2016 03:46:23;3;-40.43101;147.94571
3;04/24/2016 03:46:23;3;-40.43101;147.94571
3;04/10/2016 06:26:00;2;-40.57273;148.41413
3;04/10/2016 06:26:00;2;-40.57273;148.41413
3;04/10/2016 06:26:00;2;-40.57273;148.41413
3;04/10/2016 06:26:00;2;-40.57273;148.41413
3;04/10/2016 06:26:00;2;-40.57273;148.41413
3;04/10/2016 06:26:00;2;-40.57273;148.41413
3;04/10/2016 06:26:00;2;-40.57273;148.41413
3;04/10/2016 08:52:00;B;-40.58898;148.4267
3;04/11/2016 23:41:00;A;-40.60004;148.32712
3;04/11/2016 23:41:00;A;-40.60004;148.32712
3;04/11/2016 23:41:00;A;-40.60004;148.32712
3;04/12/2016 13:33:00;A;-40.53807;148.3033
3;04/12/2016 13:33:00;A;-40.53807;148.3033
3;04/12/2016 13:33:00;A;-40.53807;148.3033
3;04/14/2016 22:11:31;B;-40.5596;148.37321
3;04/14/2016 22:11:31;B;-40.5596;148.37321
3;04/16/2016 21:13:08;0;-40.30304;147.74462
3;04/16/2016 21:13:08;0;-40.30304;147.74462
3;04/16/2016 21:13:08;0;-40.30304;147.74462
3;04/16/2016 21:13:08;0;-40.30304;147.74462
3;04/17/2016 05:13:25;B;-40.30302;147.72642
3;04/18/2016 03:13:06;B;-40.35905;147.66374
3;04/18/2016 21:50:10;3;-40.2418;147.57622
3;04/18/2016 21:50:10;3;-40.2418;147.57622
3;04/18/2016 21:50:10;3;-40.2418;147.57622
3;04/18/2016 21:50:10;3;-40.2418;147.57622
3;04/18/2016 21:50:10;3;-40.2418;147.57622
3;04/18/2016 21:50:10;3;-40.2418;147.57622
3;04/18/2016 21:50:10;3;-40.2418;147.57622
3;04/18/2016 21:50:10;3;-40.2418;147.57622
3;04/18/2016 21:50:10;3;-40.2418;147.57622
3;04/18/2016 21:50:10;3;-40.2418;147.57622
3;04/24/2016 03:46:23;3;-40.43101;147.94571
3;04/24/2016 03:46:23;3;-40.43101;147.94571
3;04/24/2016 03:46:23;3;-40.43101;147.94571
3;04/24/2016 03:46:23;3;-40.43101;147.94571
3;04/24/2016 03:46:23;3;-40.43101;147.94571
3;04/24/2016 03:46:23;3;-40.43101;147.94571
3;04/24/2016 03:46:23;3;-40.43101;147.94571
3;04/24/2016 03:46:23;3;-40.43101;147.94571
3;04/24/2016 03:46:23;3;-40.43101;147.94571
3;05/11/2016 15:19:00;B;-40.58412;148.49763
3;05/11/2016 07:34:00;1;-40.5991;148.50535
3;05/11/2016 07:34:00;1;-40.5991;148.50535
3;05/11/2016 07:34:00;1;-40.5991;148.50535
3;05/11/2016 07:34:00;1;-40.5991;148.50535", sep = ";", header = TRUE)

df$date <- as.POSIXct(df$date, format = "%m/%d/%Y %H:%M:%S", tz = "UTC")

colnames(df) <- c("id", "date", "lc", "lat", "lon")

df <- format_data(df, date = "date", coord = c("lon", "lat"))

fit <- fit_ssm(df,
               vmax = 3,
               model = "mp",
               time.step = 24,
               control = ssm_control(verbose = 0))

It will throw this warning, maybe due to your sample data being a subset, or your data are not well suited for ssm and/or mp, but cannot confirm:

Warning messages:

1: In sqrt(as.numeric(object$diag.cov.random)) : NaNs produced

2: Hessian was not positive-definite so some standard errors could not be calculated.

Either way, here are the results:

summary(fit)
Animal id Model Time n.obs n.filt n.fit n.pred n.rr converged  AICc
        3    mp   24    63     45    18    150    .      TRUE 522.3
print individual model parameter tables? [enter / esc to stop]

--------------
3 
--------------
Parameter Estimate Std.Err
rho_p      -0.9994   5e-04
sigma_x    82.9722 24.0876
sigma_y    21.1173  6.0646
rho_o       -0.499  3.4063
tau_x       0.1055  0.3418
tau_y       0.0573  0.1397
sigma_g     0.1185  0.0827

grab(fit, "f")
# A tibble: 18 × 11
   id    date                  lon   lat      x      y    x.se     y.se logit_g
   <chr> <dttm>              <dbl> <dbl>  <dbl>  <dbl>   <dbl>    <dbl>   <dbl>
 1 3     2016-03-16 00:36:39  107. -31.5 11900. -3676.   2.50    1.26     2.22 
 2 3     2016-03-16 08:48:20  147. -39.8 16337. -4813. NaN       1.56     2.22 
 3 3     2016-03-17 06:44:00  147. -40.1 16352. -4850.   3.09    1.73    -0.247
 4 3     2016-03-19 18:53:24  148. -41.0 16490. -4989.   4.61    1.86     1.75 
 5 3     2016-04-10 06:26:00  148. -40.6 16521. -4922.   0.162   0.0739   1.58 
 6 3     2016-04-10 08:52:00  148. -40.6 16523. -4923. NaN     NaN        1.58 
 7 3     2016-04-11 23:41:00  148. -40.6 16512. -4926.   2.49    1.25     1.40 
 8 3     2016-04-12 13:33:00  148. -40.5 16509. -4917.   2.49    1.25     1.43 
 9 3     2016-04-16 21:13:08  148. -40.3 16447. -4882.   1.43    0.852    1.27 
10 3     2016-04-17 05:13:25  148. -40.3 16445. -4883.   1.80    1.49     1.27 
11 3     2016-04-18 03:13:06  148. -40.4 16438. -4890.   2.57    1.65     1.29 
12 3     2016-04-18 21:50:10  148. -40.2 16428. -4874.   0.105   0.0573   1.59 
13 3     2016-04-18 22:50:43  148. -40.2 16428. -4872. NaN     NaN        1.59 
14 3     2016-04-19 00:37:50  148. -40.2 16426. -4870. NaN     NaN        1.59 
15 3     2016-04-24 03:46:23  148. -40.4 16469. -4901.   0.105   0.0573   1.56 
16 3     2016-05-11 07:34:00  149. -40.6 16532. -4926.   0.392   0.146    3.42 
17 3     2016-05-11 15:19:00  148. -40.6 16531. -4923.   4.66    1.85     3.43 
18 3     2016-08-11 03:17:00  153. -25.5 17078. -2915.   0.392   0.146    6.09 
# ℹ 2 more variables: logit_g.se <dbl>, g <dbl>

And for the bonus question: obviously this is a sub-optimal example given the size of the data, but it is ok for illustrative purposes:

# Modify df to demonstrate loop working
df[1:20,1] <- 2

for(i in unique(df$id)) {
  
  temp <- df[which(df$id == i),]
  
  fit <- fit_ssm(temp,
                 vmax = 3,
                 model = "mp",
                 time.step = 24,
                 control = ssm_control(verbose = 0))
  
  assign(paste0("animal_id_", i), fit)
  
}

grab(animal_id_2, "f")
# A tibble: 8 × 11
  id    date                  lon   lat      x      y  x.se   y.se logit_g logit_g.se
  <chr> <dttm>              <dbl> <dbl>  <dbl>  <dbl> <dbl>  <dbl>   <dbl>      <dbl>
1 2     2016-03-16 00:36:39  107. -31.5 11900. -3676. 1.57  0.286     1.09       1.26
2 2     2016-03-16 08:48:20  147. -39.8 16338. -4813. 3.10  0.425     1.09       1.26
3 2     2016-03-17 06:44:00  147. -40.1 16352. -4851. 2.90  0.421     1.09       1.26
4 2     2016-03-19 18:53:24  148. -41.0 16490. -4989. 2.89  0.421     1.09       1.26
5 2     2016-04-10 06:26:00  148. -40.6 16521. -4922. 0.100 0.0167    1.10       1.30
6 2     2016-04-10 08:52:00  148. -40.6 16523. -4924. 4.08  0.520     1.10       1.30
7 2     2016-04-11 23:41:00  148. -40.6 16512. -4926. 1.56  0.285     1.10       1.30
8 2     2016-04-12 13:33:00  148. -40.5 16509. -4917. 1.56  0.285     1.10       1.30
# ℹ 1 more variable: g <dbl>

grab(animal_id_3, "f")
# A tibble: 12 × 11
   id    date                  lon   lat      x      y  x.se  y.se logit_g logit_g.se
   <chr> <dttm>              <dbl> <dbl>  <dbl>  <dbl> <dbl> <dbl>   <dbl>      <dbl>
 1 3     2016-04-12 13:33:00  148. -40.5 16509. -4916.    NA    NA    33.3         NA
 2 3     2016-04-14 22:11:31  148. -40.6 16517. -4923.    NA    NA    33.3         NA
 3 3     2016-04-16 21:13:08  148. -40.3 16447. -4883.    NA    NA    33.3         NA
 4 3     2016-04-17 05:13:25  148. -40.3 16445. -4883.    NA    NA    33.3         NA
 5 3     2016-04-18 03:13:06  148. -40.3 16437. -4880.    NA    NA    33.3         NA
 6 3     2016-04-18 21:50:10  148. -40.2 16428. -4874.    NA    NA    33.3         NA
 7 3     2016-04-18 22:50:43  148. -40.2 16428. -4873.    NA    NA    33.3         NA
 8 3     2016-04-19 00:37:50  148. -40.2 16427. -4873.    NA    NA    33.3         NA
 9 3     2016-04-24 03:46:23  148. -40.4 16469. -4901.    NA    NA    33.3         NA
10 3     2016-05-11 07:34:00  149. -40.6 16532. -4926.    NA    NA    33.3         NA
11 3     2016-05-11 15:19:00  148. -40.6 16531. -4923.    NA    NA    33.3         NA
12 3     2016-08-11 03:17:00  153. -25.5 17078. -2915.    NA    NA    33.3         NA
# ℹ 1 more variable: g <dbl>