Search code examples
rsocial-networkingstrptime

DSI calculation using socialindices R error


I'm trying to calculate DSI scores between male-females dyads using the socialindices by Christof Neumann package. I have three dataframes: one containing the behavioural observations, one the observation time and another the presence file. Here are their structures:

> str(observations)
tibble [476 × 6] (S3: tbl_df/tbl/data.frame)
 $ date    : Date[1:476], format: "2022-10-20" "2022-10-14" "2022-10-14" "2022-09-20" ...
 $ focal   : chr [1:476] "Sho" "Sho" "Sho" "Buk" ...
 $ actor   : chr [1:476] "Ginq" "Sho" "Ndaw" "Ginq" ...
 $ receiver: chr [1:476] "Sho" "Ndaw" "Sho" "Buk" ...
 $ beh     : chr [1:476] "Approach" "Approach" "Groom.focal" "Contact.focal" ...
 $ dur     : int [1:476] NA NA 128 661 223 899 434 NA 19 NA ...

> str(ot)
'data.frame':   209 obs. of  3 variables:
 $ focal: chr  "Buk" "Buk" "Buk" "Buk" ...
 $ date : chr  "2022-06-07" "2022-06-11" "2022-06-15" "2022-06-17" ...
 $ OT   : int  1204 1212 1212 1200 1200 1202 1205 635 1204 963 ...

> str(pres)
'data.frame':   374 obs. of  5 variables:
 $ date: chr  "2022-01-03" "2022-01-04" "2022-01-05" "2022-01-06" ...
 $ Buk : int  1 1 1 0 1 1 1 1 1 1 ...
 $ Ginq: int  1 1 1 1 1 1 1 1 1 1 ...
 $ Ndaw: int  1 1 1 1 1 1 1 1 1 1 ...
 $ Sho : int  1 1 1 1 1 1 1 1 1 1 ...

Since I am interested in seeing how male-female relationships evolve over time, I have chosen to have my males as my focal individuals. That means that in this particular group, I have two focal individuals (Buk and Sho), and three potential partners for each.

This is the code that I'd like to run:

scores <- DSI(observations, ot.source = ot, presence = pres, onlyfocaldyads = F, limit2focalnonfocal = T, duration.NA.treatm = "count")

But I keep getting this error:

Error in strptime(xx, ff, tz = "GMT") : input string is too long

All my date columns are dates and their range is the same. The same calculations worked before when I used all observations, but that doesn't really serve my purpose. If anyone has any idea why this is not working, I'd be very grateful!


Solution

  • DSI() only accepts data.frame() objects, and your observations object is a tibble(). Below is a reprex that generates your error, followed by a working solution:

    # install.packages("remotes")
    remotes::install_github("gobbios/socialindices")
    library(socialindices2)
    
    # Load sample data
    data(dataset3)
    
    # Create representative dataframes
    observations <- tibble(dataset3[[1]])
    
    ot <- data.frame(dataset3[[2]])
    
    pres <- data.frame(dataset3[[3]])
    
    # Caculate dyadic CSI
    scores <- DSI(observations,
                  ot.source = ot,
                  presence = pres,
                  onlyfocaldyads = F,
                  limit2focalnonfocal = T,
                  duration.NA.treatm = "count")
    # Error in strptime(xx, ff, tz = "GMT") : input string is too long
    
    # Convert observations to df
    observations <- data.frame(observations)
    
    # Caculate dyadic CSI again
    scores <- DSI(observations,
                  ot.source = ot,
                  presence = pres,
                  onlyfocaldyads = F,
                  limit2focalnonfocal = T,
                  duration.NA.treatm = "count")
    
    
    scores
    #   i1 i2 type  dyad  dot cores appr  gro prox supp   appr.rt    gro.rt   prox.rt   supp.rt     DSI     zDSI
    # 1  a  d  FNF a_@_d 3877   119    1 2190 1805    1 0.6675996 5.2571514 4.3288838 0.7880743 2.76043  1.17610
    # 2  a  n  FNF a_@_n 3208    98    0  325  192    0 0.0000000 0.9428686 0.5564952 0.0000000 0.37484 -0.56118
    # 3  a  v  FNF a_@_v 3877   119    0  117   67    2 0.0000000 0.2808615 0.1606843 1.5761486 0.50442 -0.38591
    # 4  d  f  FNF d_@_f 6658   200    2   87  195    3 0.7774958 0.1216123 0.2723236 1.3767035 0.63703 -0.24598
    # 5  f  n  FNF f_@_n 6026   179    2  580  421    2 0.8590387 0.8957793 0.6496022 1.0140604 0.85462 -0.11512
    # 6  f  v  FNF f_@_v 6658   200    7  342  569    0 2.7212355 0.4780623 0.7946264 0.0000000 0.99848  0.07002
    # 7  d  j  FNF d_@_j 4162   119    0   92  459    1 0.0000000 0.2057254 1.0254279 0.7341096 0.49132 -0.41839
    # 8  j  n  FNF j_@_n 3481    98    2   96  345    4 1.4870920 0.2566666 0.9215303 3.5109037 1.54405  0.55217
    # 9  j  v  FNF j_@_v 4162   119    4  251  130    0 2.4875384 0.5612726 0.2904262 0.0000000 0.83481 -0.07171