R/diagnostics.R

Defines functions diagnosticResiduals diagnosticLM

Documented in diagnosticLM diagnosticResiduals

#------------------------------------------------------------------
# Program: diagnostics.R
# Objective: graphical functions for diagnostics of linear models
# Author: I.Sanchez
# Creation: 28/07/2016
# Update: 04/09/2020
#------------------------------------------------------------------

#' a function for representing the residuals of a linear model
#' @param fitin is a list containing objects of class lm
#' @return 2 graphs in one page: a graph to check the homoscedasticity of residuals
#'         the second to check the normality of the residuals
#' @examples
#' # diagnosticLM() needs a classical linear model list result
#' @export
diagnosticLM<-function(fitin){
  par(mfrow=c(2,2))
  for (i in 1:length(fitin)){
    plot(fitin[[i]],which=1,main=names(fitin)[i])
    plot(fitin[[i]],which=2)
  }
  par(mfrow=c(1,1))
}

#' a function for representing diagnostic graphics for each trait
#' @param datain a dataframe to explore
#' @param xfitted character, name of the column of fitted values
#' @param yresidual character, name of the column of residual values
#' @return a graphic of diagnostic for linear model
#' @importFrom ggplot2 ggplot geom_point geom_hline aes_string
#' @examples
#' # diagnosticResiduals(datain,xfitted,yresidual)
#' @export
#---------------------------------------------------
diagnosticResiduals<-function(datain,xfitted,yresidual){
  g<-ggplot(data=datain,ggplot2::aes_string(x=xfitted,y=yresidual)) +
         geom_point(size=1) +
         geom_hline(yintercept = 2,col="red") + 
         geom_hline(yintercept = -2,col="red")
  print(g)
}
sanchezi/openSilexStatR documentation built on Sept. 10, 2020, 1:03 p.m.