R/criteriaComp.R

Defines functions criteriaComp

Documented in criteriaComp

#' Update the seasons for LU
#'
#' Update the seasons for LU
#' @param season.sites the season calculated for each site.
#' @param p.sites the composition of each site.
#' @keywords updating season
#' @export
#' @examples
#' data("wq_analysis_week2")
#' SPTMData(wq.raw.obs, frequency = "quarter")
criteriaComp = function(SPTMresobj){

  theta = SPTMresobj$Result$theta
  p.sites = SPTMresobj$Result$latent

  season.sites = SPTMresobj$seasonSites
  trend.sites = SPTMresobj$trendSites

  y.p = meanFunc(theta, n.mixt=ncol(p.sites), p.sites=p.sites, season.sites = season.sites)

  idx.sigma = (length(theta) - ncol(p.sites) + 1):length(theta)

  if(!is.null(SPTMresobj$GibbsOut)){
    theta[idx.sigma] = log(sqrt(theta[idx.sigma]))
  }

  df = SPTMresobj$data$data

  SiteID = unique(df$obs.data$ID)

  n.sites = length(SiteID)
RSS = 0
RSE = 0
  for(i in 1:n.sites){
    obs = df$obs.data$obs[df$obs.data$ID %in% SiteID[i]]
    idx.k = matrix(p.sites[i,], ncol=1)
    pred= y.p %*% idx.k
    RSS = RSS + sum((obs - pred)^2, na.rm=T)
    RSE = RSE + sum((obs - mean(obs, na.rm=T))^2, na.rm=T)
  }

return(list(RSS = RSS, R2 = 1 - RSS / RSE))
}
ick003/SpTMixture documentation built on May 18, 2019, 2:32 a.m.