tobit2linear | R Documentation |
Type II Tobit Bayesian Additive Regression Trees implemented using MCMC
tobit2linear(
x.train,
x.test,
w.train,
w.test,
y,
n.iter = 1000,
n.burnin = 100,
censored_value = NA,
gamma0 = 0,
G0 = 1,
nzero = 6,
S0 = 12,
sigest = NA,
n.trees_outcome = 200L,
n.trees_censoring = 200L,
n.burn = 0L,
n.samples = 1L,
n.thin = 1L,
n.chains = 1L,
n.threads = 1L,
printEvery = 100L,
printCutoffs = 0L,
rngKind = "default",
rngNormalKind = "default",
rngSeed = NA_integer_,
updateState = TRUE,
tree_power_z = 2,
tree_power_y = 2,
tree_base_z = 0.95,
tree_base_y = 0.95,
node.prior = dbarts:::normal,
resid.prior = dbarts:::chisq,
proposal.probs = c(birth_death = 0.5, swap = 0, change = 0.5, birth = 0.5),
sigmadbarts = NA_real_,
print.opt = 100,
cov_prior = "Ding",
tau = 5,
mixprob = 0.5,
simultaneous_covmat = TRUE,
fast = TRUE,
nu0 = 3,
offsetz = FALSE,
quantsig = 0.9,
sparse = FALSE,
alpha_a_y = 0.5,
alpha_b_y = 1,
alpha_a_z = 0.5,
alpha_b_z = 1,
alpha_split_prior = TRUE,
tau_hyperprior = FALSE,
alpha_tau = 1,
beta_tau = 10,
jointbetagamma = TRUE
)
x.train |
The outcome model training covariate data for all training observations. Number of rows equal to the number of observations. Number of columns equal to the number of covariates. |
x.test |
The outcome model test covariate data for all test observations. Number of rows equal to the number of observations. Number of columns equal to the number of covariates. |
w.train |
The censoring model training covariate data for all training observations. Number of rows equal to the number of observations. Number of columns equal to the number of covariates. |
w.test |
The censoring model test covariate data for all test observations. Number of rows equal to the number of observations. Number of columns equal to the number of covariates. |
y |
The training data vector of outcomes. A continuous, censored outcome variable. Censored observations must be included with values equal to censored_value |
n.iter |
Number of iterations excluding burnin. |
n.burnin |
Number of burnin iterations. |
censored_value |
The value taken by censored observations |
gamma0 |
The mean of the normal prior on the covariance of the errors in the censoring and outcome models. |
G0 |
The variance of the normal prior on the covariance of the errors in the censoring and outcome models (only if cov_prior equals Omori). |
nzero |
A prior parameter which when divided by 2 gives the mean of the normal prior on phi, where phi*gamma is the variance of the errors of the outcome model. |
S0 |
A prior parameter which when divided by 2 gives the variance of the normal prior on phi, where phi*gamma is the variance of the errors of the outcome model. |
sigest |
Estimate of variance of hte error term. |
n.trees_outcome |
(dbarts control option) A positive integer giving the number of trees used in the outcome model sum-of-trees formulation. |
n.trees_censoring |
(dbarts control option) A positive integer giving the number of trees used in the censoring model sum-of-trees formulation. |
n.chains |
(dbarts control option) A positive integer detailing the number of independent chains for the dbarts sampler to use (more than one chain is unlikely to improve speed because only one sample for each call to dbarts). |
n.threads |
(dbarts control option) A positive integer controlling how many threads will be used for various internal calculations, as well as the number of chains. Internal calculations are highly optimized so that single-threaded performance tends to be superior unless the number of observations is very large (>10k), so that it is often not necessary to have the number of threads exceed the number of chains. |
printEvery |
(dbarts control option)If verbose is TRUE, every printEvery potential samples (after thinning) will issue a verbal statement. Must be a positive integer. |
printCutoffs |
(dbarts control option) A non-negative integer specifying how many of the decision rules for a variable are printed in verbose mode |
rngKind |
(dbarts control option) Random number generator kind, as used in set.seed. For type "default", the built-in generator will be used if possible. Otherwise, will attempt to match the built-in generator’s type. Success depends on the number of threads. |
rngNormalKind |
(dbarts control option) Random number generator normal kind, as used in set.seed. For type "default", the built-in generator will be used if possible. Otherwise, will attempt to match the built-in generator’s type. Success depends on the number of threads and the rngKind |
rngSeed |
(dbarts control option) Random number generator seed, as used in set.seed. If the sampler is running single-threaded or has one chain, the behavior will be as any other sequential algorithm. If the sampler is multithreaded, the seed will be used to create an additional pRNG object, which in turn will be used sequentially seed the threadspecific pRNGs. If equal to NA, the clock will be used to seed pRNGs when applicable. |
updateState |
(dbarts control option) Logical setting the default behavior for many sampler methods with regards to the immediate updating of the cached state of the object. A current, cached state is only useful when saving/loading the sampler. |
tree_power_z |
Tree prior parameter for selection model. |
tree_power_y |
Tree prior parameter for outcome model. |
tree_base_z |
Tree prior parameter for selection model. |
tree_base_y |
Tree prior parameter for outcome model. |
node.prior |
(dbarts option) An expression of the form dbarts:::normal or dbarts:::normal(k) that sets the prior used on the averages within nodes. |
resid.prior |
(dbarts option) An expression of the form dbarts:::chisq or dbarts:::chisq(df,quant) that sets the prior used on the residual/error variance |
proposal.probs |
(dbarts option) Named numeric vector or NULL, optionally specifying the proposal rules and their probabilities. Elements should be "birth_death", "change", and "swap" to control tree change proposals, and "birth" to give the relative frequency of birth/death in the "birth_death" step. |
sigmadbarts |
(dbarts option) A positive numeric estimate of the residual standard deviation. If NA, a linear model is used with all of the predictors to obtain one. |
print.opt |
Print every print.opt number of Gibbs samples. |
cov_prior |
Prior for the covariance of the error terms. If VH, apply the prior of van Hasselt (2011), N(gamma0, tau*phi), imposing dependence between gamma and phi. If Omori, apply the prior N(gamma0,G0). If mixture, then a mixture of the VH and Omori priors with probability mixprob applied to the VH prior. |
tau |
Parameter for the prior of van Hasselt (2011) on the covariance of the error terms. |
mixprob |
If cov_prior equals Mixture, then mixprob is the probability applied to the Van Hasselt covariance prior, and one minus mixprob is the probability applied to the Omori prior. |
simultaneous_covmat |
If TRUE, jointly sample the parameters that determine the covariance matrix instead of sampling from separate full conditionals. |
fast |
If equal to true, takes faster samples of z and y and makes faster approximate calculations of selection probabilities. |
nu0 |
For the inverseWishart prior Winv(nu0,c*I_2) of Ding (2014) on an unidentified unrestricted covariance matrix. nu = 3 corresponds to a uniform correlation prior. nu > 3 centers the correlation prior at 0, while nu < 3 places more prior probability on selection on unobservables. |
quantsig |
For the inverseWishart prior Winv(nu0,c*I_2) of Ding (2014). The parameter c is determined by quantsig so that the marginal prior on the standard deviation of the outcome has 90% quantile equal to an estimate from a linear tobit model or sigest if sigest is not NA. |
sparse |
If equal to TRUE, use Linero Dirichlet prior on splitting probabilities |
alpha_a_y |
Linero alpha prior parameter for outcome equation splitting probabilities |
alpha_b_y |
Linero alpha prior parameter for outcome equation splitting probabilities |
alpha_a_z |
Linero alpha prior parameter for selection equation splitting probabilities |
alpha_b_z |
Linero alpha prior parameter for selection equation splitting probabilities |
alpha_split_prior |
If true, set hyperprior for Linero alpha parameter |
accelerate |
If TRUE, add extra parameter for accelerated sampler as descibed by Omori (2007). |
The following objects are returned:
Z.mat_train |
Matrix of draws of censoring model latent outcomes for training observations. Number of rows equals number of training observations. Number of columns equals n.iter . Rows are ordered in order of observations in the training data. |
Z.mat_test |
Matrix of draws of censoring model latent outcomes for test observations. Number of rows equals number of test observations. Number of columns equals n.iter . Rows are ordered in order of observations in the test data. |
Y.mat_train |
Matrix of draws of outcome model latent outcomes for training observations. Number of rows equals number of training observations. Number of columns equals n.iter . Rows are ordered in order of observations in the training data. |
Y.mat_test |
Matrix of draws of outcome model latent outcomes for test observations. Number of rows equals number of test observations. Number of columns equals n.iter . Rows are ordered in order of observations in the test data. |
mu_y_train |
Matrix of draws of the outcome model sums of terminal nodes, i.e. f(x_i), for all training observations. Number of rows equals number of training observations. Number of columns equals n.iter . |
mu_y_test |
Matrix of draws of the outcome model sums of terminal nodes, i.e. f(x_i), for all test observations. Number of rows equals number of test observations. Number of columns equals n.iter . |
mucens_y_train |
Matrix of draws of the outcome model sums of terminal nodes, i.e. f(x_i), for all censored training observations. Number of rows equals number of censored training observations. Number of columns equals n.iter . |
muuncens_y_train |
Matrix of draws of the outcome model sums of terminal nodes, i.e. f(x_i), for all uncensored training observations. Number of rows equals number of uncensored training observations. Number of columns equals n.iter . |
mu_z_train |
Matrix of draws of the censoring model sums of terminal nodes, i.e. f(w_i), for all training observations. Number of rows equals number of training observations. Number of columns equals n.iter . |
mu_z_test |
Matrix of draws of the censoring model sums of terminal nodes, i.e. f(w_i), for all test observations. Number of rows equals number of test observations. Number of columns equals n.iter . |
train.probcens |
Matrix of draws of probabilities of training sample observations being censored. Number of rows equals number of training observations. Number of columns equals n.iter . |
test.probcens |
Matrix of draws of probabilities of test sample observations being censored. Number of rows equals number of test observations. Number of columns equals n.iter . |
cond_exp_train |
Matrix of draws of the conditional (i.e. possibly censored) expectations of the outcome for all training observations. Number of rows equals number of training observations. Number of columns equals n.iter . |
cond_exp_test |
Matrix of draws of the conditional (i.e. possibly censored) expectations of the outcome for all test observations. Number of rows equals number of test observations. Number of columns equals n.iter . |
uncond_exp_train |
Only defined if censored_value is a number. Matrix of draws of the unconditional (i.e. possibly censored) expectations of the outcome for all training observations. Number of rows equals number of training observations. Number of columns equals n.iter . |
uncond_exp_test |
Only defined if censored_value is a number. Matrix of draws of the unconditional (i.e. possibly censored) expectations of the outcome for all test observations. Number of rows equals number of test observations. Number of columns equals n.iter . |
ystar_train |
Matrix of training sample draws of the outcome assuming uncensored. Number of rows equals number of training observations. Number of columns equals n.iter . |
ystar_test |
Matrix of test sample draws of the outcome assuming uncensored . Number of rows equals number of test observations. Number of columns equals n.iter . |
zstar_train |
Matrix of training sample draws of the censoring model latent outcome. Number of rows equals number of training observations. Number of columns equals n.iter. |
zstar_test |
Matrix of test sample draws of the censoring model latent outcome. Number of rows equals number of test observations. Number of columns equals n.iter. |
ydraws_train |
Only defined if censored_value is a number. Matrix of training sample unconditional (i.e. possibly censored) draws of the outcome. Number of rows equals number of training observations. Number of columns equals n.iter . |
ydraws_test |
Only defined if censored_value is a number. Matrix of test sample unconditional (i.e. possibly censored) draws of the outcome . Number of rows equals number of test observations. Number of columns equals n.iter . |
ycond_draws_train |
List of training sample conditional (i.e. zstar >0 for draw) draws of the outcome. Number of rows equals number of training observations. Number of columns equals n.iter . |
ycond_draws_test |
List of test sample conditional (i.e. zstar >0 for draw) draws of the outcome . Number of rows equals number of test observations. Number of columns equals n.iter . |
Sigma_draws |
3 dimensional array of MCMC draws of the covariance matrix for the censoring and outcome error terms. The numbers of rows and columns equal are equal to 2. The first row and column correspond to the censoring model. The second row and column correspond to the outcome model. The number of slices equals n.iter . |
alpha_s_y_store |
For Dirichlet prior on splitting probabilities in outcome equation, vector of alpha hyperparameter draws for each iteration. |
alpha_s_z_store |
For Dirichlet prior on splitting probabilities in selection equation, vector of alpha hyperparameter draws for each iteration |
var_count_y_store |
Matrix of counts of splits on each variable in outcome observation. The number of rows is the number of potential splitting variables. The number of columns is the number of post-burn-in iterations. |
var_count_z_store |
Matrix of counts of splits on each variable in selection observation. The number of rows is the number of potential splitting variables. The number of columns is the number of post-burn-in iterations. |
s_prob_y_store |
Splitting probabilities for the outcome equation. The number of rows is the number of potential splitting variables. The number of columns is the number of post-burn-in iterations. |
s_prob_z_store |
Splitting probabilities for the selection equation. The number of rows is the number of potential splitting variables. The number of columns is the number of post-burn-in iterations. |
#example taken from Zhang, J., Li, Z., Song, X., & Ning, H. (2021). Deep Tobit networks: A novel machine learning approach to microeconometrics. Neural Networks, 144, 279-296.
#Type II tobit simulation
num_train <- 5000
#consider increasing the number of covariates
Xmat_train <- matrix(NA,nrow = num_train,
ncol = 8)
Xmat_train[,1] <- runif(num_train, min = -1, max = 1)
Xmat_train[,2] <- rf(num_train,20,20)
Xmat_train[,3] <- rbinom(num_train, size = 1, prob = 0.75)
Xmat_train[,4] <- rnorm(num_train, mean = 1, sd = 1)
Xmat_train[,5] <- rnorm(num_train)
Xmat_train[,6] <- rbinom(num_train, size = 1, prob = 0.5)
Xmat_train[,7] <- rf(num_train,20,200)
Xmat_train[,8] <- runif(num_train, min = 0, max = 2)
#it would be better to test performance of the models when there is correlation in the error terms.
varepsilon1_train <- rnorm(num_train, mean = 0, sd = sqrt(0.00025))
varepsilon2_train <- rnorm(num_train, mean = 0, sd = sqrt(0.00025))
y1star_train <- 1 - 0.75*Xmat_train[,1] + 0.75*Xmat_train[,2] -
0.5*Xmat_train[,4] - 0.5*Xmat_train[,6] - 0.25*Xmat_train[,1]^2 -
0.75*Xmat_train[,1]*Xmat_train[,4] - 0.25*Xmat_train[,1]*Xmat_train[,2] -
1*Xmat_train[,1]*Xmat_train[,6] + 0.5*Xmat_train[,2]*Xmat_train[,6] +
varepsilon1_train
y2star_train <- 1 + 0.25*Xmat_train[,4] - 0.75*Xmat_train[,6] +
0.5*Xmat_train[,7] + 0.25*Xmat_train[,8] +
0.25*Xmat_train[,4]^2 + 0.75*Xmat_train[,7]^2 + 0.5*Xmat_train[,8]^2 -
1*Xmat_train[,4]*Xmat_train[,6] + 0.5*Xmat_train[,4]*Xmat_train[,8] +
1*Xmat_train[,6]*Xmat_train[,7] - 0.25*Xmat_train[,7]*Xmat_train[,8] +
varepsilon2_train
y2obs_train <- ifelse(y1star_train>0, y2star_train,0)
#Type II tobit simulation
num_test <- 5000
#consider increasing the number of covariates
Xmat_test <- matrix(NA,nrow = num_test,
ncol = 8)
Xmat_test[,1] <- runif(num_test, min = -1, max = 1)
Xmat_test[,2] <- rf(num_test,20,20)
Xmat_test[,3] <- rbinom(num_test, size = 1, prob = 0.75)
Xmat_test[,4] <- rnorm(num_test, mean = 1, sd = 1)
Xmat_test[,5] <- rnorm(num_test)
Xmat_test[,6] <- rbinom(num_test, size = 1, prob = 0.5)
Xmat_test[,7] <- rf(num_test,20,200)
Xmat_test[,8] <- runif(num_test, min = 0, max = 2)
#it would be better to test performance of the models when there is correlation in the error terms.
varepsilon1_test <- rnorm(num_test, mean = 0, sd = sqrt(0.00025))
varepsilon2_test <- rnorm(num_test, mean = 0, sd = sqrt(0.00025))
y1star_test <- 1 - 0.75*Xmat_test[,1] + 0.75*Xmat_test[,2] -
0.5*Xmat_test[,4] - 0.5*Xmat_test[,6] - 0.25*Xmat_test[,1]^2 -
0.75*Xmat_test[,1]*Xmat_test[,4] - 0.25*Xmat_test[,1]*Xmat_test[,2] -
1*Xmat_test[,1]*Xmat_test[,6] + 0.5*Xmat_test[,2]*Xmat_test[,6] +
varepsilon1_test
y2star_test <- 1 + 0.25*Xmat_test[,4] - 0.75*Xmat_test[,6] +
0.5*Xmat_test[,7] + 0.25*Xmat_test[,8] +
0.25*Xmat_test[,4]^2 + 0.75*Xmat_test[,7]^2 + 0.5*Xmat_test[,8]^2 -
1*Xmat_test[,4]*Xmat_test[,6] + 0.5*Xmat_test[,4]*Xmat_test[,8] +
1*Xmat_test[,6]*Xmat_test[,7] - 0.25*Xmat_test[,7]*Xmat_test[,8] +
varepsilon2_test
y2obs_test <- ifelse(y1star_test>0, y2star_test,0)
y2response_test <- ifelse(y1star_test>0, 1,0)
tbartII_example <- tbart2c(Xmat_train,
Xmat_test,
Xmat_train,
Xmat_test,
y2obs_train,
n.iter=5000,
n.burnin=1000,
censored_value = 0)
pred_probs_tbart2_test <- rowMeans(tbartII_example$test.probcens)
#Training (within-sample) Prediction Realization Table
cutoff_point <- mean(y2obs_train>0)
test_bin_preds <- ifelse(1 - pred_probs_tbart2_test > cutoff_point,1,0)
#Training (within-sample) Prediction Realization Table
pred_realization_test <- rbind(cbind(table(y2response_test, test_bin_preds)/length(y2response_test),
apply(table(y2response_test, test_bin_preds)/length(y2response_test),1,sum)),
c(t(apply(table(y2response_test, test_bin_preds)/length(y2response_test),2,sum)), 1))
hit_rate_test <- pred_realization_test[1,1] +pred_realization_test[2,2]
testpreds_tbart2 <- rowMeans(tbartII_example$uncond_exp_test)
sqrt(mean((y2obs_test - testpreds_tbart2 )^2 ))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.