R/reformat_compositions.R

Defines functions reformat_compositions

Documented in reformat_compositions

#' @title Reformat compositional data for input in to Stock Synthesis
#'
#' @description to be added
#' @details to be added
#' @export
#'
#' @param comp_data the data frame of composition data
#' @param round.places the number of decimal places you would like your compositions rounded to; default is 4
#' @param comp_type takes 3 possible values: lencomp, agecomp, caalcomp
#' @return flattened comp data set for input into SS
#'
reformat_compositions <- function(comp_data,
                                  round.places = 4,
                                  comp_type){
  if(comp_type=="lencomp"){
    comp_proportion <- comp_data %>%
      group_by(time, lower.bins, upper.bins) %>%
      summarise(comp = sum(atoutput))
  } else{
  comp_proportion <- comp_data %>%
    group_by(time) %>%
    mutate(comp = round(atoutput/sum(atoutput),round.places))
  }

  if(comp_type=="agecomp"){
  unmelt <- dcast(data = as.data.table(comp_proportion),
                  formula = time ~agecl,
                  value.var = "comp")
  samp_size <- comp_proportion %>%
    group_by(time) %>% summarise("nsamp" = sum(atoutput))
  unmelt <- left_join(unmelt, samp_size, by =c("time"))
  }

  if(comp_type=="lencomp"){
    unmelt <- dcast(data = as.data.table(comp_proportion),
                    formula = time ~lower.bins,
                    value.var = "comp")
    samp_size <- comp_proportion %>%
      group_by(time) %>% summarise("nsamp" = sum(comp))
    unmelt <- left_join(unmelt, samp_size, by =c("time"))
  }

  if(comp_type=="caalcomp"){
    unmelt <- dcast(data = as.data.table(comp_proportion),
                    formula = time + lower.bins + upper.bins ~agecl,
                    value.var = "comp")
    samp_size <- comp_proportion %>%
      group_by(time, lower.bins, upper.bins) %>% summarise("nsamp" = sum(atoutput))
    unmelt <- left_join(unmelt, samp_size, by =c("time", "lower.bins","upper.bins"))

  }


  unmelt[is.na(unmelt)] <- 0

  #Sum across non-metadata columns to get sample sizes
  columns_nonsum <- c("fleet", "year","season",
                      "Lbin_lo","Lbin_hi", "month", "sex", "part", "ageerr","Nsamp", "time", "lower.bins","upper.bins")

  comp_flat<- unmelt[order(unmelt$time),]

  return(comp_flat)
}
r4atlantis/atlantisom documentation built on Nov. 12, 2023, 2:59 a.m.