Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnaming $penalty for bipartite, fixing broken CRAN badge #14

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 47 additions & 40 deletions R/R6Class-BipartiteSBM_fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ BipartiteSBM_fit <-
classname = "BipartiteSBM_fit",
inherit = BipartiteSBM,
private = list(
J = NULL, # approximation of the log-likelihood
vICL = NULL, # approximation of the ICL
BMobject = NULL, # blockmodels output (used to stored the optimization results when blockmodels is used)
J = NULL, # approximation of the log-likelihood
vICL = NULL, # approximation of the ICL
BMobject = NULL, # blockmodels output (used to stored the optimization results when blockmodels is used)
import_from_BM = function(index = which.max(private$BMobject$ICL)) {
private$J <- private$BMobject$PL[index]
private$vICL <- private$BMobject$ICL[index]
parameters <- private$BMobject$model_parameters[[index]]
private$beta <- parameters$beta ## NULL if no covariates
private$J <- private$BMobject$PL[index]
private$vICL <- private$BMobject$ICL[index]
parameters <- private$BMobject$model_parameters[[index]]
private$beta <- parameters$beta ## NULL if no covariates
private$theta <- switch(private$BMobject$model_name,
"bernoulli" = list(mean = parameters$pi),
"bernoulli_covariates" = list(mean = .logistic(parameters$m)),
Expand All @@ -32,7 +32,6 @@ BipartiteSBM_fit <-
col = private$BMobject$memberships[[index]]$Z2
)
private$pi <- lapply(private$Z, colMeans)

}
),
public = list(
Expand All @@ -41,10 +40,9 @@ BipartiteSBM_fit <-
#' @param model character (\code{'bernoulli'}, \code{'poisson'}, \code{'gaussian'})
#' @param dimLabels labels of each dimension (in row, in columns)
#' @param covarList and optional list of covariates, each of whom must have the same dimension as \code{incidenceMatrix}
initialize = function(incidenceMatrix, model, dimLabels=c(row="row", col="col"), covarList=list()) {

initialize = function(incidenceMatrix, model, dimLabels = c(row = "row", col = "col"), covarList = list()) {
## SANITY CHECKS on data
stopifnot(is.matrix(incidenceMatrix)) # must be a matrix
stopifnot(is.matrix(incidenceMatrix)) # must be a matrix
stopifnot(all(sapply(covarList, nrow) == nrow(incidenceMatrix))) # consistency of the covariates
stopifnot(all(sapply(covarList, ncol) == ncol(incidenceMatrix))) # with the network data

Expand All @@ -57,37 +55,38 @@ BipartiteSBM_fit <-
)

## INITIALIZE THE SBM OBJECT ACCORDING TO THE DATA
super$initialize(model = model,
nbNodes = dim(incidenceMatrix),
blockProp = rep(list(vector("numeric", 0)), 2),
connectParam = connectParam,
dimLabels = dimLabels,
covarList = covarList)
super$initialize(
model = model,
nbNodes = dim(incidenceMatrix),
blockProp = rep(list(vector("numeric", 0)), 2),
connectParam = connectParam,
dimLabels = dimLabels,
covarList = covarList
)
private$Y <- incidenceMatrix
},
#' @description function to perform optimization
#' @param estimOptions a list of parameters controlling the inference algorithm and model selection. See details.
#' @inherit estimateSimpleSBM details
optimize = function(estimOptions = list()){

if(private$model == 'ZIgaussian') stop("Inference not yet implemented for Bipartite ZI gaussian network")
optimize = function(estimOptions = list()) {
if (private$model == "ZIgaussian") stop("Inference not yet implemented for Bipartite ZI gaussian network")

currentOptions <- list(
verbosity = 3,
plot = TRUE,
exploreFactor = 1.5,
exploreMin = 4,
exploreMax = Inf,
nbBlocksRange = c(4,Inf),
nbCores = 2,
fast = TRUE
verbosity = 3,
plot = TRUE,
exploreFactor = 1.5,
exploreMin = 4,
exploreMax = Inf,
nbBlocksRange = c(4, Inf),
nbCores = 2,
fast = TRUE
)
currentOptions[names(estimOptions)] <- estimOptions

## Transform estimOptions to a suited for blockmodels list of options
blockmodelsOptions <- list(
verbosity = currentOptions$verbosity,
plotting = if(currentOptions$plot) character(0) else "",
plotting = if (currentOptions$plot) character(0) else "",
explore_min = currentOptions$exploreMin,
explore_max = currentOptions$exploreMax,
ncores = currentOptions$nbCores,
Expand All @@ -103,8 +102,8 @@ BipartiteSBM_fit <-

## model construction

model_type <- ifelse(self$nbCovariates > 0, paste0(private$model,"_covariates"), private$model)
if (model_type == 'bernoulli_covariates' & fast == TRUE) model_type <- 'bernoulli_covariates_fast'
model_type <- ifelse(self$nbCovariates > 0, paste0(private$model, "_covariates"), private$model)
if (model_type == "bernoulli_covariates" & fast == TRUE) model_type <- "bernoulli_covariates_fast"
private$BMobject <- do.call(paste0("BM_", model_type), args)

## performing estimation
Expand Down Expand Up @@ -137,7 +136,7 @@ BipartiteSBM_fit <-
},
#' @description show method
#' @param type character used to specify the type of SBM
show = function(type = "Fit of a Bipartite Stochastic Block Model"){
show = function(type = "Fit of a Bipartite Stochastic Block Model") {
super$show(type)
cat("* Additional fields\n")
cat(" $probMemberships, $loglik, $ICL, $storedModels, \n")
Expand All @@ -147,26 +146,34 @@ BipartiteSBM_fit <-
),
active = list(
#' @field loglik double: approximation of the log-likelihood (variational lower bound) reached
loglik = function(value) {private$J},
loglik = function(value) {
private$J
},
#' @field ICL double: value of the integrated classification log-likelihood
ICL = function(value) {private$vICL},
ICL = function(value) {
private$vICL
},
#' @field penalty double, value of the penalty term in ICL
penalty = function(value) {(self$nbConnectParam + self$nbCovariates) * log(self$nbDyads) + (self$nbBlocks[1]-1) * log(private$dim[1]) + (self$nbBlocks[2]-1) * log(private$dim[2])},
penalty = function(value) {
unname((self$nbConnectParam + self$nbCovariates) * log(self$nbDyads) + (self$nbBlocks[1] - 1) * log(private$dim[1]) + (self$nbBlocks[2] - 1) * log(private$dim[2]))
},
#' @field entropy double, value of the entropy due to the clustering distribution
entropy = function(value) {-sum(.xlogx(private$Z[[1]]))-sum(.xlogx(private$Z[[2]]))},
entropy = function(value) {
-sum(.xlogx(private$Z[[1]])) - sum(.xlogx(private$Z[[2]]))
},
#' @field storedModels data.frame of all models fitted (and stored) during the optimization
storedModels = function(value) {
rowBlocks <- c(0, unlist(sapply(private$BMobject$memberships, function(m) ncol(m$Z1))))
colBlocks <- c(0, unlist(sapply(private$BMobject$memberships, function(m) ncol(m$Z2))))
nbConnectParam <- c(NA, unlist(sapply(private$BMobject$model_parameters, function(param) param$n_parameters)))
U <- data.frame(
indexModel = rowBlocks + colBlocks,
nbParams = nbConnectParam + rowBlocks + colBlocks - 2,
nbParams = nbConnectParam + rowBlocks + colBlocks - 2,
rowBlocks = rowBlocks,
colBlocks = colBlocks,
nbBlocks = rowBlocks + colBlocks,
ICL = private$BMobject$ICL,
loglik = private$BMobject$PL
nbBlocks = rowBlocks + colBlocks,
ICL = private$BMobject$ICL,
loglik = private$BMobject$PL
)
U[!is.na(U$nbParams), , drop = FALSE]
}
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ knitr::opts_chunk$set(
<!-- badges: start -->
[![website](https://github.com/GrossSBM/sbm/workflows/pkgdown/badge.svg)](https://grosssbm.github.io/sbm/)
![R-CMD-check](https://github.com/GrossSBM/sbm/workflows/R-CMD-check/badge.svg?branch=master)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/sbm)](https://cran.r-project.org/package=sbm)
[![CRAN status](https://www.r-pkg.org/badges/version/sbm)](https://CRAN.R-project.org/package=sbm)
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![](https://img.shields.io/github/last-commit/grossSBM/sbm.svg)](https://github.com/GrossSBM/sbm/commits/master)
[![Codecov test coverage](https://codecov.io/gh/GrossSBM/sbm/branch/master/graph/badge.svg)](
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

[![website](https://github.com/GrossSBM/sbm/workflows/pkgdown/badge.svg)](https://grosssbm.github.io/sbm/)
![R-CMD-check](https://github.com/GrossSBM/sbm/workflows/R-CMD-check/badge.svg?branch=master)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/sbm)](https://cran.r-project.org/package=sbm)
[![CRAN
status](https://www.r-pkg.org/badges/version/sbm)](https://CRAN.R-project.org/package=sbm)
[![Lifecycle:
experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![](https://img.shields.io/github/last-commit/grossSBM/sbm.svg)](https://github.com/GrossSBM/sbm/commits/master)
Expand Down
Loading
Loading