<jupyter_start><jupyter_text>Correcting the map counts to account for diving seals and higher on-the-ground countsThis file includes the code that inflates the map counts using detection rate-estimating functions fitted with data from Erebus Bay, where ground counts could be conducted.    In a nutshell, we use these models to estimate the detection rate, and then inflate the map counts according to this rate.<jupyter_code>## Clear memory
rm(list=ls())
gc()<jupyter_output><empty_output><jupyter_text>Loading libraries, functions and data<jupyter_code>libs<-c("ggplot2","plyr","dplyr")
lapply(libs, require, character.only = TRUE)
pathToLocalGit<-"/home/ubuntu/Workspace/ContinentalWESEestimates/"

source(paste0(pathToLocalGit,"scripts/countSealsFromTags_functions.R"))

## Load the data - the data were generated from notebook: "Count Seals From Tags.ipynb"
load(file=paste0(pathToLocalGit,"data/countsCorrectedByHour.Rdata"))
head(corrcounts)

## Print and check that this is the correct version of the data
estByRegionCorr<-as.data.frame(corrcounts %>% group_by(region) %>% dplyr::summarize(
    corrLclNumSeals=sum(corrLclNumSeals),corrNumSeals=sum(predicted),corrUclNumSeals=sum(corrUclNumSeals))
)
estByRegionCorr<-rbind(estByRegionCorr,
    data.frame(region="Total",
        corrLclNumSeals=sum(estByRegionCorr$corrLclNumSeals),corrNumSeals=sum(estByRegionCorr$corrNumSeals),
               corrUclNumSeals=sum(estByRegionCorr$corrUclNumSeals)))
print(estByRegionCorr)<jupyter_output><empty_output><jupyter_text>Now we use the handy function to request the detection rates under both models. Note that we can specify the "weight" of island counts. This is the proportion of map locations that resemble the islet haul-out locations in Erebus Bay (i.e., Turk's Head-Tryggve and Hutton Cliffs). According to Michelle and David, the vast majority of counts came from such locations. Here we are conservative and assume 95% of locations are islets.<jupyter_code>corrcounts$scaledNumTags<-0

adjRates<-predictDetRates(pathToGit=pathToLocalGit,dat=corrcounts,keyFieldName="regionMapId",islandWeight=0.95)
head(adjRates)<jupyter_output><empty_output><jupyter_text>Finally, we add the detection rates to our data.frame of map counts, inflate, and summarize the results.    Since detectionRate = count-in-map/count-on-the-ground, and we want count-on-the-ground, then:Count-on-the-ground = count-in-map/detectionRate<jupyter_code>countdf<-merge(corrcounts,adjRates,by="regionMapId")
nrow(countdf)==2*nrow(corrcounts)

countdf$mdlColEstimate<-round(countdf$predicted/countdf$wgtPredColRate)
countdf$mdlColUpper<-round(countdf$corrUclNumSeals/countdf$wgtPredColRate)
countdf$mdlColLower<-round(countdf$corrLclNumSeals/countdf$wgtPredColRate)

countdf$mdlIslEstimate<-round(countdf$predicted/countdf$wgtPredIslRate)
countdf$mdlIslUpper<-round(countdf$corrUclNumSeals/countdf$wgtPredIslRate)
countdf$mdlIslLower<-round(countdf$corrLclNumSeals/countdf$wgtPredIslRate)
head(countdf)
## Using the colony model for 2010:
print("Colony model with reference year 2010")
countdf10<-subset(countdf,Year=="2010")
estByRegionCol<-as.data.frame(countdf10[,c("region","mdlColLower","mdlColEstimate","mdlColUpper")] %>% group_by(region) %>% dplyr::summarize(Lower=round(sum(mdlColLower)),Estimate=round(sum(mdlColEstimate)),Upper=round(sum(mdlColUpper))))
estByRegionCol<-rbind(estByRegionCol,data.frame(region="Total",Lower=round(sum(countdf10$mdlColLower)),Estimate=round(sum(countdf10$mdlColEstimate)),Upper=round(sum(countdf10$mdlColUpper))))
print(estByRegionCol)
cat("\n")
## Using the colony model for 2011:
print("Colony model with reference year 2011")
countdf11<-subset(countdf,Year=="2011")
estByRegionCol<-as.data.frame(countdf11[,c("region","mdlColLower","mdlColEstimate","mdlColUpper")] %>% group_by(region) %>% dplyr::summarize(Lower=round(sum(mdlColLower)),Estimate=round(sum(mdlColEstimate)),Upper=round(sum(mdlColUpper))))
estByRegionCol<-rbind(estByRegionCol,data.frame(region="Total",Lower=round(sum(countdf11$mdlColLower)),Estimate=round(sum(countdf11$mdlColEstimate)),Upper=round(sum(countdf11$mdlColUpper))))
print(estByRegionCol)
## Using the islet/mainland model for 2010:
print("Islet/mainland model with reference year 2010")
countdf10<-subset(countdf,Year=="2010")
estByRegionIsl<-as.data.frame(countdf10[,c("region","mdlIslLower","mdlIslEstimate","mdlIslUpper")] %>% group_by(region) %>% dplyr::summarize(Lower=round(sum(mdlIslLower)),Estimate=round(sum(mdlIslEstimate)),Upper=round(sum(mdlIslUpper))))
estByRegionIsl<-rbind(estByRegionIsl,data.frame(region="Total",Lower=round(sum(countdf10$mdlIslLower)),Estimate=round(sum(countdf10$mdlIslEstimate)),Upper=round(sum(countdf10$mdlIslUpper))))
print(estByRegionIsl)
cat("\n")
## Using the islet/mainland model for 2011:
print("Islet/mainland model with reference year 2011")
countdf11<-subset(countdf,Year=="2011")
estByRegionIsl<-as.data.frame(countdf11[,c("region","mdlIslLower","mdlIslEstimate","mdlIslUpper")] %>% group_by(region) %>% dplyr::summarize(Lower=round(sum(mdlIslLower)),Estimate=round(sum(mdlIslEstimate)),Upper=round(sum(mdlIslUpper))))
estByRegionIsl<-rbind(estByRegionIsl,data.frame(region="Total",Lower=round(sum(countdf11$mdlIslLower)),Estimate=round(sum(countdf11$mdlIslEstimate)),Upper=round(sum(countdf11$mdlIslUpper))))
print(estByRegionIsl)
head(countdf)
save(countdf, file=paste0(pathToLocalGit,"data/FinalWESEcounts.RData"))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1329416085200147, 'fraction_numerical': 0.018178479618068306, 'mean_word_length': 10.997797356828194, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '309158', 'license': 'MIT', 'n_tokens_mistral': 2023, 'n_tokens_neox': 1875, 'n_words': 292, 'path': 'notebooks/2.Correct_countsFromGroundCounts.ipynb', 'repo_name': 'pointblue/ContinentalWESEestimates', 'size': '5446'}