diff --git a/DESCRIPTION b/DESCRIPTION index cdaba42..b8736b7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,6 +29,6 @@ Suggests: knitr, tibble, VignetteBuilder: knitr -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 Config/testthat/edition: 3 Roxygen: list(markdown = TRUE) diff --git a/NAMESPACE b/NAMESPACE index b77180b..4152177 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -16,14 +16,17 @@ S3method(loglik,likelihood_contr_model) S3method(loglik,likelihood_exact_normal) S3method(loglik,likelihood_exact_weibull) S3method(loglik,likelihood_name) +S3method(pval,hypothesis_test) S3method(sampler,likelihood_model) S3method(score,likelihood_contr_model) S3method(score,likelihood_exact_normal) S3method(score,likelihood_exact_weibull) S3method(score,likelihood_model) export(assumptions) +export(dof) export(fim) export(hess_loglik) +export(hypothesis_test) export(is_likelihood_model) export(likelihood_contr_model) export(likelihood_exact_normal) @@ -31,6 +34,7 @@ export(likelihood_exact_weibull) export(likelihood_name) export(loglik) export(lrt) +export(pval) export(score) importFrom(MASS,ginv) importFrom(R6,R6Class) @@ -46,5 +50,9 @@ importFrom(numDeriv,grad) importFrom(numDeriv,hessian) importFrom(stats,nobs) importFrom(stats,optim) +importFrom(stats,pchisq) +importFrom(stats,pf) +importFrom(stats,qnorm) +importFrom(stats,qt) importFrom(stats,var) importFrom(utils,modifyList) diff --git a/R/likelihood_contr_model.R b/R/likelihood_contr_model.R index 5305835..04bda01 100644 --- a/R/likelihood_contr_model.R +++ b/R/likelihood_contr_model.R @@ -7,12 +7,11 @@ #' - `score`: computes the score of the model #' - `hess_loglik`: computes the Hessian of the log-likelihood given #' a data frame and parameters -#' - `fim`: computes the Fisher information matrix (expectation) #' -#' It provides methods for computing the log-likelihood, score, the -#' Hessian of log-likelihood, and the Fisher information matrix (FIM). +#' It provides methods for computing the log-likelihood, score, and +#' the Hessian of log-likelihood. #' -#' It also allows for different *likelihood contributions* depending +#' It also allows for different likelihood contributions depending #' on the observation type of a row in the data frame. For example, #' if the data frame contains both exact and interval-censored #' observations, then the log-likelihood contributions for exact diff --git a/R/likelihood_name.R b/R/likelihood_name.R index fc4605d..0c69d6e 100644 --- a/R/likelihood_name.R +++ b/R/likelihood_name.R @@ -68,19 +68,17 @@ assumptions.likelihood_name <- function(model, ...) { #' parameters. #' @export loglik.likelihood_name <- function(model, ...) { + x <- as.data.frame(df)[[model$ob_col]] + n <- length(x) + stopifnot(n > 0) + if (is.null(model$type_col)) { function(df, par, ...) { - x <- as.data.frame(df)[[model$ob_col]] - n <- length(x) - stopifnot(n > 0, all(x > 0)) do.call(paste0("d", model$dist_name), c(list(x), par)) sum(do.call(paste0("d", model$dist_name), c(list(x), par), log.p = TRUE)) } } else { function(df, par, ...) { - x <- as.data.frame(df)[[model$ob_col]] - n <- length(x) - stopifnot(n > 0) type <- as.character(df[[model$type_col]]) ifelse(type == "left", do.call(paste0("p", model$dist_name), c(list(x), par)), diff --git a/man/dof.Rd b/man/dof.Rd new file mode 100644 index 0000000..5fc55e6 --- /dev/null +++ b/man/dof.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tests.R +\name{dof} +\alias{dof} +\title{Generic method for extracting the degrees of freedom from a hypothesis test} +\usage{ +dof(x, ...) +} +\arguments{ +\item{x}{a hypothesis test object} + +\item{...}{additional arguments to pass into the method} +} +\value{ +degrees of freedom +} +\description{ +Generic method for extracting the degrees of freedom from a hypothesis test +} diff --git a/man/hess_loglik.likelihood_exact_normal.Rd b/man/hess_loglik.likelihood_exact_normal.Rd index 283607e..0ffcd37 100644 --- a/man/hess_loglik.likelihood_exact_normal.Rd +++ b/man/hess_loglik.likelihood_exact_normal.Rd @@ -11,9 +11,6 @@ distribution} \item{...}{additional arguments (not used)} \item{x}{data (simple random sample)} - -\item{observed}{whether to return the observed fisher information, default is -\code{TRUE}} } \description{ Fisher information matrix generator given data \code{x} for the normal diff --git a/man/hypothesis_test.Rd b/man/hypothesis_test.Rd new file mode 100644 index 0000000..71a3261 --- /dev/null +++ b/man/hypothesis_test.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tests.R +\name{hypothesis_test} +\alias{hypothesis_test} +\title{Hypothesis test structure} +\usage{ +hypothesis_test(stat, p.value, dof, superclasses = NULL, ...) +} +\arguments{ +\item{stat}{test statistic} + +\item{p.value}{p-value} + +\item{dof}{degrees of freedom} +} +\value{ +hypothesis test +} +\description{ +Hypothesis test structure +} +\examples{ +# create a hypothesis test +test <- hypothesis_test(stat = 1.96, p.value = 0.05, dof = 1) +# print the test +test +# extract the p-value +pval(test) +# extract the degrees of freedom +dof(test) +# extract the test statistic +stat(test) +# check if the test is significant at the 5\% level +is_significant_at(test, 0.05) +} diff --git a/man/likelihood_contr_model.Rd b/man/likelihood_contr_model.Rd index f223a7c..e06bdde 100644 --- a/man/likelihood_contr_model.Rd +++ b/man/likelihood_contr_model.Rd @@ -11,13 +11,12 @@ A likelihood_model should provide the following methods: \item \code{score}: computes the score of the model \item \code{hess_loglik}: computes the Hessian of the log-likelihood given a data frame and parameters -\item \code{fim}: computes the Fisher information matrix (expectation) } -It provides methods for computing the log-likelihood, score, the -Hessian of log-likelihood, and the Fisher information matrix (FIM). +It provides methods for computing the log-likelihood, score, and +the Hessian of log-likelihood. -It also allows for different \emph{likelihood contributions} depending +It also allows for different likelihood contributions depending on the observation type of a row in the data frame. For example, if the data frame contains both exact and interval-censored observations, then the log-likelihood contributions for exact diff --git a/man/lrt.Rd b/man/lrt.Rd index 409a9b6..fe68639 100644 --- a/man/lrt.Rd +++ b/man/lrt.Rd @@ -4,20 +4,22 @@ \alias{lrt} \title{Likelihood ratio test} \usage{ -lrt(ll1, ll2, data, par1, par2, df = NULL, ...) +lrt(model1, model2, data, par1, par2, dof = NULL, ...) } \arguments{ -\item{data}{data} +\item{data}{the data which the likelihood models are fitted to} -\item{par1}{parameters for first model} - -\item{par2}{parameters for second model} +\item{dof}{degrees of freedom} \item{...}{additional arguments} -\item{model1}{first likelihood model} +\item{null}{the null (simpler) likelihood model} + +\item{alt}{the more complicated model} + +\item{null_par}{parameters for null model} -\item{model2}{second likelihood model} +\item{alt_par}{parameters for alternative model} } \value{ likelihood ratio test diff --git a/man/pval.Rd b/man/pval.Rd new file mode 100644 index 0000000..9d9c828 --- /dev/null +++ b/man/pval.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tests.R +\name{pval} +\alias{pval} +\title{Generic method for extracting the p-value from a hypothesis test} +\usage{ +pval(x, ...) +} +\arguments{ +\item{x}{a hypothesis test object} + +\item{...}{additional arguments to pass into the method} +} +\value{ +p-value +} +\description{ +Generic method for extracting the p-value from a hypothesis test +} diff --git a/man/pval.hypothesis_test.Rd b/man/pval.hypothesis_test.Rd new file mode 100644 index 0000000..1fce0c9 --- /dev/null +++ b/man/pval.hypothesis_test.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tests.R +\name{pval.hypothesis_test} +\alias{pval.hypothesis_test} +\title{p-value method for hypothesis tests} +\usage{ +\method{pval}{hypothesis_test}(x, ...) +} +\arguments{ +\item{x}{a hypothesis test} + +\item{...}{additional arguments} +} +\value{ +p-value +} +\description{ +p-value method for hypothesis tests +}