# Blue Ridge Salamander - with missing data
# Notice how the data are read in with the missing values.
# Single Species Single Season Occupancy
# Fitting a single model
# 2018-08-15 Code contributed by Carl James Schwarz (cschwarz.stat.sfu.cs@gmail.com)
library(readxl)
library(RPresence)
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 <- readxl::read_excel("../salamander.xls",
sheet="CompleteData",
na="-",
col_names=FALSE) # notice no column names in row 1 of data file.
head(input.data)
## # A tibble: 6 x 5
## X__1 X__2 X__3 X__4 X__5
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0 0 0 1 1
## 2 0 1 0 0 0
## 3 0 1 0 0 0
## 4 1 1 1 1 0
## 5 0 0 1 0 0
## 6 0 0 1 0 0
# Extract the history records
input.history <- input.data[, 1:5] # the history extracted
head(input.history)
## # A tibble: 6 x 5
## X__1 X__2 X__3 X__4 X__5
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0 0 0 1 1
## 2 0 1 0 0 0
## 3 0 1 0 0 0
## 4 1 1 1 1 0
## 5 0 0 1 0 0
## 6 0 0 1 0 0
# do some basic checks on your data
# e.g. check number of sites; number of visits etc
nrow(input.history)
## [1] 39
ncol(input.history)
## [1] 5
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
# Create the *.pao file
salamander.pao <- RPresence::createPao(input.history,
title='Salamander SSSS')
salamander.pao
## $nunits
## [1] 39
##
## $nsurveys
## [1] 5
##
## $nseasons
## [1] 1
##
## $nmethods
## [1] 1
##
## $det.data
## # A tibble: 39 x 5
## X__1 X__2 X__3 X__4 X__5
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0 0 0 1 1
## 2 0 1 0 0 0
## 3 0 1 0 0 0
## 4 1 1 1 1 0
## 5 0 0 1 0 0
## 6 0 0 1 0 0
## 7 0 0 1 0 0
## 8 0 0 1 0 0
## 9 0 0 1 0 0
## 10 1 0 0 0 0
## # ... with 29 more rows
##
## $nunitcov
## [1] 1
##
## $unitcov
## TEMP
## 1 1
## 2 2
## 3 3
## 4 4
## 5 5
## 6 6
## 7 7
## 8 8
## 9 9
## 10 10
## 11 11
## 12 12
## 13 13
## 14 14
## 15 15
## 16 16
## 17 17
## 18 18
## 19 19
## 20 20
## 21 21
## 22 22
## 23 23
## 24 24
## 25 25
## 26 26
## 27 27
## 28 28
## 29 29
## 30 30
## 31 31
## 32 32
## 33 33
## 34 34
## 35 35
## 36 36
## 37 37
## 38 38
## 39 39
##
## $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 2
## 41 2
## 42 2
## 43 2
## 44 2
## 45 2
## 46 2
## 47 2
## 48 2
## 49 2
## 50 2
## 51 2
## 52 2
## 53 2
## 54 2
## 55 2
## 56 2
## 57 2
## 58 2
## 59 2
## 60 2
## 61 2
## 62 2
## 63 2
## 64 2
## 65 2
## 66 2
## 67 2
## 68 2
## 69 2
## 70 2
## 71 2
## 72 2
## 73 2
## 74 2
## 75 2
## 76 2
## 77 2
## 78 2
## 79 3
## 80 3
## 81 3
## 82 3
## 83 3
## 84 3
## 85 3
## 86 3
## 87 3
## 88 3
## 89 3
## 90 3
## 91 3
## 92 3
## 93 3
## 94 3
## 95 3
## 96 3
## 97 3
## 98 3
## 99 3
## 100 3
## 101 3
## 102 3
## 103 3
## 104 3
## 105 3
## 106 3
## 107 3
## 108 3
## 109 3
## 110 3
## 111 3
## 112 3
## 113 3
## 114 3
## 115 3
## 116 3
## 117 3
## 118 4
## 119 4
## 120 4
## 121 4
## 122 4
## 123 4
## 124 4
## 125 4
## 126 4
## 127 4
## 128 4
## 129 4
## 130 4
## 131 4
## 132 4
## 133 4
## 134 4
## 135 4
## 136 4
## 137 4
## 138 4
## 139 4
## 140 4
## 141 4
## 142 4
## 143 4
## 144 4
## 145 4
## 146 4
## 147 4
## 148 4
## 149 4
## 150 4
## 151 4
## 152 4
## 153 4
## 154 4
## 155 4
## 156 4
## 157 5
## 158 5
## 159 5
## 160 5
## 161 5
## 162 5
## 163 5
## 164 5
## 165 5
## 166 5
## 167 5
## 168 5
## 169 5
## 170 5
## 171 5
## 172 5
## 173 5
## 174 5
## 175 5
## 176 5
## 177 5
## 178 5
## 179 5
## 180 5
## 181 5
## 182 5
## 183 5
## 184 5
## 185 5
## 186 5
## 187 5
## 188 5
## 189 5
## 190 5
## 191 5
## 192 5
## 193 5
## 194 5
## 195 5
##
## $nsurveyseason
## [1] 5
##
## $title
## [1] "Salamander 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"
##
## $surveynames
## [1] "1-1" "1-2" "1-3" "1-4" "1-5"
##
## $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
##
## attr(,"class")
## [1] "pao"
# Fit a model
# Note that formula DO NOT HAVE AN = SIGN
mod.fit <- RPresence::occMod(model=list(psi~1, p~1),
type="so",
data=salamander.pao)
summary(mod.fit)
## Model name=psi()p()
## AIC=165.7586
## -2*log-likelihood=161.7586
## num. par=2
# Look the objects returned in more details
names(mod.fit)
## [1] "modname" "model" "dmat" "data" "outfile"
## [6] "neg2loglike" "npar" "aic" "beta" "real"
## [11] "derived" "gof" "warnings" "version"
# look at estimated occupancy probability. RPresence gives for EACH site in case it depends on covariates
mod.fit$beta$psi # on the logit scale
## est se
## A1_psi 0.383108 0.508589
mod.fit$real$psi # on the regular 0-1 scale for each site
## est se lower_0.95 upper_0.95
## psi_unit1 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit2 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit3 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit4 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit5 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit6 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit7 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit8 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit9 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit10 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit11 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit12 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit13 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit14 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit15 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit16 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit17 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit18 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit19 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit20 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit21 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit22 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit23 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit24 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit25 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit26 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit27 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit28 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit29 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit30 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit31 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit32 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit33 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit34 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit35 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit36 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit37 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit38 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit39 0.5946225 0.1225937 0.3512137 0.7989789
mod.fit$real$psi[1:5,]
## est se lower_0.95 upper_0.95
## psi_unit1 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit2 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit3 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit4 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit5 0.5946225 0.1225937 0.3512137 0.7989789
# look at the estimated probability of detection. It gives an estimate for every site at very visit
mod.fit$real$p[1:5,]
## est se lower_0.95 upper_0.95
## p1_unit1 0.258729 0.0576996 0.1621604 0.3862912
## p1_unit2 0.258729 0.0576996 0.1621604 0.3862912
## p1_unit3 0.258729 0.0576996 0.1621604 0.3862912
## p1_unit4 0.258729 0.0576996 0.1621604 0.3862912
## p1_unit5 0.258729 0.0576996 0.1621604 0.3862912
# extract the detection probabilities for each visit. The row names have the unit names
mod.fit.p <- mod.fit$real$p[grepl("unit1$", row.names(mod.fit$real$p)),]
mod.fit.p
## est se lower_0.95 upper_0.95
## p1_unit1 0.258729 0.0576996 0.1621604 0.3862912
## p2_unit1 0.258729 0.0576996 0.1621604 0.3862912
## p3_unit1 0.258729 0.0576996 0.1621604 0.3862912
## p4_unit1 0.258729 0.0576996 0.1621604 0.3862912
## p5_unit1 0.258729 0.0576996 0.1621604 0.3862912
# Look at the posterior probability of detection
names(mod.fit$derived)
## [1] "psi_c"
mod.fit$derived$psi_c
## est se lower_0.95 upper_0.95
## unit1 1.0000000 0.0000000 1.00000000 1.0000
## unit2 1.0000000 0.0000000 1.00000000 1.0000
## unit3 1.0000000 0.0000000 1.00000000 1.0000
## unit4 1.0000000 0.0000000 1.00000000 1.0000
## unit5 1.0000000 0.0000000 1.00000000 1.0000
## unit6 1.0000000 0.0000000 1.00000000 1.0000
## unit7 1.0000000 0.0000000 1.00000000 1.0000
## unit8 1.0000000 0.0000000 1.00000000 1.0000
## unit9 1.0000000 0.0000000 1.00000000 1.0000
## unit10 1.0000000 0.0000000 1.00000000 1.0000
## unit11 1.0000000 0.0000000 1.00000000 1.0000
## unit12 1.0000000 0.0000000 1.00000000 1.0000
## unit13 1.0000000 0.0000000 1.00000000 1.0000
## unit14 1.0000000 0.0000000 1.00000000 1.0000
## unit15 0.2471562 0.1471721 0.06512707 0.6074
## unit16 0.2471562 0.1471721 0.06512707 0.6074
## unit17 0.2471562 0.1471721 0.06512707 0.6074
## unit18 0.2471562 0.1471721 0.06512707 0.6074
## unit19 0.2471562 0.1471721 0.06512707 0.6074
## unit20 0.2471562 0.1471721 0.06512707 0.6074
## unit21 0.2471562 0.1471721 0.06512707 0.6074
## unit22 0.2471562 0.1471721 0.06512707 0.6074
## unit23 0.2471562 0.1471721 0.06512707 0.6074
## unit24 0.2471562 0.1471721 0.06512707 0.6074
## unit25 0.2471562 0.1471721 0.06512707 0.6074
## unit26 0.2471562 0.1471721 0.06512707 0.6074
## unit27 0.2471562 0.1471721 0.06512707 0.6074
## unit28 0.2471562 0.1471721 0.06512707 0.6074
## unit29 0.2471562 0.1471721 0.06512707 0.6074
## unit30 0.2471562 0.1471721 0.06512707 0.6074
## unit31 0.2471562 0.1471721 0.06512707 0.6074
## unit32 0.2471562 0.1471721 0.06512707 0.6074
## unit33 0.2471562 0.1471721 0.06512707 0.6074
## unit34 0.2471562 0.1471721 0.06512707 0.6074
## unit35 0.2471562 0.1471721 0.06512707 0.6074
## unit36 1.0000000 0.0000000 1.00000000 1.0000
## unit37 1.0000000 0.0000000 1.00000000 1.0000
## unit38 1.0000000 0.0000000 1.00000000 1.0000
## unit39 1.0000000 0.0000000 1.00000000 1.0000
# alternatively
RPresence::print_one_site_estimates(mod.fit, site = 1)
## psi()p()
## est se lower_0.95 upper_0.95
## psi_psi_unit1 0.5946225 0.1225937 0.3512137 0.7989789
## p_p1_unit1 0.2587290 0.0576996 0.1621604 0.3862912
fitted(mod.fit, param="psi")[1:5,]
## est se lower_0.95 upper_0.95
## psi_unit1 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit2 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit3 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit4 0.5946225 0.1225937 0.3512137 0.7989789
## psi_unit5 0.5946225 0.1225937 0.3512137 0.7989789
fitted(mod.fit, param="p")[1:5,]
## est se lower_0.95 upper_0.95
## p1_unit1 0.258729 0.0576996 0.1621604 0.3862912
## p1_unit2 0.258729 0.0576996 0.1621604 0.3862912
## p1_unit3 0.258729 0.0576996 0.1621604 0.3862912
## p1_unit4 0.258729 0.0576996 0.1621604 0.3862912
## p1_unit5 0.258729 0.0576996 0.1621604 0.3862912