R/result_GAM.R

Defines functions result_GAM

Documented in result_GAM

#' Computed the R adjusted of the abundance/metric value models at each scales with gam method.
#'
#' `result_GAM()` 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 gam
#' and their signifiance.
#' @author Pierre-Gilles Lemasle <pg.lemasle@gmail.com>
#' @export
result_GAM=function(temp,dist,metrics,Sp_Name,tab=F){
  tempdata=NULL
  for (j in metrics) {
    for (i in sub("^", "w",dist)) {
      form=as.formula(paste(Sp_Name,"~",i))
      templm=lm(form,data=temp[temp$Metric==j,])
      #radj=summary(templm)$adj.r.squared
      radj=summary(gam(form,family = poisson(),data = temp[temp$Metric==j,]))$r.sq
      #color=if(summary(templm)$coefficients[,4]<=0.05){"Significant"}else{"non significant"}
      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)
  #tempdata$color=as.factor(tempdata$color)
  if(tab==T){
    return(tempdata)
  }
  else{

    listplot=list()
    # myColors <- gg_color_hue(2)
    # names(myColors) <- levels(tempdata$color)
    # colScale <- scale_colour_manual(name = "color",values = myColors)
    for (i in metrics) {
      p1=ggplot(tempdata[tempdata$Metric==i,],aes(x = Distance,y=radj))+
        geom_point()+
        ggtitle(i) +
        xlab("Distance") + ylab("Adjusted-R")
      # p1=p1+colScale
      listplot[[i]]=p1
    }
    return(listplot)
  }


}
Pintademijote/multipack documentation built on Sept. 24, 2019, 7:54 a.m.