#' @include mprocuste.R

#' Anova function for `pm` objects.
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
anova.pm = function(m,...) {

  mf   = m$model
  coef = m$coefficients

  sdxy = sqrt(diag(m$cov))

  mt = terms(mf)

  reponse = attr(mt,"response")
  explicatives = attr(mt,'term.labels')
  nXs = length(explicatives)

  coef.norm = coef * sdxy[explicatives] / sdxy[reponse]


  cors = mcor(mf)

  print(reponse)
  print(explicatives)

  cors.Y = cors[reponse,explicatives]

  SCT  = m$SCT
  SCR = sum(m$residuals^2)

  SCE = SCT * coef.norm * cors.Y

  names(SCE) = explicatives

  SCET = sum(SCE)

  SCIT = SCT - SCET - SCR

  SumSq  = c(SCE,Residuals=SCR)
  Df     = c(rep(1,nXs),m$df.residual)
  MeanSq = SumSq/Df
  Fvalue = c(MeanSq[-length(MeanSq)]/MeanSq[length(MeanSq)],NA)
  Pvalue = 1-pf(Fvalue,
                Df[-length(MeanSq)],
                Df[length(MeanSq)])

  result = data.frame(Df,
                      SumSq,
                      MeanSq,
                      Fvalue,
                      Pvalue)

  colnames(result)=c("Df","Sum Sq","Mean Sq","F value","Pr(>F)")


  attr(result, "heading")= paste("Analysis of Variance Table\n\nResponse:",
                                 all.vars(m$terms)[1]
  )
  class(result)=c("anova",class(result))

  return(result)
}
