# Analysis of killdeer data illustrating basic RMark features
# Incorportating an age variable

# 2019-05-01 CJS Initial code

# This is the killdeer data that ships with RMark
# I added arbitrary nest ages.

library(ggplot2)
## Registered S3 methods overwritten by 'ggplot2':
##   method         from 
##   [.quosures     rlang
##   c.quosures     rlang
##   print.quosures rlang
library(readxl)
library(RMark)
## This is RMark 2.2.6
##  Documentation available at http://www.phidot.org/software/mark/rmark/RMarkDocumentation.zip
# The dataframe must contain the following fields with the following names
#
#    FirstFound: day the nest was first found
#    LastPresent: last day that a chick was present in the nest
#    LastChecked: last day the nest was checked
#    Fate: fate of the nest; 0=hatch an
#    Freq: number of nests with this data
#    AgeDay1: age of nest at day 1 of study (can be negative)
#
# In this example, the multiple visits to a nest have been collapsed
# to a single record for each nest.
# In more complex examples, you may have multple records per nest
# as shown in the mallard example.
#

killdata <- readxl::read_excel(file.path("..","Killdeer.xlsx"), 
                               sheet="killdeer-age")

killdata <- as.data.frame(killdata)  # sometimes doesn't play nice with tibbles
head(killdata)
##      id FirstFound LastPresent LastChecked Fate Freq AgeDay1
## 1 /*A*/          1           9           9    0    1       0
## 2 /*B*/          5           5           9    1    1      -2
## 3 /*C*/          5          40          40    0    1      -3
## 4 /*D*/          9          32          32    0    1      -4
## 5 /*E*/          7           8           8    0    1      -4
## 6 /*F*/          3          15          15    0    1       1
# what are the parameters of the model
# There is only one parameter, the daily survival probality (S)
setup.parameters("Nest", check=TRUE)
## [1] "S"
# 1. Process the data.
# The nocc variable is the data at which hatching occurs
kill.proc <- process.data(killdata, model="Nest", nocc=max(killdata$LastChecked))
kill.proc
## $data
##       id FirstFound LastPresent LastChecked Fate freq AgeDay1
## 1  /*A*/          1           9           9    0    1       0
## 2  /*B*/          5           5           9    1    1      -2
## 3  /*C*/          5          40          40    0    1      -3
## 4  /*D*/          9          32          32    0    1      -4
## 5  /*E*/          7           8           8    0    1      -4
## 6  /*F*/          3          15          15    0    1       1
## 7  /*G*/          8          32          32    0    1      -7
## 8  /*H*/         14          14          16    1    1     -10
## 9  /*I*/          8          14          14    0    1      -7
## 10 /*J*/         13          14          14    0    1     -12
## 11 /*K*/         14          33          33    0    1     -13
## 12 /*L*/         15          37          37    0    1     -10
## 13 /*M*/         16          37          40    1    1     -11
## 14 /*N*/         16          28          32    1    1     -11
## 15 /*O*/         16          17          17    0    1     -12
## 16 /*P*/         21          28          33    1    1     -15
## 17 /*Q*/         23          33          34    1    1     -17
## 18 /*R*/         27          29          29    0    1     -23
## 
## $model
## [1] "Nest"
## 
## $mixtures
## [1] 1
## 
## $freq
##    group1
## 1       1
## 2       1
## 3       1
## 4       1
## 5       1
## 6       1
## 7       1
## 8       1
## 9       1
## 10      1
## 11      1
## 12      1
## 13      1
## 14      1
## 15      1
## 16      1
## 17      1
## 18      1
## 
## $nocc
## [1] 40
## 
## $nocc.secondary
## NULL
## 
## $time.intervals
##  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [36] 1 1 1 1 1
## 
## $begin.time
## [1] 1
## 
## $age.unit
## [1] 1
## 
## $initial.ages
## [1] 0
## 
## $group.covariates
## NULL
## 
## $nstrata
## [1] 1
## 
## $strata.labels
## [1] ""
## 
## $counts
## NULL
## 
## $reverse
## [1] FALSE
## 
## $areas
## NULL
## 
## $events
## NULL
# 2. Examine and/or modify the ddl. (Not done here)
# The Age variable here is time since the start of the study which is not useful.
kill.ddl <- make.design.data(kill.proc)
str(kill.ddl)
## List of 2
##  $ S       :'data.frame':    39 obs. of  7 variables:
##   ..$ par.index  : int [1:39] 1 2 3 4 5 6 7 8 9 10 ...
##   ..$ model.index: num [1:39] 1 2 3 4 5 6 7 8 9 10 ...
##   ..$ group      : Factor w/ 1 level "1": 1 1 1 1 1 1 1 1 1 1 ...
##   ..$ age        : Factor w/ 39 levels "0","1","2","3",..: 1 2 3 4 5 6 7 8 9 10 ...
##   ..$ time       : Factor w/ 39 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
##   ..$ Age        : num [1:39] 0 1 2 3 4 5 6 7 8 9 ...
##   ..$ Time       : num [1:39] 0 1 2 3 4 5 6 7 8 9 ...
##  $ pimtypes:List of 1
##   ..$ S:List of 1
##   .. ..$ pim.type: chr "all"
kill.ddl 
## $S
##    par.index model.index group age time Age Time
## 1          1           1     1   0    1   0    0
## 2          2           2     1   1    2   1    1
## 3          3           3     1   2    3   2    2
## 4          4           4     1   3    4   3    3
## 5          5           5     1   4    5   4    4
## 6          6           6     1   5    6   5    5
## 7          7           7     1   6    7   6    6
## 8          8           8     1   7    8   7    7
## 9          9           9     1   8    9   8    8
## 10        10          10     1   9   10   9    9
## 11        11          11     1  10   11  10   10
## 12        12          12     1  11   12  11   11
## 13        13          13     1  12   13  12   12
## 14        14          14     1  13   14  13   13
## 15        15          15     1  14   15  14   14
## 16        16          16     1  15   16  15   15
## 17        17          17     1  16   17  16   16
## 18        18          18     1  17   18  17   17
## 19        19          19     1  18   19  18   18
## 20        20          20     1  19   20  19   19
## 21        21          21     1  20   21  20   20
## 22        22          22     1  21   22  21   21
## 23        23          23     1  22   23  22   22
## 24        24          24     1  23   24  23   23
## 25        25          25     1  24   25  24   24
## 26        26          26     1  25   26  25   25
## 27        27          27     1  26   27  26   26
## 28        28          28     1  27   28  27   27
## 29        29          29     1  28   29  28   28
## 30        30          30     1  29   30  29   29
## 31        31          31     1  30   31  30   30
## 32        32          32     1  31   32  31   31
## 33        33          33     1  32   33  32   32
## 34        34          34     1  33   34  33   33
## 35        35          35     1  34   35  34   34
## 36        36          36     1  35   36  35   35
## 37        37          37     1  36   37  36   36
## 38        38          38     1  37   38  37   37
## 39        39          39     1  38   39  38   38
## 
## $pimtypes
## $pimtypes$S
## $pimtypes$S$pim.type
## [1] "all"
# 3. Fit a particular model
# This is a model with S a function of nest age
mod.res <-  RMark::mark(kill.proc, ddl=kill.ddl,
                          model="Nest",
                          model.parameters=list(
                            S   =list(formula=~NestAge)
                          )
  )
