From 8183b6c0fdbe5e159c59c3f7285f398483120ea2 Mon Sep 17 00:00:00 2001 From: Jouni Helske Date: Mon, 21 Oct 2024 18:47:46 +0300 Subject: [PATCH] nobs import --- NAMESPACE | 1 + R/bootstrap.R | 12 ++++-------- R/nobs.R | 20 ++++++++++++++++---- R/seqHMM-package.R | 2 +- man/nobs.Rd | 28 ++++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 man/nobs.Rd diff --git a/NAMESPACE b/NAMESPACE index ac65618a..38da0b31 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -142,6 +142,7 @@ importFrom(stats,coef) importFrom(stats,complete.cases) importFrom(stats,logLik) importFrom(stats,model.matrix) +importFrom(stats,nobs) importFrom(stats,predict) importFrom(stats,qnorm) importFrom(stats,quantile) diff --git a/R/bootstrap.R b/R/bootstrap.R index b9ab4341..4f6a3bc6 100644 --- a/R/bootstrap.R +++ b/R/bootstrap.R @@ -96,8 +96,7 @@ bootstrap_coefs.nhmm <- function(model, B = 1000, if (method == "nonparametric") { for (i in seq_len(B)) { mod <- bootstrap_model(model) - fit <- fit_nhmm(mod, init, init_sd = 0, restarts = 0, threads = 1, - ...) + fit <- fit_nhmm(mod, init, init_sd = 0, restarts = 0, threads = 1, ...) fit$gammas <- permute_states(fit$gammas, gammas_mle) gamma_pi[[i]] <- fit$gammas$pi gamma_A[[i]] <- fit$gammas$A @@ -119,8 +118,7 @@ bootstrap_coefs.nhmm <- function(model, B = 1000, mod <- simulate_nhmm( N, T_, M, S, formula_pi, formula_A, formula_B, data = d, time, id, init)$model - fit <- fit_nhmm(mod, init, init_sd = 0, restarts = 0, threads = 1, - ...) + fit <- fit_nhmm(mod, init, init_sd = 0, restarts = 0, threads = 1, ...) fit$gammas <- permute_states(fit$gammas, gammas_mle) gamma_pi[[i]] <- fit$gammas$pi gamma_A[[i]] <- fit$gammas$A @@ -154,8 +152,7 @@ bootstrap_coefs.mnhmm <- function(model, B = 1000, if (method == "nonparametric") { for (i in seq_len(B)) { mod <- bootstrap_model(model) - fit <- fit_mnhmm(mod, init, init_sd = 0, restarts = 0, threads = 1, - ...) + fit <- fit_mnhmm(mod, init, init_sd = 0, restarts = 0, threads = 1, ...) fit <- permute_clusters(fit, pcp_mle) for (j in seq_len(D)) { out <- permute_states( @@ -188,8 +185,7 @@ bootstrap_coefs.mnhmm <- function(model, B = 1000, mod <- simulate_mnhmm( N, T_, M, S, D, formula_pi, formula_A, formula_B, formula_omega, data = d, time, id, init)$model - fit <- fit_mnhmm(mod, init, init_sd = 0, restarts = 0, threads = 1, - ...) + fit <- fit_mnhmm(mod, init, init_sd = 0, restarts = 0, threads = 1, ...) fit <- permute_clusters(fit, pcp_mle) for (j in seq_len(D)) { out <- permute_states( diff --git a/R/nobs.R b/R/nobs.R index a59ed063..1c65e081 100644 --- a/R/nobs.R +++ b/R/nobs.R @@ -1,16 +1,28 @@ -#'@export +#' Number of Observations in Hidden Markov Model +#' +#' Extract the number of non-missing observations of HMM. When computing nobs +#' for a multichannel model with $C$ channels, each observed value in a single +#' channel amounts to $1/C$ observation, i.e. a fully observed time point for +#' a single sequence amounts to one observation. +#' @param object An object of class `hmm`, `mhmm`, `nhmm`, or `mnhmm`. +#' @param ... Ignored. +#' @rdname nobs +#' @export nobs.hmm <- function(object, ...) { attr(object, "nobs") } -#'@export +#' @rdname nobs +#' @export nobs.mhmm <- function(object, ...) { attr(object, "nobs") } -#'@export +#' @rdname nobs +#' @export nobs.nhmm <- function(object, ...) { attr(object, "nobs") } -#'@export +#' @rdname nobs +#' @export nobs.mnhmm <- function(object, ...) { attr(object, "nobs") } \ No newline at end of file diff --git a/R/seqHMM-package.R b/R/seqHMM-package.R index 4c8734cd..2bb86105 100644 --- a/R/seqHMM-package.R +++ b/R/seqHMM-package.R @@ -23,7 +23,7 @@ #' @import nloptr #' @import Rcpp #' @importFrom Rcpp loadModule evalCpp -#' @importFrom stats logLik complete.cases model.matrix BIC rnorm runif vcov predict update coef qnorm quantile +#' @importFrom stats logLik complete.cases model.matrix BIC rnorm runif vcov predict update coef qnorm quantile nobs #' @importFrom TraMineR alphabet seqstatf seqdef seqlegend #' @importFrom graphics par plot.new #' @importFrom methods hasArg diff --git a/man/nobs.Rd b/man/nobs.Rd new file mode 100644 index 00000000..f9d8ab51 --- /dev/null +++ b/man/nobs.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/nobs.R +\name{nobs.hmm} +\alias{nobs.hmm} +\alias{nobs.mhmm} +\alias{nobs.nhmm} +\alias{nobs.mnhmm} +\title{Number of Observations in Hidden Markov Model} +\usage{ +\method{nobs}{hmm}(object, ...) + +\method{nobs}{mhmm}(object, ...) + +\method{nobs}{nhmm}(object, ...) + +\method{nobs}{mnhmm}(object, ...) +} +\arguments{ +\item{object}{An object of class \code{hmm}, \code{mhmm}, \code{nhmm}, or \code{mnhmm}.} + +\item{...}{Ignored.} +} +\description{ +Extract the number of non-missing observations of HMM. When computing nobs +for a multichannel model with $C$ channels, each observed value in a single +channel amounts to $1/C$ observation, i.e. a fully observed time point for +a single sequence amounts to one observation. +}