R/result_GLM.R

#' Computed the R adjusted of the abundance/metric value models at each scales with GLM
#'
#' `result_MARS()` Return plots or tab depending of the "tab" argument of the R adjusted of abundance/metric value models
#' at each scales for gam with the corresponding signifiance if it can be tested.
#'
#' This function is meant to be used with the tab generated by formodel() merged with the tab of your abundance sampling.
#' @param temp The tab generated by formodel() function merged by the id of sampling site
#' with the tab of your abundance sampling.
#'
#' @param dist Vector of scales you choosed during you analusis in Chloe
#' @param metrics Vector of one metric or more you choosed in Chloe during your analysis in Chloe
#' @param sp_name Optionnal argument containing the name of the species you want to see analyse, the name
#' must match its column name contained in the "carab" tab
#' @param tab Argument "T" or "F" defining if you want to return plot (F) or a tab (T).
#' @return Return a list of plot(s) of the length of "metrics" or a tab of the R adjusted function of MARS.
#' @author Pierre-Gilles Lemasle <pg.lemasle@gmail.com>
#' @export
result_GLM=function (x, dist, metrics, Sp_Name, tab = F,family_choice=poisson()) {
  tempdata = NULL
  for (j in metrics) {
    for (i in sub("^", "w", dist)) {
      form = as.formula(paste(Sp_Name, "~", i))
      temp<<-x[x$Metric == j, ]
      model=glm(form,family = poisson(),
                data = temp)
      radj =  DescTools::PseudoR2(model,"McFadden")
      tempdata = rbind(tempdata, c(j, sub("w", "",
                                          i), radj))
    }
  }
  tempdata = as.data.frame(tempdata)
  colnames(tempdata) = c("Metric", "Distance",
                         "radj")
  tempdata$Distance = as.character(tempdata$Distance)
  tempdata$Distance = as.numeric(tempdata$Distance)
  tempdata$radj = as.character(tempdata$radj)
  tempdata$radj = as.numeric(tempdata$radj)
  if (tab == T) {
    return(tempdata)
  }else {
    listplot = list()
    for (i in metrics) {
      p1 = ggplot(tempdata[tempdata$Metric == i, ], aes(x = Distance,
                                                        y = radj)) + geom_point() + ggtitle(i) + xlab("Distance") +
        ylab("Adjusted-R")
      listplot[[i]] = p1
    }
    return(listplot)
  }
}
Pintademijote/multipack documentation built on Sept. 24, 2019, 7:54 a.m.