# Example using model averaging on the pronghorn dataset
# 256 sites, two sampling occasions per site
# Covariates used in this example:
# 1. sagebrush (continuos) - Sagebrush density
# 2. aspect (Categorical) - Compass direction slope faces (N,S,E,W)

# 2018-11-26 Code contributed by Neil Faught

#  RPresence package

library(readxl)
library(RPresence)
## Warning: package 'RPresence' was built under R version 3.5.0
library(ggplot2)

# get the data read in
# Data for detections should be a data frame with rows corresponding to sites
# and columns to visits.
# The usual 1=detected; 0=not detected; NA=not visited is used.

input.data<-read.csv(file.path("..","pronghorn.csv"), header=TRUE, as.is=TRUE, strip.white=TRUE)
head(input.data)
##   Plot Survey.1 Survey.2 sagebrush slope  DW aspect
## 1    1        0        0       9.0     0  25      W
## 2    2        0        1      18.0     5 150      S
## 3    3        0        0       8.4    45 150      W
## 4    4        0        0       3.2    65 375      E
## 5    5        0        1      12.0     5 375      S
## 6    6        1        1       7.8     5 150      S
input.history <- input.data[,c(2,3)] # the history extracted
head(input.history)
##   Survey.1 Survey.2
## 1        0        0
## 2        0        1
## 3        0        0
## 4        0        0
## 5        0        1
## 6        1        1
# do some basic checks on your data 
# e.g. check number of sites; number of visits etc
nrow(input.history)
## [1] 256
ncol(input.history)
## [1] 2
range(input.history, na.rm=TRUE) # check that all values are either 0 or 1
## [1] 0 1
sum(is.na(input.history))    # are there any missing values?
## [1] 0
# Get the sagebrush and aspect information. These are the covariates that will
# be used in the analysis. These are unit-level covariates.
unit.cov = input.data[,c(4,7)]
head(unit.cov)
##   sagebrush aspect
## 1       9.0      W
## 2      18.0      S
## 3       8.4      W
## 4       3.2      E
## 5      12.0      S
## 6       7.8      S
# Create the *.pao file
prong.pao <- RPresence::createPao(input.history,
                                  unitcov=unit.cov,
                                  title='Pronghorn SSSS')
prong.pao
## $nunits
## [1] 256
## 
## $nsurveys
## [1] 2
## 
## $nseasons
## [1] 1
## 
## $nmethods
## [1] 1
## 
## $det.data
##     Survey.1 Survey.2
## 1          0        0
## 2          0        1
## 3          0        0
## 4          0        0
## 5          0        1
## 6          1        1
## 7          0        0
## 8          1        0
## 9          1        0
## 10         1        1
## 11         0        0
## 12         1        1
## 13         0        0
## 14         1        1
## 15         0        0
## 16         0        1
## 17         0        0
## 18         1        0
## 19         0        0
## 20         0        0
## 21         1        0
## 22         0        1
## 23         0        0
## 24         0        0
## 25         1        0
## 26         0        0
## 27         0        0
## 28         1        0
## 29         1        1
## 30         1        0
## 31         0        0
## 32         1        0
## 33         0        1
## 34         0        1
## 35         0        1
## 36         0        1
## 37         1        0
## 38         1        0
## 39         0        0
## 40         0        0
## 41         1        1
## 42         0        1
## 43         1        1
## 44         0        0
## 45         0        1
## 46         0        0
## 47         0        0
## 48         0        0
## 49         0        1
## 50         0        1
## 51         1        1
## 52         0        0
## 53         0        1
## 54         0        1
## 55         0        0
## 56         0        0
## 57         0        0
## 58         0        0
## 59         0        1
## 60         1        0
## 61         1        0
## 62         1        1
## 63         0        0
## 64         0        1
## 65         1        0
## 66         0        1
## 67         0        0
## 68         0        0
## 69         0        1
## 70         0        1
## 71         0        0
## 72         1        0
## 73         0        1
## 74         1        0
## 75         0        0
## 76         1        1
## 77         0        1
## 78         0        0
## 79         1        1
## 80         0        1
## 81         1        0
## 82         1        1
## 83         1        1
## 84         0        0
## 85         1        1
## 86         0        0
## 87         0        0
## 88         0        0
## 89         0        0
## 90         0        0
## 91         0        0
## 92         0        0
## 93         0        0
## 94         0        0
## 95         0        0
## 96         0        0
## 97         1        1
## 98         0        0
## 99         1        1
## 100        0        1
## 101        0        1
## 102        0        1
## 103        0        0
## 104        0        1
## 105        1        1
## 106        0        0
## 107        0        0
## 108        0        1
## 109        0        1
## 110        1        1
## 111        1        0
## 112        1        1
## 113        1        0
## 114        1        1
## 115        1        1
## 116        0        0
## 117        1        1
## 118        0        0
## 119        0        1
## 120        0        0
## 121        0        0
## 122        1        1
## 123        0        1
## 124        0        0
## 125        0        0
## 126        0        0
## 127        0        0
## 128        0        0
## 129        1        1
## 130        0        0
## 131        0        0
## 132        1        0
## 133        0        0
## 134        0        0
## 135        0        1
## 136        1        0
## 137        0        0
## 138        1        1
## 139        0        0
## 140        0        0
## 141        0        0
## 142        1        0
## 143        0        1
## 144        0        0
## 145        1        0
## 146        0        1
## 147        0        0
## 148        1        0
## 149        0        1
## 150        1        1
## 151        1        1
## 152        1        1
## 153        0        0
## 154        0        0
## 155        0        1
## 156        0        0
## 157        0        0
## 158        0        0
## 159        0        1
## 160        1        1
## 161        0        0
## 162        0        0
## 163        1        1
## 164        0        0
## 165        0        1
## 166        0        0
## 167        1        0
## 168        0        0
## 169        1        1
## 170        0        1
## 171        1        0
## 172        1        0
## 173        0        1
## 174        0        0
## 175        0        0
## 176        1        1
## 177        0        1
## 178        0        0
## 179        0        1
## 180        0        0
## 181        1        1
## 182        0        0
## 183        0        0
## 184        0        1
## 185        1        1
## 186        0        0
## 187        0        0
## 188        0        0
## 189        0        1
## 190        1        0
## 191        0        0
## 192        0        1
## 193        0        0
## 194        0        0
## 195        0        0
## 196        0        0
## 197        0        0
## 198        0        1
## 199        0        1
## 200        1        1
## 201        0        1
## 202        0        1
## 203        0        0
## 204        0        0
## 205        0        0
## 206        0        1
## 207        0        1
## 208        0        0
## 209        0        0
## 210        0        0
## 211        0        0
## 212        1        0
## 213        0        0
## 214        1        0
## 215        1        0
## 216        0        0
## 217        0        0
## 218        0        0
## 219        0        0
## 220        0        0
## 221        0        0
## 222        0        0
## 223        0        1
## 224        0        0
## 225        1        1
## 226        1        0
## 227        0        0
## 228        1        0
## 229        0        0
## 230        0        0
## 231        1        1
## 232        0        0
## 233        0        1
## 234        0        1
## 235        0        1
## 236        1        1
## 237        0        0
## 238        0        0
## 239        0        0
## 240        0        0
## 241        0        0
## 242        0        0
## 243        1        0
## 244        0        0
## 245        0        0
## 246        0        1
## 247        0        0
## 248        0        0
## 249        0        0
## 250        0        0
## 251        1        0
## 252        0        1
## 253        0        0
## 254        0        0
## 255        0        0
## 256        0        0
## 
## $nunitcov
## [1] 2
## 
## $unitcov
##     sagebrush aspect
## 1         9.0      W
## 2        18.0      S
## 3         8.4      W
## 4         3.2      E
## 5        12.0      S
## 6         7.8      S
## 7         5.4      N
## 8        10.0      N
## 9        12.0      S
## 10       12.0      S
## 11        0.6      E
## 12        7.6      N
## 13        4.2      S
## 14       12.0      S
## 15        8.2      S
## 16        4.0      E
## 17       10.0      W
## 18        4.0      W
## 19        3.4      E
## 20        6.4      W
## 21        4.0      W
## 22        7.8      W
## 23       10.0      W
## 24       10.0      W
## 25        3.8      N
## 26        1.2      S
## 27        2.0      W
## 28        5.8      S
## 29        7.4      N
## 30        7.2      S
## 31        4.0      S
## 32       18.0      W
## 33        1.6      S
## 34        3.4      W
## 35        0.8      N
## 36        5.9      N
## 37        0.2      S
## 38       12.0      S
## 39        9.0      S
## 40        2.0      W
## 41        9.2      N
## 42        6.2      W
## 43        0.2      W
## 44        7.4      S
## 45        3.2      E
## 46        2.2      W
## 47        9.6      E
## 48       13.0      W
## 49       11.0      W
## 50        2.6      W
## 51        4.8      W
## 52        6.6      W
## 53       16.0      W
## 54       12.0      W
## 55        3.0      S
## 56        0.7      W
## 57        3.0      W
## 58        2.6      W
## 59        5.4      E
## 60        5.6      S
## 61        3.8      W
## 62        0.0      W
## 63        0.4      E
## 64        1.4      S
## 65        0.4      E
## 66        4.8      W
## 67        5.6      S
## 68        0.2      S
## 69        3.9      W
## 70       10.0      W
## 71        4.2      W
## 72        0.2      S
## 73        0.2      W
## 74        5.0      S
## 75        5.4      W
## 76        3.4      N
## 77       10.0      W
## 78        0.4      W
## 79        4.8      S
## 80        7.2      N
## 81        6.6      W
## 82        1.6      W
## 83        4.2      N
## 84        5.0      S
## 85        3.4      S
## 86        0.8      W
## 87        0.8      S
## 88        8.0      N
## 89        0.2      N
## 90       10.0      W
## 91        0.2      W
## 92        4.2      N
## 93        7.0      N
## 94        2.4      N
## 95       10.0      W
## 96        7.8      W
## 97       12.0      N
## 98        3.2      W
## 99        1.0      S
## 100       5.4      W
## 101      11.0      W
## 102       0.8      W
## 103       3.6      E
## 104       5.4      S
## 105       9.0      W
## 106       2.0      W
## 107       0.6      W
## 108       4.2      W
## 109       1.8      W
## 110       4.4      W
## 111       0.2      N
## 112       0.2      S
## 113       3.8      W
## 114       0.2      W
## 115       0.4      W
## 116       7.2      W
## 117       9.4      W
## 118       0.2      W
## 119       4.0      W
## 120       3.6      E
## 121       2.4      W
## 122       5.0      W
## 123       1.4      W
## 124       8.2      W
## 125       7.4      E
## 126       2.0      W
## 127       0.4      W
## 128       4.4      W
## 129       0.2      N
## 130       3.6      W
## 131       2.4      W
## 132      10.0      W
## 133       8.4      W
## 134       0.0      S
## 135       4.0      W
## 136       4.8      S
## 137       1.4      W
## 138       0.0      W
## 139       2.4      N
## 140       3.4      W
## 141       3.0      W
## 142       5.0      W
## 143       0.0      E
## 144       8.4      W
## 145       7.6      W
## 146       0.2      W
## 147       2.8      W
## 148       3.4      W
## 149       2.4      S
## 150       1.0      E
## 151       2.2      W
## 152       0.4      W
## 153       1.6      E
## 154       7.6      W
## 155       0.4      W
## 156       0.6      W
## 157       5.6      W
## 158      14.0      W
## 159       3.6      W
## 160       2.2      W
## 161       6.6      W
## 162      11.0      S
## 163       3.0      E
## 164       3.4      W
## 165      12.0      W
## 166      10.0      S
## 167       3.8      N
## 168       0.4      W
## 169      12.0      N
## 170       1.0      W
## 171       0.4      W
## 172       1.2      W
## 173       6.0      W
## 174       6.2      N
## 175       3.0      S
## 176       0.4      W
## 177       2.0      W
## 178       1.2      E
## 179       0.0      W
## 180       0.4      W
## 181       0.2      W
## 182      10.0      S
## 183       7.2      W
## 184       6.2      W
## 185       0.2      W
## 186       1.8      W
## 187       7.2      S
## 188       4.8      E
## 189       3.2      N
## 190       3.4      W
## 191       0.2      E
## 192       4.0      W
## 193       4.0      W
## 194       6.0      S
## 195       0.6      W
## 196       1.0      W
## 197       2.0      W
## 198       0.2      W
## 199       2.6      W
## 200       2.8      W
## 201       6.1      E
## 202       3.0      W
## 203       6.2      W
## 204       4.0      W
## 205       6.0      W
## 206       0.8      N
## 207       0.2      W
## 208       1.8      W
## 209       8.0      W
## 210       0.2      W
## 211       2.2      E
## 212       6.6      W
## 213       2.6      S
## 214       6.6      E
## 215       0.0      W
## 216       1.0      W
## 217       0.4      W
## 218       9.2      N
## 219       3.2      W
## 220       1.4      W
## 221       6.2      W
## 222       6.0      E
## 223       6.4      W
## 224       1.0      W
## 225       0.2      N
## 226       1.0      W
## 227       0.0      W
## 228       3.2      W
## 229       0.2      E
## 230       2.0      N
## 231       1.4      E
## 232       0.0      W
## 233       0.0      W
## 234       0.0      W
## 235       1.4      S
## 236       0.2      E
## 237       0.0      W
## 238       0.6      S
## 239       0.0      S
## 240       2.4      W
## 241       1.6      E
## 242       2.2      S
## 243       1.1      W
## 244       3.2      W
## 245       0.0      W
## 246       1.8      E
## 247       4.2      E
## 248       0.8      W
## 249       5.2      E
## 250       0.2      E
## 251       0.0      W
## 252       6.4      W
## 253       3.4      W
## 254       1.8      W
## 255       0.4      W
## 256       0.4      W
## 
## $nsurvcov
## [1] 1
## 
## $survcov
##     SURVEY
## 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
## 19       1
## 20       1
## 21       1
## 22       1
## 23       1
## 24       1
## 25       1
## 26       1
## 27       1
## 28       1
## 29       1
## 30       1
## 31       1
## 32       1
## 33       1
## 34       1
## 35       1
## 36       1
## 37       1
## 38       1
## 39       1
## 40       1
## 41       1
## 42       1
## 43       1
## 44       1
## 45       1
## 46       1
## 47       1
## 48       1
## 49       1
## 50       1
## 51       1
## 52       1
## 53       1
## 54       1
## 55       1
## 56       1
## 57       1
## 58       1
## 59       1
## 60       1
## 61       1
## 62       1
## 63       1
## 64       1
## 65       1
## 66       1
## 67       1
## 68       1
## 69       1
## 70       1
## 71       1
## 72       1
## 73       1
## 74       1
## 75       1
## 76       1
## 77       1
## 78       1
## 79       1
## 80       1
## 81       1
## 82       1
## 83       1
## 84       1
## 85       1
## 86       1
## 87       1
## 88       1
## 89       1
## 90       1
## 91       1
## 92       1
## 93       1
## 94       1
## 95       1
## 96       1
## 97       1
## 98       1
## 99       1
## 100      1
## 101      1
## 102      1
## 103      1
## 104      1
## 105      1
## 106      1
## 107      1
## 108      1
## 109      1
## 110      1
## 111      1
## 112      1
## 113      1
## 114      1
## 115      1
## 116      1
## 117      1
## 118      1
## 119      1
## 120      1
## 121      1
## 122      1
## 123      1
## 124      1
## 125      1
## 126      1
## 127      1
## 128      1
## 129      1
## 130      1
## 131      1
## 132      1
## 133      1
## 134      1
## 135      1
## 136      1
## 137      1
## 138      1
## 139      1
## 140      1
## 141      1
## 142      1
## 143      1
## 144      1
## 145      1
## 146      1
## 147      1
## 148      1
## 149      1
## 150      1
## 151      1
## 152      1
## 153      1
## 154      1
## 155      1
## 156      1
## 157      1
## 158      1
## 159      1
## 160      1
## 161      1
## 162      1
## 163      1
## 164      1
## 165      1
## 166      1
## 167      1
## 168      1
## 169      1
## 170      1
## 171      1
## 172      1
## 173      1
## 174      1
## 175      1
## 176      1
## 177      1
## 178      1
## 179      1
## 180      1
## 181      1
## 182      1
## 183      1
## 184      1
## 185      1
## 186      1
## 187      1
## 188      1
## 189      1
## 190      1
## 191      1
## 192      1
## 193      1
## 194      1
## 195      1
## 196      1
## 197      1
## 198      1
## 199      1
## 200      1
## 201      1
## 202      1
## 203      1
## 204      1
## 205      1
## 206      1
## 207      1
## 208      1
## 209      1
## 210      1
## 211      1
## 212      1
## 213      1
## 214      1
## 215      1
## 216      1
## 217      1
## 218      1
## 219      1
## 220      1
## 221      1
## 222      1
## 223      1
## 224      1
## 225      1
## 226      1
## 227      1
## 228      1
## 229      1
## 230      1
## 231      1
## 232      1
## 233      1
## 234      1
## 235      1
## 236      1
## 237      1
## 238      1
## 239      1
## 240      1
## 241      1
## 242      1
## 243      1
## 244      1
## 245      1
## 246      1
## 247      1
## 248      1
## 249      1
## 250      1
## 251      1
## 252      1
## 253      1
## 254      1
## 255      1
## 256      1
## 257      2
## 258      2
## 259      2
## 260      2
## 261      2
## 262      2
## 263      2
## 264      2
## 265      2
## 266      2
## 267      2
## 268      2
## 269      2
## 270      2
## 271      2
## 272      2
## 273      2
## 274      2
## 275      2
## 276      2
## 277      2
## 278      2
## 279      2
## 280      2
## 281      2
## 282      2
## 283      2
## 284      2
## 285      2
## 286      2
## 287      2
## 288      2
## 289      2
## 290      2
## 291      2
## 292      2
## 293      2
## 294      2
## 295      2
## 296      2
## 297      2
## 298      2
## 299      2
## 300      2
## 301      2
## 302      2
## 303      2
## 304      2
## 305      2
## 306      2
## 307      2
## 308      2
## 309      2
## 310      2
## 311      2
## 312      2
## 313      2
## 314      2
## 315      2
## 316      2
## 317      2
## 318      2
## 319      2
## 320      2
## 321      2
## 322      2
## 323      2
## 324      2
## 325      2
## 326      2
## 327      2
## 328      2
## 329      2
## 330      2
## 331      2
## 332      2
## 333      2
## 334      2
## 335      2
## 336      2
## 337      2
## 338      2
## 339      2
## 340      2
## 341      2
## 342      2
## 343      2
## 344      2
## 345      2
## 346      2
## 347      2
## 348      2
## 349      2
## 350      2
## 351      2
## 352      2
## 353      2
## 354      2
## 355      2
## 356      2
## 357      2
## 358      2
## 359      2
## 360      2
## 361      2
## 362      2
## 363      2
## 364      2
## 365      2
## 366      2
## 367      2
## 368      2
## 369      2
## 370      2
## 371      2
## 372      2
## 373      2
## 374      2
## 375      2
## 376      2
## 377      2
## 378      2
## 379      2
## 380      2
## 381      2
## 382      2
## 383      2
## 384      2
## 385      2
## 386      2
## 387      2
## 388      2
## 389      2
## 390      2
## 391      2
## 392      2
## 393      2
## 394      2
## 395      2
## 396      2
## 397      2
## 398      2
## 399      2
## 400      2
## 401      2
## 402      2
## 403      2
## 404      2
## 405      2
## 406      2
## 407      2
## 408      2
## 409      2
## 410      2
## 411      2
## 412      2
## 413      2
## 414      2
## 415      2
## 416      2
## 417      2
## 418      2
## 419      2
## 420      2
## 421      2
## 422      2
## 423      2
## 424      2
## 425      2
## 426      2
## 427      2
## 428      2
## 429      2
## 430      2
## 431      2
## 432      2
## 433      2
## 434      2
## 435      2
## 436      2
## 437      2
## 438      2
## 439      2
## 440      2
## 441      2
## 442      2
## 443      2
## 444      2
## 445      2
## 446      2
## 447      2
## 448      2
## 449      2
## 450      2
## 451      2
## 452      2
## 453      2
## 454      2
## 455      2
## 456      2
## 457      2
## 458      2
## 459      2
## 460      2
## 461      2
## 462      2
## 463      2
## 464      2
## 465      2
## 466      2
## 467      2
## 468      2
## 469      2
## 470      2
## 471      2
## 472      2
## 473      2
## 474      2
## 475      2
## 476      2
## 477      2
## 478      2
## 479      2
## 480      2
## 481      2
## 482      2
## 483      2
## 484      2
## 485      2
## 486      2
## 487      2
## 488      2
## 489      2
## 490      2
## 491      2
## 492      2
## 493      2
## 494      2
## 495      2
## 496      2
## 497      2
## 498      2
## 499      2
## 500      2
## 501      2
## 502      2
## 503      2
## 504      2
## 505      2
## 506      2
## 507      2
## 508      2
## 509      2
## 510      2
## 511      2
## 512      2
## 
## $nsurveyseason
## [1] 2
## 
## $title
## [1] "Pronghorn SSSS"
## 
## $unitnames
##   [1] "unit1"   "unit2"   "unit3"   "unit4"   "unit5"   "unit6"   "unit7"  
##   [8] "unit8"   "unit9"   "unit10"  "unit11"  "unit12"  "unit13"  "unit14" 
##  [15] "unit15"  "unit16"  "unit17"  "unit18"  "unit19"  "unit20"  "unit21" 
##  [22] "unit22"  "unit23"  "unit24"  "unit25"  "unit26"  "unit27"  "unit28" 
##  [29] "unit29"  "unit30"  "unit31"  "unit32"  "unit33"  "unit34"  "unit35" 
##  [36] "unit36"  "unit37"  "unit38"  "unit39"  "unit40"  "unit41"  "unit42" 
##  [43] "unit43"  "unit44"  "unit45"  "unit46"  "unit47"  "unit48"  "unit49" 
##  [50] "unit50"  "unit51"  "unit52"  "unit53"  "unit54"  "unit55"  "unit56" 
##  [57] "unit57"  "unit58"  "unit59"  "unit60"  "unit61"  "unit62"  "unit63" 
##  [64] "unit64"  "unit65"  "unit66"  "unit67"  "unit68"  "unit69"  "unit70" 
##  [71] "unit71"  "unit72"  "unit73"  "unit74"  "unit75"  "unit76"  "unit77" 
##  [78] "unit78"  "unit79"  "unit80"  "unit81"  "unit82"  "unit83"  "unit84" 
##  [85] "unit85"  "unit86"  "unit87"  "unit88"  "unit89"  "unit90"  "unit91" 
##  [92] "unit92"  "unit93"  "unit94"  "unit95"  "unit96"  "unit97"  "unit98" 
##  [99] "unit99"  "unit100" "unit101" "unit102" "unit103" "unit104" "unit105"
## [106] "unit106" "unit107" "unit108" "unit109" "unit110" "unit111" "unit112"
## [113] "unit113" "unit114" "unit115" "unit116" "unit117" "unit118" "unit119"
## [120] "unit120" "unit121" "unit122" "unit123" "unit124" "unit125" "unit126"
## [127] "unit127" "unit128" "unit129" "unit130" "unit131" "unit132" "unit133"
## [134] "unit134" "unit135" "unit136" "unit137" "unit138" "unit139" "unit140"
## [141] "unit141" "unit142" "unit143" "unit144" "unit145" "unit146" "unit147"
## [148] "unit148" "unit149" "unit150" "unit151" "unit152" "unit153" "unit154"
## [155] "unit155" "unit156" "unit157" "unit158" "unit159" "unit160" "unit161"
## [162] "unit162" "unit163" "unit164" "unit165" "unit166" "unit167" "unit168"
## [169] "unit169" "unit170" "unit171" "unit172" "unit173" "unit174" "unit175"
## [176] "unit176" "unit177" "unit178" "unit179" "unit180" "unit181" "unit182"
## [183] "unit183" "unit184" "unit185" "unit186" "unit187" "unit188" "unit189"
## [190] "unit190" "unit191" "unit192" "unit193" "unit194" "unit195" "unit196"
## [197] "unit197" "unit198" "unit199" "unit200" "unit201" "unit202" "unit203"
## [204] "unit204" "unit205" "unit206" "unit207" "unit208" "unit209" "unit210"
## [211] "unit211" "unit212" "unit213" "unit214" "unit215" "unit216" "unit217"
## [218] "unit218" "unit219" "unit220" "unit221" "unit222" "unit223" "unit224"
## [225] "unit225" "unit226" "unit227" "unit228" "unit229" "unit230" "unit231"
## [232] "unit232" "unit233" "unit234" "unit235" "unit236" "unit237" "unit238"
## [239] "unit239" "unit240" "unit241" "unit242" "unit243" "unit244" "unit245"
## [246] "unit246" "unit247" "unit248" "unit249" "unit250" "unit251" "unit252"
## [253] "unit253" "unit254" "unit255" "unit256"
## 
## $surveynames
## [1] "1-1" "1-2"
## 
## $paoname
## [1] "pres.pao"
## 
## $frq
##   [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 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
##  [71] 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
## [106] 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
## [141] 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
## [176] 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
## [211] 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
## [246] 1 1 1 1 1 1 1 1 1 1 1
## 
## attr(,"class")
## [1] "pao"
# define the list of models to fit
# Notice the commas between the column and the placement of the quotes
model.list.csv <- textConnection("
                                 p,               psi
                                 ~1,              ~1
                                 ~SURVEY,         ~1
                                 ~1,              ~aspect
                                 ~1,              ~sagebrush")

model.list <- read.csv(model.list.csv, header=TRUE, as.is=TRUE, strip.white=TRUE)
model.list
##         p        psi
## 1      ~1         ~1
## 2 ~SURVEY         ~1
## 3      ~1    ~aspect
## 4      ~1 ~sagebrush
# fit the models
model.fits <- plyr::alply(model.list, 1, function(x,detect.pao){
  cat("\n\n***** Starting ", unlist(x), "\n")
  fit <- RPresence::occMod(model=list(as.formula(paste("psi",x$psi)),
                                      as.formula(paste("p"  ,x$p  ))),
                           data=detect.pao,type="so")
  fit
},detect.pao=prong.pao)
## 
## 
## ***** Starting  ~1 ~1 
## PRESENCE Version 2.12.21.
## 
## 
## ***** Starting  ~SURVEY ~1 
## PRESENCE Version 2.12.21.
## 
## 
## ***** Starting  ~1 ~aspect 
## PRESENCE Version 2.12.21.
## 
## 
## ***** Starting  ~1 ~sagebrush 
## PRESENCE Version 2.12.21.
# Look the output from a specific model
check.model <- 1

names(model.fits[[check.model]])
##  [1] "modname"     "model"       "dmat"        "data"        "outfile"    
##  [6] "neg2loglike" "npar"        "aic"         "beta"        "real"       
## [11] "derived"     "gof"         "warnings"    "version"
model.fits[[check.model]]$beta
## $psi
##             est       se
## A1_psi 0.886474 0.331229
## 
## $psi.VC
##          [,1]
## [1,] 0.109713
## 
## $p
##             est       se
## B1_p1 -0.169076 0.193449
## 
## $p.VC
##          [,1]
## [1,] 0.037423
## 
## $VC
##           A1_psi     B1_p1
## A1_psi  0.109713 -0.048881
## B1_p1  -0.048881  0.037423
names(model.fits[[check.model]]$real)
## [1] "psi" "p"
model.fits[[check.model]]$real$psi[1:5,]
##                est         se lower_0.95 upper_0.95
## psi_unit1 0.708162 0.06845474  0.5590423  0.8228347
## psi_unit2 0.708162 0.06845474  0.5590423  0.8228347
## psi_unit3 0.708162 0.06845474  0.5590423  0.8228347
## psi_unit4 0.708162 0.06845474  0.5590423  0.8228347
## psi_unit5 0.708162 0.06845474  0.5590423  0.8228347
model.fits[[check.model]]$real$p[1:5,]
##                est         se lower_0.95 upper_0.95
## p1_unit1 0.4578314 0.04801857  0.3662748  0.5523276
## p1_unit2 0.4578314 0.04801857  0.3662748  0.5523276
## p1_unit3 0.4578314 0.04801857  0.3662748  0.5523276
## p1_unit4 0.4578314 0.04801857  0.3662748  0.5523276
## p1_unit5 0.4578314 0.04801857  0.3662748  0.5523276
names(model.fits[[check.model]]$derived)
## [1] "psi_c"
model.fits[[check.model]]$derived$psi_c[1:10,]
##              est        se lower_0.95 upper_0.95
## unit1  0.4163239 0.1166916  0.2177056  0.6464161
## unit2  1.0000000 0.0000000  1.0000000  1.0000000
## unit3  0.4163239 0.1166916  0.2177056  0.6464161
## unit4  0.4163239 0.1166916  0.2177056  0.6464161
## unit5  1.0000000 0.0000000  1.0000000  1.0000000
## unit6  1.0000000 0.0000000  1.0000000  1.0000000
## unit7  0.4163239 0.1166916  0.2177056  0.6464161
## unit8  1.0000000 0.0000000  1.0000000  1.0000000
## unit9  1.0000000 0.0000000  1.0000000  1.0000000
## unit10 1.0000000 0.0000000  1.0000000  1.0000000
tail(model.fits[[check.model]]$derived$psi_c)
##               est        se lower_0.95 upper_0.95
## unit251 1.0000000 0.0000000  1.0000000  1.0000000
## unit252 1.0000000 0.0000000  1.0000000  1.0000000
## unit253 0.4163239 0.1166916  0.2177056  0.6464161
## unit254 0.4163239 0.1166916  0.2177056  0.6464161
## unit255 0.4163239 0.1166916  0.2177056  0.6464161
## unit256 0.4163239 0.1166916  0.2177056  0.6464161
# Model averaging
aic.table <- RPresence::createAicTable(model.fits)
aic.table$table
##               Model      AIC   neg2ll npar warn.conv warn.VC   DAIC
## 2    psi()p(SURVEY) 635.9227 629.9227    3         0       0 0.0000
## 1          psi()p() 639.3553 635.3553    2         0       0 3.4326
## 4 psi(sagebrush)p() 640.5521 634.5521    3         0       0 4.6294
## 3    psi(aspect)p() 641.3945 631.3945    5         0       0 5.4718
##   modlike    wgt
## 2  1.0000 0.7444
## 1  0.1797 0.1338
## 4  0.0988 0.0735
## 3  0.0648 0.0483
names(aic.table)
## [1] "table"  "models" "ess"
# plot occupancy as a function of sagebrush density
psi.ma <- RPresence::modAvg(aic.table, param="psi")
head(psi.ma)
##                 est         se lower_0.95 upper_0.95
## psi_unit1 0.7017261 0.07033499  0.5490686  0.8196759
## psi_unit2 0.7081633 0.08302050  0.5247658  0.8420840
## psi_unit3 0.7013316 0.06979655  0.5499732  0.8185763
## psi_unit4 0.6904153 0.07821991  0.5211882  0.8204383
## psi_unit5 0.7048222 0.07620245  0.5380474  0.8303698
## psi_unit6 0.7021407 0.07220938  0.5450765  0.8226242
psi.ma$Site <- as.numeric(substring(row.names(psi.ma), 4+regexpr("unit",row.names(psi.ma), fixed=TRUE)))
plotdata <- data.frame(psi.ma, sagebrush = unit.cov$sagebrush)
head(plotdata)
##                 est         se lower_0.95 upper_0.95 Site sagebrush
## psi_unit1 0.7017261 0.07033499  0.5490686  0.8196759    1       9.0
## psi_unit2 0.7081633 0.08302050  0.5247658  0.8420840    2      18.0
## psi_unit3 0.7013316 0.06979655  0.5499732  0.8185763    3       8.4
## psi_unit4 0.6904153 0.07821991  0.5211882  0.8204383    4       3.2
## psi_unit5 0.7048222 0.07620245  0.5380474  0.8303698    5      12.0
## psi_unit6 0.7021407 0.07220938  0.5450765  0.8226242    6       7.8
ggplot(data=plotdata, aes(x=sagebrush, y=est))+
  ggtitle("Occupancy as a function of sagebrush density")+
  geom_point()+
  geom_ribbon(aes(ymin=lower_0.95, ymax=upper_0.95), alpha=0.2)+
  ylim(0,1)+
  ylab("Estimated occupancy")

# Plot occupancy as a function of aspect
plotdata <- data.frame(psi.ma, aspect = unit.cov$aspect)
head(plotdata)
##                 est         se lower_0.95 upper_0.95 Site aspect
## psi_unit1 0.7017261 0.07033499  0.5490686  0.8196759    1      W
## psi_unit2 0.7081633 0.08302050  0.5247658  0.8420840    2      S
## psi_unit3 0.7013316 0.06979655  0.5499732  0.8185763    3      W
## psi_unit4 0.6904153 0.07821991  0.5211882  0.8204383    4      E
## psi_unit5 0.7048222 0.07620245  0.5380474  0.8303698    5      S
## psi_unit6 0.7021407 0.07220938  0.5450765  0.8226242    6      S
ggplot(data=plotdata, aes(x=aspect, y=est))+
  ggtitle("Occupancy as a function of aspect")+
  geom_point()+
  geom_errorbar(aes(ymin=lower_0.95, ymax=upper_0.95), alpha=0.2)+
  ylim(0,1)+
  ylab("Estimated occupancy")