Package 'copulareg'

Title: Copula Regression
Description: Fits multivariate models in an R-vine pair copula construction framework, in such a way that the conditional copula can be easily evaluated. In addition, the package implements functionality to compute or approximate the conditional expectation via the conditional copula.
Authors: Simon Boge Brant [aut, cre, cph], Ingrid Hobæk Haff [aut]
Maintainer: Simon Boge Brant <[email protected]>
License: MIT + file LICENCE
Version: 0.1.0
Built: 2024-09-12 10:50:03 UTC
Source: CRAN

Help Index


copulareg

Description

This function fits joint distributions with an R-vine pair copula structure, that is constructed in a specific way so that the conditional density and distribution of the variable y can be computed explicitly.

Usage

copulareg.default(
  y,
  x,
  var_type_y,
  var_type_x,
  family_set = c("gaussian", "clayton", "gumbel"),
  extra_x = NULL,
  extra_y = NULL
)

Arguments

y

A vector of n observations of the (univariate) outcome variable y

x

A (n x p) matrix of n observations of p covariates

var_type_y

A character that has to be specified as "d" or "c" to indicate whether y is discrete or continuous, respectively.

var_type_x

A vector of p characters that have to take the value "c" or "d" to indicate whether each margin of the covariates is discrete or continuous.

family_set

A vector of strings that specifies the set of pair-copula families that the fitting algorithm chooses from. For an overview of which values that can be specified, see the documentation for bicop.

extra_x

Optional extra values of x to use for estimating the margins of the covariates.

extra_y

Optional extra values of y to use to estimate the margin of y.

Value

An object of the class 'copulareg', which contains an object of the class 'vinecop', see rvinecopulib::vinecop, an enviroment that stores the transformed variables at each level of the pair copula for the training data, the marginal distributions of the covariates, the marginal distribution of the response, and the y-training values.

Examples

# Compile some test data
data('ChickWeight') 
set.seed(10)
tr <- sample(c(TRUE, FALSE), nrow(ChickWeight), TRUE, c(0.7, 0.3))
y_tr <- ChickWeight$weight[tr]
y_te <- ChickWeight$weight[!tr]
x_tr <- apply(ChickWeight[tr, -1], 2, as.numeric)
x_te <- apply(ChickWeight[!tr, -1], 2, as.numeric)
var_type_x <- apply(x_tr, 2,
                    function(x) if(length(unique(x)) < 10) "d" else "c")

# Fit model to training data
md <- copulareg::copulareg(y_tr, x_tr, "c", var_type_x)

# Predict for a new data matrix
pred <- predict(md, new_x = x_te)

# Plot residuals for test data against covariates
plot(data.frame(residual = y_te - pred, x_te))

# Plot residuals against fitted
plot(md)

# Plot prediction error against predicted values
plot(md, new_x=x_te, new_y=y_te)

predict

Description

Plot the residuals against the fitted values for a copulareg object, or predicted values against the prediction error

Usage

## S3 method for class 'copulareg'
plot(x, new_x = NULL, new_y = NULL, ...)

Arguments

x

Model fit as returned by copulareg

new_x

optional matrix of covariate values to compute the predicted values of the outcome for. If not specified, the fitted values for the training sample are used.

new_y

Optional vector if the plot should show predicted values and prediction error.

...

additional parameters to plot.

Value

No return value, called to produce plot.


predict

Description

Computes predictions based on a fitted copulareg model.

Usage

## S3 method for class 'copulareg'
predict(object, new_x = NULL, eps = 0.01, cont_method = "Localmedian", ...)

Arguments

object

Model fit as returned by copulareg

new_x

optional matrix of covariate values to compute the predicted values of the outcome for. If not specified, the predicted values for the training sample is returned.

eps

Interval between each interpolation point when integrating to evaluate the predicted value of y, in the case where y is continuous. If y is discrete this parameter is ignored.

cont_method

Specifies the method used to compute the expected values. Can be specified as 'Localmedian' or 'Trapezoidalsurv'. The first method divides the range of the observed values of y into subintervals according to the argument 'eps', where the sub-integral is approximated as the measure of the interval weighted by the local median on the interval. The second method computes the integral by integrating the survival function using the trapezoidal rule, by transforming the outcome into a positive variable by adding a constant.

...

unused.

Value

A vector of predicted y-values for each row of new_x, or for the training data if new_x is not supplied.