R/AgentSummary.R

#' @title Summarizes mortality agents
#' @description
#'
#' Prints summary statistics related to AGENTCD results
#'
#' @export
#' @examples AgentSummary()
AgentSummary <- function() {
  cat('\n')
  mort_df <- FIA_mortality_with_explanatory
  mult_df <- mort_df[which(mort_df$dominant_AGENTCD == 'Multiple'), ]
  tree_df <- FIA_mortality_TREE_level
  tree_df$AGENT_name <- KeyAgentCode(7.2)(tree_df$AGENTCD)
  tree_df$AGENT_name <- ifelse(
    tree_df$AGENT_name == 'Missing',
    'No Mortality',
    tree_df$AGENT_name
  )
  outlier_plots <- mort_df[mort_df$mort_outlier, 'PLT_CN']
  outlier_trees <- which(tree_df$PLT_CN %in% outlier_plots)
  outlier_tree_AGENT <- tree_df[outlier_trees, 'AGENT_name']
  background_plots <- mort_df[!mort_df$mort_outlier, 'PLT_CN']
  background_trees <- which(tree_df$PLT_CN %in% background_plots)
  background_tree_AGENT <- tree_df[background_trees, 'AGENT_name']

  message('No mortality:')
  ttbl <- table(tree_df$AGENT_name)
  atbl <- table(mort_df$dominant_AGENTCD)

  ctbl <- aggregate(mort_df$percent_AGENTCD,
                    by = list(mort_df$dominant_AGENTCD),
                    FUN = mean)
  colnames(ctbl) <- c('Agent', 'Mean_Perc')
  ctbl2 <- aggregate(mort_df$percent_AGENTCD,
                     by = list(mort_df$dominant_AGENTCD),
                     FUN = sd)
  colnames(ctbl2) <- c('Agent', 'SD_Perc')
  ctbl <- dplyr::left_join(ctbl, ctbl2, by = 'Agent')
  ctbl <- ctbl[-which(ctbl$Agent == 'No Mortality'), ]
  ctbl[, c(2, 3)] <- round(ctbl[, c(2, 3)] / 100, 2)

  pnm <- round(atbl['No Mortality'] / sum(atbl) * 100, 2)
  cat('\nPlots with no mortality:', atbl['No Mortality'])
  cat('\n% plots with no mortality:', pnm)

  # Percent of that 'multiple' category is of ALL plots, all mort plots,
  # and background plots
  cat('\n')
  if ('Multiple' %in% names(atbl)) {
    message('Multiple mortality:')
    pma <- round(atbl['Multiple'] / sum(atbl) * 100, 2)
    pmm <- round(atbl['Multiple'] / (sum(atbl) - atbl['No Mortality']) * 100, 2)
    btbl <- table(mort_df$dominant_AGENTCD[!mort_df$mort_outlier])
    pmb <- round(btbl['Multiple'] / (sum(btbl) - btbl['No Mortality']) * 100, 2)
    cat('\n% of multiple category from:')
    cat('\nAll plots:', pma)
    cat('\nAll plots with mortality:', pmm)
    cat('\nAll background plots with mortality:', pmb)
  }

  cat('\n')
  message('Agent mortality:')
  cat('\nPercent of plots affected by dominant agent:\n\n')
  print(ctbl)
  cat('\nMean of Mean_Perc:', round(mean(ctbl$Mean_Perc), 2))

  otf <- round(length(outlier_tree_AGENT) / nrow(tree_df), 4)
  cat('\n\nFraction of total trees in section outliers:', otf)
  btf <- round(length(background_tree_AGENT) / nrow(tree_df), 4)
  cat('\nFraction of total trees in section background:', btf)
  opf <- round(table(mort_df$mort_outlier)['TRUE'] / nrow(mort_df), 4)
  cat('\nFraction of total plots in section outliers:', opf)
  bpf <- round(table(mort_df$mort_outlier)['FALSE'] / nrow(mort_df), 4)
  cat('\nFraction of total plots in section outliers:', bpf)

  browser()
  invisible()
}
bmcnellis/RSFIA documentation built on June 1, 2019, 7:40 a.m.