ctmm-initiative / ctmm

Continuous-Time Movement Modeling. Functions for identifying, fitting, and applying continuous-space, continuous-time stochastic movement models to animal tracking data.
http://biology.umd.edu/movement.html
47 stars 12 forks source link

Custom str and console printing methods #34

Open chfleming opened 5 years ago

chfleming commented 5 years ago

Some users get confused when inspecting ctmm objects on the command lines or when using str to inspect the object structure, because the $ slot names are all missing. (E.g., https://groups.google.com/forum/#!topic/ctmm-user/Wy8EH-8mwIg )

I put a crude str.ctmm() hack in str.R, but apparently console printing does not call R methods like print() or cat() and is hard coded at a lower level ( https://stackoverflow.com/questions/34638307/changing-how-output-is-printed-to-the-console )

The cause, I assume, is because a ctmm object FIT is just a list object FIT@.Data with some extra slots FIT@info, yet the list names are stored in the ctmm object and no names are stored in FIT@.Data, and many base R functions like str() work with FIT@.Data.

So far, I've tried the assignment names(FIT@.Data) <- names(FIT), but this does nothing.

dracodoc commented 4 years ago

I explored this for a while:

2019-10-12_164331

> ls.str(model_try_res[[1]][[1]])
AIC :  num 3792
AICc :  num 3793
axes :  chr [1:2] "x" "y"
BIC :  num 3808
circle :  num 0
COV :  num [1:4, 1:4] 7.12e+13 -2.87e+05 -5.58e+03 6.44e+11 -2.87e+05 ...
COV.mu :  num [1:2, 1:2] 2413364 186400 186400 822488
error :  logi FALSE
features :  chr [1:4] "major" "minor" "angle" "tau position"
isotropic :  logi FALSE
loglike :  num -1890
mean :  chr "stationary"
method :  chr "pHREML"
MSPE :  Named num [1:2] 52912139 Inf
mu :  num [1, 1:2] 41189.9 29.7
omega :  num 0
range :  logi TRUE
sigma : Formal class 'covm' [package "ctmm"] with 3 slots
tau :  Named num 438449
str.ctmm <- function(x, ...) {
  ls.str(x)
}

> str(model_try_res[[1]][[1]])
AIC :  num 3792
AICc :  num 3793
axes :  chr [1:2] "x" "y"
BIC :  num 3808
circle :  num 0
COV :  num [1:4, 1:4] 7.12e+13 -2.87e+05 -5.58e+03 6.44e+11 -2.87e+05 ...
COV.mu :  num [1:2, 1:2] 2413364 186400 186400 822488
error :  logi FALSE
features :  chr [1:4] "major" "minor" "angle" "tau position"
isotropic :  logi FALSE
loglike :  num -1890
mean :  chr "stationary"
method :  chr "pHREML"
MSPE :  Named num [1:2] 52912139 Inf
mu :  num [1, 1:2] 41189.9 29.7
omega :  num 0
range :  logi TRUE
sigma : Formal class 'covm' [package "ctmm"] with 3 slots
tau :  Named num 438449

This is not perfect but I think will serve the purpose. Given that RStudio can display it already, this at least will avoid the previous situation.

jfsmenezes commented 1 year ago

Hi Chris, I was passing by and saw this old issue.

I have also struggled with ctmm's long printing so I made my own console printing. My trick was to set a method for the show function. My solution still prints one block for each ctmm object in the list, but it makes it more manageable.


setMethod("show",signature(object="telemetry"),
    function(object) {
        cat("\nTelemetry object with",nrow(object),"locations and",ncol(object),"columns","\n")
        cat("Animal ID:",object@info$identity,"\n")
        cat("Timezone:",object@info$timezone,"\n")
        cat("CRS (PROJ4):",object@info$projection,"\n\n")
        cat("First ten locations:","\n")
        print(head(object,10))
    }
)

setMethod("show",signature(object="ctmm"),
    function(object) {
        if(length(object@info)==0) {
            cat("\nHypotethical Continuous-time Model","\n\n")
            cat("Periodicity (if exists):",object$circle,"\n")  # remove
            cat("Considers GPS error:",object$error,"\n")
            cat("Distribution center:",object$mu,"\n")  # remove
            cat("Distribution variances:",object$sigma[1],object$sigma[4],"\n") # remove
            cat("X-Y correlation:",object$sigma[2],"\n") # remove
            cat("Autocorrelation parameters:","tau(s) =", object$tau, "; omega =", object$omega, "\n")
        }

# add verbose=T to include the rest of the information         

        if(length(object@info)>0 & (is.null(object$AIC) & is.null(object$MSPE))) {
            cat("\nGuessed Continuous-time Model","\n")

            cat("Animal ID:",object@info$identity,"\n")
            cat("Timezone:",object@info$timezone,"\n")
            cat("CRS (PROJ4):",object@info$projection,"\n\n")

            cat("Periodicity (if exists):",object$circle,"\n")
            cat("Considers GPS error:",object$error,"\n")
            cat("Distribution center:",object$mu,"\n")
            cat("Distribution variances:",object$sigma[1],object$sigma[4],"\n")
            cat("X-Y correlation:",object$sigma[2],"\n")
            cat("Autocorrelation parameters:","tau(s) =", object$tau, "; omega =", object$omega, "\n")

        }

        if(length(object@info)>0 & (!is.null(object$AIC) | !is.null(object$MSPE))) {
            cat("\nFitted Continuous-time Model","\n")

            cat("Animal ID:",object@info$identity,"\n")
            cat("Timezone:",object@info$timezone,"\n")
            cat("CRS (PROJ4):",object@info$projection,"\n\n")

            cat("Periodicity (if exists):",object$circle,"\n")
            cat("Considers GPS error:",object$error,"\n")
            cat("Distribution center:",object$mu,"\n")
            cat("Distribution variances:",object$sigma[1],object$sigma[4],"\n")
            cat("X-Y correlation:",object$sigma[2],"\n")
            cat("Autocorrelation parameters:","tau(s) =", object$tau, "; omega =", object$omega, "\n")
            cat("\n")
            cat("Fitting statistics:","AIC=",object$AIC,"; BIC=",object$BIC,"; AICc=",object$AICc,"; MLE=",object$MLE$loglike,"\n")
            # Add name that can be extracted from summary.
        }
    }
)

setMethod("show",signature(object="UD"),function(object) {
    cat("UD distribution of animal ", object@CTMM@info$identity," \n")
    print(summary(object))
})

How it looks:


>data(buffalo)
>buffalo

$Cilla

Telemetry object with 3527 locations and 6 columns 
Animal ID: Cilla
Timezone: UTC
CRS (PROJ4): +proj=tpeqd +lon_1=31.7775407470691 +lat_1=-24.2850983757726 +lon_2=31.8880063438124 +lat_2=-25.024736901024 +datum=WGS84

First ten locations:
               timestamp longitude  latitude          t        x          y
4109 2005-07-14 05:35:00  31.88776 -24.96738 1121319300 35215.76   836.7338
4110 2005-07-14 07:35:00  31.85942 -24.94288 1121326500 32127.59 -1629.8224
4111 2005-07-14 08:34:00  31.85512 -24.94914 1121330040 32759.68 -2153.6281
4112 2005-07-14 09:35:00  31.85694 -24.95424 1121333700 33347.02 -2048.1883
4113 2005-07-14 10:35:00  31.85977 -24.94735 1121337300 32625.39 -1661.8450
4114 2005-07-14 11:34:00  31.84466 -24.93435 1121340840 30986.23 -2978.3344
4115 2005-07-14 12:35:00  31.84410 -24.93400 1121344500 30940.03 -3029.0547
4116 2005-07-14 13:34:00  31.84463 -24.93442 1121348040 30993.47 -2982.2194
4117 2005-07-14 14:35:00  31.85700 -24.93371 1121351700 31083.26 -1734.2964
4118 2005-07-14 15:35:00  31.86478 -24.93985 1121355300 31866.33 -1048.2079

$Gabs

Telemetry object with 1996 locations and 6 columns
Animal ID: Gabs
Timezone: UTC
CRS (PROJ4): +proj=tpeqd +lon_1=31.7775407470691 +lat_1=-24.2850983757726 +lon_2=31.8880063438124 +lat_2=-25.024736901024 +datum=WGS84 

First ten locations:
               timestamp longitude  latitude          t        x          y
1762 2005-04-05 05:56:00  31.87258 -25.02553 1112680560 41423.38 -1553.6525
1763 2005-04-05 06:55:00  31.88289 -25.04294 1112684100 43483.86  -784.6731
1764 2005-04-05 07:56:00  31.88463 -25.04711 1112687760 43967.56  -673.4608
1765 2005-04-05 08:55:00  31.88389 -25.04408 1112691300 43623.14  -701.7477
1766 2005-04-05 09:55:00  31.88369 -25.04962 1112694900 44231.48  -805.0512
1767 2005-04-05 10:56:00  31.88384 -25.04984 1112698560 44257.76  -793.3050
1768 2005-04-05 11:56:00  31.88379 -25.04982 1112702160 44255.05  -798.1134
1769 2005-04-05 12:55:00  31.88334 -25.04950 1112705700 44213.47  -838.2167
1770 2005-04-05 13:56:00  31.88337 -25.04951 1112709360 44215.01  -835.3206
1771 2005-04-05 14:55:00  31.88244 -25.04730 1112712900 43958.66  -895.0594

$Mvubu

Telemetry object with 2572 locations and 6 columns
Animal ID: Mvubu
Timezone: UTC
CRS (PROJ4): +proj=tpeqd +lon_1=31.7775407470691 +lat_1=-24.2850983757726 +lon_2=31.8880063438124 +lat_2=-25.024736901024 +datum=WGS84

First ten locations:
               timestamp longitude  latitude          t        x          y
7664 2005-07-15 05:02:00  31.90357 -24.96534 1121403720 35206.00  2448.3078
7665 2005-07-15 07:02:00  31.85967 -24.99756 1121410920 38162.62 -2424.6964
7666 2005-07-15 08:02:00  31.85571 -24.98359 1121414520 36567.75 -2611.0629
7667 2005-07-15 09:02:00  31.85959 -24.96424 1121418120 34486.06 -1933.0075
7668 2005-07-15 10:02:00  31.86372 -24.97278 1121421720 35484.30 -1648.0986
7669 2005-07-15 11:02:00  31.87692 -24.98153 1121425320 36629.02  -459.5551
7670 2005-07-15 12:02:00  31.88615 -24.97670 1121428920 36221.85   535.6641
7671 2005-07-15 13:02:00  31.88786 -24.97370 1121432520 35914.13   751.8073
7672 2005-07-15 14:02:00  31.89746 -24.96909 1121436120 35536.35  1781.0444
7673 2005-07-15 15:03:00  31.89042 -24.98638 1121439780 37347.49   817.2907

$Pepper

Telemetry object with 1725 locations and 6 columns
Animal ID: Pepper
Timezone: UTC
CRS (PROJ4): +proj=tpeqd +lon_1=31.7775407470691 +lat_1=-24.2850983757726 +lon_2=31.8880063438124 +lat_2=-25.024736901024 +datum=WGS84 

First ten locations:
                timestamp longitude  latitude          t         x         y
17517 2006-04-25 05:09:00  31.73749 -24.19705 1145941740 -51803.35 -2715.663
17518 2006-04-25 06:09:00  31.73653 -24.19929 1145945340 -51569.29 -2845.660
17519 2006-04-25 07:09:00  31.73946 -24.20100 1145948940 -51340.72 -2576.353
17520 2006-04-25 08:09:00  31.73987 -24.20092 1145952540 -51344.11 -2533.788
17521 2006-04-25 10:09:00  31.74086 -24.20365 1145959740 -51029.45 -2474.771
17522 2006-04-25 11:09:00  31.74098 -24.20370 1145963340 -51022.23 -2463.655
17523 2006-04-25 13:09:00  31.74032 -24.20347 1145970540 -51056.55 -2526.557
17524 2006-04-25 15:09:00  31.73919 -24.20080 1145977740 -51366.59 -2600.376
17525 2006-04-25 16:09:00  31.73850 -24.19941 1145981340 -51529.26 -2649.073
17526 2006-04-25 17:09:00  31.73961 -24.20011 1145984940 -51436.98 -2547.928

$Queen

Telemetry object with 1756 locations and 6 columns
Animal ID: Queen
Timezone: UTC
CRS (PROJ4): +proj=tpeqd +lon_1=31.7775407470691 +lat_1=-24.2850983757726 +lon_2=31.8880063438124 +lat_2=-25.024736901024 +datum=WGS84

First ten locations:
             timestamp longitude  latitude          t         x         y
1  2005-02-17 05:05:00  31.76642 -24.53730 1108616700 -13875.13 -4880.888
4  2005-02-17 06:08:00  31.76442 -24.53931 1108620480 -13680.53 -5111.571
5  2005-02-17 07:05:00  31.76114 -24.54799 1108623900 -12767.58 -5570.134
6  2005-02-17 08:05:00  31.76080 -24.54703 1108627500 -12878.15 -5590.109
7  2005-02-17 09:05:00  31.75982 -24.54681 1108631100 -12915.66 -5685.029
8  2005-02-17 10:05:00  31.75968 -24.54680 1108634700 -12918.69 -5698.963
9  2005-02-17 11:05:00  31.76011 -24.54682 1108638300 -12910.62 -5656.190
10 2005-02-17 12:05:00  31.76019 -24.54685 1108641900 -12906.15 -5648.480
11 2005-02-17 13:05:00  31.76015 -24.54686 1108645500 -12905.58 -5652.714
12 2005-02-17 14:05:00  31.76318 -24.54720 1108649100 -12827.00 -5353.800

$Toni

Telemetry object with 5766 locations and 6 columns
Animal ID: Toni
Timezone: UTC
CRS (PROJ4): +proj=tpeqd +lon_1=31.7775407470691 +lat_1=-24.2850983757726 +lon_2=31.8880063438124 +lat_2=-25.024736901024 +datum=WGS84

First ten locations: 
                timestamp longitude  latitude          t         x          y
11155 2005-08-23 06:35:00  31.75345 -24.16950 1124778900 -54625.52  -698.8675
11156 2005-08-23 07:34:00  31.73884 -24.15402 1124782440 -56531.96 -1938.6738
11157 2005-08-23 08:34:00  31.73969 -24.15359 1124786040 -56567.78 -1846.8790
11158 2005-08-23 09:35:00  31.73874 -24.15329 1124789700 -56613.92 -1938.0327
11159 2005-08-23 10:34:00  31.73946 -24.15336 1124793240 -56596.27 -1866.5957
11160 2005-08-23 11:35:00  31.73898 -24.15363 1124796900 -56573.17 -1918.9280
11161 2005-08-23 12:35:00  31.73947 -24.15346 1124800500 -56585.14 -1867.0572
11162 2005-08-23 13:35:00  31.74052 -24.15450 1124804100 -56456.17 -1776.8726
11163 2005-08-23 14:36:00  31.74146 -24.16009 1124807760 -55826.85 -1765.5313
11164 2005-08-23 15:35:00  31.74665 -24.17546 1124811300 -54060.63 -1472.2164