library(data.table) load("data/projected.RData") load("data/historical.RData") # =========== # = Species = # =========== u_prey <- as.factor(c("Ammodytes dubius", "Alosa pseudoharengus", "Scomber scombrus", "Clupea harengus", "Homarus americanus", "Loligo pealeii")) prey.common <- c("Sandlance", "Alewife", "Mackerel", "Herring", "Lobster", "Squid") u_pisc <- as.factor(c("Gadus morhua", "Merluccius bilinearis", "Lophius americanus", "Hemitripterus americanus", "Urophycis tenuis", "Squalus acanthias")) pisc.common <- c("Cod", "Silver Hake", "Goosefish", "Sea Raven", "White Hake", "Spiny Dogfish") # ===================================== # = Calculate overlap as sum of p1*p2 / sum( p1) = # ===================================== focal_spp_proj <- projected[,CJ(spp=unique(spp[spp %in% u_prey]),focal=unique(spp[spp%in%u_pisc]))][spp!=focal] proj_spp_range <- projected[,list(geo_range=mean(preds1, na.rm=T)), by=list(spp, scen)] over.cod <- projected[,j={ dt <- .SD foc_pred <- dt[spp == "Gadus morhua"]$preds1 # predicted pres/abs prey_obs <- dt[spp%in%u_prey,j={ sub <- .SD prey_occ <- sub$preds1 both <- foc_pred * prey_occ over.frac <- sum(both)/sum(prey_occ) list(over.frac=over.frac, focal="Gadus morhua") }, by=list(spp)] }, by=list(scen)] over.dog <- projected[,j={ dt <- .SD foc_pred <- dt[spp == "Squalus acanthias"]$preds1 # predicted pres/abs prey_obs <- dt[spp%in%u_prey,j={ sub <- .SD prey_occ <- sub$preds1 both <- foc_pred * prey_occ over.frac <- sum(both)/sum(prey_occ) list(over.frac=over.frac, focal="Squalus acanthias") }, by=list(spp)] }, by=list(scen)] over.shake <- projected[,j={ dt <- .SD foc_pred <- dt[spp == "Merluccius bilinearis"]$preds1 # predicted pres/abs prey_obs <- dt[spp%in%u_prey,j={ sub <- .SD prey_occ <- sub$preds1 both <- foc_pred * prey_occ over.frac <- sum(both)/sum(prey_occ) list(over.frac=over.frac, focal="Merluccius bilinearis") }, by=list(spp)] }, by=list(scen)] over.whake <- projected[,j={ dt <- .SD foc_pred <- dt[spp == "Urophycis tenuis"]$preds1 # predicted pres/abs prey_obs <- dt[spp%in%u_prey,j={ sub <- .SD prey_occ <- sub$preds1 both <- foc_pred * prey_occ over.frac <- sum(both)/sum(prey_occ) list(over.frac=over.frac, focal="Urophycis tenuis") }, by=list(spp)] }, by=list(scen)] over.proj <- rbind(over.cod, over.dog, over.shake, over.whake) ### Save mean btemp and deltaT for each simulation year along with overlap values proj_btemp <- projected[,list(mean.btemp = mean(btemp)), by=list(scen)] mean_hist_btemp <- historical[,list(overall.mean.btemp=mean(btemp, na.rm=T))] proj_btemp[,deltaT:=mean.btemp - mean_hist_btemp$overall.mean.btemp] proj_btemp[,year:=as.integer(gsub("yr","",scen))] ### Merge projected range and overlap with data on btemp and deltaT in each year of simulation proj_spp_range2 <- merge(proj_spp_range, proj_btemp, by=c("scen")) setorder(proj_spp_range2, deltaT) plot(geo_range ~ deltaT, proj_spp_range2[spp=="Gadus morhua"]) over.proj2 <- merge(over.proj, proj_btemp, by=c("scen")) setorder(over.proj2, deltaT) plot(over.frac ~ deltaT, over.proj2[spp=="Clupea harengus" & focal=="Gadus morhua"]) # ========================================================== # = Historical observed and predicted overlap = # ========================================================== over.cod.hist <- historical[,j={ dt <- .SD foc_obs <- dt[spp == "Gadus morhua"]$pres2 foc_pred <- dt[spp=="Gadus morhua"]$preds1 prey_obs <- dt[spp%in%u_prey,j={ sub <- .SD prey_occ <- sub$pres2 prey_pred <- sub$preds1 both <- foc_obs * prey_occ over.frac <- sum(both)/sum(prey_occ) both.pred <- foc_pred * prey_pred over.frac.pred <- sum(both.pred)/sum(prey_pred) list(over.frac=over.frac, over.frac.pred=over.frac.pred, focal="Gadus morhua") }, by=list(spp)] }, by=list(year)] over.dog.hist <- historical[,j={ dt <- .SD foc_obs <- dt[spp == "Squalus acanthias"]$pres2 foc_pred <- dt[spp=="Squalus acanthias"]$preds1 prey_obs <- dt[spp%in%u_prey,j={ sub <- .SD prey_occ <- sub$pres2 prey_pred <- sub$preds1 both <- foc_obs * prey_occ over.frac <- sum(both)/sum(prey_occ) both.pred <- foc_pred * prey_pred over.frac.pred <- sum(both.pred)/sum(prey_pred) list(over.frac=over.frac, over.frac.pred=over.frac.pred, focal="Squalus acanthias") }, by=list(spp)] }, by=list(year)] over.shake.hist <- historical[,j={ dt <- .SD foc_obs <- dt[spp == "Merluccius bilinearis"]$pres2 foc_pred <- dt[spp=="Merluccius bilinearis"]$preds1 prey_obs <- dt[spp%in%u_prey,j={ sub <- .SD prey_occ <- sub$pres2 prey_pred <- sub$preds1 both <- foc_obs * prey_occ over.frac <- sum(both)/sum(prey_occ) both.pred <- foc_pred * prey_pred over.frac.pred <- sum(both.pred)/sum(prey_pred) list(over.frac=over.frac, over.frac.pred=over.frac.pred, focal="Merluccius bilinearis") }, by=list(spp)] }, by=list(year)] over.whake.hist <- historical[,j={ dt <- .SD foc_obs <- dt[spp == "Urophycis tenuis"]$pres2 foc_pred <- dt[spp=="Urophycis tenuis"]$preds1 prey_obs <- dt[spp%in%u_prey,j={ sub <- .SD prey_occ <- sub$pres2 prey_pred <- sub$preds1 both <- foc_obs * prey_occ over.frac <- sum(both)/sum(prey_occ) both.pred <- foc_pred * prey_pred over.frac.pred <- sum(both.pred)/sum(prey_pred) list(over.frac=over.frac, over.frac.pred=over.frac.pred, focal="Urophycis tenuis") }, by=list(spp)] }, by=list(year)] over.hist <- rbind(over.cod.hist, over.dog.hist, over.shake.hist, over.whake.hist)