Skip to content

Commit

Permalink
add conditional loglik function
Browse files Browse the repository at this point in the history
  • Loading branch information
tnagler committed Jun 26, 2023
1 parent 1bc2415 commit 392fd33
Show file tree
Hide file tree
Showing 6 changed files with 477 additions and 225 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ S3method(fitted,vinereg)
S3method(predict,vinereg)
S3method(print,vinereg)
S3method(summary,vinereg)
export(cll)
export(cpit)
export(plot_effects)
export(vinereg)
Expand Down
8 changes: 8 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ cond_dist_cpp <- function(u, vinecop_r, num_threads) {
.Call(`_vinereg_cond_dist_cpp`, u, vinecop_r, num_threads)
}

cond_loglik_cpp <- function(u, vinecop_r, num_threads) {
.Call(`_vinereg_cond_loglik_cpp`, u, vinecop_r, num_threads)
}

with_parameters_cop_cpp <- function(vinecop_r, parameters) {
.Call(`_vinereg_with_parameters_cop_cpp`, vinecop_r, parameters)
}

34 changes: 34 additions & 0 deletions R/cpit.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,37 @@ cpit <- function(object, newdata, cores = 1) {
newdata <- to_uscale(newdata, object$margins)
cond_dist_cpp(newdata, object$vine, cores)
}

#' Conditional log-likelihood
#'
#' Calculates the conditional log-likelihood of the response given the covariates.
#'
#' @param object an object of class \code{vinereg}.
#' @param newdata matrix of response and covariate values for which to compute
#' the conditional distribution.
#' @param cores integer; the number of cores to use for computations.
#'
#' @export
#'
#' @examples
#' \dontshow{
#' set.seed(1)
#' }
#' # simulate data
#' x <- matrix(rnorm(500), 250, 2)
#' y <- x %*% c(1, -2)
#' dat <- data.frame(y = y, x = x, z = as.factor(rbinom(250, 2, 0.5)))
#'
#' # fit vine regression model
#' fit <- vinereg(y ~ ., dat)
#'
#' cll(fit, dat)
#' fit$stats$cll
cll <- function(object, newdata, cores = 1) {
newdata <- prepare_newdata(newdata, object, use_response = TRUE)
y <- prep_for_kde1d(newdata[, 1])
ll_marg <- sum(log(kde1d::dkde1d(y, fit$margins[[1]])))
newdata <- to_uscale(newdata, object$margins)
ll_cop <- cond_loglik_cpp(newdata, object$vine, cores)
ll_cop + ll_marg
}
34 changes: 34 additions & 0 deletions man/cll.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,39 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// cond_loglik_cpp
double cond_loglik_cpp(const Eigen::MatrixXd& u, const Rcpp::List& vinecop_r, size_t num_threads);
RcppExport SEXP _vinereg_cond_loglik_cpp(SEXP uSEXP, SEXP vinecop_rSEXP, SEXP num_threadsSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< const Eigen::MatrixXd& >::type u(uSEXP);
Rcpp::traits::input_parameter< const Rcpp::List& >::type vinecop_r(vinecop_rSEXP);
Rcpp::traits::input_parameter< size_t >::type num_threads(num_threadsSEXP);
rcpp_result_gen = Rcpp::wrap(cond_loglik_cpp(u, vinecop_r, num_threads));
return rcpp_result_gen;
END_RCPP
}
// with_parameters_cop_cpp
Rcpp::List with_parameters_cop_cpp(const Rcpp::List& vinecop_r, const Eigen::VectorXd parameters);
RcppExport SEXP _vinereg_with_parameters_cop_cpp(SEXP vinecop_rSEXP, SEXP parametersSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< const Rcpp::List& >::type vinecop_r(vinecop_rSEXP);
Rcpp::traits::input_parameter< const Eigen::VectorXd >::type parameters(parametersSEXP);
rcpp_result_gen = Rcpp::wrap(with_parameters_cop_cpp(vinecop_r, parameters));
return rcpp_result_gen;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"_vinereg_fit_margins_cpp", (DL_FUNC) &_vinereg_fit_margins_cpp, 9},
{"_vinereg_select_dvine_cpp", (DL_FUNC) &_vinereg_select_dvine_cpp, 11},
{"_vinereg_cond_quantile_cpp", (DL_FUNC) &_vinereg_cond_quantile_cpp, 4},
{"_vinereg_cond_dist_cpp", (DL_FUNC) &_vinereg_cond_dist_cpp, 3},
{"_vinereg_cond_loglik_cpp", (DL_FUNC) &_vinereg_cond_loglik_cpp, 3},
{"_vinereg_with_parameters_cop_cpp", (DL_FUNC) &_vinereg_with_parameters_cop_cpp, 2},
{NULL, NULL, 0}
};

Expand Down
Loading

0 comments on commit 392fd33

Please sign in to comment.