Author: Cristian Castiglione

# Initialization algorithms

#### Cristian Castiglione

## Workspace setup

Load the package in the workspace.

    library(sgdGMF)

Load other useful packages in the workspace.

    library(ggplot2)
    library(ggpubr)
    library(reshape2)

## Ant traits data

Load the ant traits data in the workspace and define the response matrix `Y` and covariate matrices `X` and `Z`.

    # install.packages("mvabund")
    # data(antTraits, package = "mvabund")

    load(url("https://raw.githubusercontent.com/cran/mvabund/master/data/antTraits.RData"))

    Y = as.matrix(antTraits$abund)
    X = as.matrix(antTraits$env[,-3])
    Z = matrix(1, nrow = ncol(Y), ncol = 1)

    attr(Y, "dimnames") = NULL
    attr(X, "dimnames") = NULL
    attr(Z, "dimnames") = NULL

    n = nrow(Y)
    m = ncol(Y)

## Model specification

Set the model family to Poisson since the response matrix contain count data.

    family = poisson()

    suppressWarnings({
      init_glm_dev = sgdGMF::sgdgmf.init(Y, X, Z, ncomp = 2, family = family, method = "glm", type = "deviance")
      init_glm_prs = sgdGMF::sgdgmf.init(Y, X, Z, ncomp = 2, family = family, method = "glm", type = "pearson")
      init_glm_lnk = sgdGMF::sgdgmf.init(Y, X, Z, ncomp = 2, family = family, method = "glm", type = "link")
      init_ols_dev = sgdGMF::sgdgmf.init(Y, X, Z, ncomp = 2, family = family, method = "ols", type = "deviance")
      init_ols_prs = sgdGMF::sgdgmf.init(Y, X, Z, ncomp = 2, family = family, method = "ols", type = "pearson")
      init_ols_lnk = sgdGMF::sgdgmf.init(Y, X, Z, ncomp = 2, family = family, method = "ols", type = "link")
    })

    data.frame(
      "Method" = rep(c("GLM", "OLS"), each = 3),
      "Resid" = rep(c("Deviance", "Pearson", "Link"), times = 2),
      "Deviance" =
        list(init_glm_dev, init_glm_prs, init_glm_lnk, init_ols_dev, init_ols_prs, init_ols_lnk) |>
        lapply(function (obj) round(100 * deviance(obj, normalize = TRUE), 2)) |> unlist() |> drop()
    )
    #>   Method    Resid Deviance
    #> 1    GLM Deviance    37.16
    #> 2    GLM  Pearson    27.65
    #> 3    GLM     Link    45.47
    #> 4    OLS Deviance    43.52
    #> 5    OLS  Pearson    49.72
    #> 6    OLS     Link    42.71