## 
## Output summary for Nest model
## Name : S(~NestAge) 
## 
## Npar :  2
## -2lnL:  42.38081
## AICc :  46.43878
## 
## Beta
##                 estimate        se        lcl       ucl
## S:(Intercept)  3.7952906 0.7988475  2.2295494 5.3610318
## S:NestAge     -0.0188125 0.0516282 -0.1200038 0.0823789
## 
## 
## Real Parameter S
##          1         2         3         4         5         6         7
##  0.9813396 0.9809919 0.9806379 0.9802775 0.9799105 0.9795368 0.9791563
##          8         9        10        11        12        13       14
##  0.9787688 0.9783744 0.9779727 0.9775638 0.9771475 0.9767236 0.976292
##         15        16      17        18        19        20       21
##  0.9758527 0.9754054 0.97495 0.9744864 0.9740145 0.9735341 0.973045
##         22        23        24        25       26       27        28
##  0.9725472 0.9720404 0.9715246 0.9709995 0.970465 0.969921 0.9693673
##         29        30        31        32        33       34        35
##  0.9688037 0.9682301 0.9676463 0.9670521 0.9664474 0.965832 0.9652057
##         36        37        38        39
##  0.9645684 0.9639198 0.9632598 0.9625882
summary(mod.res)
## Output summary for Nest model
## Name : S(~NestAge) 
## 
## Npar :  2
## -2lnL:  42.38081
## AICc :  46.43878
## 
## Beta
##                 estimate        se        lcl       ucl
## S:(Intercept)  3.7952906 0.7988475  2.2295494 5.3610318
## S:NestAge     -0.0188125 0.0516282 -0.1200038 0.0823789
## 
## 
## Real Parameter S
##          1         2         3         4         5         6         7
##  0.9813396 0.9809919 0.9806379 0.9802775 0.9799105 0.9795368 0.9791563
##          8         9        10        11        12        13       14
##  0.9787688 0.9783744 0.9779727 0.9775638 0.9771475 0.9767236 0.976292
##         15        16      17        18        19        20       21
##  0.9758527 0.9754054 0.97495 0.9744864 0.9740145 0.9735341 0.973045
##         22        23        24        25       26       27        28
##  0.9725472 0.9720404 0.9715246 0.9709995 0.970465 0.969921 0.9693673
##         29        30        31        32        33       34        35
##  0.9688037 0.9682301 0.9676463 0.9670521 0.9664474 0.965832 0.9652057
##         36        37        38        39
##  0.9645684 0.9639198 0.9632598 0.9625882
# Look the objects returned in more details
names(mod.res)
##  [1] "data"             "model"            "title"           
##  [4] "model.name"       "links"            "mixtures"        
##  [7] "call"             "parameters"       "time.intervals"  
## [10] "number.of.groups" "group.labels"     "nocc"            
## [13] "begin.time"       "covariates"       "fixed"           
## [16] "design.matrix"    "pims"             "design.data"     
## [19] "strata.labels"    "mlogit.list"      "profile.int"     
## [22] "simplify"         "model.parameters" "results"         
## [25] "output"
names(mod.res$results)
##  [1] "lnl"              "deviance"         "deviance.df"     
##  [4] "npar"             "n"                "AICc"            
##  [7] "beta"             "real"             "beta.vcv"        
## [10] "derived"          "derived.vcv"      "covariate.values"
## [13] "singular"         "real.vcv"
# look at estimates on beta and original scale
mod.res$results$beta  # on the logit scale
##                 estimate        se        lcl       ucl
## S:(Intercept)  3.7952906 0.7988475  2.2295494 5.3610318
## S:NestAge     -0.0188125 0.0516282 -0.1200038 0.0823789
mod.res$results$real# on the regular 0-1 scale for each day AVERAGE OVER NEST AGE
##               estimate        se       lcl       ucl fixed    note
## S g1 a0 t1   0.9813396 0.0222450 0.8294276 0.9982449              
## S g1 a1 t2   0.9809919 0.0217489 0.8399191 0.9980340              
## S g1 a2 t3   0.9806379 0.0212325 0.8498100 0.9977991              
## S g1 a3 t4   0.9802775 0.0206960 0.8591143 0.9975377              
## S g1 a4 t5   0.9799105 0.0201399 0.8678479 0.9972474              
## S g1 a5 t6   0.9795368 0.0195650 0.8760270 0.9969256              
## S g1 a6 t7   0.9791563 0.0189723 0.8836684 0.9965696              
## S g1 a7 t8   0.9787688 0.0183631 0.8907887 0.9961768              
## S g1 a8 t9   0.9783744 0.0177393 0.8974038 0.9957447              
## S g1 a9 t10  0.9779727 0.0171033 0.9035286 0.9952712              
## S g1 a10 t11 0.9775638 0.0164582 0.9091765 0.9947547              
## S g1 a11 t12 0.9771475 0.0158081 0.9143584 0.9941943              
## S g1 a12 t13 0.9767236 0.0151581 0.9190826 0.9935907              
## S g1 a13 t14 0.9762920 0.0145151 0.9233535 0.9929461              
## S g1 a14 t15 0.9758527 0.0138875 0.9271708 0.9922652              
## S g1 a15 t16 0.9754054 0.0132860 0.9305287 0.9915559              
## S g1 a16 t17 0.9749500 0.0127240 0.9334147 0.9908305              
## S g1 a17 t18 0.9744864 0.0122177 0.9358083 0.9901058              
## S g1 a18 t19 0.9740145 0.0117867 0.9376809 0.9894041              
## S g1 a19 t20 0.9735341 0.0114526 0.9389956 0.9887524              
## S g1 a20 t21 0.9730450 0.0112391 0.9397091 0.9881808              
## S g1 a21 t22 0.9725472 0.0111688 0.9397744 0.9877192              
## S g1 a22 t23 0.9720404 0.0112615 0.9391457 0.9873927              
## S g1 a23 t24 0.9715246 0.0115309 0.9377822 0.9872170              
## S g1 a24 t25 0.9709995 0.0119834 0.9356512 0.9871959              
## S g1 a25 t26 0.9704650 0.0126179 0.9327279 0.9873208              
## S g1 a26 t27 0.9699210 0.0134272 0.9289924 0.9875740              
## S g1 a27 t28 0.9693673 0.0144004 0.9244262 0.9879324              
## S g1 a28 t29 0.9688037 0.0155251 0.9190078 0.9883713              
## S g1 a29 t30 0.9682301 0.0167890 0.9127108 0.9888677              
## S g1 a30 t31 0.9676463 0.0181808 0.9055021 0.9894013              
## S g1 a31 t32 0.9670521 0.0196910 0.8973426 0.9899553              
## S g1 a32 t33 0.9664474 0.0213116 0.8881873 0.9905165              
## S g1 a33 t34 0.9658320 0.0230363 0.8779872 0.9910747              
## S g1 a34 t35 0.9652057 0.0248601 0.8666906 0.9916223              
## S g1 a35 t36 0.9645684 0.0267792 0.8542456 0.9921539              
## S g1 a36 t37 0.9639198 0.0287907 0.8406018 0.9926656              
## S g1 a37 t38 0.9632598 0.0308927 0.8257131 0.9931549              
## S g1 a38 t39 0.9625882 0.0330837 0.8095401 0.9936204
# The real estimates are not useful because the value for each day is averaged over the
# nest ages at that time.
# For example, the beta values are shown above and the
# logit(DSR) for nest 1 day old is
logit_DSR_1 = sum( mod.res$results$beta$estimate *c(1,1))
logit_DSR_1
## [1] 3.776478
# and estimate of survival of nest 1 day old is
1/(1+exp(-logit_DSR_1))
## [1] 0.9776096
# compared to 
head(mod.res$results$real)
##             estimate        se       lcl       ucl fixed    note
## S g1 a0 t1 0.9813396 0.0222450 0.8294276 0.9982449              
## S g1 a1 t2 0.9809919 0.0217489 0.8399191 0.9980340              
## S g1 a2 t3 0.9806379 0.0212325 0.8498100 0.9977991              
## S g1 a3 t4 0.9802775 0.0206960 0.8591143 0.9975377              
## S g1 a4 t5 0.9799105 0.0201399 0.8678479 0.9972474              
## S g1 a5 t6 0.9795368 0.0195650 0.8760270 0.9969256
# The average nest age at day 1 is
average_nest_age_1 = mean(killdata$AgeDay1)
average_nest_age_1
## [1] -8.888889
logit_DSR_1_avg = sum( mod.res$results$beta$estimate *c(1,average_nest_age_1))
logit_DSR_1_avg
## [1] 3.962513
# and estimate of survival of nest on day 1 at average age of nests is
1/(1+exp(-logit_DSR_1_avg))  # now matches the real estimates
## [1] 0.9813396
# You want to predict survival as a function of nest age.
# This is a bit tricker

# First get the all.diff.index values for each day of the study.
get.real(mod.res, param="S", se=TRUE)
##              all.diff.index par.index  estimate        se       lcl
## S g1 a0 t1                1         1 0.9813396 0.0222450 0.8294276
## S g1 a1 t2                2         2 0.9809919 0.0217489 0.8399191
## S g1 a2 t3                3         3 0.9806379 0.0212325 0.8498100
## S g1 a3 t4                4         4 0.9802775 0.0206960 0.8591143
## S g1 a4 t5                5         5 0.9799105 0.0201399 0.8678479
## S g1 a5 t6                6         6 0.9795368 0.0195650 0.8760270
## S g1 a6 t7                7         7 0.9791563 0.0189723 0.8836684
## S g1 a7 t8                8         8 0.9787688 0.0183631 0.8907887
## S g1 a8 t9                9         9 0.9783744 0.0177393 0.8974038
## S g1 a9 t10              10        10 0.9779727 0.0171033 0.9035286
## S g1 a10 t11             11        11 0.9775638 0.0164582 0.9091765
## S g1 a11 t12             12        12 0.9771475 0.0158081 0.9143584
## S g1 a12 t13             13        13 0.9767236 0.0151581 0.9190826
## S g1 a13 t14             14        14 0.9762920 0.0145151 0.9233535
## S g1 a14 t15             15        15 0.9758527 0.0138875 0.9271708
## S g1 a15 t16             16        16 0.9754054 0.0132860 0.9305287
## S g1 a16 t17             17        17 0.9749500 0.0127240 0.9334147
## S g1 a17 t18             18        18 0.9744864 0.0122177 0.9358083
## S g1 a18 t19             19        19 0.9740145 0.0117867 0.9376809
## S g1 a19 t20             20        20 0.9735341 0.0114526 0.9389956
## S g1 a20 t21             21        21 0.9730450 0.0112391 0.9397091
## S g1 a21 t22             22        22 0.9725472 0.0111688 0.9397744
## S g1 a22 t23             23        23 0.9720404 0.0112615 0.9391457
## S g1 a23 t24             24        24 0.9715246 0.0115309 0.9377822
## S g1 a24 t25             25        25 0.9709995 0.0119834 0.9356512
## S g1 a25 t26             26        26 0.9704650 0.0126179 0.9327279
## S g1 a26 t27             27        27 0.9699210 0.0134272 0.9289924
## S g1 a27 t28             28        28 0.9693673 0.0144004 0.9244262
## S g1 a28 t29             29        29 0.9688037 0.0155251 0.9190078
## S g1 a29 t30             30        30 0.9682301 0.0167890 0.9127108
## S g1 a30 t31             31        31 0.9676463 0.0181808 0.9055021
## S g1 a31 t32             32        32 0.9670521 0.0196910 0.8973426
## S g1 a32 t33             33        33 0.9664474 0.0213116 0.8881873
## S g1 a33 t34             34        34 0.9658320 0.0230363 0.8779872
## S g1 a34 t35             35        35 0.9652057 0.0248601 0.8666906
## S g1 a35 t36             36        36 0.9645684 0.0267792 0.8542456
## S g1 a36 t37             37        37 0.9639198 0.0287907 0.8406018
## S g1 a37 t38             38        38 0.9632598 0.0308927 0.8257131
## S g1 a38 t39             39        39 0.9625882 0.0330837 0.8095401
##                    ucl fixed    note group age time Age Time
## S g1 a0 t1   0.9982449                   1   0    1   0    0
## S g1 a1 t2   0.9980340                   1   1    2   1    1
## S g1 a2 t3   0.9977991                   1   2    3   2    2
## S g1 a3 t4   0.9975377                   1   3    4   3    3
## S g1 a4 t5   0.9972474                   1   4    5   4    4
## S g1 a5 t6   0.9969256                   1   5    6   5    5
## S g1 a6 t7   0.9965696                   1   6    7   6    6
## S g1 a7 t8   0.9961768                   1   7    8   7    7
## S g1 a8 t9   0.9957447                   1   8    9   8    8
## S g1 a9 t10  0.9952712                   1   9   10   9    9
## S g1 a10 t11 0.9947547                   1  10   11  10   10
## S g1 a11 t12 0.9941943                   1  11   12  11   11
## S g1 a12 t13 0.9935907                   1  12   13  12   12
## S g1 a13 t14 0.9929461                   1  13   14  13   13
## S g1 a14 t15 0.9922652                   1  14   15  14   14
## S g1 a15 t16 0.9915559                   1  15   16  15   15
## S g1 a16 t17 0.9908305                   1  16   17  16   16
## S g1 a17 t18 0.9901058                   1  17   18  17   17
## S g1 a18 t19 0.9894041                   1  18   19  18   18
## S g1 a19 t20 0.9887524                   1  19   20  19   19
## S g1 a20 t21 0.9881808                   1  20   21  20   20
## S g1 a21 t22 0.9877192                   1  21   22  21   21
## S g1 a22 t23 0.9873927                   1  22   23  22   22
## S g1 a23 t24 0.9872170                   1  23   24  23   23
## S g1 a24 t25 0.9871959                   1  24   25  24   24
## S g1 a25 t26 0.9873208                   1  25   26  25   25
## S g1 a26 t27 0.9875740                   1  26   27  26   26
## S g1 a27 t28 0.9879324                   1  27   28  27   27
## S g1 a28 t29 0.9883713                   1  28   29  28   28
## S g1 a29 t30 0.9888677                   1  29   30  29   29
## S g1 a30 t31 0.9894013                   1  30   31  30   30
## S g1 a31 t32 0.9899553                   1  31   32  31   31
## S g1 a32 t33 0.9905165                   1  32   33  32   32
## S g1 a33 t34 0.9910747                   1  33   34  33   33
## S g1 a34 t35 0.9916223                   1  34   35  34   34
## S g1 a35 t36 0.9921539                   1  35   36  35   35
## S g1 a36 t37 0.9926656                   1  36   37  36   36
## S g1 a37 t38 0.9931549                   1  37   38  37   37
## S g1 a38 t39 0.9936204                   1  38   39  38   38
# we see that all.diff.index==1 is for nest survival on day 1 of the study

# we will predict then the DSR for day 1 at various ages
pred.ages <- data.frame(NestAge1=1:20, index=1)
covariate.predictions(mod.res, data=pred.ages )$estimates[1:10,]
##    vcv.index model.index par.index NestAge1 index  estimate         se
## 1          1           1         1        1     1 0.9776096 0.01653021
## 2          1           1         1        2     1 0.9771941 0.01588040
## 3          1           1         1        3     1 0.9767711 0.01523016
## 4          1           1         1        4     1 0.9763404 0.01458597
## 5          1           1         1        5     1 0.9759019 0.01395614
## 6          1           1         1        6     1 0.9754555 0.01335112
## 7          1           1         1        7     1 0.9750010 0.01278398
## 8          1           1         1        8     1 0.9745384 0.01227066
## 9          1           1         1        9     1 0.9740674 0.01183018
## 10         1           1         1       10     1 0.9735879 0.01148422
##          lcl       ucl fixed
## 1  0.9085721 0.9948142      
## 2  0.9138054 0.9942588      
## 3  0.9185801 0.9936599      
## 4  0.9229012 0.9930196      
## 5  0.9267692 0.9923424      
## 6  0.9301786 0.9916358      
## 7  0.9331179 0.9909114      
## 8  0.9355674 0.9901857      
## 9  0.9374996 0.9894801      
## 10 0.9388784 0.9888214
# because the model does NOT include a term for day effects, these predictions will be the same
# for all days of the study
pred.ages <- data.frame(NestAge4=1:20, index=4)
covariate.predictions(mod.res, data=pred.ages )$estimates[1:10,]
##    vcv.index model.index par.index NestAge4 index  estimate         se
## 1          4           4         4        1     4 0.9776096 0.01653021
## 2          4           4         4        2     4 0.9771941 0.01588040
## 3          4           4         4        3     4 0.9767711 0.01523016
## 4          4           4         4        4     4 0.9763404 0.01458597
## 5          4           4         4        5     4 0.9759019 0.01395614
## 6          4           4         4        6     4 0.9754555 0.01335112
## 7          4           4         4        7     4 0.9750010 0.01278398
## 8          4           4         4        8     4 0.9745384 0.01227066
## 9          4           4         4        9     4 0.9740674 0.01183018
## 10         4           4         4       10     4 0.9735879 0.01148422
##          lcl       ucl fixed
## 1  0.9085721 0.9948142      
## 2  0.9138054 0.9942588      
## 3  0.9185801 0.9936599      
## 4  0.9229012 0.9930196      
## 5  0.9267692 0.9923424      
## 6  0.9301786 0.9916358      
## 7  0.9331179 0.9909114      
## 8  0.9355674 0.9901857      
## 9  0.9374996 0.9894801      
## 10 0.9388784 0.9888214
plotdata <-covariate.predictions(mod.res, data=pred.ages )$estimates
dsr.age <- ggplot(data=plotdata, aes(x=NestAge4, y=estimate, group=1))+
  ggtitle("Survival as a function of nest age")+
  geom_line()+
  geom_ribbon(aes(ymin=lcl, ymax=ucl), alpha=0.1)
dsr.age

ggsave(dsr.age,
       file=file.path("..","..","..","..","MyStuff","Images","killdear-age.png"),h=4, w=6, units="in", dpi=300)
 

#_----------------------------------------------------------------------
# Compare to the null model

mod.null <-  RMark::mark(kill.proc, ddl=kill.ddl,
                        model="Nest",
                        model.parameters=list(
                          S   =list(formula=~1)
                        )
)
## 
## Output summary for Nest model
## Name : S(~1) 
## 
## Npar :  1
## -2lnL:  42.51028
## AICc :  44.52951
## 
## Beta
##               estimate        se      lcl      ucl
## S:(Intercept) 3.557002 0.4141776 2.745214 4.368791
## 
## 
## Real Parameter S
##          1         2         3         4         5         6         7
##  0.9722669 0.9722669 0.9722669 0.9722669 0.9722669 0.9722669 0.9722669
##          8         9        10        11        12        13        14
##  0.9722669 0.9722669 0.9722669 0.9722669 0.9722669 0.9722669 0.9722669
##         15        16        17        18        19        20        21
##  0.9722669 0.9722669 0.9722669 0.9722669 0.9722669 0.9722669 0.9722669
##         22        23        24        25        26        27        28
##  0.9722669 0.9722669 0.9722669 0.9722669 0.9722669 0.9722669 0.9722669
##         29        30        31        32        33        34        35
##  0.9722669 0.9722669 0.9722669 0.9722669 0.9722669 0.9722669 0.9722669
##         36        37        38        39
##  0.9722669 0.9722669 0.9722669 0.9722669
collect.models(type="Nest")
##         model npar     AICc DeltaAICc    weight Deviance
## 1       S(~1)    1 44.52951  0.000000 0.7220459 42.51028
## 2 S(~NestAge)    2 46.43878  1.909265 0.2779541 42.38081
cleanup(ask=FALSE)