# helper functions for working with ABAP data #modified version to include covariates abapToUnmarked_single2 <- function (abap_data, pentads = NULL, cap_visits = NULL, siteCovs = NULL, siteCovs_clean = TRUE) { if (!requireNamespace("unmarked", quietly = TRUE)) { stop("Package unmarked doesn't seem to be installed. Please install before using this function.", call. = FALSE) } pentad_id <- unique(abap_data$Pentad) max_visits <- abap_data %>% dplyr::count(Pentad) %>% dplyr::pull(n) %>% max() if (!is.null(pentads)) { sf::st_agr(pentads) = "constant" pentad_xy <- pentads %>% dplyr::filter(pentad %in% pentad_id) %>% dplyr::arrange(pentad) %>% sf::st_centroid() %>% sf::st_coordinates() %>% as.data.frame() %>% dplyr::rename_all(tolower) } vpad <- rep(NA, max_visits) format_df <- abap_data %>% dplyr::select(Pentad, Spp, TotalHours, StartDate) %>% dplyr::mutate(Spp = ifelse(Spp == "-", 0L, 1L), julian_day = lubridate::yday(StartDate)) %>% dplyr::nest_by(Pentad) %>% dplyr::arrange(Pentad) %>% dplyr::mutate(hourpad = list(head(c(data$TotalHours, vpad), max_visits)), jdaypad = list(head(c(data$julian_day, vpad), max_visits))) det_hist <- format_df %>% dplyr::mutate(dets = list(head(c(data$Spp, vpad), max_visits))) Y <- do.call("rbind", det_hist$dets) rownames(Y) <- format_df$Pentad obs_hours <- do.call("rbind", format_df$hourpad) rownames(obs_hours) <- format_df$Pentad obs_jday <- do.call("rbind", format_df$jdaypad) rownames(obs_jday) <- format_df$Pentad n_sites <- nrow(Y) #reduce the data to a max number of visits (cap_visits) Y_red <- matrix(NA_integer_, n_sites, cap_visits) hours_red <- matrix(NA_real_, n_sites, cap_visits) jday_red <- matrix(NA_real_, n_sites, cap_visits) for (i in seq_len(n_sites)) { valid_cols <- which(!is.na(Y[i, ])) if (length(valid_cols) == 0L) next if (length(valid_cols) > cap_visits) { valid_cols <- valid_cols[seq_len(cap_visits)] # or sample(valid_cols, cap_visits) } n_keep <- length(valid_cols) Y_red[i, seq_len(n_keep)] <- Y[i, valid_cols] hours_red[i, seq_len(n_keep)] <- obs_hours[i, valid_cols] jday_red[i, seq_len(n_keep)] <- obs_jday[i, valid_cols] } #add totalspp as another covariate #TotalSpp_mat is a pentad by occasion matrix showing the total spp for each visit format_totalspp <- abap_data %>% select(Pentad, TotalSpp) %>% nest_by(Pentad) %>% arrange(Pentad) %>% mutate(totalspp_pad = list(head(c(data$TotalSpp, vpad), cap_visits))) TotalSpp_mat <- do.call("rbind", format_totalspp$totalspp_pad) rownames(TotalSpp_mat) <- format_totalspp$Pentad #drop sites with NAs for any siteCovs #also need to drop the correct row in other objs if (siteCovs_clean && !is.null(siteCovs)){ keep <- complete.cases(siteCovs) siteCovs <- siteCovs[keep, ] pentad_xy <- pentad_xy[keep,] Y_red <- Y_red[keep, ] hours_red <- hours_red[keep,] jday_red <- jday_red[keep,] TotalSpp_mat <- TotalSpp_mat[keep,] n_sites <- dim(Y_red)[1] } obs_covs <- list(hours = as.data.frame(hours_red), jday = as.data.frame(jday_red), totalspp = as.data.frame(TotalSpp_mat)) if (is.null(siteCovs)) { #no extra sitecovs (argument = F) if (!is.null(pentads)) { umf_abap <- unmarked::unmarkedFrameOccu(y = Y_red, siteCovs = pentad_xy, obsCovs = obs_covs) } else { umf_abap <- unmarked::unmarkedFrameOccu(y = Y_red, obsCovs = obs_covs) } } else { #extra sitecovs provided if (!is.null(pentads)) { #pentads provided site_covs <- cbind(pentad_xy, siteCovs) umf_abap <- unmarked::unmarkedFrameOccu(y = Y_red, siteCovs = site_covs, obsCovs = obs_covs) } else { umf_abap <- unmarked::unmarkedFrameOccu(y = Y_red, siteCovs = siteCovs, obsCovs = obs_covs) } } return(umf_abap) }