diff --git a/DESCRIPTION b/DESCRIPTION new file mode 100644 index 0000000..eb87624 --- /dev/null +++ b/DESCRIPTION @@ -0,0 +1,27 @@ +Package: bartBMA +Type: Package +Title: Bayesian Additive Regression Trees using Bayesian Model + Averaging +Version: 1.0 +Date: 2020-02-05 +Author: Belinda Hernandez [aut, cre] + Adrian E. Raftery [aut] + Stephen R Pennington [aut] + Andrew C. Parnell [aut] + Eoghan O'Neill [ctb] +Maintainer: Belinda Hernandez +Description: "BART-BMA Bayesian Additive Regression Trees using Bayesian Model Averaging" (Hernandez B, Raftery A.E., Parnell A.C. (2018) ) is an extension to the original BART sum-of-trees model (Chipman et al 2010). BART-BMA differs to the original BART model in two main aspects in order to implement a greedy model which + will be computationally feasible for high dimensional data. Firstly BART-BMA uses a greedy search for the best split points and variables when growing decision trees within each sum-of-trees + model. This means trees are only grown based on the most predictive set of split rules. Also rather than using Markov chain Monte Carlo (MCMC), BART-BMA uses a greedy implementation of Bayesian Model Averaging called Occam's Window + which take a weighted average over multiple sum-of-trees models to form its overall prediction. This means that only the set of sum-of-trees for which there is high support from the data + are saved to memory and used in the final model. +License: GPL (>= 2) +Imports: Rcpp (>= 1.0.0), mvnfast, Rdpack +RdMacros: Rdpack +LinkingTo: Rcpp, RcppArmadillo, BH +RoxygenNote: 7.0.2 +Encoding: UTF-8 +NeedsCompilation: yes +Packaged: 2020-03-05 17:24:23 UTC; HERNANDB +Repository: CRAN +Date/Publication: 2020-03-13 11:50:05 UTC diff --git a/MD5 b/MD5 new file mode 100644 index 0000000..bb4deff --- /dev/null +++ b/MD5 @@ -0,0 +1,51 @@ +e79e25a3ca77b49109ac20c74932c886 *DESCRIPTION +050c1fece042b3a719216d2848e814ca *NAMESPACE +6a12c83aef7560428f3d29c2c144d1e4 *R/ITEs_CATT_bartBMA_exact_par.R +92ff2fa242c053b94651e8b646cf06b3 *R/ITEs_bartBMA.R +7bd973c88993ce2dac864eb9a753cf2b *R/ITEs_bartBMA_exact_par.R +0bb2a28f04260a5d13f0ae86bea1d465 *R/RcppExports.R +b469aeb2d1d9df01bed0c42abbc9cf3a *R/bartBMA.R +97d916be4ab7171ee253805cc456a66e *R/bartBMA_with_ITEs_exact_par.R +d1d66c192ce6ba9469676a5d489d1923 *R/pred_expectation_intervals_bbma_GS.R +e24bafa48615c0f4dac8534a1e4078fc *R/pred_intervals_GS.R +056b89f61600dad068e7f399bd6aebba *R/pred_intervals_new_initials_GS.R +328d77c4357b9e1b056c55b7dab13dcd *R/pred_ints_exact.R +a1d1ca44d8039ef4f12ca2a8b20280a2 *R/pred_ints_exact_par.R +75a3eceb040ee0b38e3889a7ace10c22 *R/pred_means_bbma_GS.R +5b4a9bc8f2241125b7d4ef4ac5e019be *R/pred_means_new_initials_GS.R +bbf6c8d6e546d65ac5abcc3450436350 *R/predict_bartBMA.R +ddb16845ae209507e80ce977d4b8a212 *R/predict_probit_bartBMA.R +a91ede8823bc890a85d4e15d829035b8 *R/preds_bbma_lin_alg.R +fbcc63b72bb12bcd48d56d09f9480aff *R/printbartBMA.R +7518970253acc7f578e62e8a7252c396 *R/probit_bartBMA.R +b64587a6d4bb889d5b49ed2a34531af2 *R/varImpScores.R +880a57c4dc5377fa41fc07934dd2049f *R/varIncProb.R +ac71c648b512113b453627ee6abe2715 *README.md +0ba955f4ce5c2dc74fcc88583ecdfc4d *build/partial.rdb +e76313c15640dd3cb06af10c654c6265 *inst/REFERENCES.bib +9e1662c7a418c3b34668f8b349511ccf *man/ITEs_CATT_bartBMA_exact_par.Rd +92fbe8d5d7cbf498405db3373b083fca *man/ITEs_bartBMA.Rd +53cbfe1318b36acfe0443de07b74bb61 *man/ITEs_bartBMA_exact_par.Rd +40b7f0effe06deaebe6c1a039b4bcc69 *man/bartBMA.Rd +b21688a47846f395a32dfc6fe141b05a *man/bartBMA_with_ITEs_exact_par.Rd +60534399cef508d15915e7bbe65e8941 *man/pred_expectation_intervals_bbma_GS.Rd +bdbfd7702601d391e6c572c46ae5edc4 *man/pred_intervals_bbma_GS.Rd +3c44c2348ec50a28ee6ab996109940ad *man/pred_intervals_new_initials_GS.Rd +6b0178edbae0d6fe45fd4b763ea16986 *man/pred_ints_exact.Rd +fb1ef610de44192dc7e9a3a2dcf0f54a *man/pred_ints_exact_par.Rd +501ffd048a6ed9f26912e08ededd36f4 *man/pred_means_bbma_GS.Rd +fb670a97fd7d45009b7a8518ca0acba2 *man/pred_means_bbma_new_initials_GS.Rd +7646d42ef36befdd42219d6af14950f6 *man/predict_bartBMA.Rd +9217efa5ce4ef018684a39b2f1f8b4e4 *man/predict_probit_bartBMA.Rd +e58339f97f0eecb8b5ae18568fd5fe72 *man/preds_bbma_lin_alg.Rd +650b23959ba3b4b0906fd8a32933c5f4 *man/probit_bartBMA.Rd +7f6e136681063b52c38dc63ee6ad267f *man/varImpScores.Rd +1e82e3ed46c543494b1f6aa2c5054d9a *man/varIncProb.Rd +54030fd124e8b4d989c15438630619ae *src/161014_predict_function.cpp +91c3add20d1afe917750f259b28ca1e4 *src/201014_BART_BMA_var_importance.cpp +ab9c51353963493f7c72113cd5c3411e *src/BARTBMA_SumTreeLikelihood.cpp +4abb712c55946e7fb752a08868edeaf9 *src/GibbsSamplerTrainingTestData.cpp +61059660eb073d93e00e8ee054237071 *src/Makevars +61059660eb073d93e00e8ee054237071 *src/Makevars.win +1a8175f4305d0f4e56116609f9531738 *src/RcppExports.cpp +d41d8cd98f00b204e9800998ecf8427e *src/bbma_preds_samples.cpp diff --git a/NAMESPACE b/NAMESPACE new file mode 100644 index 0000000..de2b09e --- /dev/null +++ b/NAMESPACE @@ -0,0 +1,33 @@ +# Generated by roxygen2: do not edit by hand + +S3method(bartBMA,default) +S3method(probit_bartBMA,default) +export(ITEs_CATT_bartBMA_exact_par) +export(ITEs_bartBMA) +export(ITEs_bartBMA_exact_par) +export(bartBMA) +export(bartBMA.default) +export(bartBMA_with_ITEs_exact_par) +export(pred_expectation_intervals_bbma_GS) +export(pred_intervals_bbma_GS) +export(pred_intervals_new_initials_GS) +export(pred_ints_exact) +export(pred_ints_exact_par) +export(pred_means_bbma_GS) +export(pred_means_bbma_new_initials_GS) +export(predict_bartBMA) +export(predict_probit_bartBMA) +export(preds_bbma_lin_alg) +export(probit_bartBMA) +export(probit_bartBMA.default) +export(varImpScores) +export(varIncProb) +importFrom(Rcpp,evalCpp) +importFrom(Rdpack,reprompt) +importFrom(stats,pnorm) +importFrom(stats,qchisq) +importFrom(stats,qnorm) +importFrom(stats,quantile) +importFrom(stats,sd) +importFrom(utils,head) +useDynLib(bartBMA, .registration = TRUE) diff --git a/R/ITEs_CATT_bartBMA_exact_par.R b/R/ITEs_CATT_bartBMA_exact_par.R new file mode 100644 index 0000000..fd021d9 --- /dev/null +++ b/R/ITEs_CATT_bartBMA_exact_par.R @@ -0,0 +1,137 @@ +#' @title Estimate ITEs, CATE, CATT, CATNT and obtain credible intervals (in-sample or out-of-sample). +#' +#' @description This function takes a set of sum of tree models obtained from ITEs_bartBMA, and then estimates ITEs, and the CATE, CATT, and CATNT and obtains prediction intervals +#' @param object Output from ITEs_bartBMA of class ITE_ests.bartBMA. +#' @param l_quant Lower quantile of credible intervals for the ITEs, CATT, CATNT. +#' @param u_quant Upper quantile of credible intervals for the ITEs, CATT, CATNT. +#' @param newdata Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object. +#' @param update_resids Option for whether to update the partial residuals in the gibbs sampler. If equal to 1, updates partial residuals, if equal to zero, does not update partial residuals. The defaullt setting is to update the partial residuals. +#' @param num_cores Number of cores used in parallel. +#' @param root_alg_precision The algorithm should obtain approximate bounds that are within the distance root_alg_precision of the true quantile for the chosen average of models. +#' @param training_data The training data matrix +#' @param zvec The treatment indicator vector. Training data treatment vector for insample predictions, test data treatment vector for out of sample predictions. +#' @export +#' @return The output is a list of length 8: +#' \item{ITE_intervals}{A 3 by n matrix, where n is the number of observations. The first row gives the l_quant*100 quantiles of the individual treatment effects. The second row gives the medians of the ITEs. The third row gives the u_quant*100 quantiles of the ITEs.} +#' \item{ITE_estimates}{An n by 1 matrix containing the Individual Treatment Effect estimates.} +#' \item{CATE_estimate}{The Conditional Average Treatment Effect Estimate} +#' \item{CATE_Interval}{A 3 by 1 matrix. The first element is the l_quant*100 quantile of the CATE distribution, the second element is the median of the CATE distribution, and the thied element is the u_quant*100 quantile of the CATE distribution.} +#' \item{CATT_estimate}{The Conditional Average Treatment Effect on the Treated Estimate} +#' \item{CATT_Interval}{A 3 by 1 matrix. The first element is the l_quant*100 quantile of the CATT distribution, the second element is the median of the CATT distribution, and the thied element is the u_quant*100 quantile of the CATT distribution.} +#' \item{CATNT_estimate}{The Conditional Average Treatment Effect on the Not Treated Estimate} +#' \item{CATNT_Interval}{A 3 by 1 matrix. The first element is the l_quant*100 quantile of the CATNT distribution, the second element is the median of the CATNT distribution, and the thied element is the u_quant*100 quantile of the CATNT distribution.} +#' @examples +#' \dontrun{ +#' #Example of BART-BMA for ITE estimation +#' # Applied to data simulations from Hahn et al. (2020, Bayesian Analysis) +#' # "Bayesian Regression Tree Models for Causal Inference: Regularization, +#' # Confounding, and Heterogeneous Effects +#' n <- 250 +#' x1 <- rnorm(n) +#' x2 <- rnorm(n) +#' x3 <- rnorm(n) +#' x4 <- rbinom(n,1,0.5) +#' x5 <- as.factor(sample( LETTERS[1:3], n, replace=TRUE)) +#' +#' p= 0 +#' xnoise = matrix(rnorm(n*p), nrow=n) +#' x5A <- ifelse(x5== 'A',1,0) +#' x5B <- ifelse(x5== 'B',1,0) +#' x5C <- ifelse(x5== 'C',1,0) +#' +#' x_covs_train <- cbind(x1,x2,x3,x4,x5A,x5B,x5C,xnoise) +#' +#' #Treatment effect +#' #tautrain <- 3 +#' tautrain <- 1+2*x_covs_train[,2]*x_covs_train[,4] +#' +#' #Prognostic function +#' mutrain <- 1 + 2*x_covs_train[,5] -1*x_covs_train[,6]-4*x_covs_train[,7] + +#' x_covs_train[,1]*x_covs_train[,3] +#' sd_mtrain <- sd(mutrain) +#' utrain <- runif(n) +#' #pitrain <- 0.8*pnorm((3*mutrain/sd_mtrain)-0.5*x_covs_train[,1])+0.05+utrain/10 +#' pitrain <- 0.5 +#' ztrain <- rbinom(n,1,pitrain) +#' ytrain <- mutrain + tautrain*ztrain +#' #pihattrain <- pbart(x_covs_train,ztrain )$prob.train.mean +#' +#' #set lower and upper quantiles for intervals +#' lbound <- 0.025 +#' ubound <- 0.975 +#' +#' +#' trained_bbma <- ITEs_bartBMA(x_covariates = x_covs_train, +#' z_train = ztrain, +#' y_train = ytrain) +#' +#' example_output <- ITEs_CATT_bartBMA_exact_par(trained_bbma[[2]], +#' l_quant = lbound, +#' u_quant= ubound, +#' training_data = x_covs_train, +#' zvec = ztrain, +#' num_cores = 1) +#'} + + +ITEs_CATT_bartBMA_exact_par <-function(object,#min_possible,max_possible, + l_quant,u_quant,newdata=NULL,update_resids=1, + num_cores=1, + root_alg_precision=0.00001, + training_data,zvec){ + + + if(is.null(newdata) && length(object)==16){ + #if test data specified separately + ret<-pred_ints_ITE_CATT_outsamp_par(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,#min_possible, max_possible, + object$nrowTrain, + nrow(object$test_data),object$a,object$sigma,0,object$nu, + object$lambda,#diff_inital_resids, + object$test_data,l_quant,u_quant,num_cores, + root_alg_precision,training_data,zvec + ) + + + + }else{if(is.null(newdata) && length(object)==14){ + #else return Pred Ints for training data + ret <- pred_ints_ITE_CATT_insamp_par(object$sumoftrees, + object$obs_to_termNodesMatrix, + object$response,object$bic,#min_possible, max_possible, + object$nrowTrain,#nrow(object$test_data), + object$a,object$sigma,0,object$nu, + object$lambda,#diff_inital_resids,object$test_data, + l_quant,u_quant, + num_cores,root_alg_precision,training_data,zvec + ) + + }else{ + #if test data included in call to object + ret<-pred_ints_ITE_CATT_outsamp_par(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,#min_possible, max_possible, + object$nrowTrain, + nrow(newdata), object$a,object$sigma,0,object$nu, + object$lambda,#diff_inital_resids, + newdata,l_quant,u_quant,num_cores, + root_alg_precision,training_data,zvec + ) + + }} + + #PI<-apply(draws_from_mixture,2,function(x)quantile(x,probs=c(l_quant,0.5,u_quant))) + + + + #each row is a vector drawn from the mixture distribution + + + names(ret)<-c("ITE_intervals", + "ITE_estimates", + "CATE_estimate", + "CATE_Interval", + "CATT_estimate", + "CATT_Interval", + "CATNT_estimate", + "CATNT_Interval") + + ret +} \ No newline at end of file diff --git a/R/ITEs_bartBMA.R b/R/ITEs_bartBMA.R new file mode 100644 index 0000000..de0976a --- /dev/null +++ b/R/ITEs_bartBMA.R @@ -0,0 +1,102 @@ +#' @title ITE Predictions (in-sample) using bartBMA and the method described by Hill (2011) +#' +#' @description This function produces ITE Predictions (in-sample) using bartBMA and the method described by Hill (2011). +#' @param x_covariates Covaraite matrix for training bartBMA. +#' @param z_train treatment vector for traiing bartBMA. +#' @param y_train outcome vector for training bartBMA. +#' @param a This is a parameter that influences the variance of terminal node parameter values. Default value a=3. +#' @param nu This is a hyperparameter in the distribution of the variance of the error term. THe inverse of the variance is distributed as Gamma (nu/2, nu*lambda/2). Default value nu=3. +#' @param sigquant Calibration quantile for the inverse chi-squared prior on the variance of the error term. +#' @param c This determines the size of Occam's Window +#' @param pen This is a parameter used by the Pruned Exact Linear Time Algorithm when finding changepoints. Default value pen=12. +#' @param num_cp This is a number between 0 and 100 that determines the proportion of changepoints proposed by the changepoint detection algorithm to keep when growing trees. Default num_cp=20. +#' @param x.test Test data covariate matrix. Default x.test=matrix(0.0,0,0). +#' @param num_rounds Number of trees. (Maximum number of trees in a sum-of-tree model). Default num_rounds=5. +#' @param alpha Parameter in prior probability of tree node splitting. Default alpha=0.95 +#' @param beta Parameter in prior probability of tree node splitting. Default beta=1 +#' @param split_rule_node Binary variable. If equals 1, then find a new set of potential splitting points via a changepoint algorithm after adding each split to a tree. If equals zero, use the same set of potential split points for all splits in a tree. Default split_rule_node=0. +#' @param gridpoint Binary variable. If equals 1, then a grid search changepoint detection algorithm will be used. If equals 0, then the Pruned Exact Linear Time (PELT) changepoint detection algorithm will be used (Killick et al. 2012). Default gridpoint=0. +#' @param maxOWsize Maximum number of models to keep in Occam's window. Default maxOWsize=100. +#' @param num_splits Maximum number of splits in a tree +#' @param gridsize This integer determines the size of the grid across which to search if gridpoint=1 when finding changepoints for constructing trees. +#' @param zero_split Binary variable. If equals 1, then zero split trees can be included in a sum-of-trees model. If equals zero, then only trees with at least one split can be included in a sum-of-trees model. +#' @param only_max_num_trees Binary variable. If equals 1, then only sum-of-trees models containing the maximum number of trees, num_rounds, are selected. If equals 0, then sum-of-trees models containing less than num_rounds trees can be selected. The default is only_max_num_trees=1. +#' @param min_num_obs_for_split This integer determines the minimum number of observations in a (parent) tree node for the algorithm to consider potential splits of the node. +#' @param min_num_obs_after_split This integer determines the minimum number of observations in a child node resulting from a split in order for a split to occur. If the left or right chikd node has less than this number of observations, then the split can not occur. +#' @export +#' @return A list of length 2. The first element is A vector of Individual Treatment Effect Estimates. The second element is a bartBMA object (i.e. the trained BART-BMA model). +#' @examples +#' n <- 250 +#' x1 <- rnorm(n) +#' x2 <- rnorm(n) +#' x3 <- rnorm(n) +#' x4 <- rbinom(n,1,0.5) +#' x5 <- as.factor(sample( LETTERS[1:3], n, replace=TRUE)) +#' +#' p= 0 +#' xnoise = matrix(rnorm(n*p), nrow=n) +#' x5A <- ifelse(x5== 'A',1,0) +#' x5B <- ifelse(x5== 'B',1,0) +#' x5C <- ifelse(x5== 'C',1,0) +#' +#' x_covs_train <- cbind(x1,x2,x3,x4,x5A,x5B,x5C,xnoise) +#' +#' #Treatment effect +#' #tautrain <- 3 +#' tautrain <- 1+2*x_covs_train[,2]*x_covs_train[,4] +#' +#' #Prognostic function +#' mutrain <- 1 + 2*x_covs_train[,5] -1*x_covs_train[,6]-4*x_covs_train[,7] + +#' x_covs_train[,1]*x_covs_train[,3] +#' sd_mtrain <- sd(mutrain) +#' utrain <- runif(n) +#' #pitrain <- 0.8*pnorm((3*mutrain/sd_mtrain)-0.5*x_covs_train[,1])+0.05+utrain/10 +#' pitrain <- 0.5 +#' ztrain <- rbinom(n,1,pitrain) +#' ytrain <- mutrain + tautrain*ztrain +#' #pihattrain <- pbart(x_covs_train,ztrain )$prob.train.mean +#' +#' #set lower and upper quantiles for intervals +#' lbound <- 0.025 +#' ubound <- 0.975 +#' +#' example_output <- ITEs_bartBMA(x_covariates = x_covs_train, +#' z_train = ztrain, +#' y_train = ytrain) + +ITEs_bartBMA<-function(x_covariates,z_train ,y_train, + a=3,nu=3,sigquant=0.9,c=1000, + pen=12,num_cp=20,x.test=matrix(0.0,0,0), + num_rounds=5,alpha=0.95,beta=2,split_rule_node=0, + gridpoint=0,maxOWsize=100,num_splits=5,gridsize=10,zero_split=1,only_max_num_trees=1, + min_num_obs_for_split=2, min_num_obs_after_split=2){ + + x_train <- cbind(z_train,x_covariates) + + trained_bart_BMA <- bartBMA(x.train= x_train,y.train =y_train , + a=a,nu=nu,sigquant=sigquant,c=c, + pen=pen,num_cp=num_cp,x.test=x.test, + num_rounds=num_rounds,alpha=alpha,beta=beta,split_rule_node=split_rule_node, + gridpoint=gridpoint,maxOWsize=maxOWsize,num_splits=num_splits,gridsize=gridsize,zero_split=zero_split,only_max_num_trees=only_max_num_trees, + min_num_obs_for_split=min_num_obs_for_split, min_num_obs_after_split=min_num_obs_after_split) + + if(nrow(x.test)==0){ + all_treated_data <- cbind(rep(1,nrow(x_covariates)), x_covariates) + all_control_data <- cbind(rep(0,nrow(x_covariates)), x_covariates) + + preds_treated<-get_BART_BMA_test_predictions(all_treated_data,trained_bart_BMA$bic,trained_bart_BMA$sumoftrees,trained_bart_BMA$y_minmax) + preds_control<-get_BART_BMA_test_predictions(all_control_data,trained_bart_BMA$bic,trained_bart_BMA$sumoftrees,trained_bart_BMA$y_minmax) + }else{ + all_treated_data <- cbind(rep(1,nrow(x.test)), x.test) + all_control_data <- cbind(rep(0,nrow(x.test)), x.test) + + preds_treated<-get_BART_BMA_test_predictions(all_treated_data,trained_bart_BMA$bic,trained_bart_BMA$sumoftrees,trained_bart_BMA$y_minmax) + preds_control<-get_BART_BMA_test_predictions(all_control_data,trained_bart_BMA$bic,trained_bart_BMA$sumoftrees,trained_bart_BMA$y_minmax) + + } + + ret <- list() + ret$ITE_ests<-preds_treated[[1]]-preds_control[[1]] + ret$bbma_object <- trained_bart_BMA + ret +} \ No newline at end of file diff --git a/R/ITEs_bartBMA_exact_par.R b/R/ITEs_bartBMA_exact_par.R new file mode 100644 index 0000000..161a33a --- /dev/null +++ b/R/ITEs_bartBMA_exact_par.R @@ -0,0 +1,124 @@ +#' @title Estimate ITEs and obtain credible intervals (in-sample or out-of-sample). +#' +#' @description This function takes a set of sum of tree models obtained from ITEs_bartBMA, and then estimates ITEs, and obtains prediction intervals. +#' @param object Output from ITEs_bartBMA of class ITE_ests.bartBMA. +#' @param l_quant Lower quantile of credible intervals for the ITEs, CATT, CATNT. +#' @param u_quant Upper quantile of credible intervals for the ITEs, CATT, CATNT. +#' @param newdata Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object. +#' @param update_resids Option for whether to update the partial residuals in the gibbs sampler. If equal to 1, updates partial residuals, if equal to zero, does not update partial residuals. The defaullt setting is to update the partial residuals. +#' @param num_cores Number of cores used in parallel. +#' @param root_alg_precision The algorithm should obtain approximate bounds that are within the distance root_alg_precision of the true quantile for the chosen average of models. +#' @param training_data The training data matrix +#' @export +#' @return The output is a list of length 4: +#' \item{ITE_intervals}{A 3 by n matrix, where n is the number of observations. The first row gives the l_quant*100 quantiles of the individual treatment effects. The second row gives the medians of the ITEs. The third row gives the u_quant*100 quantiles of the ITEs.} +#' \item{ITE_estimates}{An n by 1 matrix containing the Individual Treatment Effect estimates.} +#' \item{CATE_estimate}{The Conditional Average Treatment Effect Estimates} +#' \item{CATE_Interval}{A 3 by 1 matrix. The first element is the l_quant*100 quantile of the CATE distribution, the second element is the median of the CATE distribution, and the thied element is the u_quant*100 quantile of the CATE distribution.} +#' @examples +#' \dontrun{ +#' #Example of BART-BMA for ITE estimation +#' # Applied to data simulations from Hahn et al. (2020, Bayesian Analysis) +#' # "Bayesian Regression Tree Models for Causal Inference: Regularization, +#' # Confounding, and Heterogeneous Effects +#' n <- 250 +#' x1 <- rnorm(n) +#' x2 <- rnorm(n) +#' x3 <- rnorm(n) +#' x4 <- rbinom(n,1,0.5) +#' x5 <- as.factor(sample( LETTERS[1:3], n, replace=TRUE)) +#' +#' p= 0 +#' xnoise = matrix(rnorm(n*p), nrow=n) +#' x5A <- ifelse(x5== 'A',1,0) +#' x5B <- ifelse(x5== 'B',1,0) +#' x5C <- ifelse(x5== 'C',1,0) +#' +#' x_covs_train <- cbind(x1,x2,x3,x4,x5A,x5B,x5C,xnoise) +#' +#' #Treatment effect +#' #tautrain <- 3 +#' tautrain <- 1+2*x_covs_train[,2]*x_covs_train[,4] +#' +#' #Prognostic function +#' mutrain <- 1 + 2*x_covs_train[,5] -1*x_covs_train[,6]-4*x_covs_train[,7] + +#' x_covs_train[,1]*x_covs_train[,3] +#' sd_mtrain <- sd(mutrain) +#' utrain <- runif(n) +#' #pitrain <- 0.8*pnorm((3*mutrain/sd_mtrain)-0.5*x_covs_train[,1])+0.05+utrain/10 +#' pitrain <- 0.5 +#' ztrain <- rbinom(n,1,pitrain) +#' ytrain <- mutrain + tautrain*ztrain +#' #pihattrain <- pbart(x_covs_train,ztrain )$prob.train.mean +#' +#' #set lower and upper quantiles for intervals +#' lbound <- 0.025 +#' ubound <- 0.975 +#' +#' +#' trained_bbma <- ITEs_bartBMA(x_covariates = x_covs_train, +#' z_train = ztrain, +#' y_train = ytrain) +#' +#' example_output <- ITEs_bartBMA_exact_par(trained_bbma[[2]], +#' l_quant = lbound, +#' u_quant= ubound, +#' training_data = x_covs_train) +#' } + +ITEs_bartBMA_exact_par <-function(object,#min_possible,max_possible, + l_quant,u_quant,newdata=NULL,update_resids=1, + num_cores=1, + root_alg_precision=0.00001, + training_data){ + + if(is.null(newdata) && length(object)==16){ + #if test data specified separately + ret<-pred_ints_ITE_outsamp_par(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,#min_possible, max_possible, + object$nrowTrain, + nrow(object$test_data),object$a,object$sigma,0,object$nu, + object$lambda,#diff_inital_resids, + object$test_data,l_quant,u_quant,num_cores, + root_alg_precision,training_data + ) + + + + }else{if(is.null(newdata) && length(object)==14){ + #else return Pred Ints for training data + ret <- pred_ints_ITE_insamp_par(object$sumoftrees, + object$obs_to_termNodesMatrix, + object$response,object$bic,#min_possible, max_possible, + object$nrowTrain,#nrow(object$test_data), + object$a,object$sigma,0,object$nu, + object$lambda,#diff_inital_resids,object$test_data, + l_quant,u_quant, + num_cores,root_alg_precision,training_data + ) + + }else{ + #if test data included in call to object + ret<-pred_ints_ITE_outsamp_par(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,#min_possible, max_possible, + object$nrowTrain, + nrow(newdata), object$a,object$sigma,0,object$nu, + object$lambda,#diff_inital_resids, + newdata,l_quant,u_quant,num_cores, + root_alg_precision,training_data + ) + + }} + + #PI<-apply(draws_from_mixture,2,function(x)quantile(x,probs=c(l_quant,0.5,u_quant))) + + + + #each row is a vector drawn from the mixture distribution + + + names(ret)<-c("ITE_intervals", + "ITE_estimates", + "CATE_estimate", + "CATE_Interval") + + ret +} \ No newline at end of file diff --git a/R/RcppExports.R b/R/RcppExports.R new file mode 100644 index 0000000..137d042 --- /dev/null +++ b/R/RcppExports.R @@ -0,0 +1,611 @@ +# Generated by using Rcpp::compileAttributes() -> do not edit by hand +# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 + +find_internal_nodes_pred <- function(treetable) { + .Call(`_bartBMA_find_internal_nodes_pred`, treetable) +} + +find_term_nodes_pred <- function(tree_table) { + .Call(`_bartBMA_find_term_nodes_pred`, tree_table) +} + +get_original_pred <- function(low, high, sp_low, sp_high, sum_preds) { + .Call(`_bartBMA_get_original_pred`, low, high, sp_low, sp_high, sum_preds) +} + +bartBMA_get_testdata_term_obs_pred <- function(test_data, tree_data, term_node_means) { + .Call(`_bartBMA_bartBMA_get_testdata_term_obs_pred`, test_data, tree_data, term_node_means) +} + +get_BART_BMA_test_predictions <- function(test_data, BIC, sum_trees, y_minmax) { + .Call(`_bartBMA_get_BART_BMA_test_predictions`, test_data, BIC, sum_trees, y_minmax) +} + +get_imp_vars <- function(split_vars, num_col, current_vars) { + .Call(`_bartBMA_get_imp_vars`, split_vars, num_col, current_vars) +} + +get_weighted_var_imp <- function(num_vars, BIC, sum_trees) { + .Call(`_bartBMA_get_weighted_var_imp`, num_vars, BIC, sum_trees) +} + +csample_num <- function(x, size, replace, prob = as.numeric( c())) { + .Call(`_bartBMA_csample_num`, x, size, replace, prob) +} + +add_rows <- function(prior_tree_table_temp, grow_node) { + .Call(`_bartBMA_add_rows`, prior_tree_table_temp, grow_node) +} + +addcol <- function(prior_tree_matrix_temp, grow_node, ld_obs, rd_obs) { + .Call(`_bartBMA_addcol`, prior_tree_matrix_temp, grow_node, ld_obs, rd_obs) +} + +set_daughter_to_end_tree <- function(grow_node, prior_tree_table_temp, left_daughter) { + .Call(`_bartBMA_set_daughter_to_end_tree`, grow_node, prior_tree_table_temp, left_daughter) +} + +set_daughter_to_end_mat <- function(d, prior_tree_matrix_temp, left_daughter, ld_obs, rd_obs) { + .Call(`_bartBMA_set_daughter_to_end_mat`, d, prior_tree_matrix_temp, left_daughter, ld_obs, rd_obs) +} + +remove_zero <- function(nodes_at_depth) { + .Call(`_bartBMA_remove_zero`, nodes_at_depth) +} + +order_intvec_ <- function(x) { + .Call(`_bartBMA_order_intvec_`, x) +} + +get_gnp <- function(nodes_at_depth, grow_node) { + .Call(`_bartBMA_get_gnp`, nodes_at_depth, grow_node) +} + +find_term_nodes <- function(tree_table) { + .Call(`_bartBMA_find_term_nodes`, tree_table) +} + +find_term_obs <- function(tree_matrix_temp, terminal_node) { + .Call(`_bartBMA_find_term_obs`, tree_matrix_temp, terminal_node) +} + +likelihood_function <- function(y_temp, treetable_temp, obs_to_nodes_temp, a, mu, nu, lambda) { + .Call(`_bartBMA_likelihood_function`, y_temp, treetable_temp, obs_to_nodes_temp, a, mu, nu, lambda) +} + +find_internal_nodes <- function(treetable) { + .Call(`_bartBMA_find_internal_nodes`, treetable) +} + +find_prev_nonterm <- function(find_nonterm, prev) { + .Call(`_bartBMA_find_prev_nonterm`, find_nonterm, prev) +} + +find_nodes_to_update <- function(all_ld, left_daughter) { + .Call(`_bartBMA_find_nodes_to_update`, all_ld, left_daughter) +} + +set_tree_to_middle <- function(node_to_update, prior_tree_table_temp, grow_node, left_daughter) { + .Call(`_bartBMA_set_tree_to_middle`, node_to_update, prior_tree_table_temp, grow_node, left_daughter) +} + +update_grow_obs <- function(prior_tree_matrix_temp, grow_node, left_daughter, d, ld_obs, rd_obs) { + .Call(`_bartBMA_update_grow_obs`, prior_tree_matrix_temp, grow_node, left_daughter, d, ld_obs, rd_obs) +} + +find_obs_to_update_grow <- function(prior_tree_matrix_temp, left_daughter, d, ld_obs, rd_obs) { + .Call(`_bartBMA_find_obs_to_update_grow`, prior_tree_matrix_temp, left_daughter, d, ld_obs, rd_obs) +} + +get_daughter_obs <- function(xmat, obs_to_update, split_var, split_point) { + .Call(`_bartBMA_get_daughter_obs`, xmat, obs_to_update, split_var, split_point) +} + +find_term_cols <- function(tree_matrix_temp, terminal_node) { + .Call(`_bartBMA_find_term_cols`, tree_matrix_temp, terminal_node) +} + +get_grow_obs <- function(xmat, grow_obs, split_var) { + .Call(`_bartBMA_get_grow_obs`, xmat, grow_obs, split_var) +} + +grow_tree <- function(xmat, prior_tree_matrix, grow_node, prior_tree_table, splitvar, splitpoint, grow_obs, d) { + .Call(`_bartBMA_grow_tree`, xmat, prior_tree_matrix, grow_node, prior_tree_table, splitvar, splitpoint, grow_obs, d) +} + +set_daughter <- function(left_daughter, right_daughter, ld_obs, rd_obs, tree_matrix_temp, term_cols) { + .Call(`_bartBMA_set_daughter`, left_daughter, right_daughter, ld_obs, rd_obs, tree_matrix_temp, term_cols) +} + +order_ <- function(x) { + .Call(`_bartBMA_order_`, x) +} + +orderforOW <- function(x) { + .Call(`_bartBMA_orderforOW`, x) +} + +secondKindStirlingNumber <- function(n, k) { + .Call(`_bartBMA_secondKindStirlingNumber`, n, k) +} + +get_tree_prior <- function(spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, tree_table, tree_matrix, alpha, beta) { + .Call(`_bartBMA_get_tree_prior`, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, tree_table, tree_matrix, alpha, beta) +} + +start_tree <- function(start_mean, start_sd) { + .Call(`_bartBMA_start_tree`, start_mean, start_sd) +} + +start_tree2 <- function() { + .Call(`_bartBMA_start_tree2`) +} + +start_matrix <- function(n) { + .Call(`_bartBMA_start_matrix`, n) +} + +evaluate_model_occams_window <- function(tree_lik, lowest_BIC, c, tree_list, tree_mat_list, tree_parent) { + .Call(`_bartBMA_evaluate_model_occams_window`, tree_lik, lowest_BIC, c, tree_list, tree_mat_list, tree_parent) +} + +evaluate_model_occams_window_exact <- function(tree_lik, lowest_BIC, c, tree_list, tree_mat_list, tree_parent, tree_pred_list) { + .Call(`_bartBMA_evaluate_model_occams_window_exact`, tree_lik, lowest_BIC, c, tree_list, tree_mat_list, tree_parent, tree_pred_list) +} + +get_testdata_term_obs <- function(test_data, tree_data) { + .Call(`_bartBMA_get_testdata_term_obs`, test_data, tree_data) +} + +get_initial_resids <- function(test_data, List_of_lists_tree_tables, ytrain) { + .Call(`_bartBMA_get_initial_resids`, test_data, List_of_lists_tree_tables, ytrain) +} + +resize <- function(x, n) { + .Call(`_bartBMA_resize`, x, n) +} + +resize_bigger <- function(x, n) { + .Call(`_bartBMA_resize_bigger`, x, n) +} + +J <- function(obs_to_nodes_temp, tree_term_nodes) { + .Call(`_bartBMA_J`, obs_to_nodes_temp, tree_term_nodes) +} + +mu_vector <- function(sum_treetable, n) { + .Call(`_bartBMA_mu_vector`, sum_treetable, n) +} + +W <- function(sum_treetable, sum_obs_to_nodes, n) { + .Call(`_bartBMA_W`, sum_treetable, sum_obs_to_nodes, n) +} + +likelihood_function2 <- function(y_temp, treetable_temp, obs_to_nodes_temp, a, mu, nu, lambda) { + .Call(`_bartBMA_likelihood_function2`, y_temp, treetable_temp, obs_to_nodes_temp, a, mu, nu, lambda) +} + +likelihood_function2_exact <- function(y_temp, treetable_temp, obs_to_nodes_temp, a, mu, nu, lambda) { + .Call(`_bartBMA_likelihood_function2_exact`, y_temp, treetable_temp, obs_to_nodes_temp, a, mu, nu, lambda) +} + +sumtree_likelihood_function <- function(y_temp, sum_treetable, sum_obs_to_nodes, n, a, nu, lambda) { + .Call(`_bartBMA_sumtree_likelihood_function`, y_temp, sum_treetable, sum_obs_to_nodes, n, a, nu, lambda) +} + +sumtree_likelihood_function2 <- function(y_temp, sum_treetable, sum_obs_to_nodes, n, a, nu, lambda) { + .Call(`_bartBMA_sumtree_likelihood_function2`, y_temp, sum_treetable, sum_obs_to_nodes, n, a, nu, lambda) +} + +sumtree_likelihood_function2_exact <- function(y_temp, sum_treetable, sum_obs_to_nodes, n, a, nu, lambda) { + .Call(`_bartBMA_sumtree_likelihood_function2_exact`, y_temp, sum_treetable, sum_obs_to_nodes, n, a, nu, lambda) +} + +sumtree_likelihood_function3 <- function(y_temp, sum_treetable, sum_obs_to_nodes, n, a, nu, lambda) { + .Call(`_bartBMA_sumtree_likelihood_function3`, y_temp, sum_treetable, sum_obs_to_nodes, n, a, nu, lambda) +} + +sumtree_likelihood_function4 <- function(y_temp, sum_treetable, sum_obs_to_nodes, n, a, nu, lambda) { + .Call(`_bartBMA_sumtree_likelihood_function4`, y_temp, sum_treetable, sum_obs_to_nodes, n, a, nu, lambda) +} + +get_best_split <- function(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, resids, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_mat, alpha, beta, maxOWsize, min_num_obs_for_split, min_num_obs_after_split) { + .Call(`_bartBMA_get_best_split`, less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, resids, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_mat, alpha, beta, maxOWsize, min_num_obs_for_split, min_num_obs_after_split) +} + +get_best_split_2 <- function(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, resids, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_matlist, alpha, beta, maxOWsize, min_num_obs_for_split, min_num_obs_after_split) { + .Call(`_bartBMA_get_best_split_2`, less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, resids, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_matlist, alpha, beta, maxOWsize, min_num_obs_for_split, min_num_obs_after_split) +} + +get_best_split_sum <- function(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_mat, alpha, beta, maxOWsize, sum_trees, sum_trees_mat, y_scaled, parent2, i, min_num_obs_for_split, min_num_obs_after_split) { + .Call(`_bartBMA_get_best_split_sum`, less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_mat, alpha, beta, maxOWsize, sum_trees, sum_trees_mat, y_scaled, parent2, i, min_num_obs_for_split, min_num_obs_after_split) +} + +get_best_split_sum_2 <- function(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_matlist, alpha, beta, maxOWsize, sum_trees, sum_trees_mat, y_scaled, parent2, i, min_num_obs_for_split, min_num_obs_after_split) { + .Call(`_bartBMA_get_best_split_sum_2`, less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_matlist, alpha, beta, maxOWsize, sum_trees, sum_trees_mat, y_scaled, parent2, i, min_num_obs_for_split, min_num_obs_after_split) +} + +get_best_split_exact <- function(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, resids, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_mat, alpha, beta, maxOWsize, min_num_obs_for_split, min_num_obs_after_split) { + .Call(`_bartBMA_get_best_split_exact`, less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, resids, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_mat, alpha, beta, maxOWsize, min_num_obs_for_split, min_num_obs_after_split) +} + +get_best_split_2_exact <- function(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, resids, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_matlist, alpha, beta, maxOWsize, min_num_obs_for_split, min_num_obs_after_split) { + .Call(`_bartBMA_get_best_split_2_exact`, less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, resids, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_matlist, alpha, beta, maxOWsize, min_num_obs_for_split, min_num_obs_after_split) +} + +get_best_split_sum_exact <- function(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_mat, alpha, beta, maxOWsize, sum_trees, sum_trees_mat, y_scaled, parent2, i, min_num_obs_for_split, min_num_obs_after_split) { + .Call(`_bartBMA_get_best_split_sum_exact`, less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_mat, alpha, beta, maxOWsize, sum_trees, sum_trees_mat, y_scaled, parent2, i, min_num_obs_for_split, min_num_obs_after_split) +} + +get_best_split_sum_2_exact <- function(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_matlist, alpha, beta, maxOWsize, sum_trees, sum_trees_mat, y_scaled, parent2, i, min_num_obs_for_split, min_num_obs_after_split) { + .Call(`_bartBMA_get_best_split_sum_2_exact`, less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_matlist, alpha, beta, maxOWsize, sum_trees, sum_trees_mat, y_scaled, parent2, i, min_num_obs_for_split, min_num_obs_after_split) +} + +update_mean_var <- function(tree_table, tree_matrix, resids, a) { + .Call(`_bartBMA_update_mean_var`, tree_table, tree_matrix, resids, a) +} + +update_predictions <- function(tree_table, tree_matrix, new_mean, n) { + .Call(`_bartBMA_update_predictions`, tree_table, tree_matrix, new_mean, n) +} + +subsetter <- function(a, b) { + .Call(`_bartBMA_subsetter`, a, b) +} + +order_inc_ <- function(x) { + .Call(`_bartBMA_order_inc_`, x) +} + +min_which2 <- function(array, n, minout, whichout) { + .Call(`_bartBMA_min_which2`, array, n, minout, whichout) +} + +mll_meanvar2 <- function(x, x2, n) { + .Call(`_bartBMA_mll_meanvar2`, x, x2, n) +} + +PELT_meanvar_norm2 <- function(resp, pen) { + .Call(`_bartBMA_PELT_meanvar_norm2`, resp, pen) +} + +SS <- function(x, y, split) { + .Call(`_bartBMA_SS`, x, y, split) +} + +gridCP <- function(x, y, gridSize = 10L) { + .Call(`_bartBMA_gridCP`, x, y, gridSize) +} + +gridCP_arma <- function(x, y, gridSize = 10L) { + .Call(`_bartBMA_gridCP_arma`, x, y, gridSize) +} + +make_gridpoint_cpmat <- function(data, resp, gridsize, num_cp) { + .Call(`_bartBMA_make_gridpoint_cpmat`, data, resp, gridsize, num_cp) +} + +make_gridpoint_cpmat_arma <- function(data, resp, gridsize, num_cp) { + .Call(`_bartBMA_make_gridpoint_cpmat_arma`, data, resp, gridsize, num_cp) +} + +make_pelt_cpmat <- function(data, resp, pen, num_cp) { + .Call(`_bartBMA_make_pelt_cpmat`, data, resp, pen, num_cp) +} + +get_best_trees <- function(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split) { + .Call(`_bartBMA_get_best_trees`, less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split) +} + +get_best_trees_update_splits <- function(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split) { + .Call(`_bartBMA_get_best_trees_update_splits`, less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split) +} + +get_best_trees_sum <- function(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, err_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, prev_sum_trees, prev_sum_trees_mat, y_scaled, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split) { + .Call(`_bartBMA_get_best_trees_sum`, less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, err_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, prev_sum_trees, prev_sum_trees_mat, y_scaled, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split) +} + +get_best_trees_sum_update_splits <- function(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, err_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, prev_sum_trees, prev_sum_trees_mat, y_scaled, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split) { + .Call(`_bartBMA_get_best_trees_sum_update_splits`, less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, err_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, prev_sum_trees, prev_sum_trees_mat, y_scaled, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split) +} + +get_best_trees_exact <- function(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split) { + .Call(`_bartBMA_get_best_trees_exact`, less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split) +} + +get_best_trees_update_splits_exact <- function(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split) { + .Call(`_bartBMA_get_best_trees_update_splits_exact`, less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split) +} + +get_best_trees_sum_exact <- function(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, err_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, prev_sum_trees, prev_sum_trees_mat, y_scaled, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split) { + .Call(`_bartBMA_get_best_trees_sum_exact`, less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, err_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, prev_sum_trees, prev_sum_trees_mat, y_scaled, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split) +} + +get_best_trees_sum_update_splits_exact <- function(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, err_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, prev_sum_trees, prev_sum_trees_mat, y_scaled, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split) { + .Call(`_bartBMA_get_best_trees_sum_update_splits_exact`, less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, err_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, prev_sum_trees, prev_sum_trees_mat, y_scaled, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split) +} + +scale_response <- function(a, b, c, d, y) { + .Call(`_bartBMA_scale_response`, a, b, c, d, y) +} + +get_original <- function(low, high, sp_low, sp_high, sum_preds) { + .Call(`_bartBMA_get_original`, low, high, sp_low, sp_high, sum_preds) +} + +get_original_arma <- function(low, high, sp_low, sp_high, sum_preds) { + .Call(`_bartBMA_get_original_arma`, low, high, sp_low, sp_high, sum_preds) +} + +get_original_TE_arma <- function(low, high, sp_low, sp_high, sum_preds) { + .Call(`_bartBMA_get_original_TE_arma`, low, high, sp_low, sp_high, sum_preds) +} + +get_original_TE_double <- function(low, high, sp_low, sp_high, sum_preds) { + .Call(`_bartBMA_get_original_TE_double`, low, high, sp_low, sp_high, sum_preds) +} + +get_termobs_test_data <- function(test_data, tree_data) { + .Call(`_bartBMA_get_termobs_test_data`, test_data, tree_data) +} + +get_termobs_test_data_fields <- function(test_data, tree_data) { + .Call(`_bartBMA_get_termobs_test_data_fields`, test_data, tree_data) +} + +get_termobs_testdata_overall <- function(overall_sum_trees, test_data) { + .Call(`_bartBMA_get_termobs_testdata_overall`, overall_sum_trees, test_data) +} + +get_J_test <- function(curr_termobs, tree_term_nodes, n) { + .Call(`_bartBMA_get_J_test`, curr_termobs, tree_term_nodes, n) +} + +get_W_test <- function(sum_treetable, termobs_testdata_onemodel, n) { + .Call(`_bartBMA_get_W_test`, sum_treetable, termobs_testdata_onemodel, n) +} + +preds_bbma_lin_alg_outsamp <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data) { + .Call(`_bartBMA_preds_bbma_lin_alg_outsamp`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data) +} + +preds_bbma_lin_alg_insamp <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda) { + .Call(`_bartBMA_preds_bbma_lin_alg_insamp`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda) +} + +mean_vars_lin_alg_outsamp <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data) { + .Call(`_bartBMA_mean_vars_lin_alg_outsamp`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data) +} + +mean_vars_lin_alg_insamp <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda) { + .Call(`_bartBMA_mean_vars_lin_alg_insamp`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda) +} + +BART_BMA_sumLikelihood <- function(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, data, y, start_mean, start_sd, a, mu, nu, lambda, c, sigma_mu, pen, num_cp, test_data, num_rounds, alpha, beta, split_rule_node, gridpoint, maxOWsize, num_splits, gridsize, zero_split, only_max_num_trees, min_num_obs_for_split, min_num_obs_after_split, exact_residuals) { + .Call(`_bartBMA_BART_BMA_sumLikelihood`, less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, data, y, start_mean, start_sd, a, mu, nu, lambda, c, sigma_mu, pen, num_cp, test_data, num_rounds, alpha, beta, split_rule_node, gridpoint, maxOWsize, num_splits, gridsize, zero_split, only_max_num_trees, min_num_obs_for_split, min_num_obs_after_split, exact_residuals) +} + +Quantile <- function(x, probs) { + .Call(`_bartBMA_Quantile`, x, probs) +} + +mixt_eval_cdf <- function(x_val, d_o_f, mean_vec, var_vec, weights_vec, quant_val) { + .Call(`_bartBMA_mixt_eval_cdf`, x_val, d_o_f, mean_vec, var_vec, weights_vec, quant_val) +} + +rootmixt <- function(d_o_f, a, b, mean_vec, var_vec, weights_vec, quant_val, root_alg_precision) { + .Call(`_bartBMA_rootmixt`, d_o_f, a, b, mean_vec, var_vec, weights_vec, quant_val, root_alg_precision) +} + +pred_ints_exact_outsamp <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores, root_alg_precision) { + .Call(`_bartBMA_pred_ints_exact_outsamp`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores, root_alg_precision) +} + +pred_ints_exact_outsamp_par <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores, root_alg_precision) { + .Call(`_bartBMA_pred_ints_exact_outsamp_par`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores, root_alg_precision) +} + +pred_ints_lin_alg_outsamp <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob) { + .Call(`_bartBMA_pred_ints_lin_alg_outsamp`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob) +} + +pred_ints_lin_alg_insamp <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, lower_prob, upper_prob) { + .Call(`_bartBMA_pred_ints_lin_alg_insamp`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, lower_prob, upper_prob) +} + +pred_ints_chol_attempt_outsamp <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob) { + .Call(`_bartBMA_pred_ints_chol_attempt_outsamp`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob) +} + +pred_ints_lin_alg_parallel_outsamp <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores) { + .Call(`_bartBMA_pred_ints_lin_alg_parallel_outsamp`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores) +} + +pred_ints_lin_alg_fields_outsamp <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores) { + .Call(`_bartBMA_pred_ints_lin_alg_fields_outsamp`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores) +} + +pred_ints_chol_parallel_outsamp <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores) { + .Call(`_bartBMA_pred_ints_chol_parallel_outsamp`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores) +} + +mean_vars_lin_alg_parallel_outsamp <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, num_cores) { + .Call(`_bartBMA_mean_vars_lin_alg_parallel_outsamp`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, num_cores) +} + +pred_ints_ITE_outsamp_par <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores, root_alg_precision, training_data) { + .Call(`_bartBMA_pred_ints_ITE_outsamp_par`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores, root_alg_precision, training_data) +} + +pred_ints_ITE_insamp_par <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_obs, a, sigma, mu_mu, nu, lambda, lower_prob, upper_prob, num_cores, root_alg_precision, training_data) { + .Call(`_bartBMA_pred_ints_ITE_insamp_par`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_obs, a, sigma, mu_mu, nu, lambda, lower_prob, upper_prob, num_cores, root_alg_precision, training_data) +} + +pred_ints_ITE_CATT_outsamp_par <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores, root_alg_precision, training_data, ztest) { + .Call(`_bartBMA_pred_ints_ITE_CATT_outsamp_par`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores, root_alg_precision, training_data, ztest) +} + +pred_ints_ITE_CATT_insamp_par <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_obs, a, sigma, mu_mu, nu, lambda, lower_prob, upper_prob, num_cores, root_alg_precision, training_data, ztrain) { + .Call(`_bartBMA_pred_ints_ITE_CATT_insamp_par`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_obs, a, sigma, mu_mu, nu, lambda, lower_prob, upper_prob, num_cores, root_alg_precision, training_data, ztrain) +} + +find_term_nodes_gs <- function(tree_table) { + .Call(`_bartBMA_find_term_nodes_gs`, tree_table) +} + +find_term_obs_gs <- function(tree_matrix_temp, terminal_node) { + .Call(`_bartBMA_find_term_obs_gs`, tree_matrix_temp, terminal_node) +} + +calc_rowsums <- function(predictions) { + .Call(`_bartBMA_calc_rowsums`, predictions) +} + +calculate_resids <- function(predictions, response) { + .Call(`_bartBMA_calculate_resids`, predictions, response) +} + +update_Gibbs_mean_var <- function(resids, a, sigma, mu_mu, terminal_nodes, term_obs_tree) { + .Call(`_bartBMA_update_Gibbs_mean_var`, resids, a, sigma, mu_mu, terminal_nodes, term_obs_tree) +} + +update_sigma <- function(a1, b, resids, n) { + .Call(`_bartBMA_update_sigma`, a1, b, resids, n) +} + +find_node_means <- function(sum_tree, term_nodes) { + .Call(`_bartBMA_find_node_means`, sum_tree, term_nodes) +} + +get_tree_info <- function(overall_sum_trees, overall_sum_mat, num_obs) { + .Call(`_bartBMA_get_tree_info`, overall_sum_trees, overall_sum_mat, num_obs) +} + +remove_curr_col <- function(predy, i) { + .Call(`_bartBMA_remove_curr_col`, predy, i) +} + +get_new_mean <- function(terminal_nodes, new_mean_var) { + .Call(`_bartBMA_get_new_mean`, terminal_nodes, new_mean_var) +} + +update_predictions_gs <- function(new_mean, new_var, n, terminal_nodes, term_obs_tree) { + .Call(`_bartBMA_update_predictions_gs`, new_mean, new_var, n, terminal_nodes, term_obs_tree) +} + +scale_response_gs <- function(a, b, c, d, y) { + .Call(`_bartBMA_scale_response_gs`, a, b, c, d, y) +} + +get_original_gs <- function(low, high, sp_low, sp_high, sum_preds) { + .Call(`_bartBMA_get_original_gs`, low, high, sp_low, sp_high, sum_preds) +} + +find_internal_nodes_gs <- function(treetable) { + .Call(`_bartBMA_find_internal_nodes_gs`, treetable) +} + +get_tree_info_test_data <- function(test_data, tree_data) { + .Call(`_bartBMA_get_tree_info_test_data`, test_data, tree_data) +} + +get_tree_info_testdata_overall <- function(overall_sum_trees, num_obs, test_data) { + .Call(`_bartBMA_get_tree_info_testdata_overall`, overall_sum_trees, num_obs, test_data) +} + +gibbs_sampler <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data) { + .Call(`_bartBMA_gibbs_sampler`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data) +} + +gibbs_sampler2 <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids) { + .Call(`_bartBMA_gibbs_sampler2`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids) +} + +gibbs_sampler_no_update <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data) { + .Call(`_bartBMA_gibbs_sampler_no_update`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data) +} + +gibbs_sampler_no_update2 <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids) { + .Call(`_bartBMA_gibbs_sampler_no_update2`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids) +} + +gibbs_sampler_exp <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data) { + .Call(`_bartBMA_gibbs_sampler_exp`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data) +} + +gibbs_sampler2_exp <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids) { + .Call(`_bartBMA_gibbs_sampler2_exp`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids) +} + +gibbs_sampler_no_update_exp <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data) { + .Call(`_bartBMA_gibbs_sampler_no_update_exp`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data) +} + +gibbs_sampler_no_update2_exp <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids) { + .Call(`_bartBMA_gibbs_sampler_no_update2_exp`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids) +} + +gibbs_sampler_ITE <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data) { + .Call(`_bartBMA_gibbs_sampler_ITE`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data) +} + +gibbs_sampler_ITE2 <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data) { + .Call(`_bartBMA_gibbs_sampler_ITE2`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data) +} + +gibbs_sampler_ITE_no_update <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data) { + .Call(`_bartBMA_gibbs_sampler_ITE_no_update`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data) +} + +gibbs_sampler_ITE_no_update2 <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data) { + .Call(`_bartBMA_gibbs_sampler_ITE_no_update2`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data) +} + +gibbs_sampler_new_inits <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data, new_pred_list) { + .Call(`_bartBMA_gibbs_sampler_new_inits`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data, new_pred_list) +} + +gibbs_sampler2_new_inits <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, new_pred_list) { + .Call(`_bartBMA_gibbs_sampler2_new_inits`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, new_pred_list) +} + +gibbs_sampler_no_update_new_inits <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data, new_pred_list) { + .Call(`_bartBMA_gibbs_sampler_no_update_new_inits`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data, new_pred_list) +} + +gibbs_sampler_no_update2_new_inits <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, new_pred_list) { + .Call(`_bartBMA_gibbs_sampler_no_update2_new_inits`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, new_pred_list) +} + +gibbs_sampler_exp_new_inits <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data, new_pred_list) { + .Call(`_bartBMA_gibbs_sampler_exp_new_inits`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data, new_pred_list) +} + +gibbs_sampler2_exp_new_inits <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, new_pred_list) { + .Call(`_bartBMA_gibbs_sampler2_exp_new_inits`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, new_pred_list) +} + +gibbs_sampler_no_update_exp_new_inits <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data, new_pred_list) { + .Call(`_bartBMA_gibbs_sampler_no_update_exp_new_inits`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data, new_pred_list) +} + +gibbs_sampler_no_update2_exp_new_inits <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, new_pred_list) { + .Call(`_bartBMA_gibbs_sampler_no_update2_exp_new_inits`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, new_pred_list) +} + +gibbs_sampler_ITE_new_inits <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data, new_pred_list) { + .Call(`_bartBMA_gibbs_sampler_ITE_new_inits`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data, new_pred_list) +} + +gibbs_sampler_ITE2_new_inits <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data, new_pred_list) { + .Call(`_bartBMA_gibbs_sampler_ITE2_new_inits`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data, new_pred_list) +} + +gibbs_sampler_ITE_no_update_new_inits <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data, new_pred_list) { + .Call(`_bartBMA_gibbs_sampler_ITE_no_update_new_inits`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data, new_pred_list) +} + +gibbs_sampler_ITE_no_update2_new_inits <- function(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data, new_pred_list) { + .Call(`_bartBMA_gibbs_sampler_ITE_no_update2_new_inits`, overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data, new_pred_list) +} + diff --git a/R/bartBMA.R b/R/bartBMA.R new file mode 100644 index 0000000..fca7489 --- /dev/null +++ b/R/bartBMA.R @@ -0,0 +1,176 @@ + +#' @title Bayesian Additive Regression Trees Using Bayesian Model Averaging (BART-BMA) +#' +#' @description This is an implementation of Bayesian Additive Regression Trees \insertCite{chipman2010bart}{bartBMA} using Bayesian Model Averaging \insertCite{hernandez2018bayesian}{bartBMA}. +#' @param x.train Training data covariate matrix +#' @param y.train Training data outcome vector. +#' @param a This is a parameter that influences the variance of terminal node parameter values. Default value a=3. +#' @param nu This is a hyperparameter in the distribution of the variance of the error term. THe inverse of the variance is distributed as Gamma (nu/2, nu*lambda/2). Default value nu=3. +#' @param sigquant Calibration quantile for the inverse chi-squared prior on the variance of the error term. +#' @param c This determines the size of Occam's Window +#' @param pen This is a parameter used by the Pruned Exact Linear Time Algorithm when finding changepoints. Default value pen=12. +#' @param num_cp This is a number between 0 and 100 that determines the proportion of changepoints proposed by the changepoint detection algorithm to keep when growing trees. Default num_cp=20. +#' @param x.test Test data covariate matrix. Default x.test=matrix(0.0,0,0). +#' @param num_rounds Number of trees. (Maximum number of trees in a sum-of-tree model). Default num_rounds=5. +#' @param alpha Parameter in prior probability of tree node splitting. Default alpha=0.95 +#' @param beta Parameter in prior probability of tree node splitting. Default beta=1 +#' @param split_rule_node Binary variable. If equals 1, then find a new set of potential splitting points via a changepoint algorithm after adding each split to a tree. If equals zero, use the same set of potential split points for all splits in a tree. Default split_rule_node=0. +#' @param gridpoint Binary variable. If equals 1, then a grid search changepoint detection algorithm will be used. If equals 0, then the Pruned Exact Linear Time (PELT) changepoint detection algorithm will be used (Killick et al. 2012). Default gridpoint=0. +#' @param maxOWsize Maximum number of models to keep in Occam's window. Default maxOWsize=100. +#' @param num_splits Maximum number of splits in a tree +#' @param gridsize This integer determines the size of the grid across which to search if gridpoint=1 when finding changepoints for constructing trees. +#' @param zero_split Binary variable. If equals 1, then zero split trees can be included in a sum-of-trees model. If equals zero, then only trees with at least one split can be included in a sum-of-trees model. +#' @param only_max_num_trees Binary variable. If equals 1, then only sum-of-trees models containing the maximum number of trees, num_rounds, are selected. If equals 0, then sum-of-trees models containing less than num_rounds trees can be selected. The default is only_max_num_trees=1. +#' @param min_num_obs_for_split This integer determines the minimum number of observations in a (parent) tree node for the algorithm to consider potential splits of the node. +#' @param min_num_obs_after_split This integer determines the minimum number of observations in a child node resulting from a split in order for a split to occur. If the left or right chikd node has less than this number of observations, then the split can not occur. +#' @param exact_residuals Binary variable. If equal to 1, then trees are added to sum-of-tree models within each round of the algorithm by detecting changepoints in the exact residuals. If equals zero, then changepoints are detected in residuals that are constructed from approximate predictions. +#' @param spike_tree If equal to 1, then the Spike-and-Tree prior will be used, otherwise the standard BART prior will be used. The number of splitting variables has a beta-binomial prior. The number of terminal nodes has a truncated Poisson prior, and then a uniform prior is placed on the set of valid constructions of trees given the splitting variables and number of terminal nodes. +#' @param s_t_hyperprior If equals 1 and spike_tree equals 1, then a beta distribution hyperprior is placed on the variable inclusion probabilities for the spike and tree prior. The hyperprior parameters are a_s_t and b_s_t. +#' @param p_s_t If spike_tree=1 and s_t_hyperprior=0, then p_s_t is the prior variable inclusion probability. +#' @param a_s_t If spike_tree=1 and s_t_hyperprior=1, then a_s_t is a parameter of a beta distribution hyperprior. +#' @param b_s_t If spike_tree=1 and s_t_hyperprior=1, then b_s_t is a parameter of a beta distribution hyperprior. +#' @param lambda_poisson This is a parameter for the Spike-and-Tree prior. It is the parameter for the (truncated and conditional on the number of splitting variables) Poisson prior on the number of terminal nodes. +#' @param less_greedy If equal to one, then a less greedy model search algorithm is used. +#' @param ... Further arguments. +#' @rdname bartBMA +#' @references +#' \insertAllCited{} +#' @export +#' @return The following objects are returned by bartbma: +#' \item{fitted.values}{The vector of predictions of the outcome for all training observations.} +#' \item{sumoftrees}{This is a list of lists of matrices. The outer list corresponds to a list of sum-of-tree models, and each element of the outer list is a list of matrices describing the structure of the trees within a sum-of-tree model. See details.} +#' \item{obs_to_termNodesMatrix}{This is a list of lists of matrices. The outer list corresponds to a list of sum-of-tree models, and each element of the outer list is a list of matrices describing to which node each of the observations is allocated to at all depths of each tree within a sum-of-tree model. See details.} +#' \item{bic}{This is a vector of BICs for each sum-of-tree model.} +#' \item{test.preds}{A vector of test data predictions. This output only is given if there is test data in the input.} +#' \item{sum_residuals}{CURRENTLY INCORRECT OUTPUT. A List (over sum-of-tree models) of lists (over single trees in a model) of vectors of partial residuals. Unless the maximum number of trees in a model is one, in which case the output is a list (over single tree models) of vectors of partial residuals, which are all equal to the outcome vector.} +#' \item{numvars}{This is the total number of variables in the input training data matrix.} +#' \item{call}{match.call returns a call in which all of the specified arguments are specified by their full names.} +#' \item{y_minmax}{Range of the input training data outcome vector.} +#' \item{response}{Input taining data outcome vector.} +#' \item{nrowTrain}{number of observations in the input training data.} +#' \item{sigma}{sd(y.train)/(max(y.train)-min(y.train))} +#' \item{a}{input parameter} +#' \item{nu}{input parameter} +#' \item{lambda}{parameter determined by the inputs sigma, sigquant, and nu} +#' @useDynLib bartBMA, .registration = TRUE +#' @importFrom Rdpack reprompt +#' @importFrom Rcpp evalCpp +#' @importFrom stats sd qchisq quantile qnorm qchisq pnorm +#' @examples +#' N <- 100 +#' p<- 100 +#' set.seed(100) +#' library(bartBMA) +#' epsilon <- rnorm(N) +#' xcov <- matrix(runif(N*p), nrow=N) +#' y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+ +#' 5*xcov[,5]+epsilon +#' epsilontest <- rnorm(N) +#' xcovtest <- matrix(runif(N*p), nrow=N) +#' ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ +#' 5*xcovtest[,5]+epsilontest +#' bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, +#' only_max_num_trees = 1,split_rule_node = 0) + + +bartBMA<-function(x.train,...)UseMethod("bartBMA") + +#' @rdname bartBMA +#' @export bartBMA.default +#' @export +bartBMA.default<-function(x.train,y.train, + a=3,nu=3,sigquant=0.9,c=1000, + pen=12,num_cp=20,x.test=matrix(0.0,0,0), + num_rounds=5,alpha=0.95,beta=2,split_rule_node=0, + gridpoint=0,maxOWsize=100,num_splits=5, + gridsize=10,zero_split=1,only_max_num_trees=1, + min_num_obs_for_split=2, min_num_obs_after_split=2, + exact_residuals=1, + spike_tree=0, s_t_hyperprior=1, p_s_t=0.5, a_s_t=1,b_s_t=3, + lambda_poisson=10,less_greedy=0,...){ + + + num_obs=nrow(x.train) + num_vars=ncol(x.train) + + binary=FALSE + start_mean=0 + start_sd=1 + mu=0 + sigma_mu=0 + sigma=sd(y.train)/(max(y.train)-min(y.train)) + qchi = qchisq(1.0-sigquant,nu,1,0) + lambda = (sigma*sigma*qchi)/nu + if(is.factor(y.train)) { + # if(length(levels(y.train)) != 2) stop("y.train is a factor with number of levels != 2") + binary = TRUE + # y.train = as.numeric(y.train)-1 + stop("Response must be a numeric vector") + } else { + if((length(unique(y.train)) == 2) & (max(y.train) == 1) & (min(y.train) == 0)) { + warning('NOTE: assumming numeric response is binary\n') + binary = TRUE + #stop("Response must be a numeric vector") + } + } + if(gridsize<1) stop("gridsize must be a positive integer") + + if(is.vector(x.train) | is.factor(x.train)| is.data.frame(x.train)) x.train = as.matrix(x.train) + if(is.vector(x.test) | is.factor(x.test)| is.data.frame(x.test)) x.test = as.matrix(x.test) + + if(is.matrix(x.train)) { + if(nrow(x.test)>0) { + if(!is.matrix(x.test)) stop('x.test must be a matrix') + } + } + # check input arguments: + # if((!is.matrix(x.train)) || (typeof(x.train)!="double")) stop("argument x.train must be a double matrix") + # if((!is.matrix(x.test)) || (typeof(x.test)!="double")) stop("argument x.test must be a double matrix") + if((!is.matrix(x.train))) stop("argument x.train must be a double matrix") + if((!is.matrix(x.test)) ) stop("argument x.test must be a double matrix") + if(!binary) { + if((!is.vector(y.train))) stop("argument y.train must be a double vector") + } + if(nrow(x.train) != length(y.train)) stop("number of rows in x.train must equal length of y.train") + if((nrow(x.test) >0) && (ncol(x.test)!=ncol(x.train))) stop("input x.test must have the same number of columns as x.train") + #if((!is.na(sigest)) && (typeof(sigest)!="double")) stop("input sigest must be double") + #if((!is.na(sigest)) && (sigest<0.0)) stop("input sigest must be positive") + #if((mode(sigdf)!="numeric") || (sigdf<0)) stop("input sigdf must be a positive number") + if(c<1)stop("Value of Occam's Window has to be greater than 0."); + if(num_cp<0 || num_cp>100)stop("Value of num_cp should be a value between 1 and 100."); + + + bartBMA_call=BART_BMA_sumLikelihood(less_greedy, + spike_tree, s_t_hyperprior, p_s_t, a_s_t,b_s_t, + num_obs,num_vars,lambda_poisson, + x.train,y.train,start_mean,start_sd,a,mu,nu,lambda,c,sigma_mu, + pen,num_cp,x.test,num_rounds,alpha,beta,split_rule_node, + gridpoint,maxOWsize,num_splits,gridsize,zero_split,only_max_num_trees, + min_num_obs_for_split, min_num_obs_after_split, + exact_residuals) + + if(length(bartBMA_call)==6){ + #length of bartBMA_call is 6 if test data was included in the call + names(bartBMA_call)<-c("fitted.values","sumoftrees","obs_to_termNodesMatrix","bic","test.preds","sum_residuals") + bartBMA_call[[6]]<-bartBMA_call[[6]]#[[length(bartBMA_call[[6]])]] + bartBMA_call$test_data<-x.test + }else{ + names(bartBMA_call)<-c("fitted.values","sumoftrees","obs_to_termNodesMatrix","bic","sum_residuals") + bartBMA_call[[5]]<-bartBMA_call[[5]]#[[length(bartBMA_call[[5]])]] + } + + bartBMA_call$numvars<-ncol(x.train) + bartBMA_call$call<-match.call() + bartBMA_call[[2]]<-bartBMA_call[[2]]#[[length(bartBMA_call[[2]])]] + bartBMA_call[[3]]<-bartBMA_call[[3]]#[[length(bartBMA_call[[3]])]] + bartBMA_call$y_minmax<-range(y.train) + bartBMA_call$response<-y.train + bartBMA_call$nrowTrain<-nrow(x.train) + bartBMA_call$sigma<-sigma + bartBMA_call$a<-a + bartBMA_call$nu<-nu + bartBMA_call$lambda<-lambda + + class(bartBMA_call)<-"bartBMA" + bartBMA_call +} \ No newline at end of file diff --git a/R/bartBMA_with_ITEs_exact_par.R b/R/bartBMA_with_ITEs_exact_par.R new file mode 100644 index 0000000..09d81a9 --- /dev/null +++ b/R/bartBMA_with_ITEs_exact_par.R @@ -0,0 +1,174 @@ +#' @title Prediction intervals for bart-bma output obtained using linear algebra to obtain means and variances, and using bisection to find the quantiles of the mixture of t distributions. +#' +#' @description This function produces prediction intervals for bart-bma output. +#' @param l_quant Lower quantile of credible intervals for the ITEs, CATT, CATNT. +#' @param u_quant Upper quantile of credible intervals for the ITEs, CATT, CATNT. +#' @param newdata Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object. +#' @param update_resids Option for whether to update the partial residuals in the gibbs sampler. If equal to 1, updates partial residuals, if equal to zero, does not update partial residuals. The defaullt setting is to update the partial residuals. +#' @param num_cores Number of cores used in parallel. +#' @param root_alg_precision The algorithm should obtain approximate bounds that are within the distance root_alg_precision of the true quantile for the chosen average of models. +#' @param x_covariates Covaraite matrix for training bartBMA. +#' @param z_train treatment vector for traiing bartBMA. +#' @param y_train outcome vector for training bartBMA. +#' @param a This is a parameter that influences the variance of terminal node parameter values. Default value a=3. +#' @param nu This is a hyperparameter in the distribution of the variance of the error term. THe inverse of the variance is distributed as Gamma (nu/2, nu*lambda/2). Default value nu=3. +#' @param sigquant Calibration quantile for the inverse chi-squared prior on the variance of the error term. +#' @param c This determines the size of Occam's Window +#' @param pen This is a parameter used by the Pruned Exact Linear Time Algorithm when finding changepoints. Default value pen=12. +#' @param num_cp This is a number between 0 and 100 that determines the proportion of changepoints proposed by the changepoint detection algorithm to keep when growing trees. Default num_cp=20. +#' @param x.test Test data covariate matrix. Default x.test=matrix(0.0,0,0). +#' @param num_rounds Number of trees. (Maximum number of trees in a sum-of-tree model). Default num_rounds=5. +#' @param alpha Parameter in prior probability of tree node splitting. Default alpha=0.95 +#' @param beta Parameter in prior probability of tree node splitting. Default beta=1 +#' @param split_rule_node Binary variable. If equals 1, then find a new set of potential splitting points via a changepoint algorithm after adding each split to a tree. If equals zero, use the same set of potential split points for all splits in a tree. Default split_rule_node=0. +#' @param gridpoint Binary variable. If equals 1, then a grid search changepoint detection algorithm will be used. If equals 0, then the Pruned Exact Linear Time (PELT) changepoint detection algorithm will be used (Killick et al. 2012). Default gridpoint=0. +#' @param maxOWsize Maximum number of models to keep in Occam's window. Default maxOWsize=100. +#' @param num_splits Maximum number of splits in a tree +#' @param gridsize This integer determines the size of the grid across which to search if gridpoint=1 when finding changepoints for constructing trees. +#' @param zero_split Binary variable. If equals 1, then zero split trees can be included in a sum-of-trees model. If equals zero, then only trees with at least one split can be included in a sum-of-trees model. +#' @param only_max_num_trees Binary variable. If equals 1, then only sum-of-trees models containing the maximum number of trees, num_rounds, are selected. If equals 0, then sum-of-trees models containing less than num_rounds trees can be selected. The default is only_max_num_trees=1. +#' @param min_num_obs_for_split This integer determines the minimum number of observations in a (parent) tree node for the algorithm to consider potential splits of the node. +#' @param min_num_obs_after_split This integer determines the minimum number of observations in a child node resulting from a split in order for a split to occur. If the left or right chikd node has less than this number of observations, then the split can not occur. +#' @param exact_residuals Binary variable. If equal to 1, then trees are added to sum-of-tree models within each round of the algorithm by detecting changepoints in the exact residuals. If equals zero, then changepoints are detected in residuals that are constructed from approximate predictions. +#' @param spike_tree If equal to 1, then the Spike-and-Tree prior will be used, otherwise the standard BART prior will be used. The number of splitting variables has a beta-binomial prior. The number of terminal nodes has a truncated Poisson prior, and then a uniform prior is placed on the set of valid constructions of trees given the splitting variables and number of terminal nodes. +#' @param s_t_hyperprior If equals 1 and spike_tree equals 1, then a beta distribution hyperprior is placed on the variable inclusion probabilities for the spike and tree prior. The hyperprior parameters are a_s_t and b_s_t. +#' @param p_s_t If spike_tree=1 and s_t_hyperprior=0, then p_s_t is the prior variable inclusion probability. +#' @param a_s_t If spike_tree=1 and s_t_hyperprior=1, then a_s_t is a parameter of a beta distribution hyperprior. +#' @param b_s_t If spike_tree=1 and s_t_hyperprior=1, then b_s_t is a parameter of a beta distribution hyperprior. +#' @param lambda_poisson This is a parameter for the Spike-and-Tree prior. It is the parameter for the (truncated and conditional on the number of splitting variables) Poisson prior on the number of terminal nodes. +#' @param less_greedy If equal to one, then a less greedy model search algorithm is used. +#' @export +#' @return The output is a list of length 4: +#' \item{ITE_intervals}{A 3 by n matrix, where n is the number of observations. The first row gives the l_quant*100 quantiles of the individual treatment effects. The second row gives the medians of the ITEs. The third row gives the u_quant*100 quantiles of the ITEs.} +#' \item{ITE_estimates}{An n by 1 matrix containing the Individual Treatment Effect estimates.} +#' \item{CATE_estimate}{The Conditional Average Treatment Effect Estimates} +#' \item{CATE_Interval}{A 3 by 1 matrix. The first element is the l_quant*100 quantile of the CATE distribution, the second element is the median of the CATE distribution, and the thied element is the u_quant*100 quantile of the CATE distribution.} +#' @examples +#' \dontrun{ +#' #Example of BART-BMA for ITE estimation +#' #Applied to data simulations from Hahn et al. (2020, Bayesian Analysis) +#' #"Bayesian Regression Tree Models for Causal Inference: Regularization, Confounding, +#' # and Heterogeneous Effects +#' n <- 250 +#' x1 <- rnorm(n) +#' x2 <- rnorm(n) +#' x3 <- rnorm(n) +#' x4 <- rbinom(n,1,0.5) +#' x5 <- as.factor(sample( LETTERS[1:3], n, replace=TRUE)) +#' +#' p= 0 +#' xnoise = matrix(rnorm(n*p), nrow=n) +#' x5A <- ifelse(x5== 'A',1,0) +#' x5B <- ifelse(x5== 'B',1,0) +#' x5C <- ifelse(x5== 'C',1,0) +#' +#' x_covs_train <- cbind(x1,x2,x3,x4,x5A,x5B,x5C,xnoise) +#' +#' #Treatment effect +#' #tautrain <- 3 +#' tautrain <- 1+2*x_covs_train[,2]*x_covs_train[,4] +#' +#' #Prognostic function +#' mutrain <- 1 + 2*x_covs_train[,5] -1*x_covs_train[,6]-4*x_covs_train[,7] + +#' x_covs_train[,1]*x_covs_train[,3] +#' sd_mtrain <- sd(mutrain) +#' utrain <- runif(n) +#' #pitrain <- 0.8*pnorm((3*mutrain/sd_mtrain)-0.5*x_covs_train[,1])+0.05+utrain/10 +#' pitrain <- 0.5 +#' ztrain <- rbinom(n,1,pitrain) +#' ytrain <- mutrain + tautrain*ztrain +#' #pihattrain <- pbart(x_covs_train,ztrain )$prob.train.mean +#' +#' #set lower and upper quantiles for intervals +#' lbound <- 0.025 +#' ubound <- 0.975 +#' +#' example_output <- bartBMA_with_ITEs_exact_par(l_quant = lbound, +#' u_quant= ubound, +#' x_covariates = x_covs_train, +#' z_train = ztrain, +#' y_train = ytrain) +#' } + +bartBMA_with_ITEs_exact_par <-function(l_quant,u_quant,newdata=NULL,update_resids=1, + num_cores=1,root_alg_precision=0.00001, + x_covariates,z_train ,y_train, + a=3,nu=3,sigquant=0.9,c=1000, + pen=12,num_cp=20,x.test=matrix(0.0,0,0), + num_rounds=5,alpha=0.95,beta=2,split_rule_node=0, + gridpoint=0,maxOWsize=100,num_splits=5, + gridsize=10,zero_split=1,only_max_num_trees=1, + min_num_obs_for_split=2, min_num_obs_after_split=2, + exact_residuals=1, + spike_tree=0, s_t_hyperprior=1, p_s_t=0.5, a_s_t=1,b_s_t=3, + lambda_poisson=10,less_greedy=0){ + + + x_train <- cbind(z_train,x_covariates) + + object <- bartBMA(x.train= x_train,y.train =y_train , + a=a,nu=nu,sigquant=sigquant,c=c, + pen=pen,num_cp=num_cp,x.test=x.test, + num_rounds=num_rounds,alpha=alpha,beta=beta,split_rule_node=split_rule_node, + gridpoint=gridpoint,maxOWsize=maxOWsize,num_splits=num_splits, + gridsize=gridsize,zero_split=zero_split,only_max_num_trees=only_max_num_trees, + min_num_obs_for_split=min_num_obs_for_split, min_num_obs_after_split=min_num_obs_after_split, + exact_residuals=exact_residuals, + spike_tree=spike_tree, s_t_hyperprior=s_t_hyperprior, p_s_t=p_s_t, a_s_t=a_s_t,b_s_t=b_s_t, + lambda_poisson=lambda_poisson,less_greedy=less_greedy) + + + + if(is.null(newdata) && length(object)==16){ + #if test data specified separately + ret<-pred_ints_ITE_outsamp_par(object$sumoftrees,object$obs_to_termNodesMatrix, + object$response,object$bic,#min_possible, max_possible, + object$nrowTrain, + nrow(object$test_data),object$a,object$sigma,0,object$nu, + object$lambda,#diff_inital_resids, + object$test_data,l_quant,u_quant,num_cores, + root_alg_precision,x_covariates + ) + + + + }else{if(is.null(newdata) && length(object)==14){ + #else return Pred Ints for training data + ret <- pred_ints_ITE_insamp_par(object$sumoftrees, + object$obs_to_termNodesMatrix, + object$response,object$bic,#min_possible, max_possible, + object$nrowTrain,#nrow(object$test_data), + object$a,object$sigma,0,object$nu, + object$lambda,#diff_inital_resids,object$test_data, + l_quant,u_quant, + num_cores, + root_alg_precision,x_covariates + ) + + }else{ + #if test data included in call to object + ret<-pred_ints_ITE_outsamp_par(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,#min_possible, max_possible, + object$nrowTrain, + nrow(newdata), object$a,object$sigma,0,object$nu, + object$lambda,#diff_inital_resids, + newdata,l_quant,u_quant,num_cores, + root_alg_precision,x_covariates + ) + + }} + + #PI<-apply(draws_from_mixture,2,function(x)quantile(x,probs=c(l_quant,0.5,u_quant))) + + + + #each row is a vector drawn from the mixture distribution + + + names(ret)<-c("ITE_intervals", + "ITE_estimates", + "CATE_estimate", + "CATE_Interval") + + + ret +} \ No newline at end of file diff --git a/R/pred_expectation_intervals_bbma_GS.R b/R/pred_expectation_intervals_bbma_GS.R new file mode 100644 index 0000000..d224e0a --- /dev/null +++ b/R/pred_expectation_intervals_bbma_GS.R @@ -0,0 +1,123 @@ +#' @title Prediction intervals for bart-bma output +#' +#' @description This function produces prediction intervals for f(x) in BART-BMA by post-hoc Gibbs-sampling from the full conditionals of the terminal node parameters and the variance of the error term. See Hernandez et al. (2018) Appendix D for details. +#' @param object bartBMA object obtained from function bartBMA +#' @param num_iter Total number of iterations of the Gibbs sampler (including burn-in). +#' @param burnin Number of burn-on iterations of the Gibbs sampler. +#' @param l_quant Lower quartile of the prediction interval. +#' @param u_quant Upper quartile of the prediction interval. +#' @param newdata Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object. +#' @param update_resids Option for whether to update the partial residuals in the gibbs sampler. If equal to 1, updates partial residuals, if equal to zero, does not update partial residuals. The defaullt setting is to update the partial residua;s. +#' @export +#' @return The output is a list of length 2: +#' \item{PI}{A 3 by n matrix, where n is the number of observations. The first row gives the l_quant*100 quantiles of f(x). The second row gives the medians of f(x). The third row gives the u_quant*100 quantiles of f(x).} +#' \item{meanpreds}{An n by 1 matrix containing the estimated means of f(x).} +#' @examples +#' +#' #load the package +#' library(bartBMA) +#' #set the seed +#' set.seed(100) +#' #simulate some data +#' N <- 100 +#' p<- 100 +#' epsilon <- rnorm(N) +#' xcov <- matrix(runif(N*p), nrow=N) +#' y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +#' epsilontest <- rnorm(N) +#' xcovtest <- matrix(runif(N*p), nrow=N) +#' ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ +#' 5*xcovtest[,5]+epsilontest +#' +#' #Train the object +#' bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, +#' only_max_num_trees = 1,split_rule_node = 0) +#' #Obtain the prediction intervals +#' pred_expectation_intervals_bbma_GS(bart_bma_example,1000,100,0.025,0.975, +#' newdata=NULL,update_resids=1) + +pred_expectation_intervals_bbma_GS<-function(object,num_iter,burnin,l_quant,u_quant,newdata=NULL,update_resids=1){ + if(l_quant>0.5 ||u_quant<0 ||u_quant>1){stop("Lower quantile must be lower than 0.5 and greater than 0")} + if(u_quant<0.5 ||u_quant<0 ||u_quant>1){stop("Upper quantile must be greater than 0.5 and less than 1")} + #object will be bartBMA object. + if(update_resids==0){ + if(is.null(newdata) && length(object)==16){ + #if test data specified separately + gs_chains<-gibbs_sampler_no_update_exp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(object$test_data),object$a,object$sigma,0,object$nu,object$lambda,object$sum_residuals,object$test_data) + }else if(is.null(newdata) && length(object)==14){ + #else return Pred Ints for training data + gs_chains<-gibbs_sampler_no_update2_exp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + object$a,object$sigma,0,object$nu,object$lambda,object$sum_residuals) + + }else{ + #if test data included in call to object + gs_chains<-gibbs_sampler_no_update_exp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(newdata), object$a,object$sigma,0,object$nu,object$lambda,object$sum_residuals,newdata) + } + }else{ + if(is.null(newdata) && length(object)==16){ + #if test data specified separately + gs_chains<-gibbs_sampler_exp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(object$test_data),object$a,object$sigma,0,object$nu,object$lambda,object$sum_residuals,object$test_data) + }else if(is.null(newdata) && length(object)==14){ + #else return Pred Ints for training data + gs_chains<-gibbs_sampler2_exp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + object$a,object$sigma,0,object$nu,object$lambda,object$sum_residuals) + + }else{ + #if test data included in call to object + gs_chains<-gibbs_sampler_exp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(newdata), object$a,object$sigma,0,object$nu,object$lambda,object$sum_residuals,newdata) + } + } + + #y_posterior_sum_trees<-gs_chains[[4]] + #y_orig_post_sum_trees<-gs_chains[[5]] + #sigma_chains<-gs_chains[[3]] + if(is.null(newdata) && length(object)==16){ + #y_posterior_sum_trees<-gs_chains[[4]] + y_orig_post_sum_trees<-gs_chains[[2]] ##[[5]] + sigma_chains<-gs_chains[[1]] #[[3]] + }else if(is.null(newdata) && length(object)==14){ + #y_posterior_sum_trees<-gs_chains[[1]] + y_orig_post_sum_trees<-gs_chains[[2]] #[[2]] + sigma_chains<-gs_chains[[1]] #[[3]] + + }else{ + #y_posterior_sum_trees<-gs_chains[[4]] + y_orig_post_sum_trees<-gs_chains[[2]] #[[5]] + sigma_chains<-gs_chains[[1]] #[[3]] + } + + sum_of_tree_BIC<- object$bic + weights<-exp(sum_of_tree_BIC-(max(sum_of_tree_BIC)+log(sum(exp(sum_of_tree_BIC-max(sum_of_tree_BIC)))))) + #final_length<-num_iter-burnin + num_its_to_sample<-round(weights*(num_iter-burnin)) + #final_sigma_chain<-numeric(0) + + #final_y_chain<-matrix(nrow=0,ncol=ncol(y_posterior_sum_trees[[1]])) + final_yorig_chain<-matrix(nrow=0,ncol=ncol(y_orig_post_sum_trees[[1]])) + + for(i in 1:length(sigma_chains)){ + sample_its<-sample(burnin:num_iter,num_its_to_sample[i]) + #final_sigma_chain<-c(final_sigma_chain,sigma_chains[[i]][sample_its]) + #now do the same for predicted response updates + #post_y_i<-y_posterior_sum_trees[[i]] + post_yorig_i<-y_orig_post_sum_trees[[i]] + #final_y_chain<-rbind(final_y_chain,post_y_i[sample_its,]) + final_yorig_chain<-rbind(final_yorig_chain,post_yorig_i[sample_its,]) + + } + PI<-apply(final_yorig_chain,2,function(x)quantile(x,probs=c(l_quant,0.5,u_quant))) + meanpreds<-apply(final_yorig_chain,2,mean) + + + ret<-list() + length(ret)<-1 + ret[[1]]<-PI + ret[[2]] <- meanpreds + + class(ret)<-"pred_intervals.bartBMA" + ret +} \ No newline at end of file diff --git a/R/pred_intervals_GS.R b/R/pred_intervals_GS.R new file mode 100644 index 0000000..7daff98 --- /dev/null +++ b/R/pred_intervals_GS.R @@ -0,0 +1,121 @@ +#' @title Prediction intervals for bart-bma output +#' +#' @description This function produces prediction intervals for BART-BMA estimates by post-hoc Gibbs-sampling from the full conditionals of the terminal node parameters and the variance of the error term. See Hernandez et al. (2018) Appendix D for details. +#' @param object bartBMA object obtained from function bartBMA +#' @param num_iter Total number of iterations of the Gibbs sampler (including burn-in). +#' @param burnin Number of burn-on iterations of the Gibbs sampler. +#' @param l_quant Lower quartile of the prediction interval. +#' @param u_quant Upper quartile of the prediction interval. +#' @param newdata Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object. +#' @param update_resids Option for whether to update the partial residuals in the gibbs sampler. If equal to 1, updates partial residuals, if equal to zero, does not update partial residuals. The defaullt setting is to update the partial residua;s. +#' @export +#' @return The output is a list of length 2: +#' \item{PI}{A 3 by n matrix, where n is the number of observations. The first row gives the l_quant*100 quantiles. The second row gives the medians. The third row gives the u_quant*100 quantiles.} +#' \item{meanpreds}{An n by 1 matrix containing the estimated means.} +#' @examples +#' +#' #load the package +#' library(bartBMA) +#' #set the seed +#' set.seed(100) +#' #simulate some data +#' N <- 100 +#' p<- 100 +#' epsilon <- rnorm(N) +#' xcov <- matrix(runif(N*p), nrow=N) +#' y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +#' epsilontest <- rnorm(N) +#' xcovtest <- matrix(runif(N*p), nrow=N) +#' ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ +#' 5*xcovtest[,5]+epsilontest +#' +#' #Train the object +#' bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, +#' only_max_num_trees = 1,split_rule_node = 0) +#' #Obtain the prediction intervals +#' pred_intervals_bbma_GS(bart_bma_example,1000,100,0.025,0.975,newdata=NULL,update_resids=1) + +pred_intervals_bbma_GS<-function(object,num_iter,burnin,l_quant,u_quant,newdata=NULL,update_resids=1){ + if(l_quant>0.5 ||u_quant<0 ||u_quant>1){stop("Lower quantile must be lower than 0.5 and greater than 0")} + if(u_quant<0.5 ||u_quant<0 ||u_quant>1){stop("Upper quantile must be greater than 0.5 and less than 1")} + #object will be bartBMA object. + if(update_resids==0){ + if(is.null(newdata) && length(object)==16){ + #if test data specified separately + gs_chains<-gibbs_sampler_no_update(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(object$test_data),object$a,object$sigma,0,object$nu,object$lambda,object$sum_residuals,object$test_data) + }else if(is.null(newdata) && length(object)==14){ + #else return Pred Ints for training data + gs_chains<-gibbs_sampler_no_update2(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + object$a,object$sigma,0,object$nu,object$lambda,object$sum_residuals) + + }else{ + #if test data included in call to object + gs_chains<-gibbs_sampler_no_update(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(newdata), object$a,object$sigma,0,object$nu,object$lambda,object$sum_residuals,newdata) + } + }else{ + if(is.null(newdata) && length(object)==16){ + #if test data specified separately + gs_chains<-gibbs_sampler(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(object$test_data),object$a,object$sigma,0,object$nu,object$lambda,object$sum_residuals,object$test_data) + }else if(is.null(newdata) && length(object)==14){ + #else return Pred Ints for training data + gs_chains<-gibbs_sampler2(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + object$a,object$sigma,0,object$nu,object$lambda,object$sum_residuals) + + }else{ + #if test data included in call to object + gs_chains<-gibbs_sampler(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(newdata), object$a,object$sigma,0,object$nu,object$lambda,object$sum_residuals,newdata) + } + } + # y_posterior_sum_trees<-gs_chains[[4]] + # y_orig_post_sum_trees<-gs_chains[[5]] + # sigma_chains<-gs_chains[[3]] + if(is.null(newdata) && length(object)==16){ + #y_posterior_sum_trees<-gs_chains[[1]] #[[8]] + y_orig_post_sum_trees<-gs_chains[[2]] #[[9]] + sigma_chains<-gs_chains[[1]] # [[3]] + }else if(is.null(newdata) && length(object)==14){ + #y_posterior_sum_trees<-gs_chains[[4]] + y_orig_post_sum_trees<-gs_chains[[2]] + sigma_chains<-gs_chains[[1]] + + }else{ + #y_posterior_sum_trees<-gs_chains[[1]] #[[8]] + y_orig_post_sum_trees<-gs_chains[[2]] #[[9]] + sigma_chains<-gs_chains[[1]] + } + + sum_of_tree_BIC<- -0.5*object$bic + weights<-exp(sum_of_tree_BIC-(max(sum_of_tree_BIC)+log(sum(exp(sum_of_tree_BIC-max(sum_of_tree_BIC)))))) + #final_length<-num_iter-burnin + num_its_to_sample<-round(weights*(num_iter-burnin)) + #final_sigma_chain<-numeric(0) + + #final_y_chain<-matrix(nrow=0,ncol=ncol(y_posterior_sum_trees[[1]])) + final_yorig_chain<-matrix(nrow=0,ncol=ncol(y_orig_post_sum_trees[[1]])) + + for(i in 1:length(sigma_chains)){ + sample_its<-sample(burnin:num_iter,num_its_to_sample[i]) + #final_sigma_chain<-c(final_sigma_chain,sigma_chains[[i]][sample_its]) + #now do the same for predicted response updates + #post_y_i<-y_posterior_sum_trees[[i]] + post_yorig_i<-y_orig_post_sum_trees[[i]] + #final_y_chain<-rbind(final_y_chain,post_y_i[sample_its,]) + final_yorig_chain<-rbind(final_yorig_chain,post_yorig_i[sample_its,]) + + } + PI<-apply(final_yorig_chain,2,function(x)quantile(x,probs=c(l_quant,0.5,u_quant))) + meanpreds<-apply(final_yorig_chain,2,mean) + + + ret<-list() + length(ret)<-2 + ret[[1]]<-PI + ret[[2]] <- meanpreds + + class(ret)<-"pred_intervals.bartBMA" + ret +} \ No newline at end of file diff --git a/R/pred_intervals_new_initials_GS.R b/R/pred_intervals_new_initials_GS.R new file mode 100644 index 0000000..98b0c1e --- /dev/null +++ b/R/pred_intervals_new_initials_GS.R @@ -0,0 +1,158 @@ +#' @title Prediction intervals for bart-bma output +#' +#' @description This function produces prediction intervals for BART-BMA estimates by post-hoc Gibbs-sampling from the full conditionals of the terminal node parameters and the variance of the error term. See Hernandez et al. (2018) Appendix D for details. +#' @param object bartBMA object obtained from function bartBMA +#' @param num_iter Total number of iterations of the Gibbs sampler (including burn-in). +#' @param burnin Number of burn-on iterations of the Gibbs sampler. +#' @param l_quant Lower quartile of the prediction interval. +#' @param u_quant Upper quartile of the prediction interval. +#' @param newdata Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object. +#' @param update_resids Option for whether to update the partial residuals in the gibbs sampler. If equal to 1, updates partial residuals, if equal to zero, does not update partial residuals. The defaullt setting is to update the partial residuals. +#' @param trainingdata The matrix of training data. +#' @export +#' @return The output is a list of length 2: +#' \item{PI}{A 3 by n matrix, where n is the number of observations. The first row gives the l_quant*100 quantiles. The second row gives the medians. The third row gives the u_quant*100 quantiles.} +#' \item{meanpreds}{An n by 1 matrix containing the estimated means.} +#' @examples +#' +#' #load the package +#' library(bartBMA) +#' #set the seed +#' set.seed(100) +#' #simulate some data +#' N <- 100 +#' p<- 100 +#' epsilon <- rnorm(N) +#' xcov <- matrix(runif(N*p), nrow=N) +#' y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +#' epsilontest <- rnorm(N) +#' xcovtest <- matrix(runif(N*p), nrow=N) +#' ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ +#' 5*xcovtest[,5]+epsilontest +#' +#' #Train the object +#' bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, +#' only_max_num_trees = 1,split_rule_node = 0) +#' #Obtain the prediction intervals +#' pred_intervals_new_initials_GS(bart_bma_example,1000,100,0.025,0.975, +#' newdata=NULL,update_resids=1,xcov) + +pred_intervals_new_initials_GS<-function(object,num_iter,burnin,l_quant,u_quant,newdata=NULL,update_resids=1,trainingdata){ + if(l_quant>0.5 ||u_quant<0 ||u_quant>1){stop("Lower quantile must be lower than 0.5 and greater than 0")} + if(u_quant<0.5 ||u_quant<0 ||u_quant>1){stop("Upper quantile must be greater than 0.5 and less than 1")} + + + scaled_train_y <- scale_response(min(object$response),max(object$response),-0.5,0.5,object$response) + + # diff_inital_resids <- list() + # resid_length <- length(object$sum_residuals[[1]][[1]]) + # #initial_partial_resids <- rep(0.8*mean(scaled_train_y),resid_length ) + # + # + # for(i in 1:length(object$sum_residuals)){ + # diff_inital_resids[[i]] <- rep(list(scaled_train_y- ((length(object$sum_residuals[[i]])-1 )/ length(object$sum_residuals[[i]]))*mean(scaled_train_y)), + # length(object$sum_residuals[[i]])) + # } + # + get_resids <- get_initial_resids(trainingdata,object$sumoftrees,scaled_train_y) + diff_inital_resids<- get_resids[[1]] + new_pred_list1 <- get_resids[[2]] + + + #object will be bartBMA object. + if(update_resids==0){ + if(is.null(newdata) && length(object)==16){ + #if test data specified separately + gs_chains<-gibbs_sampler_no_update_new_inits(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(object$test_data),object$a,object$sigma,0,object$nu,object$lambda, + diff_inital_resids, + object$test_data, + new_pred_list1) + }else if(is.null(newdata) && length(object)==14){ + #else return Pred Ints for training data + gs_chains<-gibbs_sampler_no_update2_new_inits(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + object$a,object$sigma,0,object$nu,object$lambda, + diff_inital_resids, + new_pred_list1) + + }else{ + #if test data included in call to object + gs_chains<-gibbs_sampler_no_update_new_inits(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(newdata), object$a,object$sigma,0,object$nu,object$lambda, + diff_inital_resids, + newdata, + new_pred_list1) + } + }else{ + if(is.null(newdata) && length(object)==16){ + #if test data specified separately + gs_chains<-gibbs_sampler_new_inits(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(object$test_data),object$a,object$sigma,0,object$nu,object$lambda, + diff_inital_resids, + object$test_data, + new_pred_list1) + }else if(is.null(newdata) && length(object)==14){ + #else return Pred Ints for training data + gs_chains<-gibbs_sampler2_new_inits(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + object$a,object$sigma,0,object$nu,object$lambda, + diff_inital_resids, + new_pred_list1) + + }else{ + #if test data included in call to object + gs_chains<-gibbs_sampler_new_inits(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(newdata), object$a,object$sigma,0,object$nu,object$lambda, + diff_inital_resids, + newdata, + new_pred_list1) + } + } + # y_posterior_sum_trees<-gs_chains[[4]] + # y_orig_post_sum_trees<-gs_chains[[5]] + # sigma_chains<-gs_chains[[3]] + if(is.null(newdata) && length(object)==16){ + #y_posterior_sum_trees<-gs_chains[[1]] #[[8]] + y_orig_post_sum_trees<-gs_chains[[2]] #[[9]] + sigma_chains<-gs_chains[[1]] # [[3]] + }else if(is.null(newdata) && length(object)==14){ + #y_posterior_sum_trees<-gs_chains[[4]] + y_orig_post_sum_trees<-gs_chains[[2]] + sigma_chains<-gs_chains[[1]] + + }else{ + #y_posterior_sum_trees<-gs_chains[[1]] #[[8]] + y_orig_post_sum_trees<-gs_chains[[2]] #[[9]] + sigma_chains<-gs_chains[[1]] + } + + sum_of_tree_BIC<- -0.5*object$bic + weights<-exp(sum_of_tree_BIC-(max(sum_of_tree_BIC)+log(sum(exp(sum_of_tree_BIC-max(sum_of_tree_BIC)))))) + #final_length<-num_iter-burnin + num_its_to_sample<-round(weights*(num_iter-burnin)) + #final_sigma_chain<-numeric(0) + + #final_y_chain<-matrix(nrow=0,ncol=ncol(y_posterior_sum_trees[[1]])) + final_yorig_chain<-matrix(nrow=0,ncol=ncol(y_orig_post_sum_trees[[1]])) + + for(i in 1:length(sigma_chains)){ + sample_its<-sample(burnin:num_iter,num_its_to_sample[i]) + #final_sigma_chain<-c(final_sigma_chain,sigma_chains[[i]][sample_its]) + #now do the same for predicted response updates + #post_y_i<-y_posterior_sum_trees[[i]] + post_yorig_i<-y_orig_post_sum_trees[[i]] + #final_y_chain<-rbind(final_y_chain,post_y_i[sample_its,]) + final_yorig_chain<-rbind(final_yorig_chain,post_yorig_i[sample_its,]) + + } + PI<-apply(final_yorig_chain,2,function(x)quantile(x,probs=c(l_quant,0.5,u_quant))) + meanpreds<-apply(final_yorig_chain,2,mean) + + + ret<-list() + length(ret)<-2 + ret[[1]]<-PI + ret[[2]] <- meanpreds + + class(ret)<-"pred_intervals.bartBMA" + ret +} \ No newline at end of file diff --git a/R/pred_ints_exact.R b/R/pred_ints_exact.R new file mode 100644 index 0000000..95bfa9c --- /dev/null +++ b/R/pred_ints_exact.R @@ -0,0 +1,81 @@ +#' @title Prediction intervals for bart-bma output obtained using linear algebra to obtain means and variances, and using bisection to find the quantiles of the mixture of t distributions. +#' +#' @description This function produces prediction intervals for bart-bma output. +#' @param object bartBMA object obtained from function bartBMA +#' @param l_quant Lower quantile of credible intervals for the ITEs, CATT, CATNT. +#' @param u_quant Upper quantile of credible intervals for the ITEs, CATT, CATNT. +#' @param newdata Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object. +#' @param num_cores Number of cores used in parallel. +#' @param root_alg_precision The algorithm should obtain approximate bounds that are within the distance root_alg_precision of the true quantile for the chosen average of models. +#' @export +#' @return The output is a list of length 2: +#' \item{PI}{A 3 by n matrix, where n is the number of observations. The first row gives the l_quant*100 quantiles. The second row gives the medians. The third row gives the u_quant*100 quantiles.} +#' \item{meanpreds}{An n by 1 matrix containing the estimated means.} +#' @examples +#' +#' #load the package +#' library(bartBMA) +#' #set the seed +#' set.seed(100) +#' #simulate some data +#' N <- 100 +#' p<- 100 +#' epsilon <- rnorm(N) +#' xcov <- matrix(runif(N*p), nrow=N) +#' y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +#' epsilontest <- rnorm(N) +#' xcovtest <- matrix(runif(N*p), nrow=N) +#' ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ +#' 5*xcovtest[,5]+epsilontest +#' +#' #Train the object +#' bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, +#' only_max_num_trees = 1,split_rule_node = 0) +#' #Obtain the prediction intervals +#' pred_ints_exact(bart_bma_example,0.025,0.975,newdata=NULL,num_cores=1) + +pred_ints_exact <-function(object,#min_possible,max_possible, + l_quant,u_quant,newdata=NULL, + num_cores=1, + root_alg_precision=0.00001){ + #object will be bartBMA object. + + + + + if(is.null(newdata) && length(object)==16){ + #if test data specified separately + ret<-pred_ints_exact_outsamp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,#min_possible, max_possible, + object$nrowTrain, + nrow(object$test_data),object$a,object$sigma,0,object$nu, + object$lambda,#diff_inital_resids, + object$test_data,l_quant,u_quant,num_cores,root_alg_precision + ) + + + + }else{if(is.null(newdata) && length(object)==14){ + #else return Pred Ints for training data + stop("Code not yet written for insample") + + #ret<-pred_ints_lin_alg_insamp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + # object$a,object$sigma,0,object$nu, + # object$lambda,l_quant,u_quant + #) + + }else{ + #if test data included in call to object + ret<-pred_ints_exact_outsamp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,#min_possible, max_possible, + object$nrowTrain, + nrow(newdata), object$a,object$sigma,0,object$nu, + object$lambda,#diff_inital_resids, + newdata,l_quant,u_quant,num_cores,root_alg_precision + ) + + }} + + names(ret) <- c("PI", "meanpreds") + class(ret)<-"pred_intervals.bartBMA" + ret + +} \ No newline at end of file diff --git a/R/pred_ints_exact_par.R b/R/pred_ints_exact_par.R new file mode 100644 index 0000000..172e0fb --- /dev/null +++ b/R/pred_ints_exact_par.R @@ -0,0 +1,81 @@ +#' @title Prediction intervals for bart-bma output obtained using linear algebra to obtain means and variances, and using bisection to find the quantiles of the mixture of t distributions. +#' +#' @description This function produces prediction intervals for bart-bma output. +#' @param object bartBMA object obtained from function bartBMA +#' @param l_quant Lower quantile of credible intervals for the ITEs, CATT, CATNT. +#' @param u_quant Upper quantile of credible intervals for the ITEs, CATT, CATNT. +#' @param newdata Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object. +#' @param num_cores Number of cores used in parallel. +#' @param root_alg_precision The algorithm should obtain approximate bounds that are within the distance root_alg_precision of the true quantile for the chosen average of models. +#' @export +#' @return The output is a list of length 2: +#' \item{PI}{A 3 by n matrix, where n is the number of observations. The first row gives the l_quant*100 quantiles. The second row gives the medians. The third row gives the u_quant*100 quantiles.} +#' \item{meanpreds}{An n by 1 matrix containing the estimated means.} +#' @examples +#' \dontrun{ +#' #load the package +#' library(bartBMA) +#' #set the seed +#' set.seed(100) +#' #simulate some data +#' N <- 100 +#' p<- 100 +#' epsilon <- rnorm(N) +#' xcov <- matrix(runif(N*p), nrow=N) +#' y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +#' epsilontest <- rnorm(N) +#' xcovtest <- matrix(runif(N*p), nrow=N) +#' ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ +#' 5*xcovtest[,5]+epsilontest +#' +#' #Train the object +#' bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, +#' only_max_num_trees = 1,split_rule_node = 0) +#' #Obtain the prediction intervals +#' pred_ints_exact_par(bart_bma_example,0.025,0.975,newdata=NULL,num_cores=1) +#' } + +pred_ints_exact_par <-function(object,#min_possible,max_possible, + l_quant,u_quant,newdata=NULL, + num_cores=1, + root_alg_precision=0.00001){ + #object will be bartBMA object. + + + + + if(is.null(newdata) && length(object)==16){ + #if test data specified separately + ret<-pred_ints_exact_outsamp_par(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,#min_possible, max_possible, + object$nrowTrain, + nrow(object$test_data),object$a,object$sigma,0,object$nu, + object$lambda,#diff_inital_resids, + object$test_data,l_quant,u_quant,num_cores,root_alg_precision + ) + + + + }else{if(is.null(newdata) && length(object)==14){ + #else return Pred Ints for training data + stop("Code not yet written for insample") + + #ret<-pred_ints_lin_alg_insamp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + # object$a,object$sigma,0,object$nu, + # object$lambda,l_quant,u_quant + #) + + }else{ + #if test data included in call to object + ret<-pred_ints_exact_outsamp_par(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,#min_possible, max_possible, + object$nrowTrain, + nrow(newdata), object$a,object$sigma,0,object$nu, + object$lambda,#diff_inital_resids, + newdata,l_quant,u_quant,num_cores,root_alg_precision + ) + + }} + + names(ret) <- c("PI", "meanpreds") + class(ret)<-"pred_intervals.bartBMA" + ret +} \ No newline at end of file diff --git a/R/pred_means_bbma_GS.R b/R/pred_means_bbma_GS.R new file mode 100644 index 0000000..6a54c6d --- /dev/null +++ b/R/pred_means_bbma_GS.R @@ -0,0 +1,113 @@ +#' @title Predictions for bart-bma output obtained from a Gibbs sampler +#' +#' @description This function produces predictions from BART-BMA by post-hoc Gibbs-sampling from the full conditionals of the terminal node parameters and the variance of the error term. See Hernandez et al. (2018) Appendix D for details. +#' @param object bartBMA object obtained from function bartBMA +#' @param num_iter Total number of iterations of the Gibbs sampler (including burn-in). +#' @param burnin Number of burn-on iterations of the Gibbs sampler. +#' @param newdata Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object. +#' @param update_resids Option for whether to update the partial residuals in the gibbs sampler. If equal to 1, updates partial residuals, if equal to zero, does not update partial residuals. The defaullt setting is to update the partial residua;s. +#' @export +#' @return The output is a vector of predictions. +#' @examples +#' set.seed(100) +#' #simulate some data +#' N <- 100 +#' p<- 100 +#' epsilon <- rnorm(N) +#' xcov <- matrix(runif(N*p), nrow=N) +#' y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +#' epsilontest <- rnorm(N) +#' xcovtest <- matrix(runif(N*p), nrow=N) +#' ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ +#' 5*xcovtest[,5]+epsilontest +#' +#' #Train the object +#' bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, +#' only_max_num_trees = 1,split_rule_node = 0) +#' #Obtain the prediction intervals +#' pred_means_bbma_GS(bart_bma_example,1000,100,newdata=NULL,update_resids=1) + + +pred_means_bbma_GS<-function(object,num_iter,burnin,newdata=NULL,update_resids=1){ + #object will be bartBMA object. + if(update_resids==0){ + if(is.null(newdata) && length(object)==16){ + #if test data specified separately + gs_chains<-gibbs_sampler_no_update_exp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(object$test_data),object$a,object$sigma,0,object$nu,object$lambda,object$sum_residuals,object$test_data) + }else if(is.null(newdata) && length(object)==14){ + #else return Pred Ints for training data + gs_chains<-gibbs_sampler_no_update2_exp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + object$a,object$sigma,0,object$nu,object$lambda,object$sum_residuals) + + }else{ + #if test data included in call to object + gs_chains<-gibbs_sampler_no_update_exp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(newdata), object$a,object$sigma,0,object$nu,object$lambda,object$sum_residuals,newdata) + } + }else{ + if(is.null(newdata) && length(object)==16){ + #if test data specified separately + gs_chains<-gibbs_sampler_exp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(object$test_data),object$a,object$sigma,0,object$nu,object$lambda,object$sum_residuals,object$test_data) + }else if(is.null(newdata) && length(object)==14){ + #else return Pred Ints for training data + gs_chains<-gibbs_sampler2_exp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + object$a,object$sigma,0,object$nu,object$lambda,object$sum_residuals) + + }else{ + #if test data included in call to object + gs_chains<-gibbs_sampler_exp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(newdata), object$a,object$sigma,0,object$nu,object$lambda,object$sum_residuals,newdata) + } + } + + #y_posterior_sum_trees<-gs_chains[[4]] + #y_orig_post_sum_trees<-gs_chains[[5]] + #sigma_chains<-gs_chains[[3]] + if(is.null(newdata) && length(object)==16){ + #y_posterior_sum_trees<-gs_chains[[4]] + y_orig_post_sum_trees<-gs_chains[[2]] ##[[5]] + sigma_chains<-gs_chains[[1]] #[[3]] + }else if(is.null(newdata) && length(object)==14){ + #y_posterior_sum_trees<-gs_chains[[1]] + y_orig_post_sum_trees<-gs_chains[[2]] #[[2]] + sigma_chains<-gs_chains[[1]] #[[3]] + + }else{ + #y_posterior_sum_trees<-gs_chains[[4]] + y_orig_post_sum_trees<-gs_chains[[2]] #[[5]] + sigma_chains<-gs_chains[[1]] #[[3]] + } + + sum_of_tree_BIC<- -0.5*object$bic + weights<-exp(sum_of_tree_BIC-(max(sum_of_tree_BIC)+log(sum(exp(sum_of_tree_BIC-max(sum_of_tree_BIC)))))) + #final_length<-num_iter-burnin + num_its_to_sample<-round(weights*(num_iter-burnin)) + #final_sigma_chain<-numeric(0) + + #final_y_chain<-matrix(nrow=0,ncol=ncol(y_posterior_sum_trees[[1]])) + final_yorig_chain<-matrix(nrow=0,ncol=ncol(y_orig_post_sum_trees[[1]])) + + for(i in 1:length(sigma_chains)){ + sample_its<-sample(burnin:num_iter,num_its_to_sample[i]) + #final_sigma_chain<-c(final_sigma_chain,sigma_chains[[i]][sample_its]) + #now do the same for predicted response updates + #post_y_i<-y_posterior_sum_trees[[i]] + post_yorig_i<-y_orig_post_sum_trees[[i]] + #final_y_chain<-rbind(final_y_chain,post_y_i[sample_its,]) + final_yorig_chain<-rbind(final_yorig_chain,post_yorig_i[sample_its,]) + + } + meanpreds<-apply(final_yorig_chain,2,mean) + + ret<-meanpreds + #length(ret)<-1 + #ret[[1]]<-meanpreds + #ret[[2]] <- final_y_chain + #ret[[3]] <- post_y_i + + class(ret)<-"pred_means_bbma.bartBMA" + ret + +} \ No newline at end of file diff --git a/R/pred_means_new_initials_GS.R b/R/pred_means_new_initials_GS.R new file mode 100644 index 0000000..551f098 --- /dev/null +++ b/R/pred_means_new_initials_GS.R @@ -0,0 +1,147 @@ +#' @title Predictions for bart-bma output obtained from a Gibbs sampler +#' +#' @description This function produces predictions from BART-BMA by post-hoc Gibbs-sampling from the full conditionals of the terminal node parameters and the variance of the error term. See Hernandez et al. (2018) Appendix D for details. +#' @param object bartBMA object obtained from function bartBMA +#' @param num_iter Total number of iterations of the Gibbs sampler (including burn-in). +#' @param burnin Number of burn-on iterations of the Gibbs sampler. +#' @param newdata Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object. +#' @param update_resids Option for whether to update the partial residuals in the gibbs sampler. If equal to 1, updates partial residuals, if equal to zero, does not update partial residuals. The defaullt setting is to update the partial residua;s. +#' @param trainingdata The matrix of training data. +#' @export +#' @return The output is a vector of predictions. +#' @examples +#' set.seed(100) +#' #simulate some data +#' N <- 100 +#' p<- 100 +#' epsilon <- rnorm(N) +#' xcov <- matrix(runif(N*p), nrow=N) +#' y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +#' epsilontest <- rnorm(N) +#' xcovtest <- matrix(runif(N*p), nrow=N) +#' ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ +#' 5*xcovtest[,5]+epsilontest +#' +#' #Train the object +#' bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, +#' only_max_num_trees = 1,split_rule_node = 0) +#' #Obtain the prediction intervals +#' pred_means_bbma_new_initials_GS(bart_bma_example,1000,100,newdata=NULL,update_resids=1,xcovtest) + +pred_means_bbma_new_initials_GS<-function(object,num_iter,burnin,newdata=NULL,update_resids=1,trainingdata){ + #object will be bartBMA object. + + scaled_train_y <- scale_response(min(object$response),max(object$response),-0.5,0.5,object$response) + # + # diff_inital_resids <- list() + # resid_length <- length(object$sum_residuals[[1]][[1]]) + # #initial_partial_resids <- rep(0.8*mean(scaled_train_y),resid_length ) + # + # + # for(i in 1:length(object$sum_residuals)){ + # diff_inital_resids[[i]] <- rep(list(scaled_train_y- ((length(object$sum_residuals[[i]])-1 )/ length(object$sum_residuals[[i]]))*mean(scaled_train_y)), + # length(object$sum_residuals[[i]])) + # } + get_resids <- get_initial_resids(trainingdata,object$sumoftrees,scaled_train_y) + diff_inital_resids<- get_resids[[1]] + new_pred_list1 <- get_resids[[2]] + + + + if(update_resids==0){ + if(is.null(newdata) && length(object)==16){ + #if test data specified separately + gs_chains<-gibbs_sampler_no_update_exp_new_inits(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(object$test_data),object$a,object$sigma,0,object$nu,object$lambda, + diff_inital_resids, + object$test_data, + new_pred_list1) + }else if(is.null(newdata) && length(object)==14){ + #else return Pred Ints for training data + gs_chains<-gibbs_sampler_no_update2_exp_new_inits(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + object$a,object$sigma,0,object$nu,object$lambda, + diff_inital_resids, + new_pred_list1) + + }else{ + #if test data included in call to object + gs_chains<-gibbs_sampler_no_update_exp_new_inits(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(newdata), object$a,object$sigma,0,object$nu,object$lambda, + diff_inital_resids, + newdata, + new_pred_list1) + } + }else{ + if(is.null(newdata) && length(object)==16){ + #if test data specified separately + gs_chains<-gibbs_sampler_exp_new_inits(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(object$test_data),object$a,object$sigma,0,object$nu,object$lambda, + diff_inital_resids, + object$test_data, + new_pred_list1) + }else if(is.null(newdata) && length(object)==14){ + #else return Pred Ints for training data + gs_chains<-gibbs_sampler2_exp_new_inits(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + object$a,object$sigma,0,object$nu,object$lambda, + diff_inital_resids, + new_pred_list1) + + }else{ + #if test data included in call to object + gs_chains<-gibbs_sampler_exp_new_inits(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(newdata), object$a,object$sigma,0,object$nu,object$lambda, + diff_inital_resids, + newdata, + new_pred_list1) + } + } + + #y_posterior_sum_trees<-gs_chains[[4]] + #y_orig_post_sum_trees<-gs_chains[[5]] + #sigma_chains<-gs_chains[[3]] + if(is.null(newdata) && length(object)==16){ + #y_posterior_sum_trees<-gs_chains[[4]] + y_orig_post_sum_trees<-gs_chains[[2]] ##[[5]] + sigma_chains<-gs_chains[[1]] #[[3]] + }else if(is.null(newdata) && length(object)==14){ + #y_posterior_sum_trees<-gs_chains[[1]] + y_orig_post_sum_trees<-gs_chains[[2]] #[[2]] + sigma_chains<-gs_chains[[1]] #[[3]] + + }else{ + #y_posterior_sum_trees<-gs_chains[[4]] + y_orig_post_sum_trees<-gs_chains[[2]] #[[5]] + sigma_chains<-gs_chains[[1]] #[[3]] + } + + sum_of_tree_BIC<- -0.5*object$bic + weights<-exp(sum_of_tree_BIC-(max(sum_of_tree_BIC)+log(sum(exp(sum_of_tree_BIC-max(sum_of_tree_BIC)))))) + #final_length<-num_iter-burnin + num_its_to_sample<-round(weights*(num_iter-burnin)) + #final_sigma_chain<-numeric(0) + + #final_y_chain<-matrix(nrow=0,ncol=ncol(y_posterior_sum_trees[[1]])) + final_yorig_chain<-matrix(nrow=0,ncol=ncol(y_orig_post_sum_trees[[1]])) + + for(i in 1:length(sigma_chains)){ + sample_its<-sample(burnin:num_iter,num_its_to_sample[i]) + #final_sigma_chain<-c(final_sigma_chain,sigma_chains[[i]][sample_its]) + #now do the same for predicted response updates + #post_y_i<-y_posterior_sum_trees[[i]] + post_yorig_i<-y_orig_post_sum_trees[[i]] + #final_y_chain<-rbind(final_y_chain,post_y_i[sample_its,]) + final_yorig_chain<-rbind(final_yorig_chain,post_yorig_i[sample_its,]) + + } + meanpreds<-apply(final_yorig_chain,2,mean) + + ret<-meanpreds + #length(ret)<-1 + #ret[[1]]<-meanpreds + #ret[[2]] <- final_y_chain + #ret[[3]] <- post_y_i + + class(ret)<-"pred_means_bbma.bartBMA" + ret + +} \ No newline at end of file diff --git a/R/predict_bartBMA.R b/R/predict_bartBMA.R new file mode 100644 index 0000000..aa47486 --- /dev/null +++ b/R/predict_bartBMA.R @@ -0,0 +1,60 @@ +#' @title Predictions for a new dataset using an existing bartbma object +#' +#' @description This function produces predictions for a new dataset using a previously obtained bartBMA object. +#' @param object A bartBMA object obtained using the barBMA function. +#' @param newdata Covariate matrix for new dataset. +#' @export +#' @return A vector of predictions for the new dataset. +#' @examples +#' set.seed(100) +#' #simulate some data +#' N <- 100 +#' p<- 100 +#' epsilon <- rnorm(N) +#' xcov <- matrix(runif(N*p), nrow=N) +#' y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +#' epsilontest <- rnorm(N) +#' xcovtest <- matrix(runif(N*p), nrow=N) +#' ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ +#' 5*xcovtest[,5]+epsilontest +#' +#' #Train the object +#' bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, +#' only_max_num_trees = 1,split_rule_node = 0) +#' #Obtain the prediction intervals +#' predict_bartBMA(bart_bma_example,newdata=xcovtest) + +predict_bartBMA<-function(object,newdata){ + #preds<-get_BART_BMA_test_predictions(newdata,object$bic,object$sumoftrees,object$y_minmax) + #orig_preds<-preds[[1]] + #class(orig_preds)<-"predict.bartBMA" + #orig_preds + + if(is.null(newdata) && length(object)==16){ + #if test data specified separately + ret<-preds_bbma_lin_alg_outsamp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,0, 0,object$nrowTrain, + nrow(object$test_data),object$a,object$sigma,0,object$nu, + object$lambda,#diff_inital_resids, + object$test_data + ) + }else{if(is.null(newdata) && length(object)==14){ + #else return Pred Ints for training data + ret<-preds_bbma_lin_alg_insamp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,0, 0,object$nrowTrain, + object$a,object$sigma,0,object$nu, + object$lambda#diff_inital_resids + ) + + }else{ + #if test data included in call to object + ret<-preds_bbma_lin_alg_outsamp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,0, 0,object$nrowTrain, + nrow(newdata), object$a,object$sigma,0,object$nu, + object$lambda,#diff_inital_resids, + newdata + ) + }} + + + class(ret)<-"predict.bartBMA" + ret + +} \ No newline at end of file diff --git a/R/predict_probit_bartBMA.R b/R/predict_probit_bartBMA.R new file mode 100644 index 0000000..bcb07ef --- /dev/null +++ b/R/predict_probit_bartBMA.R @@ -0,0 +1,77 @@ +#' @title Predictions for a new dataset using an existing probit_bartBMA object +#' +#' @description This function produces predictions for a new dataset using a previously obtained bartBMA object. +#' @param object A probit_bartBMA object obtained using the probit_bartBMA function. +#' @param newdata Covariate matrix for new dataset. +#' @export +#' @return The output is a list of length 2: +#' \item{probs}{A vector of estimated probabilities for newdata.} +#' \item{pred_binary}{A vector of binary predictions for newdata.} +#' @examples +#' #Example from BART package (McCulloch et al. 2019) +#' set.seed(99) +#' n=100 +#' x = sort(-2+4*runif(n)) +#' X=matrix(x,ncol=1) +#' f = function(x) {return((1/2)*x^3)} +#' FL = function(x) {return(exp(x)/(1+exp(x)))} +#' pv = FL(f(x)) +#' y = rbinom(n,1,pv) +#' +#' trained_probit_bbma <- probit_bartBMA(x.train = X,y.train = y) +#' +#' np=100 +#' xp=-2+4*(1:np)/np +#' Xp=matrix(xp,ncol=1) +#' +#' predict_probit_bartBMA(trained_probit_bbma,Xp) + +predict_probit_bartBMA<-function(object,newdata){ + + + #preds<-get_BART_BMA_test_predictions(newdata,object$bic,object$sumoftrees,object$y_minmax) + + + + if(is.null(newdata) && length(object)==16){ + #if test data specified separately + preds<-preds_bbma_lin_alg_outsamp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,0, 0,object$nrowTrain, + nrow(object$test_data),object$a,object$sigma,0,object$nu, + object$lambda,#diff_inital_resids, + object$test_data + ) + }else{if(is.null(newdata) && length(object)==14){ + #else return Pred Ints for training data + preds<-preds_bbma_lin_alg_insamp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,0, 0,object$nrowTrain, + object$a,object$sigma,0,object$nu, + object$lambda#diff_inital_resids + ) + + }else{ + #if test data included in call to object + preds<-preds_bbma_lin_alg_outsamp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,0, 0,object$nrowTrain, + nrow(newdata), object$a,object$sigma,0,object$nu, + object$lambda,#diff_inital_resids, + newdata + ) + }} + + + + + + + + + ret <- list() + + #probs <-pnorm(preds[[1]]) + probs <-pnorm(preds) + + pred_binary <- ifelse(probs<=0.5,0,1) + + ret$probs <- probs + ret$pred_binary <- pred_binary + + ret +} \ No newline at end of file diff --git a/R/preds_bbma_lin_alg.R b/R/preds_bbma_lin_alg.R new file mode 100644 index 0000000..2da13a2 --- /dev/null +++ b/R/preds_bbma_lin_alg.R @@ -0,0 +1,66 @@ +#' @title Predictions for bart-bma output obtained from the posterior probability weighted averaged of the posterior means for each model +#' +#' @description This function produces predictions from BART-BMA by obtaining the posterior probability weighted averaged of the posterior means for each model. +#' @param object bartBMA object obtained from function bartBMA +#' @param num_iter Total number of iterations of the Gibbs sampler (including burn-in). +#' @param burnin Number of burn-on iterations of the Gibbs sampler. +#' @param newdata Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object. +#' @param update_resids Option for whether to update the partial residuals in the gibbs sampler. If equal to 1, updates partial residuals, if equal to zero, does not update partial residuals. The defaullt setting is to update the partial residua;s. +#' @param trainingdata The matrix of training data. +#' @export +#' @return A vector of predictions. +#' @examples +#' #set the seed +#' set.seed(100) +#' #simulate some data +#' N <- 100 +#' p<- 100 +#' epsilon <- rnorm(N) +#' xcov <- matrix(runif(N*p), nrow=N) +#' y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +#' epsilontest <- rnorm(N) +#' xcovtest <- matrix(runif(N*p), nrow=N) +#' ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ +#' 5*xcovtest[,5]+epsilontest +#' +#' #Train the object +#' bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, +#' only_max_num_trees = 1,split_rule_node = 0) +#' #Obtain the prediction intervals +#' preds_bbma_lin_alg(bart_bma_example,1000,100,newdata=xcovtest) + + +preds_bbma_lin_alg<-function(object,num_iter,burnin,newdata=NULL,update_resids=1,trainingdata){ + #object will be bartBMA object. + + + + + if(is.null(newdata) && length(object)==16){ + #if test data specified separately + ret<-preds_bbma_lin_alg_outsamp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(object$test_data),object$a,object$sigma,0,object$nu, + object$lambda,#diff_inital_resids, + object$test_data + ) + }else{if(is.null(newdata) && length(object)==14){ + #else return Pred Ints for training data + ret<-preds_bbma_lin_alg_insamp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + object$a,object$sigma,0,object$nu, + object$lambda#diff_inital_resids + ) + + }else{ + #if test data included in call to object + ret<-preds_bbma_lin_alg_outsamp(object$sumoftrees,object$obs_to_termNodesMatrix,object$response,object$bic,num_iter, burnin,object$nrowTrain, + nrow(newdata), object$a,object$sigma,0,object$nu, + object$lambda,#diff_inital_resids, + newdata + ) + }} + + + + ret + +} \ No newline at end of file diff --git a/R/printbartBMA.R b/R/printbartBMA.R new file mode 100644 index 0000000..8e1cd9b --- /dev/null +++ b/R/printbartBMA.R @@ -0,0 +1,11 @@ +#write print, summary and predict S3 classes to call for package +#print will take in an object of class "bartBMA" +#' @importFrom utils head +print.bartBMA<-function(x, ...){ + cat("Call:\n") + print(x$call) + cat("Training Fitted Values:\n") + print(c(as.numeric(head(round(x$fitted.values,2)))," ...")) + cat("Sums of Trees in Occam's Window:\n") + print(x$sumoftrees) +} \ No newline at end of file diff --git a/R/probit_bartBMA.R b/R/probit_bartBMA.R new file mode 100644 index 0000000..a3d26c9 --- /dev/null +++ b/R/probit_bartBMA.R @@ -0,0 +1,184 @@ + +#' @title Probit BART_BMA for classification of a binary variable +#' +#' @description This is an implementation of Bayesian Additive Regression Trees (Chipman et al. 2018) using Bayesian Model Averaging (Hernandez et al. 2018). +#' @param x.train Training data covariate matrix +#' @param y.train Training data outcome vector. +#' @param a This is a parameter that influences the variance of terminal node parameter values. Default value a=3. +#' @param nu This is a hyperparameter in the distribution of the variance of the error term. THe inverse of the variance is distributed as Gamma (nu/2, nu*lambda/2). Default value nu=3. +#' @param sigquant Calibration quantile for the inverse chi-squared prior on the variance of the error term. +#' @param c This determines the size of Occam's Window +#' @param pen This is a parameter used by the Pruned Exact Linear Time Algorithm when finding changepoints. Default value pen=12. +#' @param num_cp This is a number between 0 and 100 that determines the proportion of changepoints proposed by the changepoint detection algorithm to keep when growing trees. Default num_cp=20. +#' @param x.test Test data covariate matrix. Default x.test=matrix(0.0,0,0). +#' @param num_rounds Number of trees. (Maximum number of trees in a sum-of-tree model). Default num_rounds=5. +#' @param alpha Parameter in prior probability of tree node splitting. Default alpha=0.95 +#' @param beta Parameter in prior probability of tree node splitting. Default beta=1 +#' @param split_rule_node Binary variable. If equals 1, then find a new set of potential splitting points via a changepoint algorithm after adding each split to a tree. If equals zero, use the same set of potential split points for all splits in a tree. Default split_rule_node=0. +#' @param gridpoint Binary variable. If equals 1, then a grid search changepoint detection algorithm will be used. If equals 0, then the Pruned Exact Linear Time (PELT) changepoint detection algorithm will be used (Killick et al. 2012). Default gridpoint=0. +#' @param maxOWsize Maximum number of models to keep in Occam's window. Default maxOWsize=100. +#' @param num_splits Maximum number of splits in a tree +#' @param gridsize This integer determines the size of the grid across which to search if gridpoint=1 when finding changepoints for constructing trees. +#' @param zero_split Binary variable. If equals 1, then zero split trees can be included in a sum-of-trees model. If equals zero, then only trees with at least one split can be included in a sum-of-trees model. +#' @param only_max_num_trees Binary variable. If equals 1, then only sum-of-trees models containing the maximum number of trees, num_rounds, are selected. If equals 0, then sum-of-trees models containing less than num_rounds trees can be selected. The default is only_max_num_trees=1. +#' @param min_num_obs_for_split This integer determines the minimum number of observations in a (parent) tree node for the algorithm to consider potential splits of the node. +#' @param min_num_obs_after_split This integer determines the minimum number of observations in a child node resulting from a split in order for a split to occur. If the left or right chikd node has less than this number of observations, then the split can not occur. +#' @param exact_residuals Binary variable. If equal to 1, then trees are added to sum-of-tree models within each round of the algorithm by detecting changepoints in the exact residuals. If equals zero, then changepoints are detected in residuals that are constructed from approximate predictions. +#' @param spike_tree If equal to 1, then the Spike-and-Tree prior will be used, otherwise the standard BART prior will be used. The number of splitting variables has a beta-binomial prior. The number of terminal nodes has a truncated Poisson prior, and then a uniform prior is placed on the set of valid constructions of trees given the splitting variables and number of terminal nodes. +#' @param s_t_hyperprior If equals 1 and spike_tree equals 1, then a beta distribution hyperprior is placed on the variable inclusion probabilities for the spike and tree prior. The hyperprior parameters are a_s_t and b_s_t. +#' @param p_s_t If spike_tree=1 and s_t_hyperprior=0, then p_s_t is the prior variable inclusion probability. +#' @param a_s_t If spike_tree=1 and s_t_hyperprior=1, then a_s_t is a parameter of a beta distribution hyperprior +#' @param b_s_t If spike_tree=1 and s_t_hyperprior=1, then b_s_t is a parameter of a beta distribution hyperprior +#' @param lambda_poisson This is a parameter for the Spike-and-Tree prior. It is the parameter for the (truncated and conditional on the number of splitting variables) Poisson prior on the number of terminal nodes. +#' @param less_greedy If equal to one, then a less greedy model search algorithm is used. +#' @param ... Further arguments. +#' @rdname probit_bartBMA +#' @export +#' @return The following objects are returned by bartbma: +#' \item{fitted.values}{The vector of predictions of the outcome for all training observations.} +#' \item{sumoftrees}{This is a list of lists of matrices. The outer list corresponds to a list of sum-of-tree models, and each element of the outer list is a list of matrices describing the structure of the trees within a sum-of-tree model. See details.} +#' \item{obs_to_termNodesMatrix}{This is a list of lists of matrices. The outer list corresponds to a list of sum-of-tree models, and each element of the outer list is a list of matrices describing to which node each of the observations is allocated to at all depths of each tree within a sum-of-tree model. See details.} +#' \item{bic}{This is a vector of BICs for each sum-of-tree model.} +#' \item{test.preds}{A vector of test data predictions. This output only is given if there is test data in the input.} +#' \item{sum_residuals}{CURRENTLY INCORRECT OUTPUT. A List (over sum-of-tree models) of lists (over single trees in a model) of vectors of partial residuals. Unless the maximum number of trees in a model is one, in which case the output is a list (over single tree models) of vectors of partial residuals, which are all equal to the outcome vector.} +#' \item{numvars}{This is the total number of variables in the input training data matrix.} +#' \item{call}{match.call returns a call in which all of the specified arguments are specified by their full names.} +#' \item{y_minmax}{Range of the input training data outcome vector.} +#' \item{response}{Input taining data outcome vector.} +#' \item{nrowTrain}{number of observations in the input training data.} +#' \item{sigma}{sd(y.train)/(max(y.train)-min(y.train))} +#' \item{a}{input parameter} +#' \item{nu}{input parameter} +#' \item{lambda}{parameter determined by the inputs sigma, sigquant, and nu} +#' \item{fitted.probs}{In-sample fitted probabilities} +#' \item{fitted.classes}{In-sample fitted classes} +#' @useDynLib bartBMA, .registration = TRUE +#' @examples +#' #Example from BART package (McCulloch et al. 2019) +#' set.seed(99) +#' n=100 +#' x = sort(-2+4*runif(n)) +#' X=matrix(x,ncol=1) +#' f = function(x) {return((1/2)*x^3)} +#' FL = function(x) {return(exp(x)/(1+exp(x)))} +#' pv = FL(f(x)) +#' y = rbinom(n,1,pv) +#' probit_bartBMA(x.train = X,y.train = y) + +probit_bartBMA<-function(x.train,...)UseMethod("probit_bartBMA") + +#' @rdname probit_bartBMA +#' @export probit_bartBMA.default +#' @export +probit_bartBMA.default<-function(x.train,y.train, + a=3,nu=3,sigquant=0.9,c=1000, + pen=12,num_cp=20,x.test=matrix(0.0,0,0), + num_rounds=5,alpha=0.95,beta=2,split_rule_node=0, + gridpoint=0,maxOWsize=100,num_splits=5,gridsize=10,zero_split=1,only_max_num_trees=1, + min_num_obs_for_split=2, min_num_obs_after_split=2, + exact_residuals=1, + spike_tree=0, s_t_hyperprior=1, p_s_t=0.5, a_s_t=1,b_s_t=3, + lambda_poisson=10,less_greedy=0,...){ + + + num_obs=nrow(x.train) + num_vars=ncol(x.train) + + if(is.factor(y.train)) { + if(length(levels(y.train)) != 2) stop("y.train is a factor with number of levels != 2") + #binary = TRUE + # y.train = as.numeric(y.train)-1 + #stop("Response must be a numeric vector") + } else { + if(!((length(unique(y.train)) == 2) & (max(y.train) == 1) & (min(y.train) == 0))) { + stop("y.train is a numeric with number of unique elements != 2") + #cat('NOTE: assumming numeric response is binary\n') + #binary = TRUE + #stop("Response must be a numeric vector") + } + } + + + Zlatent.train <- ifelse(y.train==1,3.1,-3.1) + binary=FALSE + start_mean=0 + start_sd=1 + mu=0 + sigma_mu=0 + sigma=sd(Zlatent.train)/(max(Zlatent.train)-min(Zlatent.train)) + qchi = qchisq(1.0-sigquant,nu,1,0) + lambda = (sigma*sigma*qchi)/nu + if(is.factor(Zlatent.train)) { + # if(length(levels(Zlatent.train)) != 2) stop("Zlatent.train is a factor with number of levels != 2") + binary = TRUE + # Zlatent.train = as.numeric(Zlatent.train)-1 + stop("Response must be a numeric vector") + } else { + if((length(unique(Zlatent.train)) == 2) & (max(Zlatent.train) == 1) & (min(Zlatent.train) == 0)) { + cat('NOTE: assumming numeric response is binary\n') + binary = TRUE + #stop("Response must be a numeric vector") + } + } + if(gridsize<1) stop("gridsize must be a positive integer") + + if(is.vector(x.train) | is.factor(x.train)| is.data.frame(x.train)) x.train = as.matrix(x.train) + if(is.vector(x.test) | is.factor(x.test)| is.data.frame(x.test)) x.test = as.matrix(x.test) + + if(is.matrix(x.train)) { + if(nrow(x.test)>0) { + if(!is.matrix(x.test)) stop('x.test must be a matrix') + } + } + # check input arguments: + # if((!is.matrix(x.train)) || (typeof(x.train)!="double")) stop("argument x.train must be a double matrix") + # if((!is.matrix(x.test)) || (typeof(x.test)!="double")) stop("argument x.test must be a double matrix") + if((!is.matrix(x.train))) stop("argument x.train must be a double matrix") + if((!is.matrix(x.test)) ) stop("argument x.test must be a double matrix") + if(!binary) { + if((!is.vector(Zlatent.train))) stop("argument Zlatent.train must be a double vector") + } + if(nrow(x.train) != length(Zlatent.train)) stop("number of rows in x.train must equal length of Zlatent.train") + if((nrow(x.test) >0) && (ncol(x.test)!=ncol(x.train))) stop("input x.test must have the same number of columns as x.train") + #if((!is.na(sigest)) && (typeof(sigest)!="double")) stop("input sigest must be double") + #if((!is.na(sigest)) && (sigest<0.0)) stop("input sigest must be positive") + #if((mode(sigdf)!="numeric") || (sigdf<0)) stop("input sigdf must be a positive number") + if(c<1)stop("Value of Occam's Window has to be greater than 0."); + if(num_cp<0 || num_cp>100)stop("Value of num_cp should be a value between 1 and 100."); + + bartBMA_call=BART_BMA_sumLikelihood(less_greedy, + spike_tree, s_t_hyperprior, p_s_t, a_s_t,b_s_t, + num_obs,num_vars,lambda_poisson, + x.train,Zlatent.train,start_mean,start_sd,a,mu,nu,lambda,c,sigma_mu, + pen,num_cp,x.test,num_rounds,alpha,beta,split_rule_node,gridpoint,maxOWsize, + num_splits,gridsize,zero_split,only_max_num_trees, + min_num_obs_for_split, min_num_obs_after_split, + exact_residuals) + + if(length(bartBMA_call)==6){ + #length of bartBMA_call is 6 if test data was included in the call + names(bartBMA_call)<-c("fitted.values","sumoftrees","obs_to_termNodesMatrix","bic","test.preds","sum_residuals") + bartBMA_call[[6]]<-bartBMA_call[[6]]#[[length(bartBMA_call[[6]])]] + bartBMA_call$test_data<-x.test + }else{ + names(bartBMA_call)<-c("fitted.values","sumoftrees","obs_to_termNodesMatrix","bic","sum_residuals") + bartBMA_call[[5]]<-bartBMA_call[[5]]#[[length(bartBMA_call[[5]])]] + } + + bartBMA_call$numvars<-ncol(x.train) + bartBMA_call$call<-match.call() + bartBMA_call[[2]]<-bartBMA_call[[2]]#[[length(bartBMA_call[[2]])]] + bartBMA_call[[3]]<-bartBMA_call[[3]]#[[length(bartBMA_call[[3]])]] + bartBMA_call$y_minmax<-range(Zlatent.train) + bartBMA_call$response<-Zlatent.train + bartBMA_call$nrowTrain<-nrow(x.train) + bartBMA_call$sigma<-sigma + bartBMA_call$a<-a + bartBMA_call$nu<-nu + bartBMA_call$lambda<-lambda + bartBMA_call$fitted.probs<-pnorm(bartBMA_call$fitted.values) + bartBMA_call$fitted.classes<-ifelse(bartBMA_call$fitted.probs<=0.5,0,1) + + class(bartBMA_call)<-"probit_bartBMA" + bartBMA_call +} \ No newline at end of file diff --git a/R/varImpScores.R b/R/varImpScores.R new file mode 100644 index 0000000..341aacc --- /dev/null +++ b/R/varImpScores.R @@ -0,0 +1,38 @@ +#' @title Variable importances as defined by Hernandez et al. (2018) +#' +#' @description This measure defines the importance of a variable as the model-probability weighted sum of the number of splits on the variable of interest, divided by the sum over all variables of such weighted counts of splits. +#' @param object A bartBMA object obtained using the barBMA function. +#' @export +#' @return A vector of variable importances. The variables are ordered in the same order that they occur in columns of the input covariate matrix used to obtain the input bartBMA object. +#' @examples +#' #set the seed +#' set.seed(100) +#' #simulate some data +#' N <- 100 +#' p<- 100 +#' epsilon <- rnorm(N) +#' xcov <- matrix(runif(N*p), nrow=N) +#' y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +#' epsilontest <- rnorm(N) +#' xcovtest <- matrix(runif(N*p), nrow=N) +#' ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ +#' 5*xcovtest[,5]+epsilontest +#' +#' #Train the object +#' bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, +#' only_max_num_trees = 1,split_rule_node = 0) +#' #Obtain the variable importances +#' varImpScores(bart_bma_example) + +varImpScores<-function(object){ + #object will be bartBMA object. + imp_vars2=get_weighted_var_imp(num_vars=object$numvars,BIC=object$bic,sum_trees=object$sumoftrees) + res<-apply(imp_vars2[[4]],2,sum) + #create varImpPlot command + vIP<-rep(NA,length(res)) + total_weighted_var_counts<-sum(res) + #now get variable inclusion probabilities + vIP<-res/total_weighted_var_counts + class(vIP)<-"varImpScores.bartBMA" + vIP +} \ No newline at end of file diff --git a/R/varIncProb.R b/R/varIncProb.R new file mode 100644 index 0000000..bce19d8 --- /dev/null +++ b/R/varIncProb.R @@ -0,0 +1,34 @@ +#' @title Variable inclusion probabilities as defined by Linero (2018) +#' +#' @description This measure defines the posterior inclusion probability of a variable as the model-probability weighted sum of indicator variables for whether the variable was used in any splitting rules in any of the trees in the sum-of-tree model. +#' @param object A bartBMA object obtained using the barBMA function. +#' @export +#' @return A vector of posterior inclusion probabilities. The variables are ordered in the same order that they occur in columns of the input covariate matrix used to obtain the input bartBMA object. +#' @examples +#' #set the seed +#' set.seed(100) +#' #simulate some data +#' N <- 100 +#' p<- 100 +#' epsilon <- rnorm(N) +#' xcov <- matrix(runif(N*p), nrow=N) +#' y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +#' epsilontest <- rnorm(N) +#' xcovtest <- matrix(runif(N*p), nrow=N) +#' ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ +#' 5*xcovtest[,5]+epsilontest +#' +#' #Train the object +#' bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, +#' only_max_num_trees = 1,split_rule_node = 0) +#' #Obtain the variable importances +#' varIncProb(bart_bma_example) + +varIncProb<-function(object){ + #object will be bartBMA object. + imp_vars2=get_weighted_var_imp(num_vars=object$numvars,BIC=object$bic,sum_trees=object$sumoftrees) + res<-apply((imp_vars2[[3]]>0)*imp_vars2[[1]],2,sum) + #create varImpPlot command + class(res)<-"varIncProb.bartBMA" + res +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..944c77f --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +# bartBMA + + + + +The goal of bartBMA is to provide an implementation of Bayesian Additive Regression Trees Using Bayesian Model Averaging (BART-BMA) (Hernandez et al. 2018) + +Hernández, B., Raftery, A. E., Pennington, S. R., & Parnell, A. C. (2018). Bayesian additive regression trees using Bayesian model averaging. Statistics and computing, 28(4), 869-890. + + +## Installation + +``` r +library(devtools) +install_github("bartBMA") +``` + +## Example + + +``` r +library(bartBMA) +## basic example code + +N <- 100 +p<- 100 +set.seed(100) + +epsilon <- rnorm(N) +xcov <- matrix(runif(N*p), nrow=N) +y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon + +epsilontest <- rnorm(N) +xcovtest <- matrix(runif(N*p), nrow=N) +ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+5*xcovtest[,5]+epsilontest + + +bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest, + zero_split = 1, only_max_num_trees = 1,split_rule_node = 0) + + +``` + diff --git a/build/partial.rdb b/build/partial.rdb new file mode 100644 index 0000000..699038e Binary files /dev/null and b/build/partial.rdb differ diff --git a/inst/REFERENCES.bib b/inst/REFERENCES.bib new file mode 100644 index 0000000..d6b8379 --- /dev/null +++ b/inst/REFERENCES.bib @@ -0,0 +1,87 @@ +@article{hernandez2018bayesian, + title={Bayesian additive regression trees using Bayesian model averaging}, + author={Hernandez, Belinda and Raftery, Adrian E and Pennington, Stephen R and Parnell, Andrew C}, + journal={Statistics and Computing}, + volume={28}, + number={4}, + pages={869--890}, + year={2018}, + publisher={Springer} +} +@article{doppelhofer2009jointnessa, + title={Jointness of growth determinants}, + author={Doppelhofer, Gernot and Weeks, Melvyn}, + journal={Journal of Applied Econometrics}, + volume={24}, + number={2}, + pages={209--244}, + year={2009}, + publisher={Wiley Online Library} +} +@article{ley2007jointness, + title={Jointness in Bayesian variable selection with applications to growth regression}, + author={Ley, Eduardo and Steel, Mark FJ}, + journal={Journal of Macroeconomics}, + volume={3}, + number={29}, + pages={476--493}, + year={2007} +} +@article{hofmarcher2018bivariate, + title={Bivariate jointness measures in Bayesian Model Averaging: Solving the conundrum}, + author={Hofmarcher, Paul and Cuaresma, Jesus Crespo and Grun, Bettina and Humer, Stefan and Moser, Mathias}, + journal={Journal of Macroeconomics}, + year={2018}, + publisher={Elsevier} +} +@article{cuaresma2015comprehensive, + title={A comprehensive approach to posterior jointness analysis in Bayesian model averaging applications}, + author={Cuaresma, Jesus Crespo and Grun, Bettina and Hofmarcher, Paul and Humer, Stefan and Moser, Mathias and others}, + journal={Vienna University of Economics and Business Department of Economics, Working Paper Series}, + volume={193}, + pages={1--26}, + year={2015} +} +@article{doppelhofer2009jointnessb, + title={Jointness of growth determinants: reply to comments by Rodney Strachan, Eduardo Ley and Mark FJ Steel}, + author={Doppelhofer, Gernot and Weeks, Melvyn}, + journal={Journal of Applied Econometrics}, + volume={24}, + number={2}, + pages={252--256}, + year={2009}, + publisher={Wiley Online Library} +} +@article{man2018critical, + title={Critical appraisal of jointness concepts in Bayesian model averaging: Evidence from life sciences, sociology, and other scientific fields}, + author={Man, Georg}, + journal={Journal of Applied Statistics}, + volume={45}, + number={5}, + pages={845--867}, + year={2018}, + publisher={Taylor \& Francis} +} +@article{strachan2009comment, + title={Comment on ‘Jointness of growth determinants’ by Gernot Doppelhofer and Melvyn Weeks}, + author={Strachan, Rodney W}, + journal={Journal of Applied Econometrics}, + volume={24}, + number={2}, + pages={245--247}, + year={2009}, + publisher={Wiley Online Library} +} +@article{chipman2010bart, + title={BART: Bayesian additive regression trees}, + author={Chipman, Hugh A and George, Edward I and McCulloch, Robert E and others}, + journal={The Annals of Applied Statistics}, + volume={4}, + number={1}, + pages={266--298}, + year={2010}, + publisher={Institute of Mathematical Statistics} +} + + + diff --git a/man/ITEs_CATT_bartBMA_exact_par.Rd b/man/ITEs_CATT_bartBMA_exact_par.Rd new file mode 100644 index 0000000..fd2c608 --- /dev/null +++ b/man/ITEs_CATT_bartBMA_exact_par.Rd @@ -0,0 +1,104 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ITEs_CATT_bartBMA_exact_par.R +\name{ITEs_CATT_bartBMA_exact_par} +\alias{ITEs_CATT_bartBMA_exact_par} +\title{Estimate ITEs, CATE, CATT, CATNT and obtain credible intervals (in-sample or out-of-sample).} +\usage{ +ITEs_CATT_bartBMA_exact_par( + object, + l_quant, + u_quant, + newdata = NULL, + update_resids = 1, + num_cores = 1, + root_alg_precision = 1e-05, + training_data, + zvec +) +} +\arguments{ +\item{object}{Output from ITEs_bartBMA of class ITE_ests.bartBMA.} + +\item{l_quant}{Lower quantile of credible intervals for the ITEs, CATT, CATNT.} + +\item{u_quant}{Upper quantile of credible intervals for the ITEs, CATT, CATNT.} + +\item{newdata}{Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object.} + +\item{update_resids}{Option for whether to update the partial residuals in the gibbs sampler. If equal to 1, updates partial residuals, if equal to zero, does not update partial residuals. The defaullt setting is to update the partial residuals.} + +\item{num_cores}{Number of cores used in parallel.} + +\item{root_alg_precision}{The algorithm should obtain approximate bounds that are within the distance root_alg_precision of the true quantile for the chosen average of models.} + +\item{training_data}{The training data matrix} + +\item{zvec}{The treatment indicator vector. Training data treatment vector for insample predictions, test data treatment vector for out of sample predictions.} +} +\value{ +The output is a list of length 8: +\item{ITE_intervals}{A 3 by n matrix, where n is the number of observations. The first row gives the l_quant*100 quantiles of the individual treatment effects. The second row gives the medians of the ITEs. The third row gives the u_quant*100 quantiles of the ITEs.} +\item{ITE_estimates}{An n by 1 matrix containing the Individual Treatment Effect estimates.} +\item{CATE_estimate}{The Conditional Average Treatment Effect Estimate} +\item{CATE_Interval}{A 3 by 1 matrix. The first element is the l_quant*100 quantile of the CATE distribution, the second element is the median of the CATE distribution, and the thied element is the u_quant*100 quantile of the CATE distribution.} +\item{CATT_estimate}{The Conditional Average Treatment Effect on the Treated Estimate} +\item{CATT_Interval}{A 3 by 1 matrix. The first element is the l_quant*100 quantile of the CATT distribution, the second element is the median of the CATT distribution, and the thied element is the u_quant*100 quantile of the CATT distribution.} +\item{CATNT_estimate}{The Conditional Average Treatment Effect on the Not Treated Estimate} +\item{CATNT_Interval}{A 3 by 1 matrix. The first element is the l_quant*100 quantile of the CATNT distribution, the second element is the median of the CATNT distribution, and the thied element is the u_quant*100 quantile of the CATNT distribution.} +} +\description{ +This function takes a set of sum of tree models obtained from ITEs_bartBMA, and then estimates ITEs, and the CATE, CATT, and CATNT and obtains prediction intervals +} +\examples{ +\dontrun{ +#Example of BART-BMA for ITE estimation +# Applied to data simulations from Hahn et al. (2020, Bayesian Analysis) +# "Bayesian Regression Tree Models for Causal Inference: Regularization, +# Confounding, and Heterogeneous Effects +n <- 250 +x1 <- rnorm(n) +x2 <- rnorm(n) +x3 <- rnorm(n) +x4 <- rbinom(n,1,0.5) +x5 <- as.factor(sample( LETTERS[1:3], n, replace=TRUE)) + +p= 0 +xnoise = matrix(rnorm(n*p), nrow=n) +x5A <- ifelse(x5== 'A',1,0) +x5B <- ifelse(x5== 'B',1,0) +x5C <- ifelse(x5== 'C',1,0) + +x_covs_train <- cbind(x1,x2,x3,x4,x5A,x5B,x5C,xnoise) + +#Treatment effect +#tautrain <- 3 +tautrain <- 1+2*x_covs_train[,2]*x_covs_train[,4] + +#Prognostic function +mutrain <- 1 + 2*x_covs_train[,5] -1*x_covs_train[,6]-4*x_covs_train[,7] + +x_covs_train[,1]*x_covs_train[,3] +sd_mtrain <- sd(mutrain) +utrain <- runif(n) +#pitrain <- 0.8*pnorm((3*mutrain/sd_mtrain)-0.5*x_covs_train[,1])+0.05+utrain/10 +pitrain <- 0.5 +ztrain <- rbinom(n,1,pitrain) +ytrain <- mutrain + tautrain*ztrain +#pihattrain <- pbart(x_covs_train,ztrain )$prob.train.mean + +#set lower and upper quantiles for intervals +lbound <- 0.025 +ubound <- 0.975 + + +trained_bbma <- ITEs_bartBMA(x_covariates = x_covs_train, + z_train = ztrain, + y_train = ytrain) + +example_output <- ITEs_CATT_bartBMA_exact_par(trained_bbma[[2]], + l_quant = lbound, + u_quant= ubound, + training_data = x_covs_train, + zvec = ztrain, + num_cores = 1) +} +} diff --git a/man/ITEs_bartBMA.Rd b/man/ITEs_bartBMA.Rd new file mode 100644 index 0000000..3e263ef --- /dev/null +++ b/man/ITEs_bartBMA.Rd @@ -0,0 +1,121 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ITEs_bartBMA.R +\name{ITEs_bartBMA} +\alias{ITEs_bartBMA} +\title{ITE Predictions (in-sample) using bartBMA and the method described by Hill (2011)} +\usage{ +ITEs_bartBMA( + x_covariates, + z_train, + y_train, + a = 3, + nu = 3, + sigquant = 0.9, + c = 1000, + pen = 12, + num_cp = 20, + x.test = matrix(0, 0, 0), + num_rounds = 5, + alpha = 0.95, + beta = 2, + split_rule_node = 0, + gridpoint = 0, + maxOWsize = 100, + num_splits = 5, + gridsize = 10, + zero_split = 1, + only_max_num_trees = 1, + min_num_obs_for_split = 2, + min_num_obs_after_split = 2 +) +} +\arguments{ +\item{x_covariates}{Covaraite matrix for training bartBMA.} + +\item{z_train}{treatment vector for traiing bartBMA.} + +\item{y_train}{outcome vector for training bartBMA.} + +\item{a}{This is a parameter that influences the variance of terminal node parameter values. Default value a=3.} + +\item{nu}{This is a hyperparameter in the distribution of the variance of the error term. THe inverse of the variance is distributed as Gamma (nu/2, nu*lambda/2). Default value nu=3.} + +\item{sigquant}{Calibration quantile for the inverse chi-squared prior on the variance of the error term.} + +\item{c}{This determines the size of Occam's Window} + +\item{pen}{This is a parameter used by the Pruned Exact Linear Time Algorithm when finding changepoints. Default value pen=12.} + +\item{num_cp}{This is a number between 0 and 100 that determines the proportion of changepoints proposed by the changepoint detection algorithm to keep when growing trees. Default num_cp=20.} + +\item{x.test}{Test data covariate matrix. Default x.test=matrix(0.0,0,0).} + +\item{num_rounds}{Number of trees. (Maximum number of trees in a sum-of-tree model). Default num_rounds=5.} + +\item{alpha}{Parameter in prior probability of tree node splitting. Default alpha=0.95} + +\item{beta}{Parameter in prior probability of tree node splitting. Default beta=1} + +\item{split_rule_node}{Binary variable. If equals 1, then find a new set of potential splitting points via a changepoint algorithm after adding each split to a tree. If equals zero, use the same set of potential split points for all splits in a tree. Default split_rule_node=0.} + +\item{gridpoint}{Binary variable. If equals 1, then a grid search changepoint detection algorithm will be used. If equals 0, then the Pruned Exact Linear Time (PELT) changepoint detection algorithm will be used (Killick et al. 2012). Default gridpoint=0.} + +\item{maxOWsize}{Maximum number of models to keep in Occam's window. Default maxOWsize=100.} + +\item{num_splits}{Maximum number of splits in a tree} + +\item{gridsize}{This integer determines the size of the grid across which to search if gridpoint=1 when finding changepoints for constructing trees.} + +\item{zero_split}{Binary variable. If equals 1, then zero split trees can be included in a sum-of-trees model. If equals zero, then only trees with at least one split can be included in a sum-of-trees model.} + +\item{only_max_num_trees}{Binary variable. If equals 1, then only sum-of-trees models containing the maximum number of trees, num_rounds, are selected. If equals 0, then sum-of-trees models containing less than num_rounds trees can be selected. The default is only_max_num_trees=1.} + +\item{min_num_obs_for_split}{This integer determines the minimum number of observations in a (parent) tree node for the algorithm to consider potential splits of the node.} + +\item{min_num_obs_after_split}{This integer determines the minimum number of observations in a child node resulting from a split in order for a split to occur. If the left or right chikd node has less than this number of observations, then the split can not occur.} +} +\value{ +A list of length 2. The first element is A vector of Individual Treatment Effect Estimates. The second element is a bartBMA object (i.e. the trained BART-BMA model). +} +\description{ +This function produces ITE Predictions (in-sample) using bartBMA and the method described by Hill (2011). +} +\examples{ +n <- 250 +x1 <- rnorm(n) +x2 <- rnorm(n) +x3 <- rnorm(n) +x4 <- rbinom(n,1,0.5) +x5 <- as.factor(sample( LETTERS[1:3], n, replace=TRUE)) + +p= 0 +xnoise = matrix(rnorm(n*p), nrow=n) +x5A <- ifelse(x5== 'A',1,0) +x5B <- ifelse(x5== 'B',1,0) +x5C <- ifelse(x5== 'C',1,0) + +x_covs_train <- cbind(x1,x2,x3,x4,x5A,x5B,x5C,xnoise) + +#Treatment effect +#tautrain <- 3 +tautrain <- 1+2*x_covs_train[,2]*x_covs_train[,4] + +#Prognostic function +mutrain <- 1 + 2*x_covs_train[,5] -1*x_covs_train[,6]-4*x_covs_train[,7] + +x_covs_train[,1]*x_covs_train[,3] +sd_mtrain <- sd(mutrain) +utrain <- runif(n) +#pitrain <- 0.8*pnorm((3*mutrain/sd_mtrain)-0.5*x_covs_train[,1])+0.05+utrain/10 +pitrain <- 0.5 +ztrain <- rbinom(n,1,pitrain) +ytrain <- mutrain + tautrain*ztrain +#pihattrain <- pbart(x_covs_train,ztrain )$prob.train.mean + +#set lower and upper quantiles for intervals +lbound <- 0.025 +ubound <- 0.975 + +example_output <- ITEs_bartBMA(x_covariates = x_covs_train, + z_train = ztrain, + y_train = ytrain) +} diff --git a/man/ITEs_bartBMA_exact_par.Rd b/man/ITEs_bartBMA_exact_par.Rd new file mode 100644 index 0000000..96e932f --- /dev/null +++ b/man/ITEs_bartBMA_exact_par.Rd @@ -0,0 +1,95 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ITEs_bartBMA_exact_par.R +\name{ITEs_bartBMA_exact_par} +\alias{ITEs_bartBMA_exact_par} +\title{Estimate ITEs and obtain credible intervals (in-sample or out-of-sample).} +\usage{ +ITEs_bartBMA_exact_par( + object, + l_quant, + u_quant, + newdata = NULL, + update_resids = 1, + num_cores = 1, + root_alg_precision = 1e-05, + training_data +) +} +\arguments{ +\item{object}{Output from ITEs_bartBMA of class ITE_ests.bartBMA.} + +\item{l_quant}{Lower quantile of credible intervals for the ITEs, CATT, CATNT.} + +\item{u_quant}{Upper quantile of credible intervals for the ITEs, CATT, CATNT.} + +\item{newdata}{Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object.} + +\item{update_resids}{Option for whether to update the partial residuals in the gibbs sampler. If equal to 1, updates partial residuals, if equal to zero, does not update partial residuals. The defaullt setting is to update the partial residuals.} + +\item{num_cores}{Number of cores used in parallel.} + +\item{root_alg_precision}{The algorithm should obtain approximate bounds that are within the distance root_alg_precision of the true quantile for the chosen average of models.} + +\item{training_data}{The training data matrix} +} +\value{ +The output is a list of length 4: +\item{ITE_intervals}{A 3 by n matrix, where n is the number of observations. The first row gives the l_quant*100 quantiles of the individual treatment effects. The second row gives the medians of the ITEs. The third row gives the u_quant*100 quantiles of the ITEs.} +\item{ITE_estimates}{An n by 1 matrix containing the Individual Treatment Effect estimates.} +\item{CATE_estimate}{The Conditional Average Treatment Effect Estimates} +\item{CATE_Interval}{A 3 by 1 matrix. The first element is the l_quant*100 quantile of the CATE distribution, the second element is the median of the CATE distribution, and the thied element is the u_quant*100 quantile of the CATE distribution.} +} +\description{ +This function takes a set of sum of tree models obtained from ITEs_bartBMA, and then estimates ITEs, and obtains prediction intervals. +} +\examples{ +\dontrun{ +#Example of BART-BMA for ITE estimation +# Applied to data simulations from Hahn et al. (2020, Bayesian Analysis) +# "Bayesian Regression Tree Models for Causal Inference: Regularization, +# Confounding, and Heterogeneous Effects +n <- 250 +x1 <- rnorm(n) +x2 <- rnorm(n) +x3 <- rnorm(n) +x4 <- rbinom(n,1,0.5) +x5 <- as.factor(sample( LETTERS[1:3], n, replace=TRUE)) + +p= 0 +xnoise = matrix(rnorm(n*p), nrow=n) +x5A <- ifelse(x5== 'A',1,0) +x5B <- ifelse(x5== 'B',1,0) +x5C <- ifelse(x5== 'C',1,0) + +x_covs_train <- cbind(x1,x2,x3,x4,x5A,x5B,x5C,xnoise) + +#Treatment effect +#tautrain <- 3 +tautrain <- 1+2*x_covs_train[,2]*x_covs_train[,4] + +#Prognostic function +mutrain <- 1 + 2*x_covs_train[,5] -1*x_covs_train[,6]-4*x_covs_train[,7] + +x_covs_train[,1]*x_covs_train[,3] +sd_mtrain <- sd(mutrain) +utrain <- runif(n) +#pitrain <- 0.8*pnorm((3*mutrain/sd_mtrain)-0.5*x_covs_train[,1])+0.05+utrain/10 +pitrain <- 0.5 +ztrain <- rbinom(n,1,pitrain) +ytrain <- mutrain + tautrain*ztrain +#pihattrain <- pbart(x_covs_train,ztrain )$prob.train.mean + +#set lower and upper quantiles for intervals +lbound <- 0.025 +ubound <- 0.975 + + +trained_bbma <- ITEs_bartBMA(x_covariates = x_covs_train, + z_train = ztrain, + y_train = ytrain) + +example_output <- ITEs_bartBMA_exact_par(trained_bbma[[2]], + l_quant = lbound, + u_quant= ubound, + training_data = x_covs_train) + } +} diff --git a/man/bartBMA.Rd b/man/bartBMA.Rd new file mode 100644 index 0000000..9322b7e --- /dev/null +++ b/man/bartBMA.Rd @@ -0,0 +1,143 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/bartBMA.R +\name{bartBMA} +\alias{bartBMA} +\alias{bartBMA.default} +\title{Bayesian Additive Regression Trees Using Bayesian Model Averaging (BART-BMA)} +\usage{ +bartBMA(x.train, ...) + +\method{bartBMA}{default}( + x.train, + y.train, + a = 3, + nu = 3, + sigquant = 0.9, + c = 1000, + pen = 12, + num_cp = 20, + x.test = matrix(0, 0, 0), + num_rounds = 5, + alpha = 0.95, + beta = 2, + split_rule_node = 0, + gridpoint = 0, + maxOWsize = 100, + num_splits = 5, + gridsize = 10, + zero_split = 1, + only_max_num_trees = 1, + min_num_obs_for_split = 2, + min_num_obs_after_split = 2, + exact_residuals = 1, + spike_tree = 0, + s_t_hyperprior = 1, + p_s_t = 0.5, + a_s_t = 1, + b_s_t = 3, + lambda_poisson = 10, + less_greedy = 0, + ... +) +} +\arguments{ +\item{x.train}{Training data covariate matrix} + +\item{...}{Further arguments.} + +\item{y.train}{Training data outcome vector.} + +\item{a}{This is a parameter that influences the variance of terminal node parameter values. Default value a=3.} + +\item{nu}{This is a hyperparameter in the distribution of the variance of the error term. THe inverse of the variance is distributed as Gamma (nu/2, nu*lambda/2). Default value nu=3.} + +\item{sigquant}{Calibration quantile for the inverse chi-squared prior on the variance of the error term.} + +\item{c}{This determines the size of Occam's Window} + +\item{pen}{This is a parameter used by the Pruned Exact Linear Time Algorithm when finding changepoints. Default value pen=12.} + +\item{num_cp}{This is a number between 0 and 100 that determines the proportion of changepoints proposed by the changepoint detection algorithm to keep when growing trees. Default num_cp=20.} + +\item{x.test}{Test data covariate matrix. Default x.test=matrix(0.0,0,0).} + +\item{num_rounds}{Number of trees. (Maximum number of trees in a sum-of-tree model). Default num_rounds=5.} + +\item{alpha}{Parameter in prior probability of tree node splitting. Default alpha=0.95} + +\item{beta}{Parameter in prior probability of tree node splitting. Default beta=1} + +\item{split_rule_node}{Binary variable. If equals 1, then find a new set of potential splitting points via a changepoint algorithm after adding each split to a tree. If equals zero, use the same set of potential split points for all splits in a tree. Default split_rule_node=0.} + +\item{gridpoint}{Binary variable. If equals 1, then a grid search changepoint detection algorithm will be used. If equals 0, then the Pruned Exact Linear Time (PELT) changepoint detection algorithm will be used (Killick et al. 2012). Default gridpoint=0.} + +\item{maxOWsize}{Maximum number of models to keep in Occam's window. Default maxOWsize=100.} + +\item{num_splits}{Maximum number of splits in a tree} + +\item{gridsize}{This integer determines the size of the grid across which to search if gridpoint=1 when finding changepoints for constructing trees.} + +\item{zero_split}{Binary variable. If equals 1, then zero split trees can be included in a sum-of-trees model. If equals zero, then only trees with at least one split can be included in a sum-of-trees model.} + +\item{only_max_num_trees}{Binary variable. If equals 1, then only sum-of-trees models containing the maximum number of trees, num_rounds, are selected. If equals 0, then sum-of-trees models containing less than num_rounds trees can be selected. The default is only_max_num_trees=1.} + +\item{min_num_obs_for_split}{This integer determines the minimum number of observations in a (parent) tree node for the algorithm to consider potential splits of the node.} + +\item{min_num_obs_after_split}{This integer determines the minimum number of observations in a child node resulting from a split in order for a split to occur. If the left or right chikd node has less than this number of observations, then the split can not occur.} + +\item{exact_residuals}{Binary variable. If equal to 1, then trees are added to sum-of-tree models within each round of the algorithm by detecting changepoints in the exact residuals. If equals zero, then changepoints are detected in residuals that are constructed from approximate predictions.} + +\item{spike_tree}{If equal to 1, then the Spike-and-Tree prior will be used, otherwise the standard BART prior will be used. The number of splitting variables has a beta-binomial prior. The number of terminal nodes has a truncated Poisson prior, and then a uniform prior is placed on the set of valid constructions of trees given the splitting variables and number of terminal nodes.} + +\item{s_t_hyperprior}{If equals 1 and spike_tree equals 1, then a beta distribution hyperprior is placed on the variable inclusion probabilities for the spike and tree prior. The hyperprior parameters are a_s_t and b_s_t.} + +\item{p_s_t}{If spike_tree=1 and s_t_hyperprior=0, then p_s_t is the prior variable inclusion probability.} + +\item{a_s_t}{If spike_tree=1 and s_t_hyperprior=1, then a_s_t is a parameter of a beta distribution hyperprior.} + +\item{b_s_t}{If spike_tree=1 and s_t_hyperprior=1, then b_s_t is a parameter of a beta distribution hyperprior.} + +\item{lambda_poisson}{This is a parameter for the Spike-and-Tree prior. It is the parameter for the (truncated and conditional on the number of splitting variables) Poisson prior on the number of terminal nodes.} + +\item{less_greedy}{If equal to one, then a less greedy model search algorithm is used.} +} +\value{ +The following objects are returned by bartbma: +\item{fitted.values}{The vector of predictions of the outcome for all training observations.} +\item{sumoftrees}{This is a list of lists of matrices. The outer list corresponds to a list of sum-of-tree models, and each element of the outer list is a list of matrices describing the structure of the trees within a sum-of-tree model. See details.} +\item{obs_to_termNodesMatrix}{This is a list of lists of matrices. The outer list corresponds to a list of sum-of-tree models, and each element of the outer list is a list of matrices describing to which node each of the observations is allocated to at all depths of each tree within a sum-of-tree model. See details.} +\item{bic}{This is a vector of BICs for each sum-of-tree model.} +\item{test.preds}{A vector of test data predictions. This output only is given if there is test data in the input.} +\item{sum_residuals}{CURRENTLY INCORRECT OUTPUT. A List (over sum-of-tree models) of lists (over single trees in a model) of vectors of partial residuals. Unless the maximum number of trees in a model is one, in which case the output is a list (over single tree models) of vectors of partial residuals, which are all equal to the outcome vector.} +\item{numvars}{This is the total number of variables in the input training data matrix.} +\item{call}{match.call returns a call in which all of the specified arguments are specified by their full names.} +\item{y_minmax}{Range of the input training data outcome vector.} +\item{response}{Input taining data outcome vector.} +\item{nrowTrain}{number of observations in the input training data.} +\item{sigma}{sd(y.train)/(max(y.train)-min(y.train))} +\item{a}{input parameter} +\item{nu}{input parameter} +\item{lambda}{parameter determined by the inputs sigma, sigquant, and nu} +} +\description{ +This is an implementation of Bayesian Additive Regression Trees \insertCite{chipman2010bart}{bartBMA} using Bayesian Model Averaging \insertCite{hernandez2018bayesian}{bartBMA}. +} +\examples{ +N <- 100 +p<- 100 +set.seed(100) +library(bartBMA) +epsilon <- rnorm(N) +xcov <- matrix(runif(N*p), nrow=N) +y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+ +5*xcov[,5]+epsilon +epsilontest <- rnorm(N) +xcovtest <- matrix(runif(N*p), nrow=N) +ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ +5*xcovtest[,5]+epsilontest +bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, +only_max_num_trees = 1,split_rule_node = 0) +} +\references{ +\insertAllCited{} +} diff --git a/man/bartBMA_with_ITEs_exact_par.Rd b/man/bartBMA_with_ITEs_exact_par.Rd new file mode 100644 index 0000000..c9c4a0a --- /dev/null +++ b/man/bartBMA_with_ITEs_exact_par.Rd @@ -0,0 +1,175 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/bartBMA_with_ITEs_exact_par.R +\name{bartBMA_with_ITEs_exact_par} +\alias{bartBMA_with_ITEs_exact_par} +\title{Prediction intervals for bart-bma output obtained using linear algebra to obtain means and variances, and using bisection to find the quantiles of the mixture of t distributions.} +\usage{ +bartBMA_with_ITEs_exact_par( + l_quant, + u_quant, + newdata = NULL, + update_resids = 1, + num_cores = 1, + root_alg_precision = 1e-05, + x_covariates, + z_train, + y_train, + a = 3, + nu = 3, + sigquant = 0.9, + c = 1000, + pen = 12, + num_cp = 20, + x.test = matrix(0, 0, 0), + num_rounds = 5, + alpha = 0.95, + beta = 2, + split_rule_node = 0, + gridpoint = 0, + maxOWsize = 100, + num_splits = 5, + gridsize = 10, + zero_split = 1, + only_max_num_trees = 1, + min_num_obs_for_split = 2, + min_num_obs_after_split = 2, + exact_residuals = 1, + spike_tree = 0, + s_t_hyperprior = 1, + p_s_t = 0.5, + a_s_t = 1, + b_s_t = 3, + lambda_poisson = 10, + less_greedy = 0 +) +} +\arguments{ +\item{l_quant}{Lower quantile of credible intervals for the ITEs, CATT, CATNT.} + +\item{u_quant}{Upper quantile of credible intervals for the ITEs, CATT, CATNT.} + +\item{newdata}{Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object.} + +\item{update_resids}{Option for whether to update the partial residuals in the gibbs sampler. If equal to 1, updates partial residuals, if equal to zero, does not update partial residuals. The defaullt setting is to update the partial residuals.} + +\item{num_cores}{Number of cores used in parallel.} + +\item{root_alg_precision}{The algorithm should obtain approximate bounds that are within the distance root_alg_precision of the true quantile for the chosen average of models.} + +\item{x_covariates}{Covaraite matrix for training bartBMA.} + +\item{z_train}{treatment vector for traiing bartBMA.} + +\item{y_train}{outcome vector for training bartBMA.} + +\item{a}{This is a parameter that influences the variance of terminal node parameter values. Default value a=3.} + +\item{nu}{This is a hyperparameter in the distribution of the variance of the error term. THe inverse of the variance is distributed as Gamma (nu/2, nu*lambda/2). Default value nu=3.} + +\item{sigquant}{Calibration quantile for the inverse chi-squared prior on the variance of the error term.} + +\item{c}{This determines the size of Occam's Window} + +\item{pen}{This is a parameter used by the Pruned Exact Linear Time Algorithm when finding changepoints. Default value pen=12.} + +\item{num_cp}{This is a number between 0 and 100 that determines the proportion of changepoints proposed by the changepoint detection algorithm to keep when growing trees. Default num_cp=20.} + +\item{x.test}{Test data covariate matrix. Default x.test=matrix(0.0,0,0).} + +\item{num_rounds}{Number of trees. (Maximum number of trees in a sum-of-tree model). Default num_rounds=5.} + +\item{alpha}{Parameter in prior probability of tree node splitting. Default alpha=0.95} + +\item{beta}{Parameter in prior probability of tree node splitting. Default beta=1} + +\item{split_rule_node}{Binary variable. If equals 1, then find a new set of potential splitting points via a changepoint algorithm after adding each split to a tree. If equals zero, use the same set of potential split points for all splits in a tree. Default split_rule_node=0.} + +\item{gridpoint}{Binary variable. If equals 1, then a grid search changepoint detection algorithm will be used. If equals 0, then the Pruned Exact Linear Time (PELT) changepoint detection algorithm will be used (Killick et al. 2012). Default gridpoint=0.} + +\item{maxOWsize}{Maximum number of models to keep in Occam's window. Default maxOWsize=100.} + +\item{num_splits}{Maximum number of splits in a tree} + +\item{gridsize}{This integer determines the size of the grid across which to search if gridpoint=1 when finding changepoints for constructing trees.} + +\item{zero_split}{Binary variable. If equals 1, then zero split trees can be included in a sum-of-trees model. If equals zero, then only trees with at least one split can be included in a sum-of-trees model.} + +\item{only_max_num_trees}{Binary variable. If equals 1, then only sum-of-trees models containing the maximum number of trees, num_rounds, are selected. If equals 0, then sum-of-trees models containing less than num_rounds trees can be selected. The default is only_max_num_trees=1.} + +\item{min_num_obs_for_split}{This integer determines the minimum number of observations in a (parent) tree node for the algorithm to consider potential splits of the node.} + +\item{min_num_obs_after_split}{This integer determines the minimum number of observations in a child node resulting from a split in order for a split to occur. If the left or right chikd node has less than this number of observations, then the split can not occur.} + +\item{exact_residuals}{Binary variable. If equal to 1, then trees are added to sum-of-tree models within each round of the algorithm by detecting changepoints in the exact residuals. If equals zero, then changepoints are detected in residuals that are constructed from approximate predictions.} + +\item{spike_tree}{If equal to 1, then the Spike-and-Tree prior will be used, otherwise the standard BART prior will be used. The number of splitting variables has a beta-binomial prior. The number of terminal nodes has a truncated Poisson prior, and then a uniform prior is placed on the set of valid constructions of trees given the splitting variables and number of terminal nodes.} + +\item{s_t_hyperprior}{If equals 1 and spike_tree equals 1, then a beta distribution hyperprior is placed on the variable inclusion probabilities for the spike and tree prior. The hyperprior parameters are a_s_t and b_s_t.} + +\item{p_s_t}{If spike_tree=1 and s_t_hyperprior=0, then p_s_t is the prior variable inclusion probability.} + +\item{a_s_t}{If spike_tree=1 and s_t_hyperprior=1, then a_s_t is a parameter of a beta distribution hyperprior.} + +\item{b_s_t}{If spike_tree=1 and s_t_hyperprior=1, then b_s_t is a parameter of a beta distribution hyperprior.} + +\item{lambda_poisson}{This is a parameter for the Spike-and-Tree prior. It is the parameter for the (truncated and conditional on the number of splitting variables) Poisson prior on the number of terminal nodes.} + +\item{less_greedy}{If equal to one, then a less greedy model search algorithm is used.} +} +\value{ +The output is a list of length 4: +\item{ITE_intervals}{A 3 by n matrix, where n is the number of observations. The first row gives the l_quant*100 quantiles of the individual treatment effects. The second row gives the medians of the ITEs. The third row gives the u_quant*100 quantiles of the ITEs.} +\item{ITE_estimates}{An n by 1 matrix containing the Individual Treatment Effect estimates.} +\item{CATE_estimate}{The Conditional Average Treatment Effect Estimates} +\item{CATE_Interval}{A 3 by 1 matrix. The first element is the l_quant*100 quantile of the CATE distribution, the second element is the median of the CATE distribution, and the thied element is the u_quant*100 quantile of the CATE distribution.} +} +\description{ +This function produces prediction intervals for bart-bma output. +} +\examples{ +\dontrun{ +#Example of BART-BMA for ITE estimation +#Applied to data simulations from Hahn et al. (2020, Bayesian Analysis) +#"Bayesian Regression Tree Models for Causal Inference: Regularization, Confounding, +# and Heterogeneous Effects +n <- 250 +x1 <- rnorm(n) +x2 <- rnorm(n) +x3 <- rnorm(n) +x4 <- rbinom(n,1,0.5) +x5 <- as.factor(sample( LETTERS[1:3], n, replace=TRUE)) + +p= 0 +xnoise = matrix(rnorm(n*p), nrow=n) +x5A <- ifelse(x5== 'A',1,0) +x5B <- ifelse(x5== 'B',1,0) +x5C <- ifelse(x5== 'C',1,0) + +x_covs_train <- cbind(x1,x2,x3,x4,x5A,x5B,x5C,xnoise) + +#Treatment effect +#tautrain <- 3 +tautrain <- 1+2*x_covs_train[,2]*x_covs_train[,4] + +#Prognostic function +mutrain <- 1 + 2*x_covs_train[,5] -1*x_covs_train[,6]-4*x_covs_train[,7] + +x_covs_train[,1]*x_covs_train[,3] +sd_mtrain <- sd(mutrain) +utrain <- runif(n) +#pitrain <- 0.8*pnorm((3*mutrain/sd_mtrain)-0.5*x_covs_train[,1])+0.05+utrain/10 +pitrain <- 0.5 +ztrain <- rbinom(n,1,pitrain) +ytrain <- mutrain + tautrain*ztrain +#pihattrain <- pbart(x_covs_train,ztrain )$prob.train.mean + +#set lower and upper quantiles for intervals +lbound <- 0.025 +ubound <- 0.975 + +example_output <- bartBMA_with_ITEs_exact_par(l_quant = lbound, + u_quant= ubound, + x_covariates = x_covs_train, + z_train = ztrain, + y_train = ytrain) + } +} diff --git a/man/pred_expectation_intervals_bbma_GS.Rd b/man/pred_expectation_intervals_bbma_GS.Rd new file mode 100644 index 0000000..6bec23a --- /dev/null +++ b/man/pred_expectation_intervals_bbma_GS.Rd @@ -0,0 +1,63 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/pred_expectation_intervals_bbma_GS.R +\name{pred_expectation_intervals_bbma_GS} +\alias{pred_expectation_intervals_bbma_GS} +\title{Prediction intervals for bart-bma output} +\usage{ +pred_expectation_intervals_bbma_GS( + object, + num_iter, + burnin, + l_quant, + u_quant, + newdata = NULL, + update_resids = 1 +) +} +\arguments{ +\item{object}{bartBMA object obtained from function bartBMA} + +\item{num_iter}{Total number of iterations of the Gibbs sampler (including burn-in).} + +\item{burnin}{Number of burn-on iterations of the Gibbs sampler.} + +\item{l_quant}{Lower quartile of the prediction interval.} + +\item{u_quant}{Upper quartile of the prediction interval.} + +\item{newdata}{Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object.} + +\item{update_resids}{Option for whether to update the partial residuals in the gibbs sampler. If equal to 1, updates partial residuals, if equal to zero, does not update partial residuals. The defaullt setting is to update the partial residua;s.} +} +\value{ +The output is a list of length 2: +\item{PI}{A 3 by n matrix, where n is the number of observations. The first row gives the l_quant*100 quantiles of f(x). The second row gives the medians of f(x). The third row gives the u_quant*100 quantiles of f(x).} +\item{meanpreds}{An n by 1 matrix containing the estimated means of f(x).} +} +\description{ +This function produces prediction intervals for f(x) in BART-BMA by post-hoc Gibbs-sampling from the full conditionals of the terminal node parameters and the variance of the error term. See Hernandez et al. (2018) Appendix D for details. +} +\examples{ + +#load the package +library(bartBMA) +#set the seed +set.seed(100) +#simulate some data +N <- 100 +p<- 100 +epsilon <- rnorm(N) +xcov <- matrix(runif(N*p), nrow=N) +y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +epsilontest <- rnorm(N) +xcovtest <- matrix(runif(N*p), nrow=N) +ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ + 5*xcovtest[,5]+epsilontest + +#Train the object +bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, + only_max_num_trees = 1,split_rule_node = 0) +#Obtain the prediction intervals +pred_expectation_intervals_bbma_GS(bart_bma_example,1000,100,0.025,0.975, +newdata=NULL,update_resids=1) +} diff --git a/man/pred_intervals_bbma_GS.Rd b/man/pred_intervals_bbma_GS.Rd new file mode 100644 index 0000000..11946fe --- /dev/null +++ b/man/pred_intervals_bbma_GS.Rd @@ -0,0 +1,62 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/pred_intervals_GS.R +\name{pred_intervals_bbma_GS} +\alias{pred_intervals_bbma_GS} +\title{Prediction intervals for bart-bma output} +\usage{ +pred_intervals_bbma_GS( + object, + num_iter, + burnin, + l_quant, + u_quant, + newdata = NULL, + update_resids = 1 +) +} +\arguments{ +\item{object}{bartBMA object obtained from function bartBMA} + +\item{num_iter}{Total number of iterations of the Gibbs sampler (including burn-in).} + +\item{burnin}{Number of burn-on iterations of the Gibbs sampler.} + +\item{l_quant}{Lower quartile of the prediction interval.} + +\item{u_quant}{Upper quartile of the prediction interval.} + +\item{newdata}{Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object.} + +\item{update_resids}{Option for whether to update the partial residuals in the gibbs sampler. If equal to 1, updates partial residuals, if equal to zero, does not update partial residuals. The defaullt setting is to update the partial residua;s.} +} +\value{ +The output is a list of length 2: +\item{PI}{A 3 by n matrix, where n is the number of observations. The first row gives the l_quant*100 quantiles. The second row gives the medians. The third row gives the u_quant*100 quantiles.} +\item{meanpreds}{An n by 1 matrix containing the estimated means.} +} +\description{ +This function produces prediction intervals for BART-BMA estimates by post-hoc Gibbs-sampling from the full conditionals of the terminal node parameters and the variance of the error term. See Hernandez et al. (2018) Appendix D for details. +} +\examples{ + +#load the package +library(bartBMA) +#set the seed +set.seed(100) +#simulate some data +N <- 100 +p<- 100 +epsilon <- rnorm(N) +xcov <- matrix(runif(N*p), nrow=N) +y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +epsilontest <- rnorm(N) +xcovtest <- matrix(runif(N*p), nrow=N) +ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ + 5*xcovtest[,5]+epsilontest + +#Train the object +bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, + only_max_num_trees = 1,split_rule_node = 0) +#Obtain the prediction intervals +pred_intervals_bbma_GS(bart_bma_example,1000,100,0.025,0.975,newdata=NULL,update_resids=1) +} diff --git a/man/pred_intervals_new_initials_GS.Rd b/man/pred_intervals_new_initials_GS.Rd new file mode 100644 index 0000000..5b3daca --- /dev/null +++ b/man/pred_intervals_new_initials_GS.Rd @@ -0,0 +1,66 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/pred_intervals_new_initials_GS.R +\name{pred_intervals_new_initials_GS} +\alias{pred_intervals_new_initials_GS} +\title{Prediction intervals for bart-bma output} +\usage{ +pred_intervals_new_initials_GS( + object, + num_iter, + burnin, + l_quant, + u_quant, + newdata = NULL, + update_resids = 1, + trainingdata +) +} +\arguments{ +\item{object}{bartBMA object obtained from function bartBMA} + +\item{num_iter}{Total number of iterations of the Gibbs sampler (including burn-in).} + +\item{burnin}{Number of burn-on iterations of the Gibbs sampler.} + +\item{l_quant}{Lower quartile of the prediction interval.} + +\item{u_quant}{Upper quartile of the prediction interval.} + +\item{newdata}{Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object.} + +\item{update_resids}{Option for whether to update the partial residuals in the gibbs sampler. If equal to 1, updates partial residuals, if equal to zero, does not update partial residuals. The defaullt setting is to update the partial residuals.} + +\item{trainingdata}{The matrix of training data.} +} +\value{ +The output is a list of length 2: +\item{PI}{A 3 by n matrix, where n is the number of observations. The first row gives the l_quant*100 quantiles. The second row gives the medians. The third row gives the u_quant*100 quantiles.} +\item{meanpreds}{An n by 1 matrix containing the estimated means.} +} +\description{ +This function produces prediction intervals for BART-BMA estimates by post-hoc Gibbs-sampling from the full conditionals of the terminal node parameters and the variance of the error term. See Hernandez et al. (2018) Appendix D for details. +} +\examples{ + +#load the package +library(bartBMA) +#set the seed +set.seed(100) +#simulate some data +N <- 100 +p<- 100 +epsilon <- rnorm(N) +xcov <- matrix(runif(N*p), nrow=N) +y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +epsilontest <- rnorm(N) +xcovtest <- matrix(runif(N*p), nrow=N) +ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ + 5*xcovtest[,5]+epsilontest + +#Train the object +bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, + only_max_num_trees = 1,split_rule_node = 0) +#Obtain the prediction intervals +pred_intervals_new_initials_GS(bart_bma_example,1000,100,0.025,0.975, +newdata=NULL,update_resids=1,xcov) +} diff --git a/man/pred_ints_exact.Rd b/man/pred_ints_exact.Rd new file mode 100644 index 0000000..a57093a --- /dev/null +++ b/man/pred_ints_exact.Rd @@ -0,0 +1,59 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/pred_ints_exact.R +\name{pred_ints_exact} +\alias{pred_ints_exact} +\title{Prediction intervals for bart-bma output obtained using linear algebra to obtain means and variances, and using bisection to find the quantiles of the mixture of t distributions.} +\usage{ +pred_ints_exact( + object, + l_quant, + u_quant, + newdata = NULL, + num_cores = 1, + root_alg_precision = 1e-05 +) +} +\arguments{ +\item{object}{bartBMA object obtained from function bartBMA} + +\item{l_quant}{Lower quantile of credible intervals for the ITEs, CATT, CATNT.} + +\item{u_quant}{Upper quantile of credible intervals for the ITEs, CATT, CATNT.} + +\item{newdata}{Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object.} + +\item{num_cores}{Number of cores used in parallel.} + +\item{root_alg_precision}{The algorithm should obtain approximate bounds that are within the distance root_alg_precision of the true quantile for the chosen average of models.} +} +\value{ +The output is a list of length 2: +\item{PI}{A 3 by n matrix, where n is the number of observations. The first row gives the l_quant*100 quantiles. The second row gives the medians. The third row gives the u_quant*100 quantiles.} +\item{meanpreds}{An n by 1 matrix containing the estimated means.} +} +\description{ +This function produces prediction intervals for bart-bma output. +} +\examples{ + +#load the package +library(bartBMA) +#set the seed +set.seed(100) +#simulate some data +N <- 100 +p<- 100 +epsilon <- rnorm(N) +xcov <- matrix(runif(N*p), nrow=N) +y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +epsilontest <- rnorm(N) +xcovtest <- matrix(runif(N*p), nrow=N) +ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ + 5*xcovtest[,5]+epsilontest + +#Train the object +bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, + only_max_num_trees = 1,split_rule_node = 0) +#Obtain the prediction intervals +pred_ints_exact(bart_bma_example,0.025,0.975,newdata=NULL,num_cores=1) +} diff --git a/man/pred_ints_exact_par.Rd b/man/pred_ints_exact_par.Rd new file mode 100644 index 0000000..3f5f1f8 --- /dev/null +++ b/man/pred_ints_exact_par.Rd @@ -0,0 +1,60 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/pred_ints_exact_par.R +\name{pred_ints_exact_par} +\alias{pred_ints_exact_par} +\title{Prediction intervals for bart-bma output obtained using linear algebra to obtain means and variances, and using bisection to find the quantiles of the mixture of t distributions.} +\usage{ +pred_ints_exact_par( + object, + l_quant, + u_quant, + newdata = NULL, + num_cores = 1, + root_alg_precision = 1e-05 +) +} +\arguments{ +\item{object}{bartBMA object obtained from function bartBMA} + +\item{l_quant}{Lower quantile of credible intervals for the ITEs, CATT, CATNT.} + +\item{u_quant}{Upper quantile of credible intervals for the ITEs, CATT, CATNT.} + +\item{newdata}{Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object.} + +\item{num_cores}{Number of cores used in parallel.} + +\item{root_alg_precision}{The algorithm should obtain approximate bounds that are within the distance root_alg_precision of the true quantile for the chosen average of models.} +} +\value{ +The output is a list of length 2: +\item{PI}{A 3 by n matrix, where n is the number of observations. The first row gives the l_quant*100 quantiles. The second row gives the medians. The third row gives the u_quant*100 quantiles.} +\item{meanpreds}{An n by 1 matrix containing the estimated means.} +} +\description{ +This function produces prediction intervals for bart-bma output. +} +\examples{ +\dontrun{ +#load the package +library(bartBMA) +#set the seed +set.seed(100) +#simulate some data +N <- 100 +p<- 100 +epsilon <- rnorm(N) +xcov <- matrix(runif(N*p), nrow=N) +y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +epsilontest <- rnorm(N) +xcovtest <- matrix(runif(N*p), nrow=N) +ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ + 5*xcovtest[,5]+epsilontest + +#Train the object +bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, + only_max_num_trees = 1,split_rule_node = 0) +#Obtain the prediction intervals +pred_ints_exact_par(bart_bma_example,0.025,0.975,newdata=NULL,num_cores=1) +} +} diff --git a/man/pred_means_bbma_GS.Rd b/man/pred_means_bbma_GS.Rd new file mode 100644 index 0000000..240f74f --- /dev/null +++ b/man/pred_means_bbma_GS.Rd @@ -0,0 +1,44 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/pred_means_bbma_GS.R +\name{pred_means_bbma_GS} +\alias{pred_means_bbma_GS} +\title{Predictions for bart-bma output obtained from a Gibbs sampler} +\usage{ +pred_means_bbma_GS(object, num_iter, burnin, newdata = NULL, update_resids = 1) +} +\arguments{ +\item{object}{bartBMA object obtained from function bartBMA} + +\item{num_iter}{Total number of iterations of the Gibbs sampler (including burn-in).} + +\item{burnin}{Number of burn-on iterations of the Gibbs sampler.} + +\item{newdata}{Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object.} + +\item{update_resids}{Option for whether to update the partial residuals in the gibbs sampler. If equal to 1, updates partial residuals, if equal to zero, does not update partial residuals. The defaullt setting is to update the partial residua;s.} +} +\value{ +The output is a vector of predictions. +} +\description{ +This function produces predictions from BART-BMA by post-hoc Gibbs-sampling from the full conditionals of the terminal node parameters and the variance of the error term. See Hernandez et al. (2018) Appendix D for details. +} +\examples{ +set.seed(100) +#simulate some data +N <- 100 +p<- 100 +epsilon <- rnorm(N) +xcov <- matrix(runif(N*p), nrow=N) +y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +epsilontest <- rnorm(N) +xcovtest <- matrix(runif(N*p), nrow=N) +ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ + 5*xcovtest[,5]+epsilontest + +#Train the object +bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, + only_max_num_trees = 1,split_rule_node = 0) +#Obtain the prediction intervals +pred_means_bbma_GS(bart_bma_example,1000,100,newdata=NULL,update_resids=1) +} diff --git a/man/pred_means_bbma_new_initials_GS.Rd b/man/pred_means_bbma_new_initials_GS.Rd new file mode 100644 index 0000000..9949beb --- /dev/null +++ b/man/pred_means_bbma_new_initials_GS.Rd @@ -0,0 +1,53 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/pred_means_new_initials_GS.R +\name{pred_means_bbma_new_initials_GS} +\alias{pred_means_bbma_new_initials_GS} +\title{Predictions for bart-bma output obtained from a Gibbs sampler} +\usage{ +pred_means_bbma_new_initials_GS( + object, + num_iter, + burnin, + newdata = NULL, + update_resids = 1, + trainingdata +) +} +\arguments{ +\item{object}{bartBMA object obtained from function bartBMA} + +\item{num_iter}{Total number of iterations of the Gibbs sampler (including burn-in).} + +\item{burnin}{Number of burn-on iterations of the Gibbs sampler.} + +\item{newdata}{Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object.} + +\item{update_resids}{Option for whether to update the partial residuals in the gibbs sampler. If equal to 1, updates partial residuals, if equal to zero, does not update partial residuals. The defaullt setting is to update the partial residua;s.} + +\item{trainingdata}{The matrix of training data.} +} +\value{ +The output is a vector of predictions. +} +\description{ +This function produces predictions from BART-BMA by post-hoc Gibbs-sampling from the full conditionals of the terminal node parameters and the variance of the error term. See Hernandez et al. (2018) Appendix D for details. +} +\examples{ +set.seed(100) +#simulate some data +N <- 100 +p<- 100 +epsilon <- rnorm(N) +xcov <- matrix(runif(N*p), nrow=N) +y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +epsilontest <- rnorm(N) +xcovtest <- matrix(runif(N*p), nrow=N) +ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ + 5*xcovtest[,5]+epsilontest + +#Train the object +bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, + only_max_num_trees = 1,split_rule_node = 0) +#Obtain the prediction intervals +pred_means_bbma_new_initials_GS(bart_bma_example,1000,100,newdata=NULL,update_resids=1,xcovtest) +} diff --git a/man/predict_bartBMA.Rd b/man/predict_bartBMA.Rd new file mode 100644 index 0000000..3181d1e --- /dev/null +++ b/man/predict_bartBMA.Rd @@ -0,0 +1,38 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/predict_bartBMA.R +\name{predict_bartBMA} +\alias{predict_bartBMA} +\title{Predictions for a new dataset using an existing bartbma object} +\usage{ +predict_bartBMA(object, newdata) +} +\arguments{ +\item{object}{A bartBMA object obtained using the barBMA function.} + +\item{newdata}{Covariate matrix for new dataset.} +} +\value{ +A vector of predictions for the new dataset. +} +\description{ +This function produces predictions for a new dataset using a previously obtained bartBMA object. +} +\examples{ +set.seed(100) +#simulate some data +N <- 100 +p<- 100 +epsilon <- rnorm(N) +xcov <- matrix(runif(N*p), nrow=N) +y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +epsilontest <- rnorm(N) +xcovtest <- matrix(runif(N*p), nrow=N) +ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ + 5*xcovtest[,5]+epsilontest + +#Train the object +bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, + only_max_num_trees = 1,split_rule_node = 0) +#Obtain the prediction intervals +predict_bartBMA(bart_bma_example,newdata=xcovtest) +} diff --git a/man/predict_probit_bartBMA.Rd b/man/predict_probit_bartBMA.Rd new file mode 100644 index 0000000..7cf2498 --- /dev/null +++ b/man/predict_probit_bartBMA.Rd @@ -0,0 +1,40 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/predict_probit_bartBMA.R +\name{predict_probit_bartBMA} +\alias{predict_probit_bartBMA} +\title{Predictions for a new dataset using an existing probit_bartBMA object} +\usage{ +predict_probit_bartBMA(object, newdata) +} +\arguments{ +\item{object}{A probit_bartBMA object obtained using the probit_bartBMA function.} + +\item{newdata}{Covariate matrix for new dataset.} +} +\value{ +The output is a list of length 2: +\item{probs}{A vector of estimated probabilities for newdata.} +\item{pred_binary}{A vector of binary predictions for newdata.} +} +\description{ +This function produces predictions for a new dataset using a previously obtained bartBMA object. +} +\examples{ +#Example from BART package (McCulloch et al. 2019) +set.seed(99) +n=100 +x = sort(-2+4*runif(n)) +X=matrix(x,ncol=1) +f = function(x) {return((1/2)*x^3)} +FL = function(x) {return(exp(x)/(1+exp(x)))} +pv = FL(f(x)) +y = rbinom(n,1,pv) + +trained_probit_bbma <- probit_bartBMA(x.train = X,y.train = y) + +np=100 +xp=-2+4*(1:np)/np +Xp=matrix(xp,ncol=1) + +predict_probit_bartBMA(trained_probit_bbma,Xp) +} diff --git a/man/preds_bbma_lin_alg.Rd b/man/preds_bbma_lin_alg.Rd new file mode 100644 index 0000000..ff1fa76 --- /dev/null +++ b/man/preds_bbma_lin_alg.Rd @@ -0,0 +1,54 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/preds_bbma_lin_alg.R +\name{preds_bbma_lin_alg} +\alias{preds_bbma_lin_alg} +\title{Predictions for bart-bma output obtained from the posterior probability weighted averaged of the posterior means for each model} +\usage{ +preds_bbma_lin_alg( + object, + num_iter, + burnin, + newdata = NULL, + update_resids = 1, + trainingdata +) +} +\arguments{ +\item{object}{bartBMA object obtained from function bartBMA} + +\item{num_iter}{Total number of iterations of the Gibbs sampler (including burn-in).} + +\item{burnin}{Number of burn-on iterations of the Gibbs sampler.} + +\item{newdata}{Test data for which predictions are to be produced. Default = NULL. If NULL, then produces prediction intervals for training data if no test data was used in producing the bartBMA object, or produces prediction intervals for the original test data if test data was used in producing the bartBMA object.} + +\item{update_resids}{Option for whether to update the partial residuals in the gibbs sampler. If equal to 1, updates partial residuals, if equal to zero, does not update partial residuals. The defaullt setting is to update the partial residua;s.} + +\item{trainingdata}{The matrix of training data.} +} +\value{ +A vector of predictions. +} +\description{ +This function produces predictions from BART-BMA by obtaining the posterior probability weighted averaged of the posterior means for each model. +} +\examples{ +#set the seed +set.seed(100) +#simulate some data +N <- 100 +p<- 100 +epsilon <- rnorm(N) +xcov <- matrix(runif(N*p), nrow=N) +y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +epsilontest <- rnorm(N) +xcovtest <- matrix(runif(N*p), nrow=N) +ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ + 5*xcovtest[,5]+epsilontest + +#Train the object +bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, + only_max_num_trees = 1,split_rule_node = 0) +#Obtain the prediction intervals +preds_bbma_lin_alg(bart_bma_example,1000,100,newdata=xcovtest) +} diff --git a/man/probit_bartBMA.Rd b/man/probit_bartBMA.Rd new file mode 100644 index 0000000..38c8076 --- /dev/null +++ b/man/probit_bartBMA.Rd @@ -0,0 +1,138 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/probit_bartBMA.R +\name{probit_bartBMA} +\alias{probit_bartBMA} +\alias{probit_bartBMA.default} +\title{Probit BART_BMA for classification of a binary variable} +\usage{ +probit_bartBMA(x.train, ...) + +\method{probit_bartBMA}{default}( + x.train, + y.train, + a = 3, + nu = 3, + sigquant = 0.9, + c = 1000, + pen = 12, + num_cp = 20, + x.test = matrix(0, 0, 0), + num_rounds = 5, + alpha = 0.95, + beta = 2, + split_rule_node = 0, + gridpoint = 0, + maxOWsize = 100, + num_splits = 5, + gridsize = 10, + zero_split = 1, + only_max_num_trees = 1, + min_num_obs_for_split = 2, + min_num_obs_after_split = 2, + exact_residuals = 1, + spike_tree = 0, + s_t_hyperprior = 1, + p_s_t = 0.5, + a_s_t = 1, + b_s_t = 3, + lambda_poisson = 10, + less_greedy = 0, + ... +) +} +\arguments{ +\item{x.train}{Training data covariate matrix} + +\item{...}{Further arguments.} + +\item{y.train}{Training data outcome vector.} + +\item{a}{This is a parameter that influences the variance of terminal node parameter values. Default value a=3.} + +\item{nu}{This is a hyperparameter in the distribution of the variance of the error term. THe inverse of the variance is distributed as Gamma (nu/2, nu*lambda/2). Default value nu=3.} + +\item{sigquant}{Calibration quantile for the inverse chi-squared prior on the variance of the error term.} + +\item{c}{This determines the size of Occam's Window} + +\item{pen}{This is a parameter used by the Pruned Exact Linear Time Algorithm when finding changepoints. Default value pen=12.} + +\item{num_cp}{This is a number between 0 and 100 that determines the proportion of changepoints proposed by the changepoint detection algorithm to keep when growing trees. Default num_cp=20.} + +\item{x.test}{Test data covariate matrix. Default x.test=matrix(0.0,0,0).} + +\item{num_rounds}{Number of trees. (Maximum number of trees in a sum-of-tree model). Default num_rounds=5.} + +\item{alpha}{Parameter in prior probability of tree node splitting. Default alpha=0.95} + +\item{beta}{Parameter in prior probability of tree node splitting. Default beta=1} + +\item{split_rule_node}{Binary variable. If equals 1, then find a new set of potential splitting points via a changepoint algorithm after adding each split to a tree. If equals zero, use the same set of potential split points for all splits in a tree. Default split_rule_node=0.} + +\item{gridpoint}{Binary variable. If equals 1, then a grid search changepoint detection algorithm will be used. If equals 0, then the Pruned Exact Linear Time (PELT) changepoint detection algorithm will be used (Killick et al. 2012). Default gridpoint=0.} + +\item{maxOWsize}{Maximum number of models to keep in Occam's window. Default maxOWsize=100.} + +\item{num_splits}{Maximum number of splits in a tree} + +\item{gridsize}{This integer determines the size of the grid across which to search if gridpoint=1 when finding changepoints for constructing trees.} + +\item{zero_split}{Binary variable. If equals 1, then zero split trees can be included in a sum-of-trees model. If equals zero, then only trees with at least one split can be included in a sum-of-trees model.} + +\item{only_max_num_trees}{Binary variable. If equals 1, then only sum-of-trees models containing the maximum number of trees, num_rounds, are selected. If equals 0, then sum-of-trees models containing less than num_rounds trees can be selected. The default is only_max_num_trees=1.} + +\item{min_num_obs_for_split}{This integer determines the minimum number of observations in a (parent) tree node for the algorithm to consider potential splits of the node.} + +\item{min_num_obs_after_split}{This integer determines the minimum number of observations in a child node resulting from a split in order for a split to occur. If the left or right chikd node has less than this number of observations, then the split can not occur.} + +\item{exact_residuals}{Binary variable. If equal to 1, then trees are added to sum-of-tree models within each round of the algorithm by detecting changepoints in the exact residuals. If equals zero, then changepoints are detected in residuals that are constructed from approximate predictions.} + +\item{spike_tree}{If equal to 1, then the Spike-and-Tree prior will be used, otherwise the standard BART prior will be used. The number of splitting variables has a beta-binomial prior. The number of terminal nodes has a truncated Poisson prior, and then a uniform prior is placed on the set of valid constructions of trees given the splitting variables and number of terminal nodes.} + +\item{s_t_hyperprior}{If equals 1 and spike_tree equals 1, then a beta distribution hyperprior is placed on the variable inclusion probabilities for the spike and tree prior. The hyperprior parameters are a_s_t and b_s_t.} + +\item{p_s_t}{If spike_tree=1 and s_t_hyperprior=0, then p_s_t is the prior variable inclusion probability.} + +\item{a_s_t}{If spike_tree=1 and s_t_hyperprior=1, then a_s_t is a parameter of a beta distribution hyperprior} + +\item{b_s_t}{If spike_tree=1 and s_t_hyperprior=1, then b_s_t is a parameter of a beta distribution hyperprior} + +\item{lambda_poisson}{This is a parameter for the Spike-and-Tree prior. It is the parameter for the (truncated and conditional on the number of splitting variables) Poisson prior on the number of terminal nodes.} + +\item{less_greedy}{If equal to one, then a less greedy model search algorithm is used.} +} +\value{ +The following objects are returned by bartbma: +\item{fitted.values}{The vector of predictions of the outcome for all training observations.} +\item{sumoftrees}{This is a list of lists of matrices. The outer list corresponds to a list of sum-of-tree models, and each element of the outer list is a list of matrices describing the structure of the trees within a sum-of-tree model. See details.} +\item{obs_to_termNodesMatrix}{This is a list of lists of matrices. The outer list corresponds to a list of sum-of-tree models, and each element of the outer list is a list of matrices describing to which node each of the observations is allocated to at all depths of each tree within a sum-of-tree model. See details.} +\item{bic}{This is a vector of BICs for each sum-of-tree model.} +\item{test.preds}{A vector of test data predictions. This output only is given if there is test data in the input.} +\item{sum_residuals}{CURRENTLY INCORRECT OUTPUT. A List (over sum-of-tree models) of lists (over single trees in a model) of vectors of partial residuals. Unless the maximum number of trees in a model is one, in which case the output is a list (over single tree models) of vectors of partial residuals, which are all equal to the outcome vector.} +\item{numvars}{This is the total number of variables in the input training data matrix.} +\item{call}{match.call returns a call in which all of the specified arguments are specified by their full names.} +\item{y_minmax}{Range of the input training data outcome vector.} +\item{response}{Input taining data outcome vector.} +\item{nrowTrain}{number of observations in the input training data.} +\item{sigma}{sd(y.train)/(max(y.train)-min(y.train))} +\item{a}{input parameter} +\item{nu}{input parameter} +\item{lambda}{parameter determined by the inputs sigma, sigquant, and nu} +\item{fitted.probs}{In-sample fitted probabilities} +\item{fitted.classes}{In-sample fitted classes} +} +\description{ +This is an implementation of Bayesian Additive Regression Trees (Chipman et al. 2018) using Bayesian Model Averaging (Hernandez et al. 2018). +} +\examples{ +#Example from BART package (McCulloch et al. 2019) +set.seed(99) +n=100 +x = sort(-2+4*runif(n)) +X=matrix(x,ncol=1) +f = function(x) {return((1/2)*x^3)} +FL = function(x) {return(exp(x)/(1+exp(x)))} +pv = FL(f(x)) +y = rbinom(n,1,pv) +probit_bartBMA(x.train = X,y.train = y) +} diff --git a/man/varImpScores.Rd b/man/varImpScores.Rd new file mode 100644 index 0000000..925dbf7 --- /dev/null +++ b/man/varImpScores.Rd @@ -0,0 +1,37 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/varImpScores.R +\name{varImpScores} +\alias{varImpScores} +\title{Variable importances as defined by Hernandez et al. (2018)} +\usage{ +varImpScores(object) +} +\arguments{ +\item{object}{A bartBMA object obtained using the barBMA function.} +} +\value{ +A vector of variable importances. The variables are ordered in the same order that they occur in columns of the input covariate matrix used to obtain the input bartBMA object. +} +\description{ +This measure defines the importance of a variable as the model-probability weighted sum of the number of splits on the variable of interest, divided by the sum over all variables of such weighted counts of splits. +} +\examples{ +#set the seed +set.seed(100) +#simulate some data +N <- 100 +p<- 100 +epsilon <- rnorm(N) +xcov <- matrix(runif(N*p), nrow=N) +y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +epsilontest <- rnorm(N) +xcovtest <- matrix(runif(N*p), nrow=N) +ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ + 5*xcovtest[,5]+epsilontest + +#Train the object +bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, + only_max_num_trees = 1,split_rule_node = 0) +#Obtain the variable importances +varImpScores(bart_bma_example) +} diff --git a/man/varIncProb.Rd b/man/varIncProb.Rd new file mode 100644 index 0000000..d13863a --- /dev/null +++ b/man/varIncProb.Rd @@ -0,0 +1,37 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/varIncProb.R +\name{varIncProb} +\alias{varIncProb} +\title{Variable inclusion probabilities as defined by Linero (2018)} +\usage{ +varIncProb(object) +} +\arguments{ +\item{object}{A bartBMA object obtained using the barBMA function.} +} +\value{ +A vector of posterior inclusion probabilities. The variables are ordered in the same order that they occur in columns of the input covariate matrix used to obtain the input bartBMA object. +} +\description{ +This measure defines the posterior inclusion probability of a variable as the model-probability weighted sum of indicator variables for whether the variable was used in any splitting rules in any of the trees in the sum-of-tree model. +} +\examples{ +#set the seed +set.seed(100) +#simulate some data +N <- 100 +p<- 100 +epsilon <- rnorm(N) +xcov <- matrix(runif(N*p), nrow=N) +y <- sin(pi*xcov[,1]*xcov[,2]) + 20*(xcov[,3]-0.5)^2+10*xcov[,4]+5*xcov[,5]+epsilon +epsilontest <- rnorm(N) +xcovtest <- matrix(runif(N*p), nrow=N) +ytest <- sin(pi*xcovtest[,1]*xcovtest[,2]) + 20*(xcovtest[,3]-0.5)^2+10*xcovtest[,4]+ + 5*xcovtest[,5]+epsilontest + +#Train the object +bart_bma_example <- bartBMA(x.train = xcov,y.train=y,x.test=xcovtest,zero_split = 1, + only_max_num_trees = 1,split_rule_node = 0) +#Obtain the variable importances +varIncProb(bart_bma_example) +} diff --git a/src/161014_predict_function.cpp b/src/161014_predict_function.cpp new file mode 100644 index 0000000..b4259d4 --- /dev/null +++ b/src/161014_predict_function.cpp @@ -0,0 +1,257 @@ +//####################################################################################// +#include +#include +using namespace Rcpp; +// [[Rcpp::export]] + +NumericVector find_internal_nodes_pred(NumericMatrix treetable){ + NumericVector internal_nodes; + + for(int l=0;l +using namespace Rcpp; +// [[Rcpp::export]] + +NumericVector get_original_pred(double low,double high,double sp_low,double sp_high,NumericVector sum_preds){ + NumericVector original_y=(sum_preds*(-low+high))/(-sp_low+sp_high) + (-high*sp_low+low*sp_high)/(-sp_low+sp_high); + return(original_y); +} +//###################################################################################// +#include +using namespace Rcpp; + +// [[Rcpp::export]] + +NumericVector bartBMA_get_testdata_term_obs_pred(NumericMatrix test_data,NumericMatrix tree_data,NumericVector term_node_means) { + // Function to make predictions from test data, given a single tree and the terminal node predictions, this function will be called + //for each tree accepted in Occam's Window. + + //test_data is a nxp matrix with the same variable names as the training data the model was built on... + //should have an error check for this later on or at least check it's the same dimension + + //tree_data is the tree table with the tree information i.e. split points and split variables and terminal node mean values + + //term_node_means is a vector storing the terminal node mean values + + arma::mat arma_tree(tree_data.begin(), tree_data.nrow(), tree_data.ncol(), false); + arma::mat testd(test_data.begin(), test_data.nrow(), test_data.ncol(), false); + NumericVector internal_nodes=find_internal_nodes_pred(tree_data); + NumericVector terminal_nodes=find_term_nodes_pred(tree_data); + arma::vec arma_terminal_nodes=Rcpp::as(terminal_nodes); + NumericVector tree_predictions; + + //now for each internal node find the observations that belong to the terminal nodes + + NumericVector predictions(test_data.nrow()); + if(terminal_nodes.size()==1){ + double nodemean=tree_data(terminal_nodes[0]-1,5); // let nodemean equal tree_data row terminal_nodes[i]^th row , 6th column. The minus 1 is because terminal nodes consists of indices starting at 1, but need indices to start at 0. + predictions=rep(nodemean,test_data.nrow()); + } + else{ + for(int i=0;i temp_split); + } + + arma::uvec temp_pred_indices; + //arma::uvec col_indices=seq_len(testd.n_cols); + arma::vec data_subset = testd.col(split); + data_subset=data_subset.elem(pred_indices); + //now loop through each row of node_split_mat + int n=node_split_mat.n_rows; + for(int j=1;j split_p); + } + pred_indices=pred_indices.elem(temp_pred_indices); + + if(pred_indices.size()==0){ + continue; + } + } + double nodemean=tree_data(terminal_nodes[i]-1,5); + IntegerVector predind=as(wrap(pred_indices)); + predictions[predind]= nodemean; + + } + } + return(predictions); +} + +//##############################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +using namespace Rcpp; +// [[Rcpp::export]] + +List get_BART_BMA_test_predictions(NumericMatrix test_data,NumericVector BIC,List sum_trees,NumericVector y_minmax){ + //this will take in a set of sum of trees and loop through each tree in each set. + //for each tree in each set: + //call bartBMA_get_testdata_term_obs_pred() and get the predicted values + //add up values for each set + //at end weight summed predictions by BIC and put back on original scale + NumericMatrix sum_tree_preds(test_data.nrow(),sum_trees.size()); + + for(int i=0;i(s)){ + //if set of trees is a list then loop through the list and get predicted values + List tree_set=sum_trees[i]; + for(int j=0;j +// [[Rcpp::depends(RcppArmadillo)]] +#include +using namespace Rcpp; +// [[Rcpp::export]] +NumericVector get_imp_vars(NumericVector split_vars,int num_col,NumericVector current_vars){ + + NumericVector vars_chosen=sort_unique(split_vars); + + if(vars_chosen[0]==0){ + vars_chosen.erase(0); + } + if(vars_chosen.size()!=0){ + for(int i=0;i +using namespace Rcpp; +// [[Rcpp::export]] + +List get_weighted_var_imp(int num_vars,NumericVector BIC,List sum_trees){ + + IntegerMatrix vars_for_all_trees(sum_trees.size(),num_vars); + NumericMatrix weighted_vars_for_all_trees(sum_trees.size(),num_vars); + //NumericVector weighted_BIC=BIC/sum(BIC); + + + NumericVector BICi=-0.5*BIC; + double max_BIC=max(BICi); + + // weighted_BIC is actually the posterior model probability + NumericVector weighted_BIC(BIC.size()); + + for(int k=0;k(s)){ + List tree_set=sum_trees[i]; + for(int j=0;j +// [[Rcpp::depends(RcppArmadillo)]] +#define NDEBUG 1 +using namespace Rcpp ; +// [[Rcpp::export]] +IntegerVector csample_num( IntegerVector x, + int size, + bool replace, + NumericVector prob = NumericVector::create() +) { + RNGScope scope; + IntegerVector ret = RcppArmadillo::sample(x, size, replace, prob); + return ret; +} + +//######################################################################################################################// + +#include +// [[Rcpp::depends(RcppArmadillo)]] +#include +using namespace Rcpp; +// [[Rcpp::export]] +NumericMatrix add_rows(NumericMatrix prior_tree_table_temp,int grow_node){ + arma::mat M=Rcpp::as(prior_tree_table_temp); + M(grow_node-1,5)=0; + M(grow_node-1,6)=0; + M(grow_node-1,0)=grow_node+1; + M(grow_node-1,1)=grow_node+2; + M.insert_rows(grow_node,2); + M(grow_node,4)=-1; + M(grow_node+1,4)=-1; + NumericMatrix t=as(wrap(M)); + IntegerVector rname=seq_len(t.nrow()); + + List dimnms = // two vec. with static names + List::create(rname, + CharacterVector::create("left daughter","right daughter","split var","split point","status","mean","std dev")); + // and assign it + t.attr("dimnames") = dimnms; + + return(t); +} + +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +NumericMatrix addcol(NumericMatrix prior_tree_matrix_temp,int grow_node,NumericVector ld_obs,NumericVector rd_obs){ + int ncol=prior_tree_matrix_temp.ncol(); + arma::mat M=Rcpp::as(prior_tree_matrix_temp); + M.insert_cols(ncol,1); + for(int i =0;iprior_tree_matrix_temp.nrow()){ + // throw std::range_error("can't add col because ld row index is out of range"); + // } + // }catch(...){ + // ::Rf_error("there is a problem adding col to mat don't know why"); + // } + M(ld_obs[i],ncol)=grow_node+1; + } + for(int i =0;iprior_tree_matrix_temp.nrow()){ + // throw std::range_error("can't add col because rd row index is out of range"); + // } + // }catch(...){ + // ::Rf_error("there is a problem adding rd col to mat"); + // } + M(rd_obs[i],ncol)=grow_node+2; + } + return(wrap(M)); +} + +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +NumericMatrix set_daughter_to_end_tree(int grow_node,NumericMatrix prior_tree_table_temp,double left_daughter){ + int nrow=prior_tree_table_temp.nrow(); + arma::mat M=Rcpp::as(prior_tree_table_temp); + // Rcout << "Line 82"; + M(grow_node-1,5)=0; + M(grow_node-1,6)=0; + M.insert_rows(nrow,2); + // Rcout << "Line 86"; + + M(grow_node-1,0)=left_daughter; + M(grow_node-1,1)=left_daughter+1; + M(left_daughter-1,4)=-1; + M(left_daughter,4)=-1; + // Rcout << "Line 92"; + + NumericMatrix s=as(wrap(M)); + IntegerVector rname=seq_len(s.nrow()); + + List dimnms = // two vec. with static names + List::create(rname, + CharacterVector::create("left daughter","right daughter","split var","split point","status","mean","std dev")); + // and assign it + s.attr("dimnames") = dimnms; + + return(s); +} + +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +NumericMatrix set_daughter_to_end_mat(int d,NumericMatrix prior_tree_matrix_temp,double left_daughter,NumericVector ld_obs,NumericVector rd_obs){ + int ncol_mat=prior_tree_matrix_temp.ncol(); + arma::mat N=Rcpp::as(prior_tree_matrix_temp); + arma::vec colmat=N.col(d); + NumericVector colmat2=wrap(colmat); + + if(d+1==ncol_mat){ + N.insert_cols(ncol_mat,1); + int nrow_mat=prior_tree_matrix_temp.nrow(); + NumericVector colmatzero(nrow_mat); + colmatzero[ld_obs]=left_daughter; + colmatzero[rd_obs]=left_daughter+1; + //colmat=Rcpp::as(colmatzero); + //N.col(d+1)=colmat; + N.col(d+1)=Rcpp::as(colmatzero); + + }else{ + //else just update existing column + colmat2[ld_obs]=left_daughter; + colmat2[rd_obs]=left_daughter+1; + //colmat=Rcpp::as(colmat2); + //N.col(d)=colmat; + N.col(d)=Rcpp::as(colmat2); + + } + + return(wrap(N)); +} + +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +NumericVector remove_zero(NumericVector nodes_at_depth){ + arma::vec nodes_at_depth2=Rcpp::as(nodes_at_depth); + arma::vec ret=nodes_at_depth2.elem(arma::find(nodes_at_depth2!=0)); + return(wrap(ret)); +} + +//######################################################################################################################// + +// [[Rcpp::export]] +IntegerVector order_intvec_(IntegerVector x) { + IntegerVector sorted = clone(x).sort(); + std::reverse(sorted.begin(), sorted.end()); + + return match(sorted, x); +} + +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +NumericVector get_gnp(NumericVector nodes_at_depth,int grow_node){ + arma::uvec grow_node_pos=arma::find(as(nodes_at_depth)==grow_node); + + return(wrap(arma::conv_to::from(grow_node_pos))); +} + +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +NumericVector find_term_nodes(NumericMatrix tree_table){ + arma::mat arma_tree(tree_table.begin(),tree_table.nrow(), tree_table.ncol(), false); + arma::vec colmat=arma_tree.col(4); + arma::uvec term_nodes=arma::find(colmat==-1); + term_nodes=term_nodes+1; + + return(wrap(arma::conv_to::from(term_nodes))); +} + +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +arma::uvec find_term_obs(NumericMatrix tree_matrix_temp,double terminal_node){ + arma::mat arma_tree_mat(tree_matrix_temp.begin(),tree_matrix_temp.nrow(), tree_matrix_temp.ncol(), false); + arma::uvec term_obs; + + for(int j=0;j0){ + break; + } + } + + return(term_obs); +} + +//######################################################################################################################// +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +double likelihood_function(NumericVector y_temp,NumericMatrix treetable_temp,NumericMatrix obs_to_nodes_temp,double a,double mu,double nu,double lambda){ + double tree_log_lik; + NumericVector terminal_nodes=find_term_nodes(treetable_temp); + double b=terminal_nodes.size(); + IntegerVector n(b); + double term1=0; + double term2=0; + arma::uvec term_obs; + double ni; + arma::vec y_temp2=as(y_temp); + for(int i=0;i< b;i++){ + term_obs=find_term_obs(obs_to_nodes_temp,terminal_nodes[i]); + arma::vec y_k=y_temp2.elem(term_obs); + ni=term_obs.size(); + //n[i]=ni; + double ybar=0; + if(y_k.size()!=0){ + ybar=mean(y_k); + } + term1+=log(ni+a); + arma::vec y_k_sq=pow(y_k,2); + double sum_yksq=sum(y_k_sq); + double b2=pow(ni*ybar +a*mu,2)/(ni+a); + + + //term2+=(sum_yksq+a*pow(mu,2)-b2+nu*lambda); + + + term2+=(sum_yksq+a*pow(mu,2)-b2); + } + //tree_log_lik=(b/2)*log(a)-0.5*term1-((y_temp.size()+nu)/2)*log(term2); + + + + tree_log_lik=(b/2)*log(a)-0.5*term1-((y_temp.size()+nu))*0.5*log(term2+nu*lambda); + + + // for(int i=0;i=left_daughter); + return(gr_ld); +} + +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +NumericMatrix set_tree_to_middle(NumericVector node_to_update,NumericMatrix prior_tree_table_temp,int grow_node,double left_daughter){ + + // Rcout << "Line 300 set_tree_to_middle()" ; + + for(int i=0;i(prior_tree_table_temp); + M.insert_rows(left_daughter-1,2); + M(left_daughter-1,4)=-1; + M(left_daughter,4)=-1; + + M(grow_node-1,0)=left_daughter; + M(grow_node-1,1)=left_daughter+1; + NumericMatrix t=as(wrap(M)); + IntegerVector rname=seq_len(t.nrow()); + + List dimnms = // two vec. with static names + List::create(rname, + CharacterVector::create("left daughter","right daughter","split var","split point","status","mean","std dev")); + // and assign it + t.attr("dimnames") = dimnms; + + return(t); +} + +//######################################################################################################################// +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +NumericMatrix update_grow_obs(NumericMatrix prior_tree_matrix_temp,double grow_node,double left_daughter,int d,NumericVector ld_obs,NumericVector rd_obs){ + arma::mat prior_tree_matrix_temp2(prior_tree_matrix_temp.begin(),prior_tree_matrix_temp.nrow(),prior_tree_matrix_temp.ncol(),false); + arma::vec ptm2=prior_tree_matrix_temp2.col(d); + NumericVector ptm=wrap(ptm2); + ptm[ld_obs]=left_daughter; + ptm[rd_obs]=left_daughter+1; + prior_tree_matrix_temp(_,d)=ptm; + + return(prior_tree_matrix_temp); +} +//######################################################################################################################// +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +NumericMatrix find_obs_to_update_grow(NumericMatrix prior_tree_matrix_temp,double left_daughter,int d,NumericVector ld_obs,NumericVector rd_obs){ + + int rows=prior_tree_matrix_temp.nrow(); + int cols=prior_tree_matrix_temp.ncol(); + int elements=rows*cols; + std::vector rows_obs(elements); + std::vector cols_obs(elements); + int count=0; + for(int i=0;i=left_daughter){ + rows_obs[count]=i; + cols_obs[count]=j; + count++; + } + } + } + rows_obs.resize(count); + cols_obs.resize(count); + + if(rows_obs.size()!=0){ + for(int k=0;k< count;k++){ + if(prior_tree_matrix_temp(rows_obs[k],cols_obs[k])(prior_tree_matrix_temp); // M equals arma mat copy of the matrix + //M.insert_cols(prior_tree_matrix_temp.ncol(),1); // insert one column after last column + //NumericMatrix prior_tree_matrix=as(wrap(M)); // convert back to NumericMatrix and save as prior_tree_matrix + + prior_tree_matrix_temp2.insert_cols(prior_tree_matrix_temp.ncol(),1); // insert one column after last column + } + + + arma::vec ptm2=prior_tree_matrix_temp2.col(d+1); + NumericVector ptm=wrap(ptm2); + ptm[ld_obs]=left_daughter; + ptm[rd_obs]=left_daughter+1; + prior_tree_matrix_temp(_,d+1)=ptm; + + return(prior_tree_matrix_temp); +} +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +List get_daughter_obs(arma::mat& xmat,NumericVector obs_to_update,int split_var,double split_point){ + + //arma::uvec ld_obs_ind(obs_to_update.size()); + //arma::uvec rd_obs_ind(obs_to_update.size()); + + // Rcout << "line 414.\n"; + arma::colvec sv_col; + List daughter_obs(2); + + // Rcout << "line 419.\n"; + sv_col=xmat.col(split_var-1); + // Rcout << "line 421.\n"; + + arma::vec obs_to_update_arma=as(obs_to_update); + arma::uvec obs_to_update_arma_u=as(obs_to_update); + + arma::uvec ld_obs_ind = arma::find(sv_col.elem(obs_to_update_arma_u)<=split_point); + arma::uvec rd_obs_ind = arma::find(sv_col.elem(obs_to_update_arma_u)>split_point); + // Rcout << "line 426.\n"; + + //NumericVector ld_ind2(as(wrap(ld_obs_ind))); + //NumericVector rd_ind2(as(wrap(rd_obs_ind))); + // Rcout << "ld_ind2 = "<< ld_ind2 << " .\n" ; + // Rcout << "ld_ind2 = "<< rd_ind2 << " .\n" ; + // Rcout << "obs_to_update = "<< obs_to_update << " .\n" ; + + //NumericVector ld_obs=obs_to_update[ld_ind2]; + //NumericVector rd_obs=obs_to_update[rd_ind2]; + // Rcout << "line 433.\n"; + arma::vec obs_to_update_arma_left=obs_to_update_arma.elem(ld_obs_ind); + arma::vec obs_to_update_arma_right=obs_to_update_arma.elem(rd_obs_ind); + + //daughter_obs[0]=ld_obs; + //daughter_obs[1]=rd_obs; + //NumericVector ld_obs=wrap(obs_to_update_arma_left); + //NumericVector rd_obs=wrap(obs_to_update_arma_right); + + daughter_obs[0]=wrap(obs_to_update_arma_left); + daughter_obs[1]=wrap(obs_to_update_arma_right); + + // Rcout << "line 437.\n"; + + return(daughter_obs); + +} +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +int find_term_cols(NumericMatrix tree_matrix_temp,int terminal_node){ + + arma::mat tree_matrix_temp2(tree_matrix_temp.begin(),tree_matrix_temp.nrow(),tree_matrix_temp.ncol(),false); + //int count=0; + //std::vector term_cols(tree_matrix_temp.ncol()); + int term_col=0; + for(int j=0;j0){ + //term_cols[count]=j; + //count++; + term_col=j; + break; + } + } + + //term_cols.resize(count); + + //return(wrap(term_cols)); + return(term_col); + +} + +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +NumericVector get_grow_obs(arma::mat& xmat,NumericVector grow_obs,int split_var){ + + arma::vec sv_col=xmat.col(split_var-1); + arma::uvec grow_obs2(as(grow_obs)); + arma::vec get_min=sv_col.elem(grow_obs2); + + return(wrap(get_min)); +} +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +List grow_tree(arma::mat& xmat,//NumericVector y, + NumericMatrix prior_tree_matrix,int grow_node,NumericMatrix prior_tree_table,int splitvar, + double splitpoint,//NumericVector terminal_nodes, + NumericVector grow_obs, + int d//,NumericVector get_min,arma::mat& data_curr_node +) +{ + // Rcout << "Line 489.\n"; + + NumericMatrix prior_tree_matrix_temp=clone(prior_tree_matrix); + NumericMatrix prior_tree_table_temp=clone(prior_tree_table); + // Rcout << "Line 496.\n"; + double yy=xmat.n_cols; + IntegerVector xx=seq_len(yy); + // Rcout << "Line 499.\n"; + prior_tree_table_temp(grow_node-1,3)=splitpoint; + prior_tree_table_temp(grow_node-1,2)=splitvar; + prior_tree_table_temp(grow_node-1,4)=1; + //get data subset for left and right daughter nodes + // Rcout << "Line 504.\n"; + // Rcout << "grow_obs= " << grow_obs << ".\n"; + // Rcout << "splitvar= " << splitvar << ".\n"; + // Rcout << "splitpoint= " << splitpoint << ".\n"; + + List daughter_obs=get_daughter_obs(xmat,grow_obs,splitvar,splitpoint); + // Rcout << "Line 506.\n"; + NumericVector ld_obs=daughter_obs[0]; + NumericVector rd_obs=daughter_obs[1]; + + // Rcout << "Line 510.\n"; + + if(prior_tree_table_temp.nrow()==grow_node){ + // Rcout << "Line 513"; + + prior_tree_table_temp=add_rows(prior_tree_table_temp,grow_node); + prior_tree_matrix_temp=addcol(prior_tree_matrix_temp,grow_node,ld_obs,rd_obs); + }else{ + // Rcout << "Line 518.\n"; + + //if grow node is in the middle of the tree + NumericVector nodes_d; + nodes_d=prior_tree_matrix_temp(_,d); + NumericVector nodes_at_depth=sort_unique(nodes_d); + NumericVector nodes_at_depth1=remove_zero(nodes_at_depth); + NumericVector gn_pos=get_gnp(nodes_at_depth1, grow_node); + arma::uvec prev_uvec= arma::find(as(nodes_at_depth1)(nodes_at_depth1); + nontermvec=nontermvec.elem(prev_uvec); + NumericVector prev= as(wrap(nontermvec)); + double prev_nonterm=0; + if(prev.size()!=0){ + arma::uvec find_nonterm=find_internal_nodes(prior_tree_table); + //should only find internal nodes at the current depth + prev_nonterm=find_prev_nonterm(find_nonterm,prev); + } + double left_daughter=grow_node +2*(prev_nonterm)+(nodes_at_depth1.size()-gn_pos[0]); + NumericVector ptt=prior_tree_table(_,1); + arma::uvec node_to_update=find_nodes_to_update(as(ptt),left_daughter); + //increase the node number of nodes after the grow node by two (because 2 daughter nodes are now appended to grow node) + //do this for all observations except those that already belong to a terminal node (a value of 0) + + // Rcout << "Line 542.\n"; + // Rcout << "node_to_update= " << node_to_update << ".\n"; + + if(node_to_update.size()==0){ + if(prior_tree_matrix_temp.ncol()>d+1){ + // Rcout << "Line 559.\n"; + + prior_tree_table_temp=set_daughter_to_end_tree(grow_node,prior_tree_table_temp,left_daughter); + // Rcout << "Line 562.\n"; + + prior_tree_matrix_temp=update_grow_obs(prior_tree_matrix_temp,grow_node,left_daughter,d+1,ld_obs,rd_obs); + }else{ + // Rcout << "Line 552.\n"; + //if the daughter node number already exists in the tree and existing node numbers have to be updated + //daughter nodes need to be added to the end of the table not in the center of it + prior_tree_table_temp=set_daughter_to_end_tree(grow_node,prior_tree_table_temp,left_daughter); + // Rcout << "Line 568.\n"; + + prior_tree_matrix_temp=set_daughter_to_end_mat(d,prior_tree_matrix_temp,left_daughter,ld_obs,rd_obs); + } + }else{ + // Rcout << "Line 559.\n"; + //if the daughter node number already exists in the tree and existing node numbers have to be updated + prior_tree_table_temp=set_tree_to_middle(wrap(arma::conv_to::from(node_to_update)),prior_tree_table_temp,grow_node,left_daughter); + // Rcout << "Line 562.\n"; + prior_tree_matrix_temp=find_obs_to_update_grow(prior_tree_matrix_temp,left_daughter,d,ld_obs,rd_obs); + } + } + + // Rcout << "Line 546.\n"; + + List ret(2); + ret[0]=prior_tree_table_temp; + ret[1]=prior_tree_matrix_temp; + + return(ret); +} + +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +NumericMatrix set_daughter(int left_daughter,int right_daughter,IntegerVector ld_obs,IntegerVector rd_obs,NumericMatrix tree_matrix_temp,double term_cols){ + + arma::mat tree_matrix_temp2(tree_matrix_temp.begin(),tree_matrix_temp.nrow(),tree_matrix_temp.ncol(),false); + arma::vec arma_col=tree_matrix_temp2.col(term_cols+1); + NumericVector col(as(wrap(arma_col))); + col[ld_obs]=left_daughter; + col[rd_obs]=right_daughter; + tree_matrix_temp(_,term_cols+1)=col; + + return(tree_matrix_temp); +} + +//######################################################################################################################// + +// [[Rcpp::export]] + +IntegerVector order_(NumericVector x) { //p osition of largest value, then second largest, and so on + NumericVector sorted = clone(x).sort(); + std::reverse(sorted.begin(), sorted.end()); + + return match(sorted, x); +} +//######################################################################################################################// + +// [[Rcpp::export]] + +IntegerVector orderforOW(NumericVector x) { // gives vector of position of smallest value, then position of second smallest value, and so on. + NumericVector sorted = clone(x).sort(); // sorted is x in ascending order + //std::reverse(sorted.begin(), sorted.end()); // reverse so that it is in descending order. Could use one line with std::sort(clone(x).begin(), clone(x).end(), std::greater<>()) + + return match(sorted, x); // match is the Rcpp sugar version of the R function match, which returns a vector of the positions of the first matches of the first argument in the second. +} +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +double secondKindStirlingNumber(int n, int k) { + if(k>n) + throw std::range_error("Sterling number undefined for k>n"); + if(k==0 && n==0) + return 1; + if (n == 0 || k == 0 || k > n) + return 0; + if (k == 1 || k == n) + return 1; + + arma::mat sf=arma::zeros(n + 1,n + 1); + for (int i = 0; i < k+1; i++) { + sf(i,i) = 1; + } + for(int i=1; i< n+1 ; i++){ + sf(i,1)=1; + } + for (int i = 3; i < n + 1; i++) { + for (int j = 2; j < k + 1; j++) { + sf(i,j) = j * sf(i - 1,j) + sf(i - 1,j - 1); + } + } + return sf(n,k); +} +// //######################################################################################################################// +// // Returns count of different partitions of n +// // elements in k subsets +// +// //' @export +// // [[Rcpp::export]] +// double countP(int n, int k) +// { +// // Base cases +// if(k>n) +// throw std::range_error("Sterling number undefined for k>n"); +// if(k==0 && n==0) +// return 1; +// if (n == 0 || k == 0 || k > n) +// return 0; +// if (k == 1 || k == n) +// return 1; +// +// // S(n+1, k) = k*S(n, k) + S(n, k-1) +// return k*countP(n-1, k) + countP(n-1, k-1); +// } +//######################################################################################################################// +#include /* tgamma */ +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +double get_tree_prior(double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, + double num_obs, double num_vars, double lambda_poisson, + NumericMatrix tree_table,NumericMatrix tree_matrix,double alpha,double beta){ + + + if(spike_tree==1){ + arma::uvec internal_nodes_prop=find_internal_nodes(tree_table); + arma::mat tree_table2(tree_table.begin(),tree_table.nrow(),tree_table.ncol(),false); + + //Rcout << "line 600.internal_nodes_prop = " << internal_nodes_prop <<".\n"; + + double k_temp=internal_nodes_prop.size()+1; + + //Rcout << "line 600.k_temp = " << k_temp <<".\n"; + + arma::mat split_var_rows=tree_table2.rows(internal_nodes_prop-1); + arma::vec split_var_vec=split_var_rows.col(2); + arma::vec uniquesplitvars=arma::unique(split_var_vec); + double q_temp=uniquesplitvars.n_elem; + // Rcout << "line 606.\n"; + //Rcout << "line 600.q_temp = " << q_temp <<".\n"; + // Rcout << "line 600.lambda_poisson = " << lambda_poisson <<".\n"; + // Rcout << "line 600.num_obs = " << num_obs <<".\n"; + // Rcout << "line double(tgamma(1))= " << double(std::tgamma(1)) <<".\n"; + // Rcout << "line double(tgamma(100)) = " << double(std::tgamma(100)) <<".\n"; + // Rcout << "line double(tgamma(400)) = " << double(std::tgamma(400)) <<".\n"; + // Rcout << "line double(tgamma(400)) = " << double(std::tgamma(double(400))) <<".\n"; + // Rcout << "line double(lgamma(400)) = " << double(std::lgamma(double(400))) <<".\n"; + + + //Rcout << "line double(lgamma(q_temp+1)) = " << double(std::lgamma(double(q_temp+1))) <<".\n"; + + //int qint = q_temp; + //int num_obsint = num_obs; + + //FIRST CALCULATE THE log of denom and right_truncatin + //Then take the exponential + //then take the difference + + double denom=1; + for(int i=0; i int_nodes_index(100*col); // Why 100* ? + //std::vector term_nodes_index(100*col); // + + //int index_count=0; + //int index_count2=0; + arma::uvec internal_nodes_prop=find_internal_nodes(tree_table); + //NumericVector terminal_nodes_prop_wrapped=find_term_nodes(tree_table); + + arma::mat tree_table2(tree_table.begin(),tree_table.nrow(),tree_table.ncol(),false); + arma::vec colmat=tree_table2.col(4); + arma::uvec terminal_nodes_prop=arma::find(colmat==-1)+1; + + //arma::uvec terminal_nodes_prop=as(terminal_nodes_prop_wrapped); + arma::mat tree_matrix2(tree_matrix.begin(),tree_matrix.nrow(),tree_matrix.ncol(),false); + int count=internal_nodes_prop.size(); + int count_term_nodes=terminal_nodes_prop.size(); + + if(count==0){ + propsplit=1-alpha; + }else{ + + for(int k=0;k0){ + //int_nodes_index[index_count]=j; + //index_count++; + propsplit*=alpha*pow((j+1),-beta) ; + break; + } + } + //int_nodes_index.resize(index_count); + //if(int_nodes_index.size()!=0){ + // d=unique(as(wrap(int_nodes_index))); + // double d1=d[0]; + // propsplit*=alpha*pow((d1+1),-beta) ; + //} + //std::vector temp(col); + //int_nodes_index=temp; + //index_count=0; + } + + for(int k=0;k0){ + //term_nodes_index[index_count2]=j; + //index_count2++; + propsplit*=1-alpha*pow((j+1),-beta) ; + + break; + } + } + //term_nodes_index.resize(index_count2); + //if(term_nodes_index.size()!=0){ + // d2=unique(as(wrap(term_nodes_index))); + // double d1=d2[0]; + // propsplit*=1-alpha*pow((d1+1),-beta) ; + //} + //std::vector temp2(col); + //term_nodes_index=temp2; + //index_count2=0; + } + } + + return(propsplit); + } +} +//######################################################################################################################// + +// [[Rcpp::export]] +NumericMatrix start_tree(double start_mean,double start_sd){ + + NumericMatrix treemat(1,7); + double rand=R::rnorm(start_mean,start_sd); + NumericVector testrow = NumericVector::create(0,0,0,0,-1,rand,0); + for(int k=0;k<1;k++){ + for(int j=0;j<7;j++){ + treemat(k,j)=testrow[j]; + } + } + List dimnms = // two vec. with static names + List::create(CharacterVector::create("1"), + CharacterVector::create("left daughter","right daughter","split var","split point","status","mean","std dev")); + // and assign it + treemat.attr("dimnames") = dimnms; + + return(treemat); +} +//######################################################################################################################// + +// [[Rcpp::export]] +NumericMatrix start_tree2(){ + + NumericMatrix treemat(1,7); + //double rand=R::rnorm(start_mean,start_sd); + NumericVector testrow = NumericVector::create(0,0,0,0,-1,0,0); + //for(int k=0;k<1;k++){ + for(int j=0;j<7;j++){ + //treemat(k,j)=testrow[j]; + treemat(0,j)=testrow[j]; + } + //} + List dimnms = // two vec. with static names + List::create(CharacterVector::create("1"), + CharacterVector::create("left daughter","right daughter","split var","split point","status","mean","std dev")); + // and assign it + treemat.attr("dimnames") = dimnms; + + return(treemat); +} +//######################################################################################################################// + +// [[Rcpp::export]] +NumericMatrix start_matrix(int n){ + NumericMatrix mat(n,1); + std::fill(mat.begin(), mat.end(), 1); + return(mat); +} +//######################################################################################################################// + +// [[Rcpp::export]] +List evaluate_model_occams_window(NumericVector tree_lik,double lowest_BIC,double c,List tree_list,List tree_mat_list,IntegerVector tree_parent){ + IntegerVector sorted_lik_index=order_(tree_lik); + std::vector to_be_removed(tree_lik.size()); + int s=0; + + // check tree is in Occam's window if it isn't then delete it from list + while((tree_lik[sorted_lik_index[s]-1])-(lowest_BIC) > c){ + + // if(s==(tree_lik.size()-1)){ + // break; + // } + + //delete tree from tree list + //if((tree_lik[sorted_lik_index[s]-1])-(lowest_BIC)>c){ + //set indicator for index of trees to be removed + to_be_removed[s]=sorted_lik_index[s]-1; + + if(s==(tree_lik.size()-1)){ + s+=1; + break; + } + + s+=1; + //} + } + + to_be_removed.resize(s); + IntegerVector remove_order_index(to_be_removed.size()); + //delete elements from the higest index down + remove_order_index=order_(wrap(to_be_removed)); + + for(int j=0;j to_be_removed(tree_lik.size()); + int s=0; + + + + // Rcout << "Line 852.\n"; + // Rcout << "lowest_BIC =" << lowest_BIC << ".\n"; + // Rcout << "sorted_lik_index =" << sorted_lik_index << ".\n"; + // check tree is in Occam's window if it isn't then delete it from list + while((tree_lik[sorted_lik_index[s]-1])-(lowest_BIC) > c){ + // Rcout << "Line 856.\n"; + + // if(s==(tree_lik.size()-1)){ + // // Rcout << "Line 858.\n"; + // break; + // } + + //delete tree from tree list + //if((tree_lik[sorted_lik_index[s]-1])-(lowest_BIC)>c){ + //set indicator for index of trees to be removed + // Rcout << "Line 864.\n"; + to_be_removed[s]=sorted_lik_index[s]-1; + + if(s==(tree_lik.size()-1)){ + s+=1; + break; + } + + // Rcout << "Line 866.\n"; + s+=1; + //} + } + // Rcout << "Line 870.\n"; + + to_be_removed.resize(s); + IntegerVector remove_order_index(to_be_removed.size()); + //delete elements from the higest index down + remove_order_index=order_(wrap(to_be_removed)); + // Rcout << "Line 872.\n"; + + for(int j=0;j(terminal_nodes); + //NumericVector tree_predictions; + //for each internal node find the observations that belong to the terminal nodes + NumericVector predictions(test_data.nrow()); + if(terminal_nodes.size()==1){ + double nodemean=tree_data(terminal_nodes[0]-1,5); // let nodemean equal tree_data row terminal_nodes[i]^th row , 6th column. The minus 1 is because terminal nodes consists of indices starting at 1, but need indices to start at 0. + predictions=rep(nodemean,test_data.nrow()); + } + else{ + for(int i=0;i temp_split); + } + + arma::uvec temp_pred_indices; + //arma::vec data_subset = testd.col(split); + //data_subset=data_subset.elem(pred_indices); + int n=node_split_mat.n_rows; + + for(int j=1;j split_p); + } + pred_indices=pred_indices.elem(temp_pred_indices); + + if(pred_indices.size()==0){ + continue; + } + } + double nodemean=tree_data(terminal_nodes[i]-1,5); + IntegerVector predind=as(wrap(arma::conv_to::from(pred_indices))); + predictions[predind]= nodemean; + } + } + return(predictions); +} +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List get_initial_resids(NumericMatrix test_data,List List_of_lists_tree_tables,NumericVector ytrain +) { + //Function to make predictions from test data, given a single tree and the terminal node predictions, this function will be called + //for each tree accepted in Occam's Window. + + List List_of_resid_lists(List_of_lists_tree_tables.size()); + List new_pred_list(List_of_lists_tree_tables.size()); + + for(int k=0;k(terminal_nodes); + //NumericVector tree_predictions; + //for each internal node find the observations that belong to the terminal nodes + NumericVector predictions(test_data.nrow()); + if(terminal_nodes.size()==1){ + double nodemean=mean(ytrain)/One_sum_of_tree_list.size(); // let nodemean equal tree_data row terminal_nodes[i]^th row , 6th column. The minus 1 is because terminal nodes consists of indices starting at 1, but need indices to start at 0. + predictions=rep(nodemean,test_data.nrow()); + } + else{ + for(int i=0;i temp_split); + } + + arma::uvec temp_pred_indices; + //arma::vec data_subset = testd.col(split); + //data_subset=data_subset.elem(pred_indices); + int n=node_split_mat.n_rows; + + for(int j=1;j split_p); + } + pred_indices=pred_indices.elem(temp_pred_indices); + + if(pred_indices.size()==0){ + continue; + } + } + IntegerVector predind=as(wrap(arma::conv_to::from(pred_indices))); + NumericVector ys_in_node= ytrain[predind]; + double nodemean=mean(ys_in_node)/One_sum_of_tree_list.size(); + predictions[predind]= nodemean; + } + } + + //Add predictions to tree predictions + for(int q=0;q(wrap(term_obs)); + // Rcout << "Line 1236.\n"; + // + // NumericVector obs_col(obs_to_nodes_temp.nrow()); + // // Rcout << "Line 1166.\n"; + // obs_col[term_obs2]=1; + // // Rcout << "Line 1168.\n"; + // arma::vec colmat=Rcpp::as(obs_col); + // // Rcout << "Line 1170.\n"; + // Jmat.col(i)= colmat; + + arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + colmat.elem(term_obs).fill(1); + Jmat.col(i)= colmat; + } + //Rcout << "Line 1250.\n"; + + return(Jmat); +} + +//###########################################################################################################################// +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +NumericVector mu_vector(List sum_treetable,int n){ + NumericVector mu_vec; + + for(int j=0;j +// // [[Rcpp::depends(RcppArmadillo)]] +// // [[Rcpp::export]] +// double likelihood_function_new(NumericVector y_temp,NumericMatrix treetable_temp,NumericMatrix obs_to_nodes_temp,double a,double mu,double nu,double lambda){ +// +// int n=y_temp.size(); +// NumericVector tree_term_nodes=find_term_nodes(treetable_temp); +// //int b_j=tree_term_nodes.size(); +// //will make J as we go in BART-BMA no need to create it again here.... +// arma::mat Wmat=J(obs_to_nodes_temp,tree_term_nodes); +// +// arma::vec yvec=Rcpp::as(y_temp); +// arma::mat y(n,1); +// y.col(0)=yvec; +// +// +// arma::mat WWt=(1/a)*(Wmat*Wmat.t()); +// arma::mat In(n,n); +// arma::mat covar=lambda*(In.eye()+WWt); +// arma::vec muvec=arma::zeros(y.n_rows) ; +// +// +// arma::vec rel= dmvt(y.t(), muvec, +// covar, nu, +// true); +// +// +// double rel2=as(wrap(rel)); +// +// // double tree_log_lik; +// // NumericVector terminal_nodes=find_term_nodes(treetable_temp); +// // double b=terminal_nodes.size(); +// // IntegerVector n(b); +// // double term1=0; +// // double term2=0; +// // arma::uvec term_obs; +// // int ni; +// // arma::vec y_temp2=as(y_temp); +// // for(int i=0;i< b;i++){ +// // term_obs=find_term_obs(obs_to_nodes_temp,terminal_nodes[i]); +// // arma::vec y_k=y_temp2.elem(term_obs); +// // ni=term_obs.size(); +// // n[i]=ni; +// // double ybar=0; +// // if(y_k.size()!=0){ +// // ybar=mean(y_k); +// // } +// // term1+=log(ni+a); +// // arma::vec y_k_sq=pow(y_k,2); +// // double sum_yksq=sum(y_k_sq); +// // double b2=pow(ni*ybar +a*mu,2)/(ni+a); +// // term2+=(sum_yksq+a*pow(mu,2)-b2+nu*lambda); +// // } +// // tree_log_lik=(b/2)*log(a)-0.5*term1-((y_temp.size()+nu)/2)*log(term2); +// // for(int i=0;i(y_temp); + arma::mat y(n,1); + y.col(0)=yvec; + + + //get exponent + double expon=(n+nu)*0.5; + //get y^Tpsi^{-1}y + // arma::mat psi_inv=psi.i(); + arma::mat yty=y.t()*y; + + //get t(y)inv(psi)J + arma::mat ytW=y.t()*Wmat; + //get t(J)inv(psi)J + arma::mat WtW=Wmat.t()*Wmat; + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + //arma::mat sec_term_inv=sec_term.i(); + arma::mat sec_term_inv=inv_sympd(sec_term); + //get t(J)inv(psi)y + arma::mat third_term=Wmat.t()*y; + //get m^TV^{-1}m + arma::mat mvm= ytW*sec_term_inv*third_term; + + + //arma::mat rel=(b/2)*log(a)-(1/2)*log(det(sec_term))-expon*log(nu*lambda - mvm +yty); + arma::mat rel=(b*0.5)*log(a)-0.5*real(arma::log_det(sec_term))-expon*log(nu*lambda - mvm +yty); + + + double rel2=as(wrap(rel)); + return(rel2); +} +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List likelihood_function2_exact(NumericVector y_temp,NumericMatrix treetable_temp,NumericMatrix obs_to_nodes_temp,double a,double mu,double nu,double lambda){ + + //double likelihood_function2(NumericVector y_temp,NumericMatrix treetable_temp,NumericMatrix obs_to_nodes_temp,double a,double mu,double nu,double lambda){ + + // Rcout << "Line 1420.\n"; + + int n=y_temp.size(); + // Rcout << "Line 1328.\n"; + // Rcout << "treetable_temp.ncol() = " << treetable_temp.ncol() << ".\n"; + NumericVector tree_term_nodes=find_term_nodes(treetable_temp); + // Rcout << "Line 1426.\n"; + + //int b_j=tree_term_nodes.size(); + //will make J as we go in BART-BMA no need to create it again here.... + arma::mat Wmat=J(obs_to_nodes_temp,tree_term_nodes); + double b=Wmat.n_cols; + // Rcout << "Line 1432.\n"; + + arma::vec yvec=Rcpp::as(y_temp); + arma::mat y(n,1); + y.col(0)=yvec; + // Rcout << "Line 1325"; + + + //get exponent + double expon=(n+nu)*0.5; + //get y^Tpsi^{-1}y + // arma::mat psi_inv=psi.i(); + arma::mat yty=y.t()*y; + + //get t(y)inv(psi)J + arma::mat ytW=y.t()*Wmat; + //get t(J)inv(psi)J + arma::mat WtW=Wmat.t()*Wmat; + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + //arma::mat sec_term_inv=sec_term.i(); + arma::mat sec_term_inv=inv_sympd(sec_term); + //get t(J)inv(psi)y + arma::mat third_term=Wmat.t()*y; + //get m^TV^{-1}m + arma::mat mvm= ytW*sec_term_inv*third_term; + + // Rcout << "Line 1461"; + + //arma::mat rel=(b/2)*log(a)-(1/2)*log(det(sec_term))-expon*log(nu*lambda - mvm +yty); + arma::mat rel=(b*0.5)*log(a)-0.5*real(arma::log_det(sec_term))-expon*log(nu*lambda - mvm +yty); + + // Rcout << "Line 1466"; + + double rel2=as(wrap(rel)); + + //double rel2=arma::as_scalar(rel); + // Rcout << "Line 1471"; + + arma::vec predsoutput=Wmat*sec_term_inv*third_term; + + // Rcout << "Line 1372"; + + List ret(2); + ret[0]=rel2; + ret[1]=wrap(predsoutput); + + + //return(rel2); + return(ret); + +} +//############################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +double sumtree_likelihood_function(NumericVector y_temp,List sum_treetable ,List sum_obs_to_nodes,int n,double a,double nu,double lambda){ + //make W and mu matrices for the sum of trees + arma::mat Wmat=W(sum_treetable,sum_obs_to_nodes,n); + double b=Wmat.n_cols; + arma::vec yvec=Rcpp::as(y_temp); + arma::mat y(n,1); + y.col(0)=yvec; + //get exponent + double expon=(n+nu+b)*0.5; + //get y^Tpsi^{-1}y + // arma::mat psi_inv=psi.i(); + arma::mat yty=y.t()*y; + + //get t(y)inv(psi)J + arma::mat ytW=y.t()*Wmat; + //get t(J)inv(psi)J + arma::mat WtW=Wmat.t()*Wmat; + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + arma::mat sec_term_inv=sec_term.i(); + //get t(J)inv(psi)y + arma::mat third_term=Wmat.t()*y; + //get m^TV^{-1}m + arma::mat mvm= ytW*sec_term_inv*third_term; + arma::mat rel=-expon*log(nu*lambda - mvm +yty); + double rel2=as(wrap(rel)); + return(rel2); +} +//############################################################################################################################// +// +// #include +// // [[Rcpp::depends(RcppArmadillo)]] +// // [[Rcpp::export]] +// double sumtree_likelihood_function_new(NumericVector y_temp,List sum_treetable ,List sum_obs_to_nodes,int n,double a,double nu,double lambda){ +// //make W and mu matrices for the sum of trees +// +// arma::mat Wmat=W(sum_treetable,sum_obs_to_nodes,n); +// +// //double b=Wmat.n_cols; +// arma::vec yvec=Rcpp::as(y_temp); +// arma::mat y(n,1); +// y.col(0)=yvec; +// //get exponent +// //double expon=(n+nu+b)/2; +// //get y^Tpsi^{-1}y +// // arma::mat psi_inv=psi.i(); +// //arma::mat yty=y.t()*y; +// +// arma::mat WWt=(1/a)*(Wmat*Wmat.t()); +// arma::mat In(n,n); +// arma::mat covar=lambda*(In.eye()+WWt); +// +// arma::vec muvec=arma::zeros(y.n_rows) ; +// +// arma::vec rel= dmvt(y.t(), muvec, +// covar, nu, +// true); +// +// +// double rel2=as(wrap(rel)); +// +// //get t(y)inv(psi)J +// //arma::mat ytW=y.t()*Wmat; +// //get t(J)inv(psi)J +// //arma::mat WtW=Wmat.t()*Wmat; +// //get jpsij +aI +// //arma::mat aI(b,b); +// //aI=a*aI.eye(); +// //arma::mat sec_term=WtW+aI; +// //arma::mat sec_term_inv=sec_term.i(); +// //get t(J)inv(psi)y +// //arma::mat third_term=Wmat.t()*y; +// //get m^TV^{-1}m +// //arma::mat mvm= ytW*sec_term_inv*third_term; +// //arma::mat rel=-expon*log(nu*lambda - mvm +yty); +// //double rel2=as(wrap(rel)); +// +// return(rel2); +// } +//############################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +double sumtree_likelihood_function2(NumericVector y_temp,List sum_treetable ,List sum_obs_to_nodes,int n,double a,double nu,double lambda){ + //make W and mu matrices for the sum of trees + arma::mat Wmat=W(sum_treetable,sum_obs_to_nodes,n); + double b=Wmat.n_cols; + arma::vec yvec=Rcpp::as(y_temp); + arma::mat y(n,1); + y.col(0)=yvec; + //get exponent + double expon=(n+nu)*0.5; + //get y^Tpsi^{-1}y + // arma::mat psi_inv=psi.i(); + arma::mat yty=y.t()*y; + + //get t(y)inv(psi)J + arma::mat ytW=y.t()*Wmat; + //get t(J)inv(psi)J + arma::mat WtW=Wmat.t()*Wmat; + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + //arma::mat sec_term_inv=sec_term.i(); + arma::mat sec_term_inv=inv_sympd(sec_term); + //get t(J)inv(psi)y + arma::mat third_term=Wmat.t()*y; + //get m^TV^{-1}m + arma::mat mvm= ytW*sec_term_inv*third_term; + //arma::mat rel=(b/2)*log(a)-(1/2)*log(det(sec_term))-expon*log(nu*lambda - mvm +yty); + arma::mat rel=(b/2)*log(a)-0.5*log(det(sec_term))-expon*log(nu*lambda - mvm +yty); + + double rel2=as(wrap(rel)); + return(rel2); +} +//############################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List sumtree_likelihood_function2_exact(NumericVector y_temp,List sum_treetable ,List sum_obs_to_nodes,int n,double a,double nu,double lambda){ + + //double sumtree_likelihood_function2(NumericVector y_temp,List sum_treetable ,List sum_obs_to_nodes,int n,double a,double nu,double lambda){ + //make W and mu matrices for the sum of trees + + // Rcout<< "Line 1617 .\n"; + + arma::mat Wmat=W(sum_treetable,sum_obs_to_nodes,n); + // Rcout << "Line 1620.\n"; + + double b=Wmat.n_cols; + arma::vec yvec=Rcpp::as(y_temp); + arma::mat y(n,1); + y.col(0)=yvec; + //get exponent + double expon=(n+nu)*0.5; + //get y^Tpsi^{-1}y + // arma::mat psi_inv=psi.i(); + arma::mat yty=y.t()*y; + + //get t(y)inv(psi)J + arma::mat ytW=y.t()*Wmat; + //get t(J)inv(psi)J + arma::mat WtW=Wmat.t()*Wmat; + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + //arma::mat sec_term_inv=sec_term.i(); + // Rcout<< "Line 1492 .\n"; + + arma::mat sec_term_inv=inv_sympd(sec_term); + //get t(J)inv(psi)y + // Rcout<< "Line 1496 .\n"; + + arma::mat third_term=Wmat.t()*y; + //get m^TV^{-1}m + arma::mat mvm= ytW*sec_term_inv*third_term; + //arma::mat rel=(b/2)*log(a)-(1/2)*log(det(sec_term))-expon*log(nu*lambda - mvm +yty); + // Rcout<< "Line 1659 .\n"; + + arma::mat rel=(b/2)*log(a)-0.5*real(arma::log_det(sec_term))-expon*log(nu*lambda - mvm +yty); + // Rcout<< "Line 1652 .\n"; + + double rel2=as(wrap(rel)); + // Rcout<< "Line 1655 .\n"; + + + arma::vec predsoutput=Wmat*sec_term_inv*third_term; + + List ret(2); + ret[0]=rel2; + ret[1]=wrap(predsoutput); + + + //return(rel2); + return(ret); +} +//############################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +double sumtree_likelihood_function3(NumericVector y_temp,List sum_treetable ,List sum_obs_to_nodes,int n,double a,double nu,double lambda){ + + //Cholesky attempt + + //make W and mu matrices for the sum of trees + arma::mat Wmat=W(sum_treetable,sum_obs_to_nodes,n); + double b=Wmat.n_cols; + arma::vec yvec=Rcpp::as(y_temp); + arma::mat y(n,1); + y.col(0)=yvec; + //get exponent + double expon=(n+nu)*0.5; + //get y^Tpsi^{-1}y + // arma::mat psi_inv=psi.i(); + arma::mat yty=y.t()*y; + + //get t(y)inv(psi)J + arma::mat ytW=y.t()*Wmat; + //get t(J)inv(psi)J + arma::mat WtW=Wmat.t()*Wmat; + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + + //Obtain (lower triangular?) matrix t(L) by Cholesky decomposition such that sec_term_inv=L*t(L) + arma::mat rooti = arma::trans(arma::inv(trimatu(arma::chol(sec_term)))); + //obtain the log of the root of the determinant + double rootisum = arma::sum(log(rooti.diag())); + + + //arma::mat sec_term_inv=sec_term.i(); + //get t(J)inv(psi)y + //arma::mat third_term=Wmat.t()*y; + //get m^TV^{-1}m + //arma::mat mvm= ytW*sec_term_inv*third_term; + + //t(:)*W^T*Y + arma::mat LtWtY= rooti*(Wmat.t()*y); + + + arma::mat rel=(b/2)*log(a)+rootisum -expon*log(nu*lambda - arma::sum(LtWtY%LtWtY) +yty); + double rel2=as(wrap(rel)); + return(rel2); +} +//############################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +double sumtree_likelihood_function4(NumericVector y_temp,List sum_treetable ,List sum_obs_to_nodes,int n,double a,double nu,double lambda){ + + //Eigenvalue attempt + + //make W and mu matrices for the sum of trees + arma::mat Wmat=W(sum_treetable,sum_obs_to_nodes,n); + double b=Wmat.n_cols; + arma::vec yvec=Rcpp::as(y_temp); + arma::mat y(n,1); + y.col(0)=yvec; + //get exponent + double expon=(n+nu)*0.5; + //get y^Tpsi^{-1}y + // arma::mat psi_inv=psi.i(); + arma::mat yty=y.t()*y; + + //get t(y)inv(psi)J + arma::mat ytW=y.t()*Wmat; + //get t(J)inv(psi)J + arma::mat WtW=Wmat.t()*Wmat; + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + + + + arma::mat sec_term_inv=sec_term.i(); + //get t(J)inv(psi)y + arma::mat third_term=Wmat.t()*y; + //get m^TV^{-1}m + arma::mat mvm= ytW*sec_term_inv*third_term; + + double logdet = sum(arma::log(arma::eig_sym(sec_term))); + + + //arma::mat rel=(b/2)*log(a)-(1/2)*logdet-expon*log(nu*lambda - mvm +yty); + arma::mat rel=(b/2)*log(a)-0.5*logdet-expon*log(nu*lambda - mvm +yty); + + double rel2=as(wrap(rel)); + return(rel2); +} +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +List get_best_split(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, + NumericVector resids,arma::mat& data,NumericMatrix treetable,NumericMatrix tree_mat, + double a,double mu,double nu,double lambda,double c,double lowest_BIC,int parent + ,NumericMatrix cp_mat,double alpha,double beta,int maxOWsize, + unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split//,int first_round +){ + //this function will search through all predictive split points and return those within Occam's Window. + int split_var; + NumericMatrix treetable_c=treetable; + NumericMatrix treemat_c=tree_mat; + + NumericVector terminal_nodes=find_term_nodes(treetable_c); + //IntegerVector change_node1; + int list_size=1000; + std::vector tree_lik(list_size); + List proposal_tree; + //List ret(9); + List ret(4); + + bool no_tree_err=0; + //List likeliest_tree; + List tree_list(list_size); + List tree_mat_list(list_size); + int count=0; + //std::vector tree_parent(list_size); + //int best_sv; + //double best_sp; + double tree_prior=0; + //List changetree; + double BIC; + //int p; + List eval_model; + //NumericVector int_nodes; + //arma::colvec curr_col=data.col(0); + //arma::uvec grow_obs=find_term_obs(treemat_c,terminal_nodes[0]); + //NumericVector d1=unique(find_term_cols(treemat_c,terminal_nodes[0])); + //arma::mat data_curr_node=data.rows(grow_obs); + //double d=d1[0]; + //NumericVector get_min=get_grow_obs(data,wrap(grow_obs),cp_mat(0,0)+1); + double lik; + + for(int l=0;l split_point)); + + if(ld_prop.size()<=min_num_obs_after_split || rd_prop.size()<=min_num_obs_after_split){ + continue; + } + proposal_tree=grow_tree(data,//resids, + treemat_c,terminal_nodes[l],treetable_c, + split_var,split_point,//terminal_nodes, + wrap(arma::conv_to::from(grow_obs)), + d//,get_min,data_curr_node + ); + + // Test lines below have been removed + // NumericMatrix test =proposal_tree[0]; + // NumericMatrix test1 =proposal_tree[1]; + + // if(test1.ncol()==3){ + // NumericVector u1=unique(test1(_,0)); + // NumericVector u2=unique(test1(_,1)); + // NumericVector u3=unique(test1(_,2)); + // } + + // get_best_split should only be used in the first_round. Removing the if condition below + //if(first_round==1){ + lik=likelihood_function(resids,proposal_tree[0],proposal_tree[1],a,mu,nu,lambda); + + // }else{ + // //have a sum of trees + // lik=likelihood_function(resids,proposal_tree[0],proposal_tree[1],a,mu,nu,lambda); + // } + tree_prior=get_tree_prior(spike_tree, s_t_hyperprior, p_s_t, a_s_t,b_s_t,num_obs,num_vars,lambda_poisson,proposal_tree[0],proposal_tree[1],alpha,beta); + //int_nodes=find_term_nodes(proposal_tree[0]); + //p=int_nodes.size(); + //BIC=-2*(lik+log(tree_prior))+p*log(data.n_rows); + BIC=-2*(lik+log(tree_prior)); + + if(BIC(proposal_tree[0])){ + // }else{ + // throw std::range_error("proposal tree not a matrix"); + // } + tree_list[count]=proposal_tree[0]; + tree_mat_list[count]=proposal_tree[1]; + tree_lik[count]=BIC; + //tree_parent[count]=parent; + count++; + if(count==(tree_list.size()-1)){ + list_size=list_size*2; + tree_list=resize_bigger(tree_list,list_size); + tree_mat_list=resize_bigger(tree_mat_list,list_size); + tree_lik.resize(list_size); + //tree_parent.resize(list_size); + } + } + } + } + } + tree_list=resize(tree_list,count); + tree_mat_list=resize(tree_mat_list,count); + tree_lik.resize(count); + //tree_parent.resize(count); + IntegerVector tree_parent(count, parent); + + + if(count>0){ + + if(less_greedy==1){ + ret[0]=tree_list; + ret[1]=tree_lik; + ret[2]=tree_mat_list; + ret[3]=tree_parent; + + return (ret); + } + + //eval_model=evaluate_model_occams_window(wrap(tree_lik),lowest_BIC,log(c),wrap(tree_list),wrap(tree_mat_list),wrap(tree_parent)); + eval_model=evaluate_model_occams_window(wrap(tree_lik),lowest_BIC, c,wrap(tree_list),wrap(tree_mat_list),tree_parent); + NumericVector testlik =eval_model[0]; + List testtree =eval_model[1]; + List testmat =eval_model[2]; + IntegerVector testpar =eval_model[3]; + + if(testlik.size()>0){ + //check if number of trees to be returned is greater than maxOWsize if so only return the best maxOWsize models + if(testlik.size()>maxOWsize){ + IntegerVector owindices=orderforOW(testlik); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + for(int t=0;t tree_lik(list_size); + List proposal_tree; + //List ret(9); + List ret(4); + + bool no_tree_err=0; + //List likeliest_tree; + List tree_list(list_size); + List tree_mat_list(list_size); + int count=0; + //std::vector tree_parent(list_size); + //int best_sv; + //double best_sp; + double tree_prior=0; + //List changetree; + double BIC; + //int p; + List eval_model; + //NumericVector int_nodes; + //arma::colvec curr_col=data.col(0); + //arma::uvec grow_obs=find_term_obs(treemat_c,terminal_nodes[0]); + //NumericVector d1=unique(find_term_cols(treemat_c,terminal_nodes[0])); + //arma::mat data_curr_node=data.rows(grow_obs); + //double d=d1[0]; + //NumericVector get_min=get_grow_obs(data,wrap(grow_obs),cp_mat(0,0)+1); + double lik; + + for(int l=0;l split_point)); + + if(ld_prop.size()<=min_num_obs_after_split || rd_prop.size()<=min_num_obs_after_split){ + continue; + } + proposal_tree=grow_tree(data,//resids, + treemat_c,terminal_nodes[l],treetable_c, + split_var,split_point,//terminal_nodes, + wrap(arma::conv_to::from(grow_obs)), + d//,get_min,data_curr_node + ); + + // Test lines below have been removed + // NumericMatrix test =proposal_tree[0]; + // NumericMatrix test1 =proposal_tree[1]; + + // if(test1.ncol()==3){ + // NumericVector u1=unique(test1(_,0)); + // NumericVector u2=unique(test1(_,1)); + // NumericVector u3=unique(test1(_,2)); + // } + + // get_best_split should only be used in the first_round. Removing the if condition below + //if(first_round==1){ + lik=likelihood_function(resids,proposal_tree[0],proposal_tree[1],a,mu,nu,lambda); + + // }else{ + // //have a sum of trees + // lik=likelihood_function(resids,proposal_tree[0],proposal_tree[1],a,mu,nu,lambda); + // } + tree_prior=get_tree_prior(spike_tree, s_t_hyperprior, p_s_t, a_s_t,b_s_t,num_obs,num_vars,lambda_poisson,proposal_tree[0],proposal_tree[1],alpha,beta); + //int_nodes=find_term_nodes(proposal_tree[0]); + //p=int_nodes.size(); + //BIC=-2*(lik+log(tree_prior))+p*log(data.n_rows); + BIC=-2*(lik+log(tree_prior)); + + if(BIC(proposal_tree[0])){ + // }else{ + // throw std::range_error("proposal tree not a matrix"); + // } + tree_list[count]=proposal_tree[0]; + tree_mat_list[count]=proposal_tree[1]; + tree_lik[count]=BIC; + //tree_parent[count]=parent; + count++; + if(count==(tree_list.size()-1)){ + list_size=list_size*2; + tree_list=resize_bigger(tree_list,list_size); + tree_mat_list=resize_bigger(tree_mat_list,list_size); + tree_lik.resize(list_size); + //tree_parent.resize(list_size); + } + } + } + } + } + tree_list=resize(tree_list,count); + tree_mat_list=resize(tree_mat_list,count); + tree_lik.resize(count); + //tree_parent.resize(count); + IntegerVector tree_parent(count, parent); + if(count>0){ + if(less_greedy==1){ + ret[0]=tree_list; + ret[1]=tree_lik; + ret[2]=tree_mat_list; + ret[3]=tree_parent; + + return (ret); + } + //eval_model=evaluate_model_occams_window(wrap(tree_lik),lowest_BIC,log(c),wrap(tree_list),wrap(tree_mat_list),wrap(tree_parent)); + eval_model=evaluate_model_occams_window(wrap(tree_lik),lowest_BIC,c,wrap(tree_list),wrap(tree_mat_list),tree_parent); + NumericVector testlik =eval_model[0]; + List testtree =eval_model[1]; + List testmat =eval_model[2]; + IntegerVector testpar =eval_model[3]; + + if(testlik.size()>0){ + //check if number of trees to be returned is greater than maxOWsize if so only return the best maxOWsize models + if(testlik.size()>maxOWsize){ + IntegerVector owindices=orderforOW(testlik); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + for(int t=0;t tree_lik(list_size); + List proposal_tree; + //List ret(9); + List ret(4); + bool no_tree_err=0; + //List likeliest_tree; + List tree_list(list_size); + List tree_mat_list(list_size); + int count=0; + //std::vector tree_parent(list_size); + //int best_sv; + //double best_sp; + double tree_prior=1; + //List changetree; + double BIC; + //int p; + //int p_other=0; + List eval_model; + //NumericVector int_nodes; + //NumericVector other_int_nodes; + //arma::colvec curr_col=data.col(0); + //arma::uvec grow_obs=find_term_obs(treemat_c,terminal_nodes[0]); + //NumericVector d1=unique(find_term_cols(treemat_c,terminal_nodes[0])); + //arma::mat data_curr_node=data.rows(grow_obs); + //double d=d1[0]; + //NumericVector get_min=get_grow_obs(data,wrap(grow_obs),cp_mat(0,0)+1); + double lik; + + for(int l=0;l split_point)); + + if(ld_prop.size()<=min_num_obs_after_split || rd_prop.size()<=min_num_obs_after_split){ + continue; + } + proposal_tree=grow_tree(data,//resids, + treemat_c,terminal_nodes[l],treetable_c,split_var, + split_point,//terminal_nodes, + wrap(arma::conv_to::from(grow_obs)), + d//,get_min,data_curr_node + ); + + //Test lines below have been removed + //NumericMatrix test =proposal_tree[0]; + //NumericMatrix test1 =proposal_tree[1]; + + //if(test1.ncol()==3){ + // NumericVector u1=unique(test1(_,0)); + // NumericVector u2=unique(test1(_,1)); + // NumericVector u3=unique(test1(_,2)); + //} + + //It should not be possible for get_best_split to be used outside of the first round, therefore removing the if condition below + //if(first_round==1){ + // lik=likelihood_function(resids,proposal_tree[0],proposal_tree[1],a,mu,nu,lambda); + // }else{ + SEXP s = sum_trees[parent2[i]]; + if(is(s)){ + List sum_trees2=sum_trees[parent2[i]]; + List sum_trees_mat2=sum_trees_mat[parent2[i]]; + sum_trees2.push_back(proposal_tree[0]); + sum_trees_mat2.push_back(proposal_tree[1]); + lik=sumtree_likelihood_function2(y_scaled,sum_trees2,sum_trees_mat2,y_scaled.size(),a,nu,lambda); + for(int t=0;t(proposal_tree[0])){ + //std::cout<<"its a matrix "<<"\n"; + }else{ + throw std::range_error("proposal tree not a matrix"); + } + tree_list[count]=proposal_tree[0]; + tree_mat_list[count]=proposal_tree[1]; + tree_lik[count]=BIC; + //tree_parent[count]=parent; + count++; + if(count==(tree_list.size()-1)){ + list_size=list_size*2; + tree_list=resize_bigger(tree_list,list_size); + tree_mat_list=resize_bigger(tree_mat_list,list_size); + tree_lik.resize(list_size); + //tree_parent.resize(list_size); + } + } + } + } + } + tree_list=resize(tree_list,count); + tree_mat_list=resize(tree_mat_list,count); + tree_lik.resize(count); + //tree_parent.resize(count); + IntegerVector tree_parent(count, parent); + + if(count>0){ + if(less_greedy==1){ + ret[0]=tree_list; + ret[1]=tree_lik; + ret[2]=tree_mat_list; + ret[3]=tree_parent; + + return (ret); + } + //eval_model=evaluate_model_occams_window(wrap(tree_lik),lowest_BIC,log(c),wrap(tree_list),wrap(tree_mat_list),wrap(tree_parent)); + eval_model=evaluate_model_occams_window(wrap(tree_lik),lowest_BIC,c,wrap(tree_list),wrap(tree_mat_list),tree_parent); + NumericVector testlik =eval_model[0]; + List testtree =eval_model[1]; + List testmat =eval_model[2]; + IntegerVector testpar =eval_model[3]; + + if(testlik.size()>0){ + //check if number of trees to be returned is greater than maxOWsize if so only return the best maxOWsize models + if(testlik.size()>maxOWsize){ + IntegerVector owindices=orderforOW(testlik); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + for(int t=0;t tree_lik(list_size); + List proposal_tree; + //List ret(9); + List ret(4); + bool no_tree_err=0; + //List likeliest_tree; + List tree_list(list_size); + List tree_mat_list(list_size); + int count=0; + //std::vector tree_parent(list_size); + //int best_sv; + //double best_sp; + double tree_prior=1; + //List changetree; + double BIC; + //int p; + //int p_other=0; + List eval_model; + //NumericVector int_nodes; + //NumericVector other_int_nodes; + //arma::colvec curr_col=data.col(0); + //arma::uvec grow_obs=find_term_obs(treemat_c,terminal_nodes[0]); + //NumericVector d1=unique(find_term_cols(treemat_c,terminal_nodes[0])); + //arma::mat data_curr_node=data.rows(grow_obs); + //double d=d1[0]; + //NumericVector get_min=get_grow_obs(data,wrap(grow_obs),cp_mat(0,0)+1); + double lik; + + for(int l=0;l split_point)); + + if(ld_prop.size()<=min_num_obs_after_split || rd_prop.size()<=min_num_obs_after_split){ + continue; + } + proposal_tree=grow_tree(data,//resids, + treemat_c,terminal_nodes[l],treetable_c,split_var, + split_point,//terminal_nodes, + wrap(arma::conv_to::from(grow_obs)), + d//,get_min,data_curr_node + ); + + //Test lines below have been removed + //NumericMatrix test =proposal_tree[0]; + //NumericMatrix test1 =proposal_tree[1]; + + //if(test1.ncol()==3){ + // NumericVector u1=unique(test1(_,0)); + // NumericVector u2=unique(test1(_,1)); + // NumericVector u3=unique(test1(_,2)); + //} + + //It should not be possible for get_best_split to be used outside of the first round, therefore removing the if condition below + //if(first_round==1){ + // lik=likelihood_function(resids,proposal_tree[0],proposal_tree[1],a,mu,nu,lambda); + // }else{ + SEXP s = sum_trees[parent2[i]]; + if(is(s)){ + List sum_trees2=sum_trees[parent2[i]]; + List sum_trees_mat2=sum_trees_mat[parent2[i]]; + sum_trees2.push_back(proposal_tree[0]); + sum_trees_mat2.push_back(proposal_tree[1]); + lik=sumtree_likelihood_function2(y_scaled,sum_trees2,sum_trees_mat2,y_scaled.size(),a,nu,lambda); + for(int t=0;t(proposal_tree[0])){ + //std::cout<<"its a matrix "<<"\n"; + }else{ + throw std::range_error("proposal tree not a matrix"); + } + tree_list[count]=proposal_tree[0]; + tree_mat_list[count]=proposal_tree[1]; + tree_lik[count]=BIC; + //tree_parent[count]=parent; + count++; + if(count==(tree_list.size()-1)){ + list_size=list_size*2; + tree_list=resize_bigger(tree_list,list_size); + tree_mat_list=resize_bigger(tree_mat_list,list_size); + tree_lik.resize(list_size); + //tree_parent.resize(list_size); + } + } + } + } + } + tree_list=resize(tree_list,count); + tree_mat_list=resize(tree_mat_list,count); + tree_lik.resize(count); + //tree_parent.resize(count); + IntegerVector tree_parent(count, parent); + + if(count>0){ + if(less_greedy==1){ + ret[0]=tree_list; + ret[1]=tree_lik; + ret[2]=tree_mat_list; + ret[3]=tree_parent; + + return (ret); + } + //eval_model=evaluate_model_occams_window(wrap(tree_lik),lowest_BIC,log(c),wrap(tree_list),wrap(tree_mat_list),wrap(tree_parent)); + eval_model=evaluate_model_occams_window(wrap(tree_lik),lowest_BIC,c,wrap(tree_list),wrap(tree_mat_list),tree_parent); + NumericVector testlik =eval_model[0]; + List testtree =eval_model[1]; + List testmat =eval_model[2]; + IntegerVector testpar =eval_model[3]; + + if(testlik.size()>0){ + //check if number of trees to be returned is greater than maxOWsize if so only return the best maxOWsize models + if(testlik.size()>maxOWsize){ + IntegerVector owindices=orderforOW(testlik); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + for(int t=0;t tree_lik(list_size); + List proposal_tree; + //List ret(9); + //List ret(4); + List ret(5); + + bool no_tree_err=0; + //List likeliest_tree; + List tree_list(list_size); + List tree_mat_list(list_size); + + List tree_preds(list_size); + + + int count=0; + //std::vector tree_parent(list_size); + //int best_sv; + //double best_sp; + double tree_prior=0; + //List changetree; + double BIC; + //int p; + List eval_model; + //NumericVector int_nodes; + //arma::colvec curr_col=data.col(0); + //arma::uvec grow_obs=find_term_obs(treemat_c,terminal_nodes[0]); + //NumericVector d1=unique(find_term_cols(treemat_c,terminal_nodes[0])); + //arma::mat data_curr_node=data.rows(grow_obs); + //double d=d1[0]; + //NumericVector get_min=get_grow_obs(data,wrap(grow_obs),cp_mat(0,0)+1); + double lik; + List lik_listoutput; + // Rcout << "Line 2774 .\n"; + + for(int l=0;l split_point)); + + if(ld_prop.size()<=min_num_obs_after_split || rd_prop.size()<=min_num_obs_after_split){ + continue; + } + + // Rcout << "Line 2814 in get_best_split_exact() .\n"; + + proposal_tree=grow_tree(data,//resids, + treemat_c,terminal_nodes[l],treetable_c, + split_var,split_point,//terminal_nodes, + wrap(arma::conv_to::from(grow_obs)), + d//,get_min,data_curr_node + ); + + // Test lines below have been removed + // NumericMatrix test =proposal_tree[0]; + // NumericMatrix test1 =proposal_tree[1]; + + // if(test1.ncol()==3){ + // NumericVector u1=unique(test1(_,0)); + // NumericVector u2=unique(test1(_,1)); + // NumericVector u3=unique(test1(_,2)); + // } + + // get_best_split_exact should only be used in the first_round. Removing the if condition below + //if(first_round==1){ + //lik=likelihood_function(resids,proposal_tree[0],proposal_tree[1],a,mu,nu,lambda); + + // Rcout << "Line 2837 in get_best_split_exact() .\n"; + + //NumericMatrix proptabtemp = proposal_tree[0]; + //NumericMatrix propmattemp = proposal_tree[1]; + + // Rcout << "proposal_tree[0] = "<< proptabtemp << " .\n"; + // Rcout << "proposal_tree[1] = "<< propmattemp << " .\n"; + + lik_listoutput=likelihood_function2_exact(resids,proposal_tree[0],proposal_tree[1],a,mu,nu,lambda); + // Rcout << "Line 2846 in get_best_split_exact(). .\n"; + + lik=as(lik_listoutput[0]); + NumericVector temp_predvec=lik_listoutput[1]; + + + // }else{ + // //have a sum of trees + // lik=likelihood_function(resids,proposal_tree[0],proposal_tree[1],a,mu,nu,lambda); + // } + + // Rcout << "Line 2857 in get_best_split_exact(). before get_tree_prior() .\n"; + + tree_prior=get_tree_prior(spike_tree, s_t_hyperprior, p_s_t, a_s_t,b_s_t,num_obs,num_vars,lambda_poisson,proposal_tree[0],proposal_tree[1],alpha,beta); + + // Rcout << "Line 2696 in get_best_split_exact(). before get_tree_prior() .\n"; + // Rcout << "k = "<< k << " .\n"; + // Rcout << "w = "<< w << " .\n"; + // Rcout << "l = "<< l << " .\n"; + // Rcout << "terminal_nodes.size() = "<< terminal_nodes.size() << " .\n"; + + + //int_nodes=find_term_nodes(proposal_tree[0]); + //p=int_nodes.size(); + //BIC=-2*(lik+log(tree_prior))+p*log(data.n_rows); + BIC=-2*(lik+log(tree_prior)); + + if(BIC(proposal_tree[0])){ + // }else{ + // throw std::range_error("proposal tree not a matrix"); + // } + tree_list[count]=proposal_tree[0]; + tree_mat_list[count]=proposal_tree[1]; + tree_lik[count]=BIC; + //tree_parent[count]=parent; + + tree_preds[count]=temp_predvec; + + count++; + if(count==(tree_list.size()-1)){ + list_size=list_size*2; + tree_list=resize_bigger(tree_list,list_size); + tree_mat_list=resize_bigger(tree_mat_list,list_size); + tree_lik.resize(list_size); + //tree_parent.resize(list_size); + + tree_preds=resize_bigger(tree_preds,list_size); + + } + } + } + } + } + + // Rcout << "Line 2928 in get_best_split_exact().\n"; + + tree_list=resize(tree_list,count); + tree_mat_list=resize(tree_mat_list,count); + tree_lik.resize(count); + //tree_parent.resize(count); + IntegerVector tree_parent(count, parent); + + tree_preds=resize(tree_preds,count); + + // Rcout << "Line 2930 in get_best_split_exact().\n"; + + if(count>0){ + if(less_greedy==1){ + ret[0]=tree_list; + ret[1]=tree_lik; + ret[2]=tree_mat_list; + ret[3]=tree_parent; + ret[4]=tree_preds; + + return (ret); + } + //eval_model=evaluate_model_occams_window_exact(wrap(tree_lik),lowest_BIC,log(c),wrap(tree_list),wrap(tree_mat_list),wrap(tree_parent)); + //eval_model=evaluate_model_occams_window_exact(wrap(tree_lik),lowest_BIC,log(c),wrap(tree_list),wrap(tree_mat_list),tree_parent); + eval_model=evaluate_model_occams_window_exact(wrap(tree_lik),lowest_BIC,c,wrap(tree_list),wrap(tree_mat_list),tree_parent, + tree_preds); + + NumericVector testlik =eval_model[0]; + List testtree =eval_model[1]; + List testmat =eval_model[2]; + IntegerVector testpar =eval_model[3]; + + List testpredlist=eval_model[4]; + + if(testlik.size()>0){ + //check if number of trees to be returned is greater than maxOWsize if so only return the best maxOWsize models + if(testlik.size()>maxOWsize){ + IntegerVector owindices=orderforOW(testlik); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + + List temp_opreds(maxOWsize); + + + for(int t=0;t tree_lik(list_size); + List proposal_tree; + //List ret(9); + //List ret(4); + List ret(5); + + bool no_tree_err=0; + //List likeliest_tree; + List tree_list(list_size); + List tree_mat_list(list_size); + + List tree_preds(list_size); + + + int count=0; + //std::vector tree_parent(list_size); + //int best_sv; + //double best_sp; + double tree_prior=0; + //List changetree; + double BIC; + //int p; + List eval_model; + //NumericVector int_nodes; + //arma::colvec curr_col=data.col(0); + //arma::uvec grow_obs=find_term_obs(treemat_c,terminal_nodes[0]); + //NumericVector d1=unique(find_term_cols(treemat_c,terminal_nodes[0])); + //arma::mat data_curr_node=data.rows(grow_obs); + //double d=d1[0]; + //NumericVector get_min=get_grow_obs(data,wrap(grow_obs),cp_mat(0,0)+1); + double lik; + List lik_listoutput; + + for(int l=0;l split_point)); + + if(ld_prop.size()<=min_num_obs_after_split || rd_prop.size()<=min_num_obs_after_split){ + continue; + } + proposal_tree=grow_tree(data,//resids, + treemat_c,terminal_nodes[l],treetable_c, + split_var,split_point,//terminal_nodes, + wrap(arma::conv_to::from(grow_obs)), + d//,get_min,data_curr_node + ); + + // Test lines below have been removed + // NumericMatrix test =proposal_tree[0]; + // NumericMatrix test1 =proposal_tree[1]; + + // if(test1.ncol()==3){ + // NumericVector u1=unique(test1(_,0)); + // NumericVector u2=unique(test1(_,1)); + // NumericVector u3=unique(test1(_,2)); + // } + + // get_best_split_exact should only be used in the first_round. Removing the if condition below + //if(first_round==1){ + lik_listoutput=likelihood_function2_exact(resids,proposal_tree[0],proposal_tree[1],a,mu,nu,lambda); + lik=as(lik_listoutput[0]); + NumericVector temp_predvec=lik_listoutput[1]; + + + // }else{ + // //have a sum of trees + // lik=likelihood_function(resids,proposal_tree[0],proposal_tree[1],a,mu,nu,lambda); + // } + tree_prior=get_tree_prior(spike_tree, s_t_hyperprior, p_s_t, a_s_t,b_s_t,num_obs,num_vars,lambda_poisson,proposal_tree[0],proposal_tree[1],alpha,beta); + //int_nodes=find_term_nodes(proposal_tree[0]); + //p=int_nodes.size(); + //BIC=-2*(lik+log(tree_prior))+p*log(data.n_rows); + BIC=-2*(lik+log(tree_prior)); + + if(BIC(proposal_tree[0])){ + // }else{ + // throw std::range_error("proposal tree not a matrix"); + // } + tree_list[count]=proposal_tree[0]; + tree_mat_list[count]=proposal_tree[1]; + tree_lik[count]=BIC; + //tree_parent[count]=parent; + + tree_preds[count]=temp_predvec; + + count++; + if(count==(tree_list.size()-1)){ + list_size=list_size*2; + tree_list=resize_bigger(tree_list,list_size); + tree_mat_list=resize_bigger(tree_mat_list,list_size); + tree_lik.resize(list_size); + //tree_parent.resize(list_size); + + tree_preds=resize_bigger(tree_preds,list_size); + + } + } + } + } + } + tree_list=resize(tree_list,count); + tree_mat_list=resize(tree_mat_list,count); + tree_lik.resize(count); + //tree_parent.resize(count); + IntegerVector tree_parent(count, parent); + + tree_preds=resize(tree_preds,count); + + + if(count>0){ + if(less_greedy==1){ + ret[0]=tree_list; + ret[1]=tree_lik; + ret[2]=tree_mat_list; + ret[3]=tree_parent; + ret[4]=tree_preds; + + return (ret); + } + //eval_model=evaluate_model_occams_window_exact(wrap(tree_lik),lowest_BIC,log(c),wrap(tree_list),wrap(tree_mat_list),wrap(tree_parent)); + //eval_model=evaluate_model_occams_window_exact(wrap(tree_lik),lowest_BIC,log(c),wrap(tree_list),wrap(tree_mat_list),tree_parent); + eval_model=evaluate_model_occams_window_exact(wrap(tree_lik),lowest_BIC,c,wrap(tree_list),wrap(tree_mat_list),tree_parent, + tree_preds); + + NumericVector testlik =eval_model[0]; + List testtree =eval_model[1]; + List testmat =eval_model[2]; + IntegerVector testpar =eval_model[3]; + + List testpredlist=eval_model[4]; + + if(testlik.size()>0){ + //check if number of trees to be returned is greater than maxOWsize if so only return the best maxOWsize models + if(testlik.size()>maxOWsize){ + IntegerVector owindices=orderforOW(testlik); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + + List temp_opreds(maxOWsize); + + + for(int t=0;t tree_lik(list_size); + List proposal_tree; + //List ret(9); + //List ret(4); + List ret(5); + //Rcout << " line 3233. no update .\n"; + + bool no_tree_err=0; + //List likeliest_tree; + List tree_list(list_size); + List tree_mat_list(list_size); + + List tree_preds(list_size); + //Rcout << " line 3241. no update .\n"; + + int count=0; + //std::vector tree_parent(list_size); + //int best_sv; + //double best_sp; + double tree_prior=1; + //List changetree; + double BIC; + //int p; + //int p_other=0; + List eval_model; + //NumericVector int_nodes; + //NumericVector other_int_nodes; + //arma::colvec curr_col=data.col(0); + //arma::uvec grow_obs=find_term_obs(treemat_c,terminal_nodes[0]); + //NumericVector d1=unique(find_term_cols(treemat_c,terminal_nodes[0])); + //arma::mat data_curr_node=data.rows(grow_obs); + //double d=d1[0]; + //NumericVector get_min=get_grow_obs(data,wrap(grow_obs),cp_mat(0,0)+1); + double lik; + List lik_list; + NumericVector temppredoutput; + + // Rcout << " line 3343. no update .\n"; + + for(int l=0;l split_point)); + + if(ld_prop.size()<=min_num_obs_after_split || rd_prop.size()<=min_num_obs_after_split){ + continue; + } + + + proposal_tree=grow_tree(data,//resids, + treemat_c,terminal_nodes[l],treetable_c,split_var, + split_point,//terminal_nodes, + wrap(arma::conv_to::from(grow_obs)), + d//,get_min,data_curr_node + ); + + //Test lines below have been removed + //NumericMatrix test =proposal_tree[0]; + //NumericMatrix test1 =proposal_tree[1]; + + //if(test1.ncol()==3){ + // NumericVector u1=unique(test1(_,0)); + // NumericVector u2=unique(test1(_,1)); + // NumericVector u3=unique(test1(_,2)); + //} + + //It should not be possible for get_best_split_exact to be used outside of the first round, therefore removing the if condition below + //if(first_round==1){ + // lik=likelihood_function(resids,proposal_tree[0],proposal_tree[1],a,mu,nu,lambda); + // }else{ + + //Rcout << " line 3406. no update .\n"; + + SEXP s = sum_trees[parent2[i]]; + if(is(s)){ + List sum_trees2=sum_trees[parent2[i]]; + List sum_trees_mat2=sum_trees_mat[parent2[i]]; + sum_trees2.push_back(proposal_tree[0]); + sum_trees_mat2.push_back(proposal_tree[1]); + //lik=sumtree_likelihood_function2_exact(y_scaled,sum_trees2,sum_trees_mat2,y_scaled.size(),a,nu,lambda); + lik_list=sumtree_likelihood_function2_exact(y_scaled,sum_trees2,sum_trees_mat2,y_scaled.size(),a,nu,lambda); + lik=as(lik_list[0]); + temppredoutput=lik_list[1]; + + for(int t=0;t(lik_list[0]); + temppredoutput=lik_list[1]; + + for(int t=0;t(proposal_tree[0])){ + //std::cout<<"its a matrix "<<"\n"; + }else{ + throw std::range_error("proposal tree not a matrix"); + } + tree_list[count]=proposal_tree[0]; + tree_mat_list[count]=proposal_tree[1]; + tree_lik[count]=BIC; + //tree_parent[count]=parent; + + tree_preds[count]=temppredoutput; + + count++; + if(count==(tree_list.size()-1)){ + list_size=list_size*2; + tree_list=resize_bigger(tree_list,list_size); + tree_mat_list=resize_bigger(tree_mat_list,list_size); + tree_lik.resize(list_size); + //tree_parent.resize(list_size); + + tree_preds=resize_bigger(tree_preds,list_size); + + } + } + } + } + } + tree_list=resize(tree_list,count); + tree_mat_list=resize(tree_mat_list,count); + tree_lik.resize(count); + //tree_parent.resize(count); + + tree_preds=resize(tree_preds,count); + + // Rcout << " line 3518. no update .\n"; + + IntegerVector tree_parent(count, parent); + + if(count>0){ + if(less_greedy==1){ + ret[0]=tree_list; + ret[1]=tree_lik; + ret[2]=tree_mat_list; + ret[3]=tree_parent; + ret[4]=tree_preds; + + return (ret); + } + //eval_model=evaluate_model_occams_window_exact(wrap(tree_lik),lowest_BIC,log(c),wrap(tree_list),wrap(tree_mat_list),wrap(tree_parent)); + //eval_model=evaluate_model_occams_window_exact(wrap(tree_lik),lowest_BIC,log(c),wrap(tree_list),wrap(tree_mat_list),tree_parent); + eval_model=evaluate_model_occams_window_exact(wrap(tree_lik),lowest_BIC,c,wrap(tree_list),wrap(tree_mat_list),tree_parent, + tree_preds); + NumericVector testlik =eval_model[0]; + List testtree =eval_model[1]; + List testmat =eval_model[2]; + IntegerVector testpar =eval_model[3]; + List testpredlist=eval_model[4]; + + // Rcout << " line 3541. no update .\n"; + + if(testlik.size()>0){ + //check if number of trees to be returned is greater than maxOWsize if so only return the best maxOWsize models + if(testlik.size()>maxOWsize){ + IntegerVector owindices=orderforOW(testlik); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + List temp_opreds(maxOWsize); + + for(int t=0;t tree_lik(list_size); + List proposal_tree; + //List ret(9); + //List ret(4); + List ret(5); + + + bool no_tree_err=0; + //List likeliest_tree; + List tree_list(list_size); + List tree_mat_list(list_size); + + List tree_preds(list_size); + + + int count=0; + //std::vector tree_parent(list_size); + //int best_sv; + //double best_sp; + double tree_prior=1; + //List changetree; + double BIC; + //int p; + //int p_other=0; + List eval_model; + //NumericVector int_nodes; + //NumericVector other_int_nodes; + //arma::colvec curr_col=data.col(0); + //arma::uvec grow_obs=find_term_obs(treemat_c,terminal_nodes[0]); + //NumericVector d1=unique(find_term_cols(treemat_c,terminal_nodes[0])); + //arma::mat data_curr_node=data.rows(grow_obs); + //double d=d1[0]; + //NumericVector get_min=get_grow_obs(data,wrap(grow_obs),cp_mat(0,0)+1); + double lik; + List lik_list; + NumericVector temppredoutput; + + // Rcout << "Line 3650. \n"; + + for(int l=0;l split_point)); + + if(ld_prop.size()<=min_num_obs_after_split || rd_prop.size()<=min_num_obs_after_split){ + continue; + } + // Rcout << "Line 3693. \n"; + + proposal_tree=grow_tree(data,//resids, + treemat_c,terminal_nodes[l],treetable_c,split_var, + split_point,//terminal_nodes, + wrap(arma::conv_to::from(grow_obs)), + d//,get_min,data_curr_node + ); + // Rcout << "Line 3701. \n"; + + //Test lines below have been removed + //NumericMatrix test =proposal_tree[0]; + //NumericMatrix test1 =proposal_tree[1]; + + //if(test1.ncol()==3){ + // NumericVector u1=unique(test1(_,0)); + // NumericVector u2=unique(test1(_,1)); + // NumericVector u3=unique(test1(_,2)); + //} + + //It should not be possible for get_best_split_exact to be used outside of the first round, therefore removing the if condition below + //if(first_round==1){ + // lik=likelihood_function(resids,proposal_tree[0],proposal_tree[1],a,mu,nu,lambda); + // }else{ + SEXP s = sum_trees[parent2[i]]; + if(is(s)){ + List sum_trees2=sum_trees[parent2[i]]; + List sum_trees_mat2=sum_trees_mat[parent2[i]]; + sum_trees2.push_back(proposal_tree[0]); + sum_trees_mat2.push_back(proposal_tree[1]); + //lik=sumtree_likelihood_function2_exact(y_scaled,sum_trees2,sum_trees_mat2,y_scaled.size(),a,nu,lambda); + + lik_list=sumtree_likelihood_function2_exact(y_scaled,sum_trees2,sum_trees_mat2,y_scaled.size(),a,nu,lambda); + lik=as(lik_list[0]); + temppredoutput=lik_list[1]; + + for(int t=0;t(lik_list[0]); + temppredoutput=lik_list[1]; + + for(int t=0;t(proposal_tree[0])){ + //std::cout<<"its a matrix "<<"\n"; + }else{ + throw std::range_error("proposal tree not a matrix"); + } + tree_list[count]=proposal_tree[0]; + tree_mat_list[count]=proposal_tree[1]; + tree_lik[count]=BIC; + //tree_parent[count]=parent; + + tree_preds[count]=temppredoutput; + + + count++; + if(count==(tree_list.size()-1)){ + list_size=list_size*2; + tree_list=resize_bigger(tree_list,list_size); + tree_mat_list=resize_bigger(tree_mat_list,list_size); + tree_lik.resize(list_size); + //tree_parent.resize(list_size); + + tree_preds=resize_bigger(tree_preds,list_size); + + + } + } + } + } + } + tree_list=resize(tree_list,count); + tree_mat_list=resize(tree_mat_list,count); + tree_lik.resize(count); + //tree_parent.resize(count); + IntegerVector tree_parent(count, parent); + + tree_preds=resize(tree_preds,count); + + // Rcout << "Line 3835. \n"; + + if(count>0){ + if(less_greedy==1){ + ret[0]=tree_list; + ret[1]=tree_lik; + ret[2]=tree_mat_list; + ret[3]=tree_parent; + ret[4]=tree_preds; + + return (ret); + } + //eval_model=evaluate_model_occams_window_exact(wrap(tree_lik),lowest_BIC,log(c),wrap(tree_list),wrap(tree_mat_list),wrap(tree_parent)); + //eval_model=evaluate_model_occams_window_exact(wrap(tree_lik),lowest_BIC,log(c),wrap(tree_list),wrap(tree_mat_list),tree_parent); + eval_model=evaluate_model_occams_window_exact(wrap(tree_lik),lowest_BIC,c,wrap(tree_list),wrap(tree_mat_list),tree_parent, + tree_preds); + NumericVector testlik =eval_model[0]; + List testtree =eval_model[1]; + List testmat =eval_model[2]; + IntegerVector testpar =eval_model[3]; + List testpredlist=eval_model[4]; + // Rcout << "Line 3856. \n"; + + if(testlik.size()>0){ + //check if number of trees to be returned is greater than maxOWsize if so only return the best maxOWsize models + if(testlik.size()>maxOWsize){ + IntegerVector owindices=orderforOW(testlik); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + List temp_opreds(maxOWsize); + + for(int t=0;t(resids); + + for(int k=0;k< terminal_nodes.size();k++){ + term_obs=find_term_obs(tree_matrix,terminal_nodes[k]); + //get the number of observations in node k + Tj[k]=term_obs.size(); + NumericVector get_mean(term_obs.size()); + for(int i=0;i::from(term_obs)); + new_preds[term_obs2]=new_mean[k]; + } + updated_preds[0]=tree_table; + updated_preds[1]=new_preds; + + return(updated_preds); +} +//######################################################################################################################// + +using namespace Rcpp; +using namespace std; + +const double flagval = __DBL_MIN__; +inline double flag(double a, bool b) { return b ? a : flagval; } + +// [[Rcpp::export]] +NumericVector subsetter(NumericVector a, LogicalVector b) { + NumericVector a1=clone(a); + transform(a1.begin(), a1.end(), b.begin(), a1.begin(), flag); + NumericVector res = NumericVector(sum(b)); + remove_copy(a1.begin(), a1.end(), res.begin(), flagval); + + return res; +} +//######################################################################################################################// + +// [[Rcpp::export]] +IntegerVector order_inc_(NumericVector x) { + NumericVector sorted = clone(x).sort(); + return match(sorted, x); +} + +//######################################################################################################################// + +// [[Rcpp::export]] +List min_which2(NumericVector array,int n,double minout,int whichout){ + // Function to find minimum of an array with n elements that is put in min + minout=array[0]; + whichout=0; + + for(int i=1;i +// [[Rcpp::export]] +double mll_meanvar2(double x, double x2, int n){ + double sigsq=(x2-((x*x)/n))/n; + if(sigsq<=0){sigsq=0.00000000001;} + + return(n*(log(2*M_PI)+log(sigsq)+1)); /* M_PI is in Rmath.h */ + + //if(sigsq<=0){//sigsq=0.00000000001; + // + //return(n*(log(2*M_PI)+-26.32844)); /* M_PI is in Rmath.h */ + //}else{ + // return(n*(log(2*M_PI)+log(sigsq)+1)); /* M_PI is in Rmath.h */ + //} + + //if(sigsq<=0){//sigsq=0.00000000001; + // + // return(n*(1.837877+-24.32844)); /* M_PI is in Rmath.h */ + //}else{ + // return(n*(1.837877+log(sigsq)+1)); /* M_PI is in Rmath.h */ + //} +} +//######################################################################################################################// + +// [[Rcpp::export]] +IntegerVector PELT_meanvar_norm2(NumericVector resp,double pen) + // 0 by default, nonzero indicates error in code +{ + int n=resp.size(); + NumericVector y2=cumsum(pow(resp,2)); + y2.push_front(0); + NumericVector y=cumsum(resp); + y.push_front(0); + IntegerVector cptsout(n,0); + IntegerVector lastchangecpts(2*(n+1)); + NumericVector lastchangelike(n+1); + IntegerVector checklist(n+1); + int nchecklist; + double minout; + NumericVector tmplike(n+1); + IntegerVector tmpt(n+1); + int tstar,i,whichout,nchecktmp; + //double mll_meanvar(); + //void min_which(); + lastchangelike[0]= -pen; + lastchangecpts[0]=0; lastchangecpts[n]=0; + double x=y[1]; + double x2=y2[1]; + lastchangelike[1]=mll_meanvar2(x,x2,1); + lastchangecpts[1]=0; lastchangecpts[n+1]=1; + lastchangelike[2]=mll_meanvar2(y[2],y2[2],2); + lastchangecpts[2]=0; lastchangecpts[n+2]=2; + lastchangelike[3]=mll_meanvar2(y[3],y2[3],3); + lastchangecpts[3]=0; lastchangecpts[n+3]=3; + + minout=lastchangelike[checklist[0]] + mll_meanvar2(x,x2,0)+pen; + whichout=0; + + nchecklist=2; + checklist[0]=0; + checklist[1]=2; + + for(tstar=4;tstar<(n+1);tstar++){ + //R_CheckUserInterrupt(); // checks if user has interrupted the R session and quits if true + + for(i=0;i0]; + std::sort(cptsoutret.begin(), cptsoutret.end()); + + return(cptsoutret); +} + +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +double SS(arma::vec x, arma::vec y, double split){ + double meanLeft, meanRight; + int n = x.n_rows; + arma::vec meanAll(n); + arma::vec xLeft = y.elem(arma::find(x <= split)); + arma::vec xRight = y.elem(arma::find(x > split)); + meanLeft = mean(xLeft); + meanRight = mean(xRight); + + for(int j=0;j(wrap(trans(y-meanAll)*(y-meanAll))); + + return tSS; +} +//######################################################################################################################// +// [[Rcpp::export]] + +List gridCP(arma::vec x, arma::vec y, int gridSize = 10) { + + NumericVector out(gridSize-2); + NumericVector cp_strength(gridSize-2); + arma::vec no_split(gridSize-2); + double xGrid, gridStep = (max(x)-min(x))/((double)gridSize-1.0); + xGrid = min(x); + List currSS(2); + + for(int i=1;i<(gridSize-1);i++) { + xGrid += gridStep; + arma::vec ld_size= y.elem(arma::find(x <= xGrid)); + arma::vec rd_size=y.elem(arma::find(x > xGrid)); + + if(ld_size.size()>2 && rd_size.size()>2) + { + out[i-1]=xGrid; + double testSS=SS(x,y,xGrid); + cp_strength[i-1] = testSS; + no_split[i-1]=0; + }else{ + no_split[i-1]=1; + } + } + arma::uvec to_remove=find(no_split ==1); + //IntegerVector remove_order_index=as(wrap(order_(as (wrap(to_remove))))); + //IntegerVector remove_order_index=order_(as (wrap(arma::conv_to::from(to_remove)))); + //IntegerVector remove_order_index=wrap(arma::conv_to::from(to_remove)); + int num_to_remove = to_remove.size()-1; + + if(to_remove.size()>0){ + //for(int k=0;k0){ + currSS[0]= out; + currSS[1]= cp_strength; + + return currSS; + }else{ + List ret(2); + ret[0]=0; + ret[1]=0; + + return ret; + } +} +//######################################################################################################################// +// [[Rcpp::export]] + +arma::field gridCP_arma(arma::vec x, arma::vec y, int gridSize = 10) { + + arma::vec out(gridSize-2); + arma::vec cp_strength(gridSize-2); + arma::vec no_split(gridSize-2); + double xGrid, gridStep = (max(x)-min(x))/((double)gridSize-1.0); + xGrid = min(x); + arma::field currSS(2); + + for(int i=1;i<(gridSize-1);i++) { + xGrid += gridStep; + arma::vec ld_size= y.elem(arma::find(x <= xGrid)); + arma::vec rd_size=y.elem(arma::find(x > xGrid)); + + if(ld_size.size()>2 && rd_size.size()>2) + { + out[i-1]=xGrid; + double testSS=SS(x,y,xGrid); + cp_strength[i-1] = testSS; + no_split[i-1]=0; + }else{ + no_split[i-1]=1; + } + } + arma::uvec to_remove=find(no_split ==1); + //IntegerVector remove_order_index=as(wrap(order_(as (wrap(to_remove))))); + + if(to_remove.size()>0){ + //perhaps should be ordered, but not sure why + + out=out(to_remove); + cp_strength=cp_strength(to_remove); + + //out.shed_rows(to_remove); + //cp_strength.shed_rows(to_remove); + //for(int k=0;k0){ + currSS[0]= out; + currSS[1]= cp_strength; + + return currSS; + }else{ + //List ret(2); + arma::vec veczerotemp = {0}; ; + currSS[0]= veczerotemp; + currSS[1]= veczerotemp; + + return currSS; + } +} +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List make_gridpoint_cpmat(NumericMatrix data,NumericVector resp,int gridsize,int num_cp){ + int err1=0; + int numvar = data.ncol(); + int numrows=gridsize*numvar-1; + int numcols=3; + arma::mat change_points(numrows,numcols); + int row_count=-1; + //IntegerVector sorted_var; + + arma::vec resp_ar=Rcpp::as(resp); + + for(int i=0;i(coli); + + //arma::vec t=Rcpp::as(resp); + List ans1 = gridCP(coli,resp_ar,gridsize); + NumericVector ans2=ans1[0]; + NumericVector cp_strength=ans1[1]; + + if(ans1.size()!=1 && ans2[0]!=0){ + for(int j=0;j(wrap(te)); + //IntegerVector ordered_dev=order_inc_(col_to_order); + //ordered_dev=ordered_dev-1; + change_points.shed_col(2); + int cp=change_points.n_rows; + double cp_prop=(double)num_cp/(double)100; + int num_cp2=round(cp*(cp_prop)); + num_cp=round(num_cp2); + + if(num_cp==0 && cp!=0){ + num_cp=cp; + } + if(cp(ordered_dev); + arma::uvec t=arma::sort_index(te); + + t=t.subvec(0,num_cp-1); + change_points=change_points.rows(t); + } + List ret(2); + ret[0]=wrap(change_points); + ret[1]=err1; + + return(ret); +} +//######################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List make_gridpoint_cpmat_arma(arma::mat data,arma::vec resp,int gridsize,int num_cp){ + int err1=0; + int numvar = data.n_cols; + int numrows=gridsize*numvar-1; + int numcols=3; + arma::mat change_points(numrows,numcols); + int row_count=-1; + //IntegerVector sorted_var; + + for(int i=0;i(coli); + //arma::vec t=Rcpp::as(resp); + arma::vec coli_ar=data.col(i); + //arma::vec t=Rcpp::as(resp); + arma::field ans1 = gridCP_arma(coli_ar,resp,gridsize); + + //List ans1 = gridCP(coli,t,gridsize); + arma::vec ans2=ans1[0]; + arma::vec cp_strength=ans1[1]; + + if(ans1.size()!=1 && ans2[0]!=0){ + for(unsigned int j=0;j(wrap(te)); + //IntegerVector ordered_dev=order_inc_(col_to_order); + //ordered_dev=ordered_dev-1; + change_points.shed_col(2); + int cp=change_points.n_rows; + double cp_prop=(double)num_cp/(double)100; + int num_cp2=round(cp*(cp_prop)); + num_cp=round(num_cp2); + + if(num_cp==0 && cp!=0){ + num_cp=cp; + } + if(cp(ordered_dev); + arma::uvec t=arma::sort_index(te); + + t=t.subvec(0,num_cp-1); + change_points=change_points.rows(t); + } + List ret(2); + ret[0]=wrap(change_points); + ret[1]=err1; + + return(ret); +} +//######################################################################################################################// +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List make_pelt_cpmat(NumericMatrix data,NumericVector resp,double pen,int num_cp){ + int err1=0; + int n=data.nrow(); + arma::mat change_points; + change_points.set_size(10000000,3); + int row_count=-1; + IntegerVector sorted_var; + + for(int i=0;i(coli); + sorted_var=order_inc_(coli); + sorted_var=sorted_var-1; + IntegerVector ans = PELT_meanvar_norm2(resp[sorted_var],pen); + ans=ans-1; + arma::vec resp_ar=as(resp); + //NumericVector allMean(n); + arma::vec allMean_ar(n); + + if(ans.size()!=1 && ans[0]!=n){ + double meanRight; + double meanLeft; + IntegerVector pelt_sp=sorted_var[ans]; + NumericVector split_values=coli[pelt_sp]; + NumericVector unique_sp=unique(split_values); + //NumericVector all_sp=coli[pelt_sp]; + + //if(unique_sp.size()(index); + IntegerVector t4=ifelse(is_na(index),1,0); + arma::ivec t4arma=as(t4); + arma::uvec t42=find(t4arma==0); + IntegerVector index2(t42.size()); + + arma::ivec index3=indexarma.elem(t42); + + index2=wrap(index3); + index2=index2-1; + ans=index2; + } + + for(int j=0;jsplitpoint)); + }else{ + splitpoint = coli[sorted_var[ans[j]]]; + xLeft = resp_ar.elem(arma::find(coli_ar<=splitpoint)); + xRight = resp_ar.elem(arma::find(coli_ar>splitpoint)); + } + if(xLeft.size()>2 && xRight.size()>2) + { + row_count +=1; + meanLeft = mean(xLeft); + meanRight = mean(xRight); + arma::uvec left_ind=find(coli_ar<=splitpoint); + arma::uvec right_ind=find(coli_ar>splitpoint); + //NumericVector left_ind2=wrap(left_ind); + //NumericVector right_ind2=wrap(right_ind); + //allMean[left_ind2]=meanLeft; + //allMean[right_ind2]=meanRight; + //arma::vec allMean_ar=as(allMean); + + allMean_ar.elem(left_ind).fill(meanLeft); + allMean_ar.elem(right_ind).fill(meanRight); + + + double cp_strength; + cp_strength=as(wrap(trans(resp_ar-allMean_ar)*(resp_ar-allMean_ar))); + change_points(row_count,0)=i; + change_points(row_count,1)=coli[sorted_var[ans[j]]]; + change_points(row_count,2)=cp_strength; + } + } + } + } + change_points.shed_rows(row_count+1,change_points.n_rows-1); + arma::vec te=change_points.col(2); + //NumericVector col_to_order=as(wrap(te)); + //IntegerVector ordered_dev=order_inc_(col_to_order); + //ordered_dev=ordered_dev-1; + change_points.shed_col(2); + int cp=change_points.n_rows; + double cp_prop=(double)num_cp/(double)100; + int num_cp2=round(cp*(cp_prop)); + num_cp=round(num_cp2); + if(num_cp==0 && cp!=0){ + num_cp=cp; + } + if(cp(ordered_dev); + arma::uvec t=arma::sort_index(te); + + t=t.subvec(0,num_cp-1); + change_points=change_points.rows(t); + } + List ret(2); + ret[0]=wrap(change_points); + ret[1]=err1; + + return(ret); +} +//###################################################################################// + +// [[Rcpp::export]] + +List get_best_trees(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, + arma::mat& D1,NumericMatrix resids,double a,double mu,double nu,double lambda,double c, + double sigma_mu,List tree_table,List tree_mat,double lowest_BIC,//int first_round, + IntegerVector parent,List cp_mat_list,//IntegerVector err_list, + NumericMatrix test_data,double alpha,double beta,bool is_test_data,double pen,int num_cp,bool split_rule_node,bool gridpoint,int maxOWsize,int num_splits,int gridsize, bool zero_split, + unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split +){ + List eval_model; + NumericVector lik_list; + List best_subset; + int overall_size=1000; + List overall_trees(overall_size); + NumericVector overall_lik2; + IntegerVector overall_parent2; + List overall_mat(overall_size); + std::vector overall_parent(overall_size); + std::vector overall_lik(overall_size); + int overall_count=0; + //Rcout << "Line 2800"; + if(zero_split==1){ + //Rcout << "Line 2802"; + + //COMMENTED OUT ATTEMPT TO FIX NO ZERO SPLIT TREES BUG + overall_trees[0]= tree_table[0]; + overall_mat[0]= tree_mat[0]; + overall_parent[0]=-1; + overall_parent2[0]=-1; + double lik_temp=likelihood_function(resids,tree_table[0],tree_mat[0],a,mu,nu,lambda); + double tree_prior_temp=get_tree_prior(spike_tree, s_t_hyperprior, p_s_t, a_s_t,b_s_t,num_obs,num_vars,lambda_poisson,tree_table[0],tree_mat[0],alpha,beta); + //double lowest_BIC_temp=-2*(lik_temp+log(tree_prior_temp))+1*log(D1.n_rows); + double lowest_BIC_temp=-2*(lik_temp+log(tree_prior_temp)); + + overall_lik[0]= lowest_BIC_temp; + //Rcout << "Next zero split tree lowest_BIC_temp = " << lowest_BIC_temp << ".\n"; + NumericVector templikvec(1); + templikvec[0]=lowest_BIC_temp; + overall_lik2=templikvec; + + + overall_count=1; + + } + //Rcout << "Line 1759 .\n"; + NumericVector test_preds; + + //for(int j=0;j<0;j++){ + + for(int j=0;j lik_subset_curr_round(lsize); + List mat_subset_curr_round(lsize); + std::vector parent_curr_round(lsize); + int count=0; + for(int i=0;i(wrap(lik_subset_curr_round)), + lowest_BIC, + log(c), + table_subset_curr_round, + mat_subset_curr_round, + as(wrap(parent_curr_round))); + + + lik_subset_curr_round=Rcpp::as>(eval_modeltemp[0]); + table_subset_curr_round=eval_modeltemp[1]; + mat_subset_curr_round=eval_modeltemp[2]; + //overall_count=overall_trees.size(); + parent_curr_round=Rcpp::as>(eval_modeltemp[3]); + + + + + if(table_subset_curr_round.size()==0){ + break; + } + + for(int k=0;k(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent))); + overall_lik2=eval_model[0]; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + //add in check to see if OW accepted more than the top maxOW models... + if(overall_lik2.size()>maxOWsize){ + IntegerVector owindices=orderforOW(overall_lik2); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + //Rcout << "Line 1849. j = " << j << ". \n"; + //now only select those elements + for(int t=0;t(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent))); + overall_lik2=eval_model[0]; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + + } + //Rcout << "overall_lik" << overall_lik.size() << " .\n"; + //Rcout << "overall_trees" << overall_trees.size() << " .\n"; + //Rcout << "overall_count" << overall_count << " .\n"; + + + NumericVector temp_preds; + List updated_preds; + NumericVector new_mean; + NumericMatrix overall_test_preds(test_data.nrow(),overall_trees.size()); + NumericMatrix overallpreds(D1.n_rows,overall_trees.size()); + lowest_BIC=min(overall_lik2); + //Rcout << "Line 2596. \n"; + for(int k=0;k overall_parent(overall_size); + std::vector overall_lik(overall_size); + int overall_count=0; + //Rcout << "Line 1747"; + if(zero_split==1){ + //COMMENTED OUT ATTEMPT TO FIX NO ZERO SPLIT TREES BUG + overall_trees[0]= tree_table[0]; + overall_mat[0]= tree_mat[0]; + overall_parent[0]=-1; + overall_parent2[0]=-1; + double lik_temp=likelihood_function(resids,tree_table[0],tree_mat[0],a,mu,nu,lambda); + double tree_prior_temp=get_tree_prior(spike_tree, s_t_hyperprior, p_s_t, a_s_t,b_s_t,num_obs,num_vars,lambda_poisson,tree_table[0],tree_mat[0],alpha,beta); + //double lowest_BIC_temp=-2*(lik_temp+log(tree_prior_temp))+1*log(D1.n_rows); + double lowest_BIC_temp=-2*(lik_temp+log(tree_prior_temp)); + + overall_lik[0]= lowest_BIC_temp; + + + NumericVector templikvec(1); + templikvec[0]=lowest_BIC_temp; + overall_lik2=templikvec; + + + overall_count=1; + } + //Rcout << "Line 1759 .\n"; + NumericVector test_preds; + //for(int j=0;j<0;j++){ + + for(int j=0;j lik_subset_curr_round(lsize); + List mat_subset_curr_round(lsize); + std::vector parent_curr_round(lsize); + int count=0; + for(int i=0;i(wrap(lik_subset_curr_round)), + lowest_BIC, + log(c), + table_subset_curr_round, + mat_subset_curr_round, + as(wrap(parent_curr_round))); + + + lik_subset_curr_round=Rcpp::as>(eval_modeltemp[0]); + table_subset_curr_round=eval_modeltemp[1]; + mat_subset_curr_round=eval_modeltemp[2]; + //overall_count=overall_trees.size(); + parent_curr_round=Rcpp::as>(eval_modeltemp[3]); + + + + + + if(table_subset_curr_round.size()==0){ + break; + } + + + for(int k=0;k(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent))); + overall_lik2=eval_model[0]; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + //add in check to see if OW accepted more than the top maxOW models... + if(overall_lik2.size()>maxOWsize){ + IntegerVector owindices=orderforOW(overall_lik2); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + //Rcout << "Line 1849. j = " << j << ". \n"; + //now only select those elements + for(int t=0;t::from(term_obsarma)); + + //Rcout << "Line 3373. j = " << j << ". k = " << k << " . \n"; + //Rcout << "term_obs = " << term_obs << " . \n"; + //Rcout << "term_obs.size() = " << term_obs.size() << " . \n"; + //Rcout << "temp_curr_resids.size() = " << temp_curr_resids.size() << " . \n"; + + NumericVector tempsubset = test_res[term_obs]; + + + List cp_mat_list1; + arma::mat tempdata_subset = D1.rows(term_obsarma); + if(gridpoint==0){ + //cp_mat_list1=make_pelt_cpmat(wrap(tempdata_subset),temp_curr_resids[term_obs],pen,num_cp); + cp_mat_list1=make_pelt_cpmat(wrap(tempdata_subset),tempsubset,pen,num_cp); + }else{ + //cp_mat_list1=make_gridpoint_cpmat(wrap(tempdata_subset),temp_curr_resids[term_obs],gridsize,num_cp); + cp_mat_list1=make_gridpoint_cpmat(wrap(tempdata_subset),tempsubset,gridsize,num_cp); + } + //Rcout << "Line 3386. j = " << j << ". k = " << k << " . \n"; + + Tempcpmatlist[nodeind]=cp_mat_list1[0]; + //Rcout << "Line 3389. j = " << j << ". k = " << k << " . \n"; + + } + //Rcout << "Line 3384. j = " << j << ". k = " << k << " . \n"; + + cp_mat_list[k]=Tempcpmatlist; + //Rcout << "Line 3395. j = " << j << ". k = " << k << " . \n"; + + + //cp_mat_list[k]=cp_mat_list1[0]; + + //Rcout << "Line 3387. j = " << j << ". k = " << k << " . \n"; + + + + } + + + // List temp(0); + // + // cp_mat_list=temp; + // + // for(int f=0;f(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent))); + overall_lik2=eval_model[0]; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + } + + + + + NumericVector temp_preds; + List updated_preds; + NumericVector new_mean; + NumericMatrix overall_test_preds(test_data.nrow(),overall_trees.size()); + NumericMatrix overallpreds(D1.n_rows,overall_trees.size()); + lowest_BIC=min(overall_lik2); + //Rcout << "Line 3434. \n"; + for(int k=0;k overall_parent(overall_size); + std::vector overall_lik(overall_size); + NumericVector test_preds; + + + ////// //COMMENTED OUT ATTEMPT TO FIX NO ZERO SPLIT TREES BUG + if(zero_split==1){ + for(int q=0; q(s_temp)){ + //Rcout << "s is a list. \n"; + List sum_trees2_temp=prev_sum_trees[parent[q]]; + List sum_trees_mat2_temp=prev_sum_trees_mat[parent[q]]; + sum_trees2_temp.push_back(tree_table[0]); + sum_trees_mat2_temp.push_back(tree_mat[0]); + double lik_temp=sumtree_likelihood_function2(y_scaled,sum_trees2_temp,sum_trees_mat2_temp,y_scaled.size(),a,nu,lambda); + double tree_prior_temp=1; + //int p_other=0; + //NumericVector other_int_nodes; + //Rcout << "Get to loop over t. \n"; + + for(int t=0;t(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent))); + overall_lik2=eval_model[0]; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + //Rcout <<"line 2094.\n"; + + //add in check to see if OW accepted more than the top maxOW models... + if(overall_lik2.size()>maxOWsize){ + //find the maxOWsize best models and continue with those! + IntegerVector owindices=order_(overall_lik2); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + //Rcout <<"line 2106.\n"; + + //now only select those elements + for(int t=0;t lik_subset_curr_round(lsize); + List mat_subset_curr_round(lsize); + std::vector parent_curr_round(lsize); + int count=0; + //Rcout <<"LENGTH OF TREE TABLE LIST = " << tree_table.size() << " !!!!!!!!!.\n"; + for(int i=0;i(wrap(lik_subset_curr_round)), + lowest_BIC, + log(c), + table_subset_curr_round, + mat_subset_curr_round, + as(wrap(parent_curr_round))); + + + lik_subset_curr_round=Rcpp::as>(eval_modeltemp[0]); + table_subset_curr_round=eval_modeltemp[1]; + mat_subset_curr_round=eval_modeltemp[2]; + //overall_count=overall_trees.size(); + parent_curr_round=Rcpp::as>(eval_modeltemp[3]); + + + + if(table_subset_curr_round.size()==0){ + //Rcout << "BREAK. \n"; + break; + } + //Rcout << "inner loop round WITHOUT CONTINUE OR BREAK. \n"; + + for(int k=0;k(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent))); + overall_lik2=eval_model[0]; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + //Rcout << "overall_parent2[0] AFTER OW EVALUATION" << overall_parent2[0] << ".\n"; + //Rcout << "overall_parent2[0] AFTER OW EVALUATION" << overall_parent2[0] << ".\n"; + + //add in check to see if OW accepted more than the top maxOW models... + if(overall_lik2.size()>maxOWsize){ + //Rcout << "MORE THAN MAXOWSIZE!!!!!!!!!!!" << overall_parent2[0] << ".\n"; + + //find the maxOWsize best models and continue with those! + IntegerVector owindices=orderforOW(overall_lik2); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + + //now only select those elements + for(int t=0;t(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent))); + overall_lik2=eval_model[0]; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + } + + + + NumericVector temp_preds; + List updated_preds; + NumericVector new_mean; + NumericMatrix overall_test_preds(test_data.nrow(),overall_trees.size()); + NumericMatrix overallpreds(D1.n_rows,overall_trees.size()); + lowest_BIC=min(overall_lik2); + //Rcout << "Get to start of update mean loop. \n"; + //Rcout << "overall_trees.size()= " << overall_trees.size() << " \n"; + //Rcout << "overall_mat.size()= " << overall_mat.size() << " \n"; + //Rcout << "overall_lik.size()= " << overall_lik.size() << " \n"; + //Rcout << "overall_parent.size()= " << overall_parent.size() << " \n"; + //Rcout << "overall_parent2.size()= " << overall_parent2.size() << " \n"; + + for(int k=0;k(tree_checktype)){ + //Rcout << "tree_checktype is a NumericMatrix. \n"; + }else{ + //Rcout << "tree_checktype is NOT a NumericMatrix. \n"; + } + SEXP mat_checktype = overall_mat[k]; + if(is(mat_checktype)){ + //Rcout << "mat_checktype is a NumericMatrix. \n"; + }else{ + //Rcout << "mat_checktype is NOT a NumericMatrix. \n"; + } + // SEXP vec_checktype = resids(_,overall_parent2[k]); + // if(is(vec_checktype)){ + //Rcout << "vec_checktype is a NumericMatrix. \n"; + // }else{ + //Rcout << "vec_checktype is NOT a NumericMatrix. \n"; + // } + new_mean=update_mean_var(overall_trees[k],overall_mat[k],resids(_,overall_parent2[k]),a); + } + //Rcout << "Get past update mean_var. k= " << k << " \n"; + + //terminal_nodes=find_term_nodes(overall_trees[k]); + updated_preds=update_predictions(overall_trees[k],overall_mat[k],new_mean,D1.n_rows); + //get the predicted values for the test data. + if(is_test_data) test_preds=get_testdata_term_obs(test_data,overall_trees[k]//,new_mean + ); + temp_preds=updated_preds[1]; + overallpreds(_,k)=temp_preds; + if(is_test_data)overall_test_preds(_,k)=test_preds; + } + //Rcout << "Get to end of update mean loop. \n"; + + arma::mat M1(overallpreds.begin(), overallpreds.nrow(), overallpreds.ncol(), false); + //arma::colvec predicted_values=sum(M1,1); + arma::mat M2(overall_test_preds.begin(), overall_test_preds.nrow(), overall_test_preds.ncol(), false); + //arma::colvec predicted_test_values=sum(M2,1); + //Rcout << "Get to end of get_est_trees_sum. \n"; + // List ret(8); + // ret[0]=overall_lik2; + // ret[1]=overall_trees; + // ret[2]=overall_mat; + // ret[3]=predicted_values; + // ret[4]=overall_parent2; + // ret[5]=wrap(M1); + // ret[6]=lowest_BIC; + // ret[7]=wrap(M2); + List ret(7); + ret[0]=overall_lik2; + ret[1]=overall_trees; + ret[2]=overall_mat; + ret[3]=overall_parent2; + ret[4]=wrap(M1); + ret[5]=lowest_BIC; + ret[6]=wrap(M2); + return(ret); +} +//######################################################################################################################// +// [[Rcpp::export]] + +List get_best_trees_sum_update_splits(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, + arma::mat& D1,NumericMatrix resids,double a,double mu,double nu,double lambda, + double c,double sigma_mu,List tree_table,List tree_mat,double lowest_BIC,//int first_round, + IntegerVector parent,List cp_mat_list,IntegerVector err_list,NumericMatrix test_data,double alpha,double beta,bool is_test_data,double pen,int num_cp,bool split_rule_node,bool gridpoint,int maxOWsize,List prev_sum_trees,List prev_sum_trees_mat,NumericVector y_scaled,int num_splits,int gridsize,bool zero_split, + unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split +){ + //Rcout << "Get to start of get_best_trees_sum. \n"; + + List eval_model; + NumericVector lik_list; + List best_subset; + int overall_size=1000; + List overall_trees(overall_size); + NumericVector overall_lik2; + IntegerVector overall_parent2; + List overall_mat(overall_size); + int overall_count=0; + std::vector overall_parent(overall_size); + std::vector overall_lik(overall_size); + NumericVector test_preds; + + + ////// //COMMENTED OUT ATTEMPT TO FIX NO ZERO SPLIT TREES BUG + if(zero_split==1){ + for(int q=0; q(s_temp)){ + //Rcout << "s is a list. \n"; + List sum_trees2_temp=prev_sum_trees[parent[q]]; + List sum_trees_mat2_temp=prev_sum_trees_mat[parent[q]]; + sum_trees2_temp.push_back(tree_table[0]); + sum_trees_mat2_temp.push_back(tree_mat[0]); + double lik_temp=sumtree_likelihood_function2(y_scaled,sum_trees2_temp,sum_trees_mat2_temp,y_scaled.size(),a,nu,lambda); + double tree_prior_temp=1; + //int p_other=0; + //NumericVector other_int_nodes; + //Rcout << "Get to loop over t. \n"; + + for(int t=0;t(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent))); + overall_lik2=eval_model[0]; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + //Rcout <<"line 2094.\n"; + + //add in check to see if OW accepted more than the top maxOW models... + if(overall_lik2.size()>maxOWsize){ + //find the maxOWsize best models and continue with those! + IntegerVector owindices=order_(overall_lik2); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + //Rcout <<"line 2106.\n"; + + //now only select those elements + for(int t=0;t lik_subset_curr_round(lsize); + List mat_subset_curr_round(lsize); + std::vector parent_curr_round(lsize); + int count=0; + //Rcout << "begin loop over splits. j == " << j << ".\n"; + + //Rcout <<"LENGTH OF TREE TABLE LIST = " << tree_table.size() << " !!!!!!!!!.\n"; + for(int i=0;i(wrap(lik_subset_curr_round)), + lowest_BIC, + log(c), + table_subset_curr_round, + mat_subset_curr_round, + as(wrap(parent_curr_round))); + + + lik_subset_curr_round=Rcpp::as>(eval_modeltemp[0]); + table_subset_curr_round=eval_modeltemp[1]; + mat_subset_curr_round=eval_modeltemp[2]; + //overall_count=overall_trees.size(); + parent_curr_round=Rcpp::as>(eval_modeltemp[3]); + + + + + if(table_subset_curr_round.size()==0){ + //Rcout << "BREAK. \n"; + break; + } + //Rcout << "inner loop round WITHOUT CONTINUE OR BREAK. \n"; + + for(int k=0;k(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent))); + overall_lik2=eval_model[0]; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + //Rcout << "overall_parent2[0] AFTER OW EVALUATION" << overall_parent2[0] << ".\n"; + //Rcout << "overall_parent2[0] AFTER OW EVALUATION" << overall_parent2[0] << ".\n"; + + //add in check to see if OW accepted more than the top maxOW models... + if(overall_lik2.size()>maxOWsize){ + //Rcout << "MORE THAN MAXOWSIZE!!!!!!!!!!!" << overall_parent2[0] << ".\n"; + + //find the maxOWsize best models and continue with those! + IntegerVector owindices=orderforOW(overall_lik2); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + + //now only select those elements + for(int t=0;t::from(term_obsarma)); + + //Rcout << "term_obs = " << term_obs << ".\n"; + //Rcout << "test_res= " << test_res << ".\n"; + + //Rcout << "test_res[term_obs] = " << test_res[term_obs] << ".\n"; + NumericVector tempsubset = test_res[term_obs]; + //Rcout << "tempsubset = " << tempsubset << ".\n"; + + + List cp_mat_list1; + arma::mat tempdata_subset = D1.rows(term_obsarma); + if(gridpoint==0){ + //cp_mat_list1=make_pelt_cpmat(wrap(tempdata_subset),temp_curr_resids[term_obs],pen,num_cp); + cp_mat_list1=make_pelt_cpmat(wrap(tempdata_subset),tempsubset,pen,num_cp); + }else{ + //cp_mat_list1=make_gridpoint_cpmat(wrap(tempdata_subset),temp_curr_resids[term_obs],gridsize,num_cp); + cp_mat_list1=make_gridpoint_cpmat(wrap(tempdata_subset),tempsubset,gridsize,num_cp); + } + Tempcpmatlist[nodeind]=cp_mat_list1[0]; + } + cp_mat_list[k]=Tempcpmatlist; + + } + + + + //List temp(0); + + //cp_mat_list=temp; + + // for(int f=0;f(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent))); + overall_lik2=eval_model[0]; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + } + + NumericVector temp_preds; + List updated_preds; + NumericVector new_mean; + NumericMatrix overall_test_preds(test_data.nrow(),overall_trees.size()); + NumericMatrix overallpreds(D1.n_rows,overall_trees.size()); + lowest_BIC=min(overall_lik2); + //Rcout << "Get to start of update mean loop. \n"; + //Rcout << "overall_trees.size()= " << overall_trees.size() << " \n"; + //Rcout << "overall_mat.size()= " << overall_mat.size() << " \n"; + //Rcout << "overall_lik.size()= " << overall_lik.size() << " \n"; + //Rcout << "overall_parent.size()= " << overall_parent.size() << " \n"; + //Rcout << "overall_parent2.size()= " << overall_parent2.size() << " \n"; + + for(int k=0;k(tree_checktype)){ + //Rcout << "tree_checktype is a NumericMatrix. \n"; + }else{ + //Rcout << "tree_checktype is NOT a NumericMatrix. \n"; + } + SEXP mat_checktype = overall_mat[k]; + if(is(mat_checktype)){ + //Rcout << "mat_checktype is a NumericMatrix. \n"; + }else{ + //Rcout << "mat_checktype is NOT a NumericMatrix. \n"; + } + // SEXP vec_checktype = resids(_,overall_parent2[k]); + // if(is(vec_checktype)){ + //Rcout << "vec_checktype is a NumericMatrix. \n"; + // }else{ + //Rcout << "vec_checktype is NOT a NumericMatrix. \n"; + // } + new_mean=update_mean_var(overall_trees[k],overall_mat[k],resids(_,overall_parent2[k]),a); + } + //Rcout << "Get past update mean_var. k= " << k << " \n"; + + //terminal_nodes=find_term_nodes(overall_trees[k]); + updated_preds=update_predictions(overall_trees[k],overall_mat[k],new_mean,D1.n_rows); + //get the predicted values for the test data. + if(is_test_data) test_preds=get_testdata_term_obs(test_data,overall_trees[k]//,new_mean + ); + temp_preds=updated_preds[1]; + overallpreds(_,k)=temp_preds; + if(is_test_data)overall_test_preds(_,k)=test_preds; + } + //Rcout << "Get to end of update mean loop. \n"; + + arma::mat M1(overallpreds.begin(), overallpreds.nrow(), overallpreds.ncol(), false); + //arma::colvec predicted_values=sum(M1,1); + arma::mat M2(overall_test_preds.begin(), overall_test_preds.nrow(), overall_test_preds.ncol(), false); + //arma::colvec predicted_test_values=sum(M2,1); + //Rcout << "Get to end of get_est_trees_sum. \n"; + // List ret(8); + // ret[0]=overall_lik2; + // ret[1]=overall_trees; + // ret[2]=overall_mat; + // ret[3]=predicted_values; + // ret[4]=overall_parent2; + // ret[5]=wrap(M1); + // ret[6]=lowest_BIC; + // ret[7]=wrap(M2); + List ret(7); + ret[0]=overall_lik2; + ret[1]=overall_trees; + ret[2]=overall_mat; + ret[3]=overall_parent2; + ret[4]=wrap(M1); + ret[5]=lowest_BIC; + ret[6]=wrap(M2); + return(ret); +} +//###################################################################################// + +// [[Rcpp::export]] + +List get_best_trees_exact(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, + arma::mat& D1,NumericMatrix resids,double a,double mu,double nu,double lambda,double c, + double sigma_mu,List tree_table,List tree_mat,double lowest_BIC,//int first_round, + IntegerVector parent,List cp_mat_list,//IntegerVector err_list, + NumericMatrix test_data,double alpha,double beta,bool is_test_data,double pen,int num_cp,bool split_rule_node,bool gridpoint,int maxOWsize,int num_splits,int gridsize, bool zero_split, + unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split +){ + List eval_model; + NumericVector lik_list; + List best_subset; + int overall_size=1000; + List overall_trees(overall_size); + NumericVector overall_lik2; + IntegerVector overall_parent2; + List overall_mat(overall_size); + std::vector overall_parent(overall_size); + std::vector overall_lik(overall_size); + + List overall_predvecs(overall_size); + + + int overall_count=0; + // Rcout << "Line 6275"; + if(zero_split==1){ + // Rcout << "Line 3016"; + + //COMMENTED OUT ATTEMPT TO FIX NO ZERO SPLIT TREES BUG + overall_trees[0]= tree_table[0]; + overall_mat[0]= tree_mat[0]; + overall_parent[0]=-1; + overall_parent2[0]=-1; + //double lik_temp=likelihood_function(resids,tree_table[0],tree_mat[0],a,mu,nu,lambda); + //Rcout << "Line 3024"; + + List lik_templist=likelihood_function2_exact(resids,tree_table[0],tree_mat[0],a,mu,nu,lambda); + //Rcout << "Line 3027"; + + double lik_temp= as(lik_templist[0]); + NumericVector temp_predvec=lik_templist[1]; + + // Rcout << "Line 6156"; + // Rcout << "Line 6307. lik_temp= " << lik_temp << ".\n"; + NumericMatrix tabletemptest = tree_table[0]; + // Rcout << "Line 6307. num rows= " < lik_subset_curr_round(lsize); + List mat_subset_curr_round(lsize); + std::vector parent_curr_round(lsize); + + List predvecs_curr_round(lsize); + + + int count=0; + // Rcout << "Line 6327. tree_table.size() = " << tree_table.size() << ".\n"; + + for(int i=0;i(wrap(lik_subset_curr_round)), + lowest_BIC, + log(c), + table_subset_curr_round, + mat_subset_curr_round, + as(wrap(parent_curr_round)), + predvecs_curr_round); + + + lik_subset_curr_round=Rcpp::as>(eval_modeltemp[0]); + table_subset_curr_round=eval_modeltemp[1]; + mat_subset_curr_round=eval_modeltemp[2]; + //overall_count=overall_trees.size(); + parent_curr_round=Rcpp::as>(eval_modeltemp[3]); + + predvecs_curr_round=eval_modeltemp[4]; + + + + if(table_subset_curr_round.size()==0){ + // Rcout << "Line 6431. j = " << j << ".\n"; + + break; + } + + + for(int k=0;k(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent))); + eval_model=evaluate_model_occams_window_exact(as(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent)), + overall_predvecs); + overall_lik2=eval_model[0]; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + overall_predvecs=eval_model[4]; + + //add in check to see if OW accepted more than the top maxOW models... + if(overall_lik2.size()>maxOWsize){ + IntegerVector owindices=orderforOW(overall_lik2); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + + List temp_opreds(maxOWsize); + //Rcout << "Line 1849. j = " << j << ". \n"; + //now only select those elements + for(int t=0;t(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent)), + overall_predvecs); + // Rcout << "Line 6633.\n"; + overall_lik2=eval_model[0]; + + // Rcout << "Line 6634.\n"; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + overall_predvecs=eval_model[4]; + // Rcout << "Line 6640.\n"; + + } + + //Rcout << "overall_lik" << overall_lik.size() << " .\n"; + //Rcout << "overall_trees" << overall_trees.size() << " .\n"; + //Rcout << "overall_count" << overall_count << " .\n"; + + // Rcout << "Line 6643.\n"; + + + NumericVector temp_preds; + NumericVector temp_true_preds; + + List updated_preds; + NumericVector new_mean; + NumericMatrix overall_test_preds(test_data.nrow(),overall_trees.size()); + NumericMatrix overallpreds(D1.n_rows,overall_trees.size()); + lowest_BIC=min(overall_lik2); + + + NumericMatrix overallpreds_total(D1.n_rows,overall_trees.size()); + + // Rcout << "Line 6508. \n"; + for(int k=0;k overall_parent(overall_size); + std::vector overall_lik(overall_size); + + List overall_predvecs(overall_size); + + + int overall_count=0; + // Rcout << "Line 2800"; + if(zero_split==1){ + //Rcout << "Line 2802"; + + //COMMENTED OUT ATTEMPT TO FIX NO ZERO SPLIT TREES BUG + overall_trees[0]= tree_table[0]; + overall_mat[0]= tree_mat[0]; + overall_parent[0]=-1; + overall_parent2[0]=-1; + //double lik_temp=likelihood_function(resids,tree_table[0],tree_mat[0],a,mu,nu,lambda); + List lik_templist=likelihood_function2_exact(resids,tree_table[0],tree_mat[0],a,mu,nu,lambda); + double lik_temp= as(lik_templist[0]); + NumericVector temp_predvec=lik_templist[1]; + + + double tree_prior_temp=get_tree_prior(spike_tree, s_t_hyperprior, p_s_t, a_s_t,b_s_t,num_obs,num_vars,lambda_poisson,tree_table[0],tree_mat[0],alpha,beta); + //double lowest_BIC_temp=-2*(lik_temp+log(tree_prior_temp))+1*log(D1.n_rows); + double lowest_BIC_temp=-2*(lik_temp+log(tree_prior_temp)); + + overall_lik[0]= lowest_BIC_temp; + //Rcout << "Next zero split tree lowest_BIC_temp = " << lowest_BIC_temp << ".\n"; + NumericVector templikvec(1); + templikvec[0]=lowest_BIC_temp; + overall_lik2=templikvec; + + overall_predvecs[0]=temp_predvec; + + overall_count=1; + + } + //Rcout << "Line 1759 .\n"; + NumericVector test_preds; + //for(int j=0;j<0;j++){ + + for(int j=0;j lik_subset_curr_round(lsize); + List mat_subset_curr_round(lsize); + std::vector parent_curr_round(lsize); + + List predvecs_curr_round(lsize); + + int count=0; + for(int i=0;i(wrap(lik_subset_curr_round)), + lowest_BIC, + log(c), + table_subset_curr_round, + mat_subset_curr_round, + as(wrap(parent_curr_round)), + predvecs_curr_round); + + lik_subset_curr_round=Rcpp::as>(eval_modeltemp[0]); + table_subset_curr_round=eval_modeltemp[1]; + mat_subset_curr_round=eval_modeltemp[2]; + //overall_count=overall_trees.size(); + parent_curr_round=Rcpp::as>(eval_modeltemp[3]); + predvecs_curr_round=eval_modeltemp[4]; + + + + + + if(table_subset_curr_round.size()==0){ + break; + } + + + for(int k=0;k(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent))); + eval_model=evaluate_model_occams_window_exact(as(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent)), + overall_predvecs); + overall_lik2=eval_model[0]; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + overall_predvecs=eval_model[4]; + + //add in check to see if OW accepted more than the top maxOW models... + if(overall_lik2.size()>maxOWsize){ + IntegerVector owindices=orderforOW(overall_lik2); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + + List temp_opreds(maxOWsize); + //Rcout << "Line 1849. j = " << j << ". \n"; + //now only select those elements + for(int t=0;t::from(term_obsarma)); + + //Rcout << "Line 3373. j = " << j << ". k = " << k << " . \n"; + //Rcout << "term_obs = " << term_obs << " . \n"; + //Rcout << "term_obs.size() = " << term_obs.size() << " . \n"; + //Rcout << "temp_curr_resids.size() = " << temp_curr_resids.size() << " . \n"; + + NumericVector tempsubset = test_res[term_obs]; + + + List cp_mat_list1; + arma::mat tempdata_subset = D1.rows(term_obsarma); + if(gridpoint==0){ + //cp_mat_list1=make_pelt_cpmat(wrap(tempdata_subset),temp_curr_resids[term_obs],pen,num_cp); + cp_mat_list1=make_pelt_cpmat(wrap(tempdata_subset),tempsubset,pen,num_cp); + }else{ + //cp_mat_list1=make_gridpoint_cpmat(wrap(tempdata_subset),temp_curr_resids[term_obs],gridsize,num_cp); + cp_mat_list1=make_gridpoint_cpmat(wrap(tempdata_subset),tempsubset,gridsize,num_cp); + } + //Rcout << "Line 3386. j = " << j << ". k = " << k << " . \n"; + + Tempcpmatlist[nodeind]=cp_mat_list1[0]; + //Rcout << "Line 3389. j = " << j << ". k = " << k << " . \n"; + + } + //Rcout << "Line 3384. j = " << j << ". k = " << k << " . \n"; + + cp_mat_list[k]=Tempcpmatlist; + //Rcout << "Line 3395. j = " << j << ". k = " << k << " . \n"; + + + //cp_mat_list[k]=cp_mat_list1[0]; + + //Rcout << "Line 3387. j = " << j << ". k = " << k << " . \n"; + + + + } + + + // List temp(0); + // + // cp_mat_list=temp; + // + // for(int f=0;f(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent)), + overall_predvecs); + overall_lik2=eval_model[0]; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + overall_predvecs=eval_model[4]; + + } + + + //Rcout << "overall_lik" << overall_lik.size() << " .\n"; + //Rcout << "overall_trees" << overall_trees.size() << " .\n"; + //Rcout << "overall_count" << overall_count << " .\n"; + + + NumericVector temp_preds; + NumericVector temp_true_preds; + + List updated_preds; + NumericVector new_mean; + NumericMatrix overall_test_preds(test_data.nrow(),overall_trees.size()); + NumericMatrix overallpreds(D1.n_rows,overall_trees.size()); + lowest_BIC=min(overall_lik2); + + + NumericMatrix overallpreds_total(D1.n_rows,overall_trees.size()); + + //Rcout << "Line 2596. \n"; + for(int k=0;k overall_parent(overall_size); + std::vector overall_lik(overall_size); + NumericVector test_preds; + + // Rcout <<"line 7302.\n"; + + ////// //COMMENTED OUT ATTEMPT TO FIX NO ZERO SPLIT TREES BUG + if(zero_split==1){ + for(int q=0; q(s_temp)){ + //Rcout << "s is a list. \n"; + List sum_trees2_temp=prev_sum_trees[parent[q]]; + List sum_trees_mat2_temp=prev_sum_trees_mat[parent[q]]; + sum_trees2_temp.push_back(tree_table[0]); + sum_trees_mat2_temp.push_back(tree_mat[0]); + //double lik_temp=sumtree_likelihood_function2_exact(y_scaled,sum_trees2_temp,sum_trees_mat2_temp,y_scaled.size(),a,nu,lambda); + + + List lik_listtemp=sumtree_likelihood_function2_exact(y_scaled,sum_trees2_temp,sum_trees_mat2_temp,y_scaled.size(),a,nu,lambda); + double lik_temp=as(lik_listtemp[0]); + NumericVector temppredoutput=lik_listtemp[1]; + + double tree_prior_temp=1; + //int p_other=0; + //NumericVector other_int_nodes; + //Rcout << "Get to loop over t. \n"; + + for(int t=0;t(lik_listtemp[0]); + NumericVector temppredoutput=lik_listtemp[1]; + + double tree_prior_temp=1; + //int p_other=0; + //NumericVector other_int_nodes; + for(int t=0;t(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent))); + eval_model=evaluate_model_occams_window_exact(as(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent)), + overall_predvecs); + + overall_lik2=eval_model[0]; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + overall_predvecs=eval_model[4]; + //Rcout <<"line 2094.\n"; + + //add in check to see if OW accepted more than the top maxOW models... + if(overall_lik2.size()>maxOWsize){ + //find the maxOWsize best models and continue with those! + IntegerVector owindices=order_(overall_lik2); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + List temp_opreds(maxOWsize); + //Rcout <<"line 2106.\n"; + + //now only select those elements + for(int t=0;t lik_subset_curr_round(lsize); + List mat_subset_curr_round(lsize); + std::vector parent_curr_round(lsize); + + List predvecs_curr_round(lsize); + + // Rcout <<"line 7510.\n"; + + + int count=0; + //// Rcout <<"LENGTH OF TREE TABLE LIST = " << tree_table.size() << " !!!!!!!!!.\n"; + //Rcout <<"LENGTH OF cp_mat_list LIST = " << cp_mat_list.size() << " !!!!!!!!!.\n"; + for(int i=0;i(wrap(lik_subset_curr_round)), + lowest_BIC, + log(c), + table_subset_curr_round, + mat_subset_curr_round, + as(wrap(parent_curr_round)), + predvecs_curr_round); + + + lik_subset_curr_round=Rcpp::as>(eval_modeltemp[0]); + table_subset_curr_round=eval_modeltemp[1]; + mat_subset_curr_round=eval_modeltemp[2]; + //overall_count=overall_trees.size(); + parent_curr_round=Rcpp::as>(eval_modeltemp[3]); + + predvecs_curr_round=eval_modeltemp[4]; + + + if(table_subset_curr_round.size()==0){ + //Rcout << "BREAK. \n"; + break; + } + //Rcout << "inner loop round WITHOUT CONTINUE OR BREAK. \n"; + + for(int k=0;k(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent))); + // + eval_model=evaluate_model_occams_window_exact(as(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent)), + overall_predvecs); + + + overall_lik2=eval_model[0]; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + overall_predvecs=eval_model[4]; + + //Rcout << "overall_parent2[0] AFTER OW EVALUATION" << overall_parent2[0] << ".\n"; + //Rcout << "overall_parent2[0] AFTER OW EVALUATION" << overall_parent2[0] << ".\n"; + + //add in check to see if OW accepted more than the top maxOW models... + if(overall_lik2.size()>maxOWsize){ + //Rcout << "MORE THAN MAXOWSIZE!!!!!!!!!!!" << overall_parent2[0] << ".\n"; + + //find the maxOWsize best models and continue with those! + IntegerVector owindices=orderforOW(overall_lik2); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + List temp_opreds(maxOWsize); + + //now only select those elements + for(int t=0;t(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent)), + overall_predvecs); + + + overall_lik2=eval_model[0]; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + overall_predvecs=eval_model[4]; + } + + // Rcout << "Line 7882"; + + NumericVector temp_preds; + NumericVector temp_true_preds; + + List updated_preds; + NumericVector new_mean; + NumericMatrix overall_test_preds(test_data.nrow(),overall_trees.size()); + NumericMatrix overallpreds(D1.n_rows,overall_trees.size()); + lowest_BIC=min(overall_lik2); + + NumericMatrix overallpreds_total(D1.n_rows,overall_trees.size()); + + + //Rcout << "Get to start of update mean loop. \n"; + //Rcout << "overall_trees.size()= " << overall_trees.size() << " \n"; + //Rcout << "overall_mat.size()= " << overall_mat.size() << " \n"; + //Rcout << "overall_lik.size()= " << overall_lik.size() << " \n"; + //Rcout << "overall_parent.size()= " << overall_parent.size() << " \n"; + //Rcout << "overall_parent2.size()= " << overall_parent2.size() << " \n"; + + for(int k=0;k(tree_checktype)){ + //Rcout << "tree_checktype is a NumericMatrix. \n"; + }else{ + //Rcout << "tree_checktype is NOT a NumericMatrix. \n"; + } + SEXP mat_checktype = overall_mat[k]; + if(is(mat_checktype)){ + //Rcout << "mat_checktype is a NumericMatrix. \n"; + }else{ + //Rcout << "mat_checktype is NOT a NumericMatrix. \n"; + } + // SEXP vec_checktype = resids(_,overall_parent2[k]); + // if(is(vec_checktype)){ + //Rcout << "vec_checktype is a NumericMatrix. \n"; + // }else{ + //Rcout << "vec_checktype is NOT a NumericMatrix. \n"; + // } + new_mean=update_mean_var(overall_trees[k],overall_mat[k],resids(_,overall_parent2[k]),a); + } + //Rcout << "Get past update mean_var. k= " << k << " \n"; + + //terminal_nodes=find_term_nodes(overall_trees[k]); + updated_preds=update_predictions(overall_trees[k],overall_mat[k],new_mean,D1.n_rows); + //get the predicted values for the test data. + if(is_test_data) test_preds=get_testdata_term_obs(test_data,overall_trees[k]//,new_mean + ); + temp_preds=updated_preds[1]; + overallpreds(_,k)=temp_preds; + if(is_test_data)overall_test_preds(_,k)=test_preds; + + + temp_true_preds=overall_predvecs[k]; + overallpreds_total(_,k)=temp_true_preds; + + } + //Rcout << "Get to end of update mean loop. \n"; + + arma::mat M1(overallpreds.begin(), overallpreds.nrow(), overallpreds.ncol(), false); + //arma::colvec predicted_values=sum(M1,1); + arma::mat M2(overall_test_preds.begin(), overall_test_preds.nrow(), overall_test_preds.ncol(), false); + //arma::colvec predicted_test_values=sum(M2,1); + //Rcout << "Get to end of get_est_trees_sum. \n"; + // List ret(8); + // ret[0]=overall_lik2; + // ret[1]=overall_trees; + // ret[2]=overall_mat; + // ret[3]=predicted_values; + // ret[4]=overall_parent2; + // ret[5]=wrap(M1); + // ret[6]=lowest_BIC; + // ret[7]=wrap(M2); + List ret(8); + ret[0]=overall_lik2; + ret[1]=overall_trees; + ret[2]=overall_mat; + ret[3]=overall_parent2; + ret[4]=wrap(M1); + ret[5]=lowest_BIC; + ret[6]=wrap(M2); + ret[7]=overallpreds_total; + return(ret); +} +//######################################################################################################################// +// [[Rcpp::export]] + +List get_best_trees_sum_update_splits_exact(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, + arma::mat& D1,NumericMatrix resids,double a,double mu,double nu,double lambda, + double c,double sigma_mu,List tree_table,List tree_mat,double lowest_BIC,//int first_round, + IntegerVector parent,List cp_mat_list,IntegerVector err_list,NumericMatrix test_data,double alpha,double beta,bool is_test_data,double pen,int num_cp,bool split_rule_node,bool gridpoint,int maxOWsize,List prev_sum_trees,List prev_sum_trees_mat,NumericVector y_scaled,int num_splits,int gridsize,bool zero_split, + unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split +){ + // Rcout << "Get to start of get_best_trees_sum_update_splits_exact. \n"; + + List eval_model; + NumericVector lik_list; + List best_subset; + int overall_size=1000; + List overall_trees(overall_size); + NumericVector overall_lik2; + IntegerVector overall_parent2; + List overall_mat(overall_size); + + List overall_predvecs(overall_size); + + + int overall_count=0; + std::vector overall_parent(overall_size); + std::vector overall_lik(overall_size); + NumericVector test_preds; + + // Rcout << "Get to line 8006. \n"; + + ////// //COMMENTED OUT ATTEMPT TO FIX NO ZERO SPLIT TREES BUG + ////// //COMMENTED OUT ATTEMPT TO FIX NO ZERO SPLIT TREES BUG + if(zero_split==1){ + for(int q=0; q(s_temp)){ + //Rcout << "s is a list. \n"; + List sum_trees2_temp=prev_sum_trees[parent[q]]; + List sum_trees_mat2_temp=prev_sum_trees_mat[parent[q]]; + sum_trees2_temp.push_back(tree_table[0]); + sum_trees_mat2_temp.push_back(tree_mat[0]); + //double lik_temp=sumtree_likelihood_function2_exact(y_scaled,sum_trees2_temp,sum_trees_mat2_temp,y_scaled.size(),a,nu,lambda); + + + List lik_listtemp=sumtree_likelihood_function2_exact(y_scaled,sum_trees2_temp,sum_trees_mat2_temp,y_scaled.size(),a,nu,lambda); + double lik_temp=as(lik_listtemp[0]); + NumericVector temppredoutput=lik_listtemp[1]; + + double tree_prior_temp=1; + //int p_other=0; + //NumericVector other_int_nodes; + //Rcout << "Get to loop over t. \n"; + + for(int t=0;t(lik_listtemp[0]); + NumericVector temppredoutput=lik_listtemp[1]; + + double tree_prior_temp=1; + //int p_other=0; + //NumericVector other_int_nodes; + for(int t=0;t(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent))); + eval_model=evaluate_model_occams_window_exact(as(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent)), + overall_predvecs); + + overall_lik2=eval_model[0]; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + overall_predvecs=eval_model[4]; + //Rcout <<"line 2094.\n"; + + //add in check to see if OW accepted more than the top maxOW models... + if(overall_lik2.size()>maxOWsize){ + //find the maxOWsize best models and continue with those! + IntegerVector owindices=order_(overall_lik2); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + List temp_opreds(maxOWsize); + //Rcout <<"line 2106.\n"; + + //now only select those elements + for(int t=0;t lik_subset_curr_round(lsize); + List mat_subset_curr_round(lsize); + std::vector parent_curr_round(lsize); + + + List predvecs_curr_round(lsize); + + + int count=0; + // Rcout << "begin loop over splits. j == " << j << ".\n"; + + //Rcout <<"LENGTH OF TREE TABLE LIST = " << tree_table.size() << " !!!!!!!!!.\n"; + for(int i=0;i(wrap(lik_subset_curr_round)), + lowest_BIC, + log(c), + table_subset_curr_round, + mat_subset_curr_round, + as(wrap(parent_curr_round)), + predvecs_curr_round); + + lik_subset_curr_round=Rcpp::as>(eval_modeltemp[0]); + table_subset_curr_round=eval_modeltemp[1]; + mat_subset_curr_round=eval_modeltemp[2]; + //overall_count=overall_trees.size(); + parent_curr_round=Rcpp::as>(eval_modeltemp[3]); + predvecs_curr_round=eval_modeltemp[4]; + + if(table_subset_curr_round.size()==0){ + //Rcout << "BREAK. \n"; + break; + } + //Rcout << "inner loop round WITHOUT CONTINUE OR BREAK. \n"; + + for(int k=0;k(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent))); + // + eval_model=evaluate_model_occams_window_exact(as(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent)), + overall_predvecs); + + + overall_lik2=eval_model[0]; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + overall_predvecs=eval_model[4]; + + //Rcout << "overall_parent2[0] AFTER OW EVALUATION" << overall_parent2[0] << ".\n"; + //Rcout << "overall_parent2[0] AFTER OW EVALUATION" << overall_parent2[0] << ".\n"; + + //add in check to see if OW accepted more than the top maxOW models... + if(overall_lik2.size()>maxOWsize){ + //Rcout << "MORE THAN MAXOWSIZE!!!!!!!!!!!" << overall_parent2[0] << ".\n"; + + //find the maxOWsize best models and continue with those! + IntegerVector owindices=orderforOW(overall_lik2); + owindices=owindices-1; + //get the top maxOWsize indices to keep in OW + NumericVector temp_olik(maxOWsize); + List temp_otrees(maxOWsize); + List temp_omat(maxOWsize); + IntegerVector temp_oparent(maxOWsize); + List temp_opreds(maxOWsize); + + //now only select those elements + for(int t=0;t::from(term_obsarma)); + + + + //Rcout << "term_obs = " << term_obs << ".\n"; + //Rcout << "test_res= " << test_res << ".\n"; + + //Rcout << "test_res[term_obs] = " << test_res[term_obs] << ".\n"; + NumericVector tempsubset = test_res[term_obs]; + //Rcout << "tempsubset = " << tempsubset << ".\n"; + + + List cp_mat_list1; + arma::mat tempdata_subset = D1.rows(term_obsarma); + if(gridpoint==0){ + //cp_mat_list1=make_pelt_cpmat(wrap(tempdata_subset),temp_curr_resids[term_obs],pen,num_cp); + cp_mat_list1=make_pelt_cpmat(wrap(tempdata_subset),tempsubset,pen,num_cp); + }else{ + //cp_mat_list1=make_gridpoint_cpmat(wrap(tempdata_subset),temp_curr_resids[term_obs],gridsize,num_cp); + cp_mat_list1=make_gridpoint_cpmat(wrap(tempdata_subset),tempsubset,gridsize,num_cp); + } + Tempcpmatlist[nodeind]=cp_mat_list1[0]; + } + cp_mat_list[k]=Tempcpmatlist; + + } + + + + //List temp(0); + + //cp_mat_list=temp; + + // for(int f=0;f(wrap(overall_lik)),lowest_BIC,log(c),overall_trees,overall_mat,as(wrap(overall_parent)), + overall_predvecs); + + + overall_lik2=eval_model[0]; + overall_trees=eval_model[1]; + overall_mat=eval_model[2]; + overall_count=overall_trees.size(); + overall_parent2=eval_model[3]; + overall_predvecs=eval_model[4]; + } + + + NumericVector temp_preds; + NumericVector temp_true_preds; + + List updated_preds; + NumericVector new_mean; + NumericMatrix overall_test_preds(test_data.nrow(),overall_trees.size()); + NumericMatrix overallpreds(D1.n_rows,overall_trees.size()); + lowest_BIC=min(overall_lik2); + + NumericMatrix overallpreds_total(D1.n_rows,overall_trees.size()); + + + //Rcout << "Get to start of update mean loop. \n"; + //Rcout << "overall_trees.size()= " << overall_trees.size() << " \n"; + //Rcout << "overall_mat.size()= " << overall_mat.size() << " \n"; + //Rcout << "overall_lik.size()= " << overall_lik.size() << " \n"; + //Rcout << "overall_parent.size()= " << overall_parent.size() << " \n"; + //Rcout << "overall_parent2.size()= " << overall_parent2.size() << " \n"; + + for(int k=0;k(tree_checktype)){ + //Rcout << "tree_checktype is a NumericMatrix. \n"; + }else{ + //Rcout << "tree_checktype is NOT a NumericMatrix. \n"; + } + SEXP mat_checktype = overall_mat[k]; + if(is(mat_checktype)){ + //Rcout << "mat_checktype is a NumericMatrix. \n"; + }else{ + //Rcout << "mat_checktype is NOT a NumericMatrix. \n"; + } + // SEXP vec_checktype = resids(_,overall_parent2[k]); + // if(is(vec_checktype)){ + //Rcout << "vec_checktype is a NumericMatrix. \n"; + // }else{ + //Rcout << "vec_checktype is NOT a NumericMatrix. \n"; + // } + new_mean=update_mean_var(overall_trees[k],overall_mat[k],resids(_,overall_parent2[k]),a); + } + //Rcout << "Get past update mean_var. k= " << k << " \n"; + + //terminal_nodes=find_term_nodes(overall_trees[k]); + updated_preds=update_predictions(overall_trees[k],overall_mat[k],new_mean,D1.n_rows); + //get the predicted values for the test data. + if(is_test_data) test_preds=get_testdata_term_obs(test_data,overall_trees[k]//,new_mean + ); + temp_preds=updated_preds[1]; + overallpreds(_,k)=temp_preds; + if(is_test_data)overall_test_preds(_,k)=test_preds; + + + temp_true_preds=overall_predvecs[k]; + overallpreds_total(_,k)=temp_true_preds; + + } + // Rcout << "Get to end of update mean loop. \n"; + + arma::mat M1(overallpreds.begin(), overallpreds.nrow(), overallpreds.ncol(), false); + //arma::colvec predicted_values=sum(M1,1); + arma::mat M2(overall_test_preds.begin(), overall_test_preds.nrow(), overall_test_preds.ncol(), false); + //arma::colvec predicted_test_values=sum(M2,1); + //// Rcout << "Get to end of get_est_trees_sum. \n"; + // List ret(8); + // ret[0]=overall_lik2; + // ret[1]=overall_trees; + // ret[2]=overall_mat; + // ret[3]=predicted_values; + // ret[4]=overall_parent2; + // ret[5]=wrap(M1); + // ret[6]=lowest_BIC; + // ret[7]=wrap(M2); + List ret(8); + ret[0]=overall_lik2; + ret[1]=overall_trees; + ret[2]=overall_mat; + ret[3]=overall_parent2; + ret[4]=wrap(M1); + ret[5]=lowest_BIC; + ret[6]=wrap(M2); + ret[7]=overallpreds_total; + return(ret); +} +//######################################################################################################################// +// [[Rcpp::export]] +NumericVector scale_response(double a,double b,double c,double d,NumericVector y){ + NumericVector y_scaled = -((-b*c+a*d)/(-a+b))+((-c+d)*y/(-a+b)); + + return(y_scaled); +} +//######################################################################################################################// + +// [[Rcpp::export]] +NumericVector get_original(double low,double high,double sp_low,double sp_high,NumericVector sum_preds){ + NumericVector original_y=(sum_preds*(-low+high))/(-sp_low+sp_high) + (-high*sp_low+low*sp_high)/(-sp_low+sp_high); + + return(original_y); +} +//######################################################################################################################// +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +arma::vec get_original_arma(double low,double high,double sp_low,double sp_high,arma::vec sum_preds){ + arma::vec original_y=(sum_preds*(-low+high))/(-sp_low+sp_high) + (-high*sp_low+low*sp_high)/(-sp_low+sp_high); + + return(original_y); +} +//######################################################################################################################// +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +arma::vec get_original_TE_arma(double low,double high,double sp_low,double sp_high,arma::vec sum_preds){ + arma::vec original_y=sum_preds*((-low+high)/(-sp_low+sp_high)); + + return(original_y); // reverse scaling of predictions of scaled variable (??) +} +//######################################################################################################################// + +// [[Rcpp::export]] +double get_original_TE_double(double low,double high,double sp_low,double sp_high,double sum_preds){ + double original_y=sum_preds*((-low+high)/(-sp_low+sp_high)); + + return(original_y); // reverse scaling of predictions of scaled variable (??) +} +//###########################################################################################################################// + +// [[Rcpp::export]] + +List get_termobs_test_data(NumericMatrix test_data,NumericMatrix tree_data) { + // Function to make predictions from test data, given a single tree and the terminal node predictions, this function will be called + //for each tree accepted in Occam's Window. + + //test_data is a nxp matrix with the same variable names as the training data the model was built on + + //tree_data is the tree table with the tree information i.e. split points and split variables and terminal node mean values + + //term_node_means is a vector storing the terminal node mean values + + arma::mat arma_tree(tree_data.begin(), tree_data.nrow(), tree_data.ncol(), false); + arma::mat testd(test_data.begin(), test_data.nrow(), test_data.ncol(), false); + //NumericVector internal_nodes=find_internal_nodes_gs(tree_data); + NumericVector terminal_nodes=find_term_nodes(tree_data); + //arma::vec arma_terminal_nodes=Rcpp::as(terminal_nodes); + //NumericVector tree_predictions; + + //now for each internal node find the observations that belong to the terminal nodes + + //NumericVector predictions(test_data.nrow()); + List term_obs(terminal_nodes.size()); + if(terminal_nodes.size()==1){ + //double nodemean=tree_data(terminal_nodes[0]-1,5); // let nodemean equal tree_data row terminal_nodes[i]^th row , 6th column. The minus 1 is because terminal nodes consists of indices starting at 1, but need indices to start at 0. + //predictions=rep(nodemean,test_data.nrow()); + IntegerVector temp_obsvec = seq_len(test_data.nrow())-1; + term_obs[0]= temp_obsvec; + } + else{ + for(int i=0;i temp_split); + } + + arma::uvec temp_pred_indices; + arma::vec data_subset = testd.col(split); + data_subset=data_subset.elem(pred_indices); + + //now loop through each row of node_split_mat + int n=node_split_mat.n_rows; + + for(int j=1;j split_p); + } + pred_indices=pred_indices.elem(temp_pred_indices); + + if(pred_indices.size()==0){ + continue; + } + + } + + //double nodemean=tree_data(terminal_nodes[i]-1,5); + IntegerVector predind=as(wrap(arma::conv_to::from(pred_indices))); + //predictions[predind]= nodemean; + term_obs[i]=predind; + } + } + //List ret(1); + //ret[0] = term_obs; + + //ret[0] = terminal_nodes; + //ret[1] = term_obs; + //ret[2] = predictions; + return(term_obs); +} + +//###########################################################################################################################// + +// [[Rcpp::export]] + +arma::field get_termobs_test_data_fields(NumericMatrix test_data,NumericMatrix tree_data) { + // Function to make predictions from test data, given a single tree and the terminal node predictions, this function will be called + //for each tree accepted in Occam's Window. + + //test_data is a nxp matrix with the same variable names as the training data the model was built on + + //tree_data is the tree table with the tree information i.e. split points and split variables and terminal node mean values + + //term_node_means is a vector storing the terminal node mean values + + arma::mat arma_tree(tree_data.begin(), tree_data.nrow(), tree_data.ncol(), false); + arma::mat testd(test_data.begin(), test_data.nrow(), test_data.ncol(), false); + //NumericVector internal_nodes=find_internal_nodes_gs(tree_data); + NumericVector terminal_nodes=find_term_nodes(tree_data); + //arma::vec arma_terminal_nodes=Rcpp::as(terminal_nodes); + //NumericVector tree_predictions; + + //now for each internal node find the observations that belong to the terminal nodes + + //NumericVector predictions(test_data.nrow()); + //List term_obs(terminal_nodes.size()); + arma::field term_obsF(terminal_nodes.size()); + + if(terminal_nodes.size()==1){ + //double nodemean=tree_data(terminal_nodes[0]-1,5); // let nodemean equal tree_data row terminal_nodes[i]^th row , 6th column. The minus 1 is because terminal nodes consists of indices starting at 1, but need indices to start at 0. + //predictions=rep(nodemean,test_data.nrow()); + IntegerVector temp_obsvec = seq_len(test_data.nrow())-1; + term_obsF(0)= Rcpp::as(temp_obsvec); + } + else{ + for(int i=0;i temp_split); + } + + arma::uvec temp_pred_indices; + arma::vec data_subset = testd.col(split); + data_subset=data_subset.elem(pred_indices); + + //now loop through each row of node_split_mat + int n=node_split_mat.n_rows; + + for(int j=1;j split_p); + } + pred_indices=pred_indices.elem(temp_pred_indices); + + if(pred_indices.size()==0){ + continue; + } + + } + + //double nodemean=tree_data(terminal_nodes[i]-1,5); + //IntegerVector predind=as(wrap(arma::conv_to::from((pred_indices))); + //predictions[predind]= nodemean; + //term_obsF(i)=Rcpp::as(predind); + term_obsF(i)=pred_indices; + } + } + //List ret(1); + //ret[0] = term_obs; + + //ret[0] = terminal_nodes; + //ret[1] = term_obs; + //ret[2] = predictions; + return(term_obsF); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +List get_termobs_testdata_overall(List overall_sum_trees,NumericMatrix test_data){ + + //List overall_term_nodes_trees(overall_sum_trees.size()); + List overall_term_obs_trees(overall_sum_trees.size()); + //List overall_predictions(overall_sum_trees.size()); + + for(int i=0;i(s)){ + //if current set of trees contains more than one tree...usually does! + List sum_tree=overall_sum_trees[i]; + //save all info in list of list format the same as the trees. + //List term_nodes_trees(sum_tree.size()); + List term_obs_trees(sum_tree.size()); + //NumericMatrix predictions(num_obs,sum_tree.size()); + + for(int k=0;k>> get_termobs_testdata_fields_overall(List overall_sum_trees,NumericMatrix test_data){ + + //List overall_term_nodes_trees(overall_sum_trees.size()); + // List overall_term_obs_trees(overall_sum_trees.size()); + //List overall_predictions(overall_sum_trees.size()); + + arma::field>> OutputF(overall_sum_trees.size()) ; + + for(int i=0;i(s)){ + //if current set of trees contains more than one tree...usually does! + List sum_tree=overall_sum_trees[i]; + //save all info in list of list format the same as the trees. + //List term_nodes_trees(sum_tree.size()); + //List term_obs_trees(sum_tree.size()); + arma::field> term_obs_treesF(sum_tree.size()); + //NumericMatrix predictions(num_obs,sum_tree.size()); + + for(int k=0;k tree_infoF=get_termobs_test_data_fields(test_data, tree_table) ; + //NumericVector term_nodes=tree_info[0]; + //term_nodes_trees[k]=term_nodes; + term_obs_treesF(k)=tree_infoF; + //umericVector term_preds=tree_info[2]; + //predictions(_,k)=term_preds; + } + //overall_term_nodes_trees[i]=term_nodes_trees; + OutputF(i)= term_obs_treesF; + //overall_predictions[i]=predictions; + }else{ + NumericMatrix sum_tree=overall_sum_trees[i]; + arma::field tree_infoF=get_termobs_test_data_fields(test_data, sum_tree) ; + //overall_term_nodes_trees[i]=tree_info[0]; + //List term_obs_trees(1); + arma::field> term_obs_treesF(1); + term_obs_treesF(0)=tree_infoF ; + //NumericVector term_preds=tree_info[2]; + //NumericVector predictions=term_preds; + OutputF(i)= term_obs_treesF; + //overall_predictions[i]=predictions; + } + } + //List ret(1); + //ret[0]=overall_term_nodes_trees; + //ret[0]=overall_term_obs_trees; + //ret[2]=overall_predictions; + return(OutputF); +} +//###########################################################################################################################// +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +arma::mat get_J_test(List curr_termobs,NumericVector tree_term_nodes, int n){ + //this function will make a binary nxb matrix where each column assigns observations to terminal nodes + + //create J matrix with correct dimensions and fill with zeros + arma::mat Jmat(n, tree_term_nodes.size()); + Jmat.zeros(); + + //for each terminal node get the observations associated with it and set column + for(int i=0;i(obs_col); + Jmat.col(i)= colmat; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(i)= colmat; + } + return(Jmat); +} + +//###########################################################################################################################// +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +arma::mat get_W_test(List sum_treetable , + List termobs_testdata_onemodel, + int n){ + //this will take in a list of obs to node matrices for each tree in the sum make the J matrix assigning observations to terminal nodes + //J is an nxb_j matrix. It will then iteratively append the J matrix to itself to get the overall W matrix which has dimensions nxsumb_j + + //create empty matrix to which we will append individual J matrices + arma::mat W(n,0); + int upsilon=0; + for(int j=0;j(y_scaled); + arma::mat y_arma(num_obs,1); + y_arma.col(0)=yvec; + //get exponent + //double expon=(n+nu)/2; + //get y^Tpsi^{-1}y + // arma::mat psi_inv=psi.i(); + + + //arma::mat yty=y_arma.t()*y_arma; + + //get t(y)inv(psi)J + //arma::mat ytW=y_arma.t()*Wmat; + + + //get t(J)inv(psi)J + arma::mat WtW=Wmat.t()*Wmat; + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + //arma::mat sec_term_inv=sec_term.i(); + arma::mat sec_term_inv=inv_sympd(sec_term); + //get t(J)inv(psi)y + arma::mat third_term=Wmat.t()*y_arma; + + //get m^TV^{-1}m + //arma::mat mvm= ytW*sec_term_inv*third_term; + + arma::vec preds_temp_arma= W_tilde*sec_term_inv*third_term; + + + + + + + + + double weight=exp(BICi[i]-(max_BIC+log(sum(exp(BICi-max_BIC))))); + + preds_all_models_arma.col(i)=preds_temp_arma*weight; + + + } + + //arma::colvec predicted_values; + + //arma::mat M1(preds_all_models.begin(), preds_all_models.nrow(), preds_all_models.ncol(), false); + arma::colvec predicted_values=sum(preds_all_models_arma,1); + + + NumericVector orig_preds=get_original(min(y),max(y),-0.5,0.5,wrap(predicted_values)) ; + + + + return(orig_preds); +} + +//###########################################################################################################################// + + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +NumericVector preds_bbma_lin_alg_insamp(List overall_sum_trees,List overall_sum_mat, + NumericVector y,NumericVector BIC_weights, + int num_iter,int burnin,int num_obs, + double a,double sigma,double mu_mu,double nu, + double lambda//,List resids +){ + + NumericVector y_scaled=scale_response(min(y),max(y),-0.5,0.5,y); + + //List termobs_testdata_overall= get_termobs_testdata_overall(overall_sum_trees,test_data); + //NumericMatrix preds_all_models(num_test_obs,BIC_weights.size()); + arma::mat preds_all_models_arma(num_obs,BIC_weights.size()); + + // for all sums of trees + + NumericVector BICi=-0.5*BIC_weights; + double max_BIC=max(BICi); + + for(int i=0;i(y_scaled); + arma::mat y_arma(num_obs,1); + y_arma.col(0)=yvec; + //get exponent + //double expon=(n+nu)/2; + //get y^Tpsi^{-1}y + // arma::mat psi_inv=psi.i(); + + + //arma::mat yty=y_arma.t()*y_arma; + + //get t(y)inv(psi)J + //arma::mat ytW=y_arma.t()*Wmat; + + + //get t(J)inv(psi)J + arma::mat WtW=Wmat.t()*Wmat; + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + //arma::mat sec_term_inv=sec_term.i(); + arma::mat sec_term_inv=inv_sympd(sec_term); + //get t(J)inv(psi)y + arma::mat third_term=Wmat.t()*y_arma; + + //get m^TV^{-1}m + //arma::mat mvm= ytW*sec_term_inv*third_term; + + arma::vec preds_temp_arma= Wmat*sec_term_inv*third_term; + + + + + + + + + double weight=exp(BICi[i]-(max_BIC+log(sum(exp(BICi-max_BIC))))); + + preds_all_models_arma.col(i)=preds_temp_arma*weight; + + + } + + //arma::colvec predicted_values; + + //arma::mat M1(preds_all_models.begin(), preds_all_models.nrow(), preds_all_models.ncol(), false); + arma::colvec predicted_values=sum(preds_all_models_arma,1); + + NumericVector orig_preds=get_original(min(y),max(y),-0.5,0.5,wrap(predicted_values)) ; + + + + //return(wrap(predicted_values)); + return(orig_preds); +} + + + +//###########################################################################################################################// + + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List mean_vars_lin_alg_outsamp(List overall_sum_trees, + List overall_sum_mat, + NumericVector y, + NumericVector BIC_weights, + int num_iter,int burnin,int num_obs,int num_test_obs, + double a,double sigma,double mu_mu,double nu, + double lambda,//List resids, + NumericMatrix test_data){ + + NumericVector y_scaled=scale_response(min(y),max(y),-0.5,0.5,y); + + List termobs_testdata_overall= get_termobs_testdata_overall(overall_sum_trees,test_data); + //NumericMatrix preds_all_models(num_test_obs,BIC_weights.size()); + arma::mat preds_all_models_arma(num_test_obs,BIC_weights.size()); + arma::mat weighted_preds_all_models_arma(num_test_obs,BIC_weights.size()); + // for all sums of trees + + NumericVector BICi=-0.5*BIC_weights; + double max_BIC=max(BICi); + + + List covar_matrices(BIC_weights.size()); + NumericVector model_weights(BIC_weights.size()); + + for(int i=0;i(y_scaled); + arma::mat y_arma(num_obs,1); + y_arma.col(0)=yvec; + //get exponent + //double expon=(n+nu)/2; + //get y^Tpsi^{-1}y + // arma::mat psi_inv=psi.i(); + + + arma::mat yty=y_arma.t()*y_arma; + + //get t(y)inv(psi)J + arma::mat ytW=y_arma.t()*Wmat; + + + //get t(J)inv(psi)J + arma::mat WtW=Wmat.t()*Wmat; + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + //arma::mat sec_term_inv=sec_term.i(); + arma::mat sec_term_inv=inv_sympd(sec_term); + //get t(J)inv(psi)y + arma::mat third_term=Wmat.t()*y_arma; + + //get m^TV^{-1}m + arma::mat mvm= ytW*sec_term_inv*third_term; + + arma::mat w_tilde_M_inv = W_tilde*sec_term_inv; + + //Rcout << "Line 4151"; + + //arma::vec preds_temp_arma= w_tilde_M_inv*third_term; + arma::vec preds_temp_arma= W_tilde*sec_term_inv*(Wmat.t())*y_arma; + + arma::mat I_test(num_test_obs,num_test_obs); + I_test=I_test.eye(); + + arma::mat temp_for_scal = ((nu*lambda+yty-mvm)/(nu+num_obs)); + double temp_scal= as_scalar(temp_for_scal) ; + //Rcout << "Line 4156"; + arma::mat covar_t=temp_scal*(I_test+w_tilde_M_inv*(W_tilde.t())); + + + //Rcout << "Line 4162"; + + + double weight=exp(BICi[i]-(max_BIC+log(sum(exp(BICi-max_BIC))))); + //Rcout << "Line 4167"; + + model_weights[i]=weight; + //Rcout << "Line 4170"; + + weighted_preds_all_models_arma.col(i)=preds_temp_arma*weight; + preds_all_models_arma.col(i)=preds_temp_arma; + //Rcout << "Line 4173"; + + covar_matrices[i]= wrap(covar_t); + //Rcout << "Line 4176"; + + + + + } + + //arma::colvec predicted_values; + + //arma::mat M1(preds_all_models.begin(), preds_all_models.nrow(), preds_all_models.ncol(), false); + arma::colvec predicted_values=sum(weighted_preds_all_models_arma,1); + + + //NumericVector orig_preds=get_original(min(y),max(y),-0.5,0.5,wrap(predicted_values)) ; + + List ret(4); + ret[0]=wrap(predicted_values); + ret[1]=model_weights; + ret[2]=wrap(preds_all_models_arma.t()); + ret[3]=covar_matrices; + + return(ret); +} +//###########################################################################################################################// + + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List mean_vars_lin_alg_insamp(List overall_sum_trees, + List overall_sum_mat, + NumericVector y, + NumericVector BIC_weights, + int num_iter,int burnin,int num_obs,int num_test_obs, + double a,double sigma,double mu_mu,double nu, + double lambda//,List resids, +){ + + NumericVector y_scaled=scale_response(min(y),max(y),-0.5,0.5,y); + + //List termobs_testdata_overall= get_termobs_testdata_overall(overall_sum_trees,test_data); + //NumericMatrix preds_all_models(num_test_obs,BIC_weights.size()); + arma::mat preds_all_models_arma(num_obs,BIC_weights.size()); + arma::mat weighted_preds_all_models_arma(num_test_obs,BIC_weights.size()); + + // for all sums of trees + + NumericVector BICi=-0.5*BIC_weights; + double max_BIC=max(BICi); + + + List covar_matrices(BIC_weights.size()); + NumericVector model_weights(BIC_weights.size()); + + for(int i=0;i(y_scaled); + arma::mat y_arma(num_obs,1); + y_arma.col(0)=yvec; + //get exponent + //double expon=(n+nu)/2; + //get y^Tpsi^{-1}y + // arma::mat psi_inv=psi.i(); + + + arma::mat yty=y_arma.t()*y_arma; + + //get t(y)inv(psi)J + arma::mat ytW=y_arma.t()*Wmat; + + + //get t(J)inv(psi)J + arma::mat WtW=Wmat.t()*Wmat; + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + //arma::mat sec_term_inv=sec_term.i(); + arma::mat sec_term_inv=inv_sympd(sec_term); + //get t(J)inv(psi)y + arma::mat third_term=Wmat.t()*y_arma; + + //get m^TV^{-1}m + arma::mat mvm= ytW*sec_term_inv*third_term; + + + arma::mat W_M_inv = Wmat*sec_term_inv; + arma::vec preds_temp_arma= W_M_inv*third_term; + + //arma::mat I_test(num_test_obs,num_test_obs); + //I_test=I_test.eye(); + + arma::mat covar_t=as_scalar((1/(nu+num_obs))*(nu*lambda+yty-mvm))*(W_M_inv*(Wmat.t())); + + + + + double weight=exp(BICi[i]-(max_BIC+log(sum(exp(BICi-max_BIC))))); + + model_weights[i]=weight; + weighted_preds_all_models_arma.col(i)=preds_temp_arma*weight; + preds_all_models_arma.col(i)=preds_temp_arma; + covar_matrices[i]= covar_t; + + + + + } + + //arma::colvec predicted_values; + + //arma::mat M1(preds_all_models.begin(), preds_all_models.nrow(), preds_all_models.ncol(), false); + arma::colvec predicted_values=sum(weighted_preds_all_models_arma,1); + + + //NumericVector orig_preds=get_original(min(y),max(y),-0.5,0.5,wrap(predicted_values)) ; + + List ret(4); + ret[0]=wrap(predicted_values); + ret[1]=model_weights; + ret[2]=wrap(preds_all_models_arma.t()); + ret[3]=covar_matrices; + + return(ret); +} + + +//###########################################################################################################################// + + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List BART_BMA_sumLikelihood(double less_greedy,double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, + NumericMatrix data,NumericVector y,double start_mean,double start_sd, + double a,double mu,double nu,double lambda,double c, + double sigma_mu,double pen,int num_cp,NumericMatrix test_data,int num_rounds, + double alpha,double beta,bool split_rule_node,bool gridpoint,int maxOWsize,int num_splits,int gridsize, bool zero_split,bool only_max_num_trees, + unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split, int exact_residuals){ + + + bool is_test_data=0; + if(test_data.nrow()>0){ + is_test_data=1; + } + if(y.size() !=data.nrow()){ + if(y.size()0 && (data.ncol() != test_data.ncol())){ + throw std::range_error("Test data and training data must have the same number of variables. BART BMA assumes variables are in the same order."); + } + + //check value of c is greater than 1! + // if(c<1){ + // throw std::range_error("Value of Occam's Window has to be greater than 0."); + // } + if(num_cp<0 || num_cp>100){ + throw std::range_error("Value of num_cp should be a value between 1 and 100."); + } + //NumericMatrix treetable=start_tree(mu,sigma_mu); + NumericMatrix treetable=start_tree2(); + NumericMatrix treemat=start_matrix(data.nrow()); + //initialize the tree table and matrix + NumericVector y_scaled=scale_response(min(y),max(y),-0.5,0.5,y); + //initialize the tree table and matrix + arma::mat D1(data.begin(), data.nrow(), data.ncol(), false); + double n=D1.n_rows; + // double lik=likelihood_function(y_scaled,treetable,treemat,a,mu,nu,lambda); + double lik=as(likelihood_function2_exact(y_scaled,treetable,treemat,a,mu,nu,lambda)[0]); + double tree_prior=get_tree_prior(spike_tree, s_t_hyperprior, p_s_t, a_s_t,b_s_t,num_obs,num_vars,lambda_poisson,treetable,treemat,alpha,beta); + //double lowest_BIC=-2*(lik+log(tree_prior))+1*log(n); + double lowest_BIC=-2*(lik+log(tree_prior)); + // Rcout << "Initial lowest BIC = " << lowest_BIC << ".\n"; + List best_subset; + List tree_table; + List tree_mat; + tree_table.push_back(treetable); + tree_mat.push_back(treemat); + List CART_BMA; + arma::mat r; + arma::colvec yarma=clone(y_scaled); + r.insert_cols(0,yarma); + NumericMatrix resids=wrap(r); + //int first_round; + int first_round_break=0; + + //List overall_trees(num_rounds); + List overall_mat; + List overall_lik; + NumericMatrix prev_round_preds; + NumericVector prev_round_BIC; + NumericVector prev_round_BIC2; + arma::mat prev_round_preds2; + NumericMatrix prev_round_test_preds; + arma::mat prev_round_test_preds2; + arma::mat overall_overall_sum_test_preds; + arma::colvec predicted_test_values; + List prev_sum_trees; + List prev_sum_tree_resids; + List prev_sum_trees_mat; + List cp_mat_list; + //int oo_size=300; + //List overall_overall_sum_trees(oo_size); + //List overall_overall_sum_tree_resids(oo_size); + //List overall_overall_sum_trees_mat(oo_size); + //List overall_overall_sum_BIC(oo_size); + + List overall_overall_sum_trees; + List overall_overall_sum_tree_resids; + List overall_overall_sum_trees_mat; + NumericVector overall_overall_sum_BIC; + + //int oo_count=0; + arma::mat overall_overall_sum_preds; + IntegerVector prev_par; + arma::colvec predicted_values; + + for(int j=0;j0){ + if(only_max_num_trees==1){ + lowest_BIC=100000; + } + } + int overall_size=300; + List overall_sum_trees(overall_size); + List overall_sum_trees_mat(overall_size); + List overall_sum_tree_resids(overall_size); + int overall_count=0; + IntegerVector parent; + NumericVector curr_round_lik; + List curr_round_trees; + List curr_round_mat; + NumericVector curr_BIC; + IntegerVector curr_round_parent; + NumericVector overall_sum_BIC; + arma::mat overall_sum_preds; + arma::mat overall_sum_test_preds; + + if(j==0){ + parent.push_back(0); + //first_round=1; + }//else{ + // first_round=0; + //} + //// Rcout << "resids = " << resids << ".\n"; + + List resids_cp_mat(resids.ncol()); + int resids_count=0; + std::vector err_list(resids.ncol()); + //get best splits + for(int f=0;f(tempresidvec),gridsize,num_cp); + + } + resids_cp_mat[resids_count]=cp_mat_list[0]; + err_list[resids_count]=cp_mat_list[1]; + resids_count++; + } + resids_cp_mat=resize(resids_cp_mat,resids_count); + err_list.resize(resids_count); + parent=seq_len(tree_table.size())-1; + if(is_true(all(as(wrap(err_list))==1))){ + if(j==0){ + throw std::range_error("No split points could be found to grow trees"); + }else{ + throw std::range_error("No trees can be grown for the number of iterations desired, as no splits were found.Please try fewer iterations."); + } + } + + //Rcout << "Get to before get_best_trees in outer loop number " << j << " . \n"; + + + if(exact_residuals==1){ + //get current set of trees. + if(j==0){ + if(split_rule_node==1){ + CART_BMA=get_best_trees_update_splits_exact(less_greedy,spike_tree, s_t_hyperprior, p_s_t, a_s_t,b_s_t,num_obs,num_vars,lambda_poisson,D1, resids, a,mu,nu,lambda,c,sigma_mu,tree_table,tree_mat,lowest_BIC,//first_round, + parent,resids_cp_mat,//as(wrap(err_list)), + test_data,alpha,beta,is_test_data,pen,num_cp,split_rule_node,gridpoint,maxOWsize,num_splits,gridsize,zero_split, + min_num_obs_for_split, min_num_obs_after_split); + + }else{ + CART_BMA=get_best_trees_exact(less_greedy,spike_tree, s_t_hyperprior, p_s_t, a_s_t,b_s_t,num_obs,num_vars,lambda_poisson,D1, resids, a,mu,nu,lambda,c,sigma_mu,tree_table,tree_mat,lowest_BIC,//first_round, + parent,resids_cp_mat,//as(wrap(err_list)), + test_data,alpha,beta,is_test_data,pen,num_cp,split_rule_node,gridpoint,maxOWsize,num_splits,gridsize,zero_split, + min_num_obs_for_split, min_num_obs_after_split); + } + + }else{ + + // Rcout << "Line 9426. Length of tree_table = " << tree_table.size() << " . \n"; + // Rcout << "Line 9427. Length of prev_sum_trees = " << prev_sum_trees.size() << " . \n"; + + //if j >0 then sum of trees become a list so need to read in list and get likelihood for each split point and terminal node + if(split_rule_node==1){ + CART_BMA=get_best_trees_sum_update_splits_exact(less_greedy,spike_tree, s_t_hyperprior, p_s_t, a_s_t,b_s_t,num_obs,num_vars,lambda_poisson,D1, resids, a,mu,nu,lambda,c,sigma_mu,tree_table,tree_mat,lowest_BIC,//first_round, + parent,resids_cp_mat,as(wrap(err_list)),test_data,alpha,beta,is_test_data,pen,num_cp,split_rule_node,gridpoint,maxOWsize,prev_sum_trees,prev_sum_trees_mat,y_scaled,num_splits,gridsize,zero_split, + min_num_obs_for_split, min_num_obs_after_split); + + + }else{ + CART_BMA=get_best_trees_sum_exact(less_greedy,spike_tree, s_t_hyperprior, p_s_t, a_s_t,b_s_t,num_obs,num_vars,lambda_poisson,D1, resids, a,mu,nu,lambda,c,sigma_mu,tree_table,tree_mat,lowest_BIC,//first_round, + parent,resids_cp_mat,as(wrap(err_list)),test_data,alpha,beta,is_test_data,pen,num_cp,split_rule_node,gridpoint,maxOWsize,prev_sum_trees,prev_sum_trees_mat,y_scaled,num_splits,gridsize,zero_split, + min_num_obs_for_split, min_num_obs_after_split); + } + } + }else{ + if(j==0){ + if(split_rule_node==1){ + CART_BMA=get_best_trees_update_splits(less_greedy,spike_tree, s_t_hyperprior, p_s_t, a_s_t,b_s_t,num_obs,num_vars,lambda_poisson,D1, resids, a,mu,nu,lambda,c,sigma_mu,tree_table,tree_mat,lowest_BIC,//first_round, + parent,resids_cp_mat,//as(wrap(err_list)), + test_data,alpha,beta,is_test_data,pen,num_cp,split_rule_node,gridpoint,maxOWsize,num_splits,gridsize,zero_split, + min_num_obs_for_split, min_num_obs_after_split); + + }else{ + CART_BMA=get_best_trees(less_greedy,spike_tree, s_t_hyperprior, p_s_t, a_s_t,b_s_t,num_obs,num_vars,lambda_poisson,D1, resids, a,mu,nu,lambda,c,sigma_mu,tree_table,tree_mat,lowest_BIC,//first_round, + parent,resids_cp_mat,//as(wrap(err_list)), + test_data,alpha,beta,is_test_data,pen,num_cp,split_rule_node,gridpoint,maxOWsize,num_splits,gridsize,zero_split, + min_num_obs_for_split, min_num_obs_after_split); + } + + }else{ + + // Rcout << "Line 9459. Length of tree_table = " << tree_table.size() << " . \n"; + // Rcout << "Line 9460. Length of prev_sum_trees = " << prev_sum_trees.size() << " . \n"; + + //if j >0 then sum of trees become a list so need to read in list and get likelihood for each split point and terminal node + if(split_rule_node==1){ + CART_BMA=get_best_trees_sum_update_splits(less_greedy,spike_tree, s_t_hyperprior, p_s_t, a_s_t,b_s_t,num_obs,num_vars,lambda_poisson,D1, resids, a,mu,nu,lambda,c,sigma_mu,tree_table,tree_mat,lowest_BIC,//first_round, + parent,resids_cp_mat,as(wrap(err_list)),test_data,alpha,beta,is_test_data,pen,num_cp,split_rule_node,gridpoint,maxOWsize,prev_sum_trees,prev_sum_trees_mat,y_scaled,num_splits,gridsize,zero_split, + min_num_obs_for_split, min_num_obs_after_split); + + + }else{ + CART_BMA=get_best_trees_sum(less_greedy,spike_tree, s_t_hyperprior, p_s_t, a_s_t,b_s_t,num_obs,num_vars,lambda_poisson,D1, resids, a,mu,nu,lambda,c,sigma_mu,tree_table,tree_mat,lowest_BIC,//first_round, + parent,resids_cp_mat,as(wrap(err_list)),test_data,alpha,beta,is_test_data,pen,num_cp,split_rule_node,gridpoint,maxOWsize,prev_sum_trees,prev_sum_trees_mat,y_scaled,num_splits,gridsize,zero_split, + min_num_obs_for_split, min_num_obs_after_split); + } + } + } + + + + //Rcout << "Get past get_best_trees in outer loop number " << j << " . \n"; + + curr_round_lik=CART_BMA[0]; + + // Rcout << "Line 9625 outer loop number " << j << " . \n"; + + curr_round_trees=CART_BMA[1]; + //// Rcout << "Line 9628 outer loop number " << j << " . \n"; + curr_round_mat=CART_BMA[2]; + //Rcout << "Line 9630 outer loop number " << j << " . \n"; + curr_round_parent=CART_BMA[3]; + //Rcout << "Line 9632 outer loop number " << j << " . \n"; + NumericMatrix curr_round_preds=CART_BMA[4]; + //Rcout << "Line 9634 outer loop number " << j << " . \n"; + NumericMatrix curr_round_test_preds=CART_BMA[6]; + //Rcout << "Line 9636 outer loop number " << j << " . \n"; + curr_BIC=CART_BMA[5]; + // Rcout << "Line 9638 outer loop number " << j << " . \n"; + + //Predictions from whole sum-of-tree models + //Matrix dimensions: number of training observations by number of models + //Each column is a separate model + NumericMatrix curr_true_preds; + if(exact_residuals==1){ + NumericMatrix temp_truepredmat = CART_BMA[7]; + curr_true_preds=temp_truepredmat; + } + + // Rcout << "Line 9640 outer loop number " << j << " . \n"; + + // + //Rcout << "curr_round_preds = " << curr_round_preds << ".\n"; + + //Rcout << "curr_true_preds = " << curr_true_preds << ".\n"; + + if(curr_round_lik.size()==0) { + //Rcout << "Break because curr_round_lik.size()==0 in outer loop number " << j << " . \n"; + if(j==0){ + first_round_break=1; + }else{ + overall_overall_sum_trees=prev_sum_trees; + overall_overall_sum_trees_mat=prev_sum_trees_mat; + overall_overall_sum_tree_resids=prev_sum_tree_resids; + overall_overall_sum_BIC=prev_round_BIC2; + overall_overall_sum_preds=prev_round_preds2; + if(is_test_data==1) overall_overall_sum_test_preds= prev_round_test_preds2; + } + + break; + } + + if(curr_BIC[0](other_tree[f])){ + }else{ + throw std::range_error(" tree is not a numeric matrix!"); + } + NumericMatrix treetoadd=other_tree[f]; + if(is(other_tree_resids[f])){ + }else{ + throw std::range_error("other resids not a numeric matrix!"); + } + NumericVector resids_prevroundtemp=other_tree_resids[f]; + NumericVector residstoadd=resids_prevroundtemp-curr_round_preds(_,k); + + if(is(other_mat[f])){ + + }else{ + throw std::range_error(" other mat not a numeric matrix!"); + } + NumericMatrix mattoadd=other_mat[f]; + sum_of_trees[count]=treetoadd; + sum_of_tree_resids[count]=residstoadd; + sum_of_trees_mat[count]=mattoadd; + count++; + + if(count==(size_mat-1)){ + size_mat=size_mat*2; + sum_of_trees=resize_bigger(sum_of_trees,size_mat); + sum_of_tree_resids=resize_bigger(sum_of_tree_resids,size_mat); + sum_of_trees_mat=resize_bigger(sum_of_trees_mat,size_mat); + } + } + } + sum_of_trees[count]=temp_sum_trees[k]; + sum_of_tree_resids[count]=temp_sum_tree_resids[k]; + sum_of_trees_mat[count]=temp_sum_trees_mat[k]; + count++; + + if(count==(size_mat-1)){ + size_mat=size_mat*2; + sum_of_trees=resize_bigger(sum_of_trees,size_mat); + sum_of_trees_mat=resize_bigger(sum_of_trees_mat,size_mat); + sum_of_tree_resids=resize_bigger(sum_of_tree_resids,size_mat); + } + } + sum_of_trees=resize(sum_of_trees,count); + sum_of_trees_mat=resize(sum_of_trees_mat,count); + sum_of_tree_resids=resize(sum_of_tree_resids,count); + + if(curr_round_parent[k]!=-1){ + overall_sum_trees[overall_count]=sum_of_trees; + overall_sum_tree_resids[overall_count]=sum_of_tree_resids; + overall_sum_trees_mat[overall_count]=sum_of_trees_mat; + overall_sum_BIC=temp_BIC; + overall_sum_preds=Rcpp::as(temp_preds); + if(is_test_data==1) overall_sum_test_preds=Rcpp::as(temp_test_preds); + overall_count++; + if(overall_count==(overall_size-1)){ + overall_size=overall_size*2; + overall_sum_trees=resize_bigger(overall_sum_trees,overall_size); + overall_sum_tree_resids=resize_bigger(overall_sum_tree_resids,overall_size); + overall_sum_trees_mat=resize_bigger(overall_sum_trees_mat,overall_size); + } + } + } + + + //// Rcout << "Line 4990 curr_round_lik.size()=" << curr_round_lik.size() << ".\n"; + //// Rcout << "Line 4991 overall_sum_trees.size()=" << overall_sum_trees.size() << ".\n"; + //// Rcout << "Line 4992 overall_count=" << overall_count << ".\n"; + // Rcout << "Line 9841. j= " << j << " . \n"; + + + if(only_max_num_trees==0){ + //Rcout << "Get to checking for models from previous rounds in outer loop number " << j << " . \n"; + //check if there were any trees from the previous round that didn't have daughter trees grown. + //create vector to count number of possible parents for previous round + if(j>0){ + IntegerVector prev_par_no_child=match(prev_par,curr_round_parent); + if(any(is_na(prev_par_no_child))){ + IntegerVector t4=ifelse(is_na(prev_par_no_child),1,0); + for(int h=0;h(s)){ + List tree_no_child=prev_sum_trees[h]; + List resids_no_child=prev_sum_tree_resids[h]; + List treemat_no_child=prev_sum_trees_mat[h]; + overall_sum_trees[overall_count]=tree_no_child; + overall_sum_tree_resids[overall_count]=resids_no_child; + overall_sum_trees_mat[overall_count]=treemat_no_child; + overall_count++; + + if(overall_count==(overall_size-1)){ + overall_size=overall_size*2; + overall_sum_trees=resize_bigger(overall_sum_trees,overall_size); + overall_sum_tree_resids=resize_bigger(overall_sum_tree_resids,overall_size); + overall_sum_trees_mat=resize_bigger(overall_sum_trees_mat,overall_size); + } + double BIC_to_add=prev_round_BIC2[h]; + overall_sum_BIC.push_back(BIC_to_add); + overall_sum_preds.insert_cols(overall_sum_preds.n_cols,prev_round_preds2.col(h)); + if(is_test_data==1) overall_sum_test_preds.insert_cols(overall_sum_test_preds.n_cols,prev_round_test_preds2.col(h)); + }else{ + NumericMatrix tree_no_child=prev_sum_trees[h]; + NumericVector resids_no_child= prev_sum_tree_resids[h]; + NumericMatrix treemat_no_child=prev_sum_trees_mat[h]; + overall_sum_trees[overall_count]=tree_no_child; + overall_sum_tree_resids[overall_count]=resids_no_child; + overall_sum_trees_mat[overall_count]=treemat_no_child; + overall_count++; + + if(overall_count==(overall_size-1)){ + overall_size=overall_size*2; + overall_sum_trees=resize_bigger(overall_sum_trees,overall_size); + overall_sum_tree_resids=resize_bigger(overall_sum_tree_resids,overall_size); + overall_sum_trees_mat=resize_bigger(overall_sum_trees_mat,overall_size); + } + + double BIC_to_add=prev_round_BIC2[h]; + overall_sum_BIC.push_back(BIC_to_add); + + overall_sum_preds.insert_cols(overall_sum_preds.n_cols,prev_round_preds2.col(h)); + if(is_test_data==1) overall_sum_test_preds.insert_cols(overall_sum_test_preds.n_cols,prev_round_test_preds2.col(h)); + } + } + } + } + } + } + // Rcout << "Get to end of checking for models from previous rounds in outer loop number " << j << " . \n"; + + } + + + //Rcout << "Line 2828 curr_round_lik.size()=" << curr_round_lik.size() << ".\n"; + //Rcout << "Line 2828 overall_sum_trees.size()=" << overall_sum_trees.size() << ".\n"; + //Rcout << "Line 2828 overall_count=" << overall_count << ".\n"; + + //Rcout << "temp_preds = " << temp_preds << ".\n"; + // Rcout << "Line 9912. j= " << j << " . \n"; + + prev_round_preds=temp_preds; + if(is_test_data==1) prev_round_test_preds=temp_test_preds; + prev_round_BIC=temp_BIC; + prev_round_BIC2=temp_BIC; + prev_round_preds2=Rcpp::as(temp_preds); + if(is_test_data==1) prev_round_test_preds2=Rcpp::as(temp_test_preds); + resids=temp_resids; + parent=temp_parent; + overall_sum_trees=resize(overall_sum_trees,overall_count); + overall_sum_tree_resids=resize(overall_sum_tree_resids,overall_count); + overall_sum_trees_mat=resize(overall_sum_trees_mat,overall_count); + + // Rcout << "Line 9926. j= " << j << " . \n"; + + if(j==0){ + prev_sum_trees=temp_sum_trees; + prev_sum_tree_resids=temp_sum_tree_resids; + //NumericMatrix test=prev_sum_trees[0]; + prev_sum_trees_mat=temp_sum_trees_mat; + //overall_sum_trees=resize(temp_sum_trees,temp_sum_trees.size()); + overall_sum_trees=temp_sum_trees; + //overall_sum_tree_resids=resize(temp_sum_tree_resids,temp_sum_tree_resids.size()); + overall_sum_tree_resids=temp_sum_tree_resids; + overall_sum_trees_mat=temp_sum_trees_mat; + //overall_sum_trees_mat=resize(temp_sum_trees_mat,temp_sum_trees.size()); + overall_sum_BIC=temp_BIC; + overall_sum_preds= Rcpp::as(temp_preds); + if(is_test_data==1) overall_sum_test_preds= Rcpp::as(temp_test_preds); + }else{ + + //Rcout << "Line 2856. Length of tree_table = " << tree_table.size() << " . \n"; + //Rcout << "Line 2857. Length of prev_sum_trees = " << prev_sum_trees.size() << " . \n"; + //Rcout << "Line 2858. Length of overall_sum_trees = " << overall_sum_trees.size() << " . \n"; + + + prev_sum_trees=overall_sum_trees; + prev_sum_tree_resids=overall_sum_tree_resids; + prev_sum_trees_mat=overall_sum_trees_mat; + prev_round_BIC2=overall_sum_BIC; + prev_round_preds2=overall_sum_preds; + if(is_test_data==1) prev_round_test_preds2=overall_sum_test_preds; + } + // overall_overall_sum_trees[oo_count]=overall_sum_trees; + // overall_overall_sum_tree_resids[oo_count]=overall_sum_tree_resids; + // overall_overall_sum_trees_mat[oo_count]=overall_sum_trees_mat; + // overall_overall_sum_BIC[oo_count]=overall_sum_BIC; + // oo_count ++; + // if(oo_count==(oo_size-1)){ + // oo_size=oo_size*2; + // overall_overall_sum_trees=resize_bigger(overall_overall_sum_trees,oo_size); + // overall_overall_sum_tree_resids=resize_bigger(overall_overall_sum_tree_resids,oo_size); + // overall_overall_sum_trees_mat=resize_bigger(overall_overall_sum_trees_mat,oo_size); + // overall_overall_sum_BIC=resize_bigger(overall_overall_sum_BIC,oo_size); + // } + if(j==num_rounds-1){ + overall_overall_sum_trees=overall_sum_trees; + overall_overall_sum_tree_resids=overall_sum_tree_resids; + overall_overall_sum_trees_mat=overall_sum_trees_mat; + overall_overall_sum_BIC=overall_sum_BIC; + overall_overall_sum_preds=overall_sum_preds; + if(is_test_data==1) overall_overall_sum_test_preds=overall_sum_test_preds; + } + + //overall_trees[j]=curr_round_trees; + //overall_mat.push_back(curr_round_mat); + overall_lik.push_back(curr_round_lik); + prev_par=seq_len(overall_sum_trees.size())-1; + // Rcout << "Get to end of outer loop number " << j << " . \n"; + + } + // Rcout << "Get past outer loop \n"; + + if(first_round_break==1){ + throw std::range_error("BART-BMA didnt find any suitable model for the data. Maybe limit for Occam's window is too small. Alternatively, try using more observations or change parameter values."); + } + + //overall_overall_sum_trees=resize(overall_overall_sum_trees,oo_count); + //overall_overall_sum_tree_resids=resize(overall_overall_sum_tree_resids,oo_count); + //overall_overall_sum_trees_mat=resize(overall_overall_sum_trees_mat,oo_count); + //overall_overall_sum_BIC=resize(overall_overall_sum_BIC,oo_count); + + + + // Rcout << "Get to defining end_BIC \n"; + //NumericVector end_BIC=overall_overall_sum_BIC[overall_overall_sum_BIC.size()-1] ; + NumericVector end_BIC=overall_overall_sum_BIC ; + // + // + // //Rcout << "Get to defining past defining end_BIC \n"; + // NumericMatrix overallpreds(n,end_BIC.size()); + // NumericMatrix overall_test_preds(test_data.nrow(),end_BIC.size()); + // NumericVector post_weights(end_BIC.size()); + // //Rcout << "Get to loop that defines temp_pred M1 \n"; + // for(int k=0;k(wrap(overall_overall_sum_preds)); + // NumericVector temp_preds=oosp(_,k); + // NumericVector temp_test_preds; + // if(is_test_data==1){ + // NumericMatrix oostp=Rcpp::as(wrap(overall_overall_sum_test_preds)); + // temp_test_preds=oostp(_,k); + // } + // //Rcout << "Get to defining orig_temp_pred \n"; + // NumericVector orig_temp_preds=get_original(min(y),max(y),-0.5,0.5,temp_preds) ; + // NumericVector BICi=-0.5*end_BIC; + // double max_BIC=max(BICi); + // double weight=exp(BICi[k]-(max_BIC+log(sum(exp(BICi-max_BIC))))); + // post_weights[k]=weight; + // overallpreds(_,k) = temp_preds*weight; + // if(is_test_data==1){ + // overall_test_preds(_,k) = temp_test_preds*weight; + // } + // } + // //Rcout << "Get to defining M1 \n"; + // arma::mat M1(overallpreds.begin(), overallpreds.nrow(), overallpreds.ncol(), false); + // predicted_values=sum(M1,1); + // arma::mat M2(overall_test_preds.begin(), overall_test_preds.nrow(), overall_test_preds.ncol(), false); + // + //if(is_test_data==1) predicted_test_values=sum(M2,1); + if(overall_lik.size()==0){ + throw std::range_error("BART-BMA didnt find any suitable model for the data. Maybe limit for Occam's window is too small."); + }else{ + + //NumericVector orig_preds=get_original(min(y),max(y),-0.5,0.5,wrap(predicted_values)) ; + //NumericVector orig_test_preds; + //if(is_test_data==1){ + // orig_test_preds=get_original(min(y),max(y),-0.5,0.5,wrap(predicted_test_values)) ; + //} + + NumericVector orig_preds=preds_bbma_lin_alg_insamp(overall_overall_sum_trees, + overall_overall_sum_trees_mat, + y, + end_BIC, + 100, + 10, + y.size() , + a, + 0, + 0, + nu, + lambda//,List resids + ); + + NumericVector orig_test_preds; + if(is_test_data==1){ + orig_test_preds=preds_bbma_lin_alg_outsamp(overall_overall_sum_trees, + overall_overall_sum_trees_mat, + y, + end_BIC, + 100, + 10, + y.size() , + test_data.nrow(), + a, + 0, 0, nu, + lambda,//List resids, + test_data); + } + + //NumericVector minmax(2); + //minmax[0]=min(y); + //minmax[1]=max(y); + // Rcout << "Get to defining ret \n"; + if(is_test_data==1){ + List ret(6); + ret[0] = orig_preds; + ret[1] = overall_overall_sum_trees; + ret[2] =overall_overall_sum_trees_mat; + ret[3] = end_BIC; + ret[4] = orig_test_preds; + ret[5] =overall_overall_sum_tree_resids; + return(ret); + }else{ + List ret(5); + ret[0] = orig_preds; + ret[1] = overall_overall_sum_trees; + ret[2] = overall_overall_sum_trees_mat; + ret[3] = end_BIC; + ret[4] =overall_overall_sum_tree_resids; + return(ret); + } + } +} + + +//###########################################################################################################################// + +// [[Rcpp::export]] +Rcpp::NumericVector Quantile(Rcpp::NumericVector x, Rcpp::NumericVector probs) { + // implementation of type 7 + const size_t n=x.size(), np=probs.size(); + if (n==0) return x; + if (np==0) return probs; + Rcpp::NumericVector index = (n-1.)*probs, y=x.sort(), x_hi(np), qs(np); + Rcpp::NumericVector lo = Rcpp::floor(index), hi = Rcpp::ceiling(index); + Rcpp::NumericVector probs_names=Rcpp::round(probs*100., 6); + Rcpp::CharacterVector qs_names(np); + std::string _name, zero=".000000"; + + for (size_t i=0; i(probs_names[i])); + if(_name=="100.000000") _name=std::string("100"); + else if(_name.substr(1)==zero) _name=_name.substr(0, 1); + else if(_name.substr(2)==zero) _name=_name.substr(0, 2); + else if(_name.substr(3)==zero.substr(2)) _name=_name.substr(0, 3); + else if(_name.substr(4)==zero.substr(3)) _name=_name.substr(0, 4); + else if(_name.substr(5)==zero.substr(4)) _name=_name.substr(0, 5); + else if(_name.substr(6)==zero.substr(5)) _name=_name.substr(0, 6); + else if(_name.substr(7)==zero.substr(6)) _name=_name.substr(0, 7); + else if(_name.substr(4)==zero.substr(2)) _name=_name.substr(0, 4); + else if(_name.substr(5)==zero.substr(3)) _name=_name.substr(0, 5); + else if(_name.substr(6)==zero.substr(4)) _name=_name.substr(0, 6); + else if(_name.substr(7)==zero.substr(5)) _name=_name.substr(0, 7); + else if(_name.substr(8)==zero.substr(6)) _name=_name.substr(0, 8); + /* // why does this not work? + else { + for(size_t j=3; j<8; ++j) { + if(_name.substr(j)==zero.substr(j-1)) _name=_name.substr(0, j); + else if(_name.substr(j+1)==zero.substr(j-1)) _name=_name.substr(0, j+1); + } + } + */ + qs_names[i] = _name + std::string("%"); + qs[i] = y[lo[i]]; + x_hi[i] = y[hi[i]]; + if ((index[i]>lo[i]) && (x_hi[i] != qs[i])) { + double h; + h = index[i]-lo[i]; + qs[i] = (1.-h)*qs[i] + h*x_hi[i]; + } + } + + qs.names()=qs_names; + return qs; + } + + +//###########################################################################################################################// +#include +#include +#include + +template +static inline double Lerp(T v0, T v1, T t) +{ + return (1 - t)*v0 + t*v1; +} + +//###########################################################################################################################// +template +static inline std::vector Quantile2(const std::vector& inData, const std::vector& probs) +{ + // function from https://stackoverflow.com/questions/11964552/finding-quartiles + if (inData.empty()) + { + return std::vector(); + } + + if (1 == inData.size()) + { + return std::vector(1, inData[0]); + } + + std::vector data = inData; + std::sort(data.begin(), data.end()); + std::vector quantiles; + + for (size_t i = 0; i < probs.size(); ++i) + { + T poi = Lerp(-0.5, data.size() - 0.5, probs[i]); + + size_t left = std::max(int64_t(std::floor(poi)), int64_t(0)); + size_t right = std::min(int64_t(std::ceil(poi)), int64_t(data.size() - 1)); + + T datLeft = data.at(left); + T datRight = data.at(right); + + T quantile = Lerp(datLeft, datRight, poi - left); + + quantiles.push_back(quantile); + } + + return quantiles; +} + +//###########################################################################################################################// +#include + +// [[Rcpp::depends(BH)]] +// [[Rcpp::export]] +double mixt_eval_cdf(double x_val, double d_o_f, std::vector mean_vec, std::vector var_vec, std::vector weights_vec, double quant_val) { + + boost::math::students_t dist(d_o_f); + + double ret_val=0; + for(unsigned int i=0; i < weights_vec.size();i++){ + if(var_vec[i]>0){ + double tempx = (x_val-mean_vec[i])/sqrt(var_vec[i]); + ret_val += weights_vec[i]*boost::math::cdf(dist,tempx); + }else{//in some cases (for ITEs) there is zero variance, and can't divide by zero + //Rcout << " \n \n VARIANCE = " << var_vec[i] << ".\n \n" ; + if(x_val>=mean_vec[i]){ //if no variance, cdf is zero below mean, and one above mean + ret_val += weights_vec[i]; + } // no else statement because add zero if below mean (when there is zero variance) + } + } + + return (ret_val-quant_val) ; // approximation +} + +//###########################################################################################################################// +// [[Rcpp::export]] +double rootmixt(double d_o_f, double a, double b, + std::vector mean_vec, + std::vector var_vec, + std::vector weights_vec, double quant_val, double root_alg_precision){ + + static const double EPS = root_alg_precision;//1e-15; // 1×10^(-15) + + double fa = mixt_eval_cdf(a, d_o_f, mean_vec, var_vec, weights_vec,quant_val), fb = mixt_eval_cdf(b, d_o_f, mean_vec, var_vec, weights_vec,quant_val); + + // if either f(a) or f(b) are the root, return that + // nothing else to do + if (fa == 0) return a; + if (fb == 0) return b; + + //Rcout << "quant_val = " << quant_val << ".\n"; + + //Rcout << "fa = " << fa << ".\n"; + //Rcout << "fb = " << fb << ".\n"; + + // this method only works if the signs of f(a) and f(b) + + if(fa * fb >= 0){throw std::range_error("fa * fb >= 0.This method only works if the signs of f(a) and f(b) are different.");} + do { + // calculate fun at the midpoint of a,b + // if that's the root, we're done + // prefer: + double midpt = (a + b) / 2; + double fmid = mixt_eval_cdf(midpt, d_o_f, mean_vec, var_vec, weights_vec,quant_val); + + if (fmid == 0) return midpt; + + // adjust our bounds to either [a,midpt] or [midpt,b] + // based on where fmid ends up being. I'm pretty + // sure the code in the question is wrong, so I fixed it + if (fa * fmid < 0) { // fmid, not f1 + fb = fmid; + b = midpt; + } + else { + fa = fmid; + a = midpt; + } + } while (b-a > EPS); // only loop while + // a and b are sufficiently far + // apart + //Rcout << "a = " << a << ".\n"; + //Rcout << "b = " << b << ".\n"; + return (a + b) / 2; // approximation +} + + +//###########################################################################################################################// + +std::vector mixt_find_boundsQ(double d_o_f, std::vector mean_vec, std::vector var_vec, double quant_val) { + //boost::math::students_t dist1(d_o_f); + + std::vector tempbounds(mean_vec.size()); + + for(unsigned int i=0; i< mean_vec.size();i++){ + //tempbounds[i]= mean_vec[i]+sqrt(var_vec[i])*boost::math::quantile(dist1,quant_val); + tempbounds[i]= mean_vec[i]+sqrt(var_vec[i])*quant_val; + } + + std::vector ret(2); + ret[0]= *std::min_element(tempbounds.begin(), tempbounds.end()); + ret[1]= *std::max_element(tempbounds.begin(), tempbounds.end()); + + return(ret) ; +} +//###########################################################################################################################// + +// [[Rcpp::plugins(openmp)]] +// Protect against compilers without OpenMP +#ifdef _OPENMP +#include +#endif + + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List pred_ints_exact_outsamp(List overall_sum_trees, + List overall_sum_mat, + NumericVector y, + NumericVector BIC_weights,//double min_possible,double max_possible, + int num_obs,int num_test_obs, + double a,double sigma,double mu_mu,double nu, + double lambda,//List resids, + NumericMatrix test_data, double lower_prob, double upper_prob, int num_cores, + double root_alg_precision){ + + + List termobs_testdata_overall= get_termobs_testdata_overall(overall_sum_trees,test_data); + + + //NumericMatrix preds_all_models(num_test_obs,BIC_weights.size()); + arma::mat preds_all_models_arma(num_test_obs,BIC_weights.size()); + arma::mat weighted_preds_all_models_arma(num_test_obs,BIC_weights.size()); + arma::mat t_vars_arma(num_test_obs,BIC_weights.size()); + + // for all sums of trees + + NumericVector BICi=-0.5*BIC_weights; + double max_BIC=max(BICi); + + + NumericVector post_weights(BIC_weights.size()); + + for(int k=0;k(y_scaled); + arma::mat y_arma(num_obs,1); + y_arma.col(0)=yvec; + arma::mat yty=y_arma.t()*y_arma; + + + //#pragma omp parallel num_threads(1) + //#pragma omp for + for(int i=0;i probs_for_quantiles {lower_prob, 0.5, upper_prob}; + + + + typedef std::vector stdvec; + std::vector weights_vec= as(post_weights); + + boost::math::students_t dist2(nu+num_obs); + double lq_tstandard= boost::math::quantile(dist2,lower_prob); + double med_tstandard= boost::math::quantile(dist2,0.5); //This is just 0 ?? + double uq_tstandard= boost::math::quantile(dist2,upper_prob); + + if(weights_vec.size()==1){ + + for(int i=0;i tempmeans= arma::conv_to::from(preds_all_models_arma.row(i)); + std::vector tempvars= arma::conv_to::from(t_vars_arma.row(i)); + + boost::math::students_t dist2(nu+num_obs); + + + output(0,i)= tempmeans[0]+sqrt(tempvars[0])*lq_tstandard; + output(1,i)= tempmeans[0]+sqrt(tempvars[0])*med_tstandard; + output(2,i)= tempmeans[0]+sqrt(tempvars[0])*uq_tstandard; + + + } + }else{ + + for(int i=0;i tempmeans= arma::conv_to::from(preds_all_models_arma.row(i)); + std::vector tempvars= arma::conv_to::from(t_vars_arma.row(i)); + + + std::vector bounds_lQ = mixt_find_boundsQ( nu+num_obs, tempmeans, tempvars, lq_tstandard); + + output(0,i)=rootmixt(nu+num_obs, + bounds_lQ[0]-0.0001, + bounds_lQ[1]+0.0001, + tempmeans, + tempvars, + weights_vec, lower_prob,root_alg_precision); + + std::vector bounds_med = mixt_find_boundsQ( nu+num_obs, tempmeans, tempvars, med_tstandard); + + output(1,i)=rootmixt(nu+num_obs, + bounds_med[0]-0.0001, + bounds_med[1]+0.0001, + tempmeans, + tempvars, + weights_vec, 0.5,root_alg_precision); + + std::vector bounds_uQ = mixt_find_boundsQ( nu+num_obs, tempmeans, tempvars, uq_tstandard); + + + output(2,i)=rootmixt(nu+num_obs, + bounds_uQ[0]-0.0001, + bounds_uQ[1]+0.0001, + tempmeans, + tempvars, + weights_vec, upper_prob,root_alg_precision); + + + } + } + + + + for(int i=0;i +#endif + + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List pred_ints_exact_outsamp_par(List overall_sum_trees, + List overall_sum_mat, + NumericVector y, + NumericVector BIC_weights,//double min_possible,double max_possible, + int num_obs,int num_test_obs, + double a,double sigma,double mu_mu,double nu, + double lambda,//List resids, + NumericMatrix test_data, double lower_prob, double upper_prob, int num_cores, + double root_alg_precision){ + + + //List termobs_testdata_overall= get_termobs_testdata_overall(overall_sum_trees,test_data); + + arma::field>> termobs_testdata_overallF=get_termobs_testdata_fields_overall(overall_sum_trees,test_data); + + //NumericMatrix preds_all_models(num_test_obs,BIC_weights.size()); + arma::mat preds_all_models_arma(num_test_obs,BIC_weights.size()); + arma::mat weighted_preds_all_models_arma(num_test_obs,BIC_weights.size()); + arma::mat t_vars_arma(num_test_obs,BIC_weights.size()); + + // for all sums of trees + + NumericVector BICi=-0.5*BIC_weights; + double max_BIC=max(BICi); + + + NumericVector post_weights(BIC_weights.size()); + + for(int k=0;k(post_weights); + + //int num_models= BIC_weights.size(); + + + + + + NumericVector y_scaled=scale_response(min(y),max(y),-0.5,0.5,y); + arma::vec yvec=Rcpp::as(y_scaled); + arma::mat y_arma(num_obs,1); + y_arma.col(0)=yvec; + arma::mat yty=y_arma.t()*y_arma; + + + arma::mat I_test(num_test_obs,num_test_obs); + I_test=I_test.eye(); + + //create field (armadillo list) of models + //each model is a field (armadillo list) of trees represented by matrices + arma::field> modelsF(overall_sum_trees.size()); + for(int i=0;i treesF(temp_tree_list.size()); + for(int q=0;q(temp_tree_list[q]); + treesF(q)=temp_tree_mat; + } + //Rcout << "Line 5669.\n"; + + modelsF(i)=treesF; + } + + + arma::field> matsF(overall_sum_mat.size()); + for(int i=0;i treesF(temp_tree_list.size()); + for(int q=0;q(temp_tree_list[q]); + treesF(q)=temp_tree_mat; + } + matsF(i)=treesF; + } + + + + + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(int i=0;i tree_list = modelsF(i); + + arma::mat W(num_obs,0); + int upsilon=0; + for(unsigned int j=0;j0){ + break; + } + } + + //end find_term_obs + + + //assign term_obs to the correct index of J + //NumericVector term_obs2=as(wrap(term_obs)); + //NumericVector obs_col(obs_to_nodes_temp.nrow()); + arma::vec obs_col= arma::zeros(tree_matrix_temp.n_rows); + //Rcout << "Line 5747.\n"; + obs_col.elem(term_obs)= arma::ones(term_obs.n_elem); + //Rcout << "Line 5749.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(i)= colmat; + } + + + + + W.insert_cols(upsilon,Jmat); + upsilon+=b_j; + } + + + //Rcout << "Line 5759.\n"; + + + //arma::mat W_tilde=get_W_test(overall_sum_trees[i],termobs_testdata_overall[i],num_test_obs); + + //////////////////////////////////////// + + arma::mat W_tilde(num_test_obs,0); + int upsilon2=0; + for(unsigned int j=0;j curr_termobs=(termobs_testdata_overallF(i))(j); + + //begin find termnodes + //NumericVector tree_term_nodes=find_term_nodes(curr_tree); + + //arma::mat arma_tree(tree_table.begin(),tree_table.nrow(), tree_table.ncol(), false); + arma::vec colmat=curr_tree.col(4); + arma::uvec term_nodes=arma::find(colmat==-1); + term_nodes=term_nodes+1; + + //return(wrap(term_nodes)); + //Rcout << "Line 5782.\n"; + + //end find termnodes + + int b_j=term_nodes.size(); + //will make J as we go in BART-BMA no need to create it again here.... + //arma::mat Jmat=get_J_test(curr_termobs,tree_term_nodes,n); + //arma::mat Jmat=get_J_test(curr_termobs,term_nodes,num_test_obs); + + //begin J test function + arma::mat Jmat(num_test_obs, term_nodes.n_elem); + Jmat.zeros(); + + //for each terminal node get the observations associated with it and set column + for(unsigned int q=0;q(num_test_obs); + //Rcout << "Line 5809.\n"; + obs_col.elem(term_obs2)=arma::ones(term_obs2.n_elem); + //Rcout << "Line 5811.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(q)= colmat; + } + //return(Jmat); + + //end J test function + + W_tilde.insert_cols(upsilon2,Jmat); + upsilon2+=b_j; + } + + //////////////////////////////////////// + //Rcout << "Line 5819.\n"; + + + double b=W.n_cols; + + + //get t(y)inv(psi)J + arma::mat ytW=y_arma.t()*W; + + + //get t(J)inv(psi)J + arma::mat WtW=W.t()*W; + + + + + + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + + + //arma::mat L_mat = arma::chol(sec_term,"lower"); + + //arma::mat L_inv = arma::inv(L_mat); + //arma::mat L_inv = arma::inv(arma::chol(sec_term)); + //arma::mat L_inv = arma::inv((L_mat)); + //arma::mat L_inv_t = arma::trans(arma::inv(trimatu(L_mat))); + //arma::mat L_inv_t = arma::trans(arma::inv((L_mat))); + + + + //arma::mat sec_term_inv=sec_term.i(); + arma::mat sec_term_inv=inv_sympd(sec_term); + + + //get t(J)inv(psi)y + arma::mat third_term=W.t()*y_arma; + + + //arma::mat coeff = solve(L_mat.t(), solve(L_mat, Wmat.t()*y_arma)); + //arma::mat coeff = L_inv.t()*L_inv*Wmat.t()*y_arma; + + + + //get m^TV^{-1}m + arma::mat mvm= ytW*sec_term_inv*third_term; + + + + arma::mat w_tilde_M_inv = W_tilde*sec_term_inv; + + //Rcout << "Line 4151"; + + + + // //Obtain (lower triangular?) matrix t(L) by Cholesky decomposition such that sec_term_inv=L*t(L) + // arma::mat rooti = arma::trans(arma::inv(trimatu(arma::chol(sec_term)))); + // //obtain the log of the root of the determinant + // double rootisum = arma::sum(log(rooti.diag())); + // + // arma::mat LtWtY= rooti*(Wmat.t()*y); + // + // arma::mat rel=(b/2)*log(a)+rootisum -expon*log(nu*lambda - arma::sum(LtWtY%LtWtY) +yty); + // + // + + + // arma::mat rooti = arma::trans(arma::inv(trimatu(arma::chol(sec_term)))); + // arma::mat LtWtY= rooti*(Wmat.t()*y); + + + + arma::vec preds_temp_arma= w_tilde_M_inv*third_term; + + // arma::vec preds_temp_arma= W_tilde*sec_term_inv*(Wmat.t())*y_arma; + + //arma::vec preds_temp_arma= W_tilde*coeff; + //arma::mat mvm= coeff*y_arma; + //arma::mat mvm= y_arma.t()*Wmat*coeff; + + + + + + arma::mat temp_for_scal = ((nu*lambda+yty-mvm)/(nu+num_obs)); + double temp_scal= as_scalar(temp_for_scal) ; + //Rcout << "Line 4156"; + //arma::mat covar_t=temp_scal*(I_test+w_tilde_M_inv*(W_tilde.t())); + arma::mat covar_t=temp_scal*(I_test+w_tilde_M_inv*(W_tilde.t())); + + + //arma::mat W_tilde_L_inv_t= W_tilde*L_inv_t; + //arma::mat covar_t=temp_scal*(I_test+W_tilde_L_inv_t*(W_tilde_L_inv_t.t())); + + + + // Rcout << "Line 4459. i= "<< i << ".\n"; + + + + //double weight=exp(BICi[i]-(max_BIC+log(sum(exp(BICi-max_BIC))))); + + weighted_preds_all_models_arma.col(i)=preds_temp_arma*post_weights_arma[i]; + + preds_all_models_arma.col(i)=preds_temp_arma; + + + + t_vars_arma.col(i)=covar_t.diag(); + + } + + //} +#pragma omp barrier + + //arma::colvec predicted_values; + + //arma::mat M1(preds_all_models.begin(), preds_all_models.nrow(), preds_all_models.ncol(), false); + arma::colvec predicted_values=sum(weighted_preds_all_models_arma,1); + + //NumericMatrix draws_wrapped= wrap(draws_for_preds); + arma::mat output(3, num_test_obs); + //NumericVector probs_for_quantiles = NumericVector::create(lower_prob, 0.5, upper_prob); + + //std::vector probs_for_quantiles {lower_prob, 0.5, upper_prob}; + + + + typedef std::vector stdvec; + std::vector weights_vec= as(post_weights); + + boost::math::students_t dist2(nu+num_obs); + double lq_tstandard= boost::math::quantile(dist2,lower_prob); + double med_tstandard= boost::math::quantile(dist2,0.5); //This is just 0 ?? + double uq_tstandard= boost::math::quantile(dist2,upper_prob); + + + if(weights_vec.size()==1){ +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(int i=0;i tempmeans= arma::conv_to::from(preds_all_models_arma.row(i)); + std::vector tempvars= arma::conv_to::from(t_vars_arma.row(i)); + + //boost::math::students_t dist2(nu+num_obs); + + + output(0,i)= tempmeans[0]+sqrt(tempvars[0])*lq_tstandard; + output(1,i)= tempmeans[0]+sqrt(tempvars[0])*med_tstandard; + output(2,i)= tempmeans[0]+sqrt(tempvars[0])*uq_tstandard; + + + } +#pragma omp barrier + }else{ +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(int i=0;i tempmeans= arma::conv_to::from(preds_all_models_arma.row(i)); + std::vector tempvars= arma::conv_to::from(t_vars_arma.row(i)); + + + std::vector bounds_lQ = mixt_find_boundsQ( nu+num_obs, tempmeans, tempvars, lq_tstandard); + + output(0,i)=rootmixt(nu+num_obs, + bounds_lQ[0]-0.0001, + bounds_lQ[1]+0.0001, + tempmeans, + tempvars, + weights_vec, lower_prob,root_alg_precision); + + + std::vector bounds_med = mixt_find_boundsQ( nu+num_obs, tempmeans, tempvars, med_tstandard); + + output(1,i)=rootmixt(nu+num_obs, + bounds_med[0]-0.0001, + bounds_med[1]+0.0001, + tempmeans, + tempvars, + weights_vec, 0.5,root_alg_precision); + + std::vector bounds_uQ = mixt_find_boundsQ( nu+num_obs, tempmeans, tempvars, uq_tstandard); + + output(2,i)=rootmixt(nu+num_obs, + bounds_uQ[0]-0.0001, + bounds_uQ[1]+0.0001, + tempmeans, + tempvars, + weights_vec, upper_prob,root_alg_precision); + + + } +#pragma omp barrier + } + + arma::mat output_rescaled(output.n_rows, output.n_cols); + + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(unsigned int i=0;i +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List pred_ints_lin_alg_outsamp(List overall_sum_trees, + List overall_sum_mat, + NumericVector y, + NumericVector BIC_weights, + int num_iter,int burnin,int num_obs,int num_test_obs, + double a,double sigma,double mu_mu,double nu, + double lambda,//List resids, + NumericMatrix test_data, double lower_prob, double upper_prob){ + + + List termobs_testdata_overall= get_termobs_testdata_overall(overall_sum_trees,test_data); + //NumericMatrix preds_all_models(num_test_obs,BIC_weights.size()); + arma::mat preds_all_models_arma(num_test_obs,BIC_weights.size()); + arma::mat weighted_preds_all_models_arma(num_test_obs,BIC_weights.size()); + + // for all sums of trees + + NumericVector BICi=-0.5*BIC_weights; + double max_BIC=max(BICi); + + + NumericVector post_weights(BIC_weights.size()); + + for(int k=0;k(y_scaled); + arma::mat y_arma(num_obs,1); + y_arma.col(0)=yvec; + //get exponent + //double expon=(n+nu)/2; + //get y^Tpsi^{-1}y + // arma::mat psi_inv=psi.i(); + + + arma::mat yty=y_arma.t()*y_arma; + + arma::mat I_test(num_test_obs,num_test_obs); + I_test=I_test.eye(); + + for(int i=0;i probs_for_quantiles {lower_prob, 0.5, upper_prob}; + + + + // Rcout << "Line 4492"; + //Rcout << "probs_for_quantiles = " << probs_for_quantiles << ".\n"; + typedef std::vector stdvec; + + for(int i=0;i tempcol= arma::conv_to::from(draws_for_preds.col(i)); + std::vector tempquant= Quantile2(tempcol, probs_for_quantiles); + NumericVector tempforoutput = wrap(tempquant); + output(_,i)= tempforoutput; + + } + + for(int i=0;i(y_scaled); + arma::mat y_arma(num_obs,1); + y_arma.col(0)=yvec; + //get exponent + //double expon=(n+nu)/2; + //get y^Tpsi^{-1}y + // arma::mat psi_inv=psi.i(); + + + arma::mat yty=y_arma.t()*y_arma; + + //get t(y)inv(psi)J + arma::mat ytW=y_arma.t()*Wmat; + + + //get t(J)inv(psi)J + arma::mat WtW=Wmat.t()*Wmat; + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + //arma::mat sec_term_inv=sec_term.i(); + arma::mat sec_term_inv=inv_sympd(sec_term); + //get t(J)inv(psi)y + arma::mat third_term=Wmat.t()*y_arma; + + //get m^TV^{-1}m + arma::mat mvm= ytW*sec_term_inv*third_term; + + arma::vec preds_temp_arma= Wmat*sec_term_inv*third_term; + + //arma::mat I_test(num_test_obs,num_test_obs); + //I_test=I_test.eye(); + + arma::mat covar_t=as_scalar((nu*lambda+yty-mvm)/(nu+num_obs))*(Wmat*sec_term_inv*Wmat.t()); + + + + + //double weight=exp(BICi[i]-(max_BIC+log(sum(exp(BICi-max_BIC))))); + + //weighted_preds_all_models_arma.col(i)=preds_temp_arma*weight; + weighted_preds_all_models_arma.col(i)=preds_temp_arma*post_weights[i]; + + preds_all_models_arma.col(i)=preds_temp_arma; + + + + //int num_its_to_sample = round(weight*(num_iter)); + int num_its_temp = num_its_to_sample[i]; + + arma::uword m = num_obs; + arma::vec U = arma::chi2rnd(nu+num_obs,num_its_temp); + U = sqrt(nu+num_obs / U); + arma::mat Y = mvnrnd( arma::vec(m, arma::fill::zeros), covar_t,num_its_temp); + //arma::mat temp_draws(m, num_its_temp); + //for ( arma::uword i = 0; i < temp_draws.n_cols; ++i ) { + // temp_draws.col(i) = preds_temp_arma + Y.col(i) * U[i]; + //} + + + arma::rowvec Ut=U.t(); + Y.each_row() %= Ut; + arma::mat temp_draws = arma::repmat(preds_temp_arma, 1, num_its_temp) + Y; + + + + //draws_for_preds = join_cols(draws_for_preds,temp_draws.t()); + //if(i==0){ + // draws_for_preds.rows(0,num_its_sum_arma[i]-1)=temp_draws.t(); + //}else{ + // draws_for_preds.rows(num_its_sum_arma[i-1],num_its_sum_arma[i]-1)=temp_draws.t(); + //} + + draws_for_preds = join_cols(draws_for_preds,temp_draws.t()); + + } + + //arma::colvec predicted_values; + + //arma::mat M1(preds_all_models.begin(), preds_all_models.nrow(), preds_all_models.ncol(), false); + arma::colvec predicted_values=sum(weighted_preds_all_models_arma,1); + + //NumericMatrix draws_wrapped= wrap(draws_for_preds); + NumericMatrix output(3, num_obs); + //NumericVector probs_for_quantiles = NumericVector::create(lower_prob, 0.5, upper_prob); + std::vector probs_for_quantiles {lower_prob, 0.5, upper_prob}; + + + + typedef std::vector stdvec; + + for(int i=0;i tempcol= arma::conv_to::from(draws_for_preds.col(i)); + std::vector tempquant= Quantile2(tempcol, probs_for_quantiles); + NumericVector tempforoutput = wrap(tempquant); + output(_,i)= tempforoutput; + + } + + for(int i=0;i(y_scaled); + arma::mat y_arma(num_obs,1); + y_arma.col(0)=yvec; + //get exponent + //double expon=(n+nu)/2; + //get y^Tpsi^{-1}y + // arma::mat psi_inv=psi.i(); + + + arma::mat yty=y_arma.t()*y_arma; + + //get t(y)inv(psi)J + arma::mat ytW=y_arma.t()*Wmat; + + + //get t(J)inv(psi)J + arma::mat WtW=Wmat.t()*Wmat; + + + + + + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + + + arma::mat L_mat = arma::chol(sec_term,"lower"); + + //arma::mat L_inv = arma::inv(L_mat); + //arma::mat L_inv = arma::inv(arma::chol(sec_term)); + //arma::mat L_inv = arma::inv((L_mat)); + //arma::mat L_inv_t = arma::trans(arma::inv(trimatu(L_mat))); + arma::mat L_inv_t = arma::trans(arma::inv((L_mat))); + + + + //arma::mat sec_term_inv=sec_term.i(); + //arma::mat sec_term_inv=inv_sympd(sec_term); + + + //get t(J)inv(psi)y + // arma::mat third_term=Wmat.t()*y_arma; + + + arma::mat coeff = solve(L_mat.t(), solve(L_mat, Wmat.t()*y_arma)); + //arma::mat coeff = L_inv.t()*L_inv*Wmat.t()*y_arma; + + + + //get m^TV^{-1}m + //arma::mat mvm= ytW*sec_term_inv*third_term; + + + + //arma::mat w_tilde_M_inv = W_tilde*sec_term_inv; + + //Rcout << "Line 4151"; + + + + // //Obtain (lower triangular?) matrix t(L) by Cholesky decomposition such that sec_term_inv=L*t(L) + // arma::mat rooti = arma::trans(arma::inv(trimatu(arma::chol(sec_term)))); + // //obtain the log of the root of the determinant + // double rootisum = arma::sum(log(rooti.diag())); + // + // arma::mat LtWtY= rooti*(Wmat.t()*y); + // + // arma::mat rel=(b/2)*log(a)+rootisum -expon*log(nu*lambda - arma::sum(LtWtY%LtWtY) +yty); + // + // + + + // arma::mat rooti = arma::trans(arma::inv(trimatu(arma::chol(sec_term)))); + // arma::mat LtWtY= rooti*(Wmat.t()*y); + + + + //arma::vec preds_temp_arma= w_tilde_M_inv*third_term; + + // arma::vec preds_temp_arma= W_tilde*sec_term_inv*(Wmat.t())*y_arma; + + arma::vec preds_temp_arma= W_tilde*coeff; + //arma::mat mvm= coeff*y_arma; + arma::mat mvm= y_arma.t()*Wmat*coeff; + + + + arma::mat I_test(num_test_obs,num_test_obs); + I_test=I_test.eye(); + + arma::mat temp_for_scal = ((nu*lambda+yty-mvm)/(nu+num_obs)); + double temp_scal= as_scalar(temp_for_scal) ; + //Rcout << "Line 4156"; + //arma::mat covar_t=temp_scal*(I_test+w_tilde_M_inv*(W_tilde.t())); + //arma::mat covar_t=temp_scal*(I_test+w_tilde_M_inv*(W_tilde.t())); + + + arma::mat W_tilde_L_inv_t= W_tilde*L_inv_t; + arma::mat covar_t=temp_scal*(I_test+W_tilde_L_inv_t*(W_tilde_L_inv_t.t())); + + + + // Rcout << "Line 4459. i= "<< i << ".\n"; + + + + //double weight=exp(BICi[i]-(max_BIC+log(sum(exp(BICi-max_BIC))))); + + weighted_preds_all_models_arma.col(i)=preds_temp_arma*post_weights[i]; + preds_all_models_arma.col(i)=preds_temp_arma; + + // Rcout << "Line 4468. i= "<< i << ".\n"; + + + //int num_its_to_sample = round(weight*(num_iter)); + + int num_its_temp = num_its_to_sample[i]; + + // arma::uword m = num_test_obs; + // arma::vec U = arma::chi2rnd(nu+num_obs,num_its_temp); + // U = sqrt(nu+num_obs / U); + // arma::mat Y = arma::mvnrnd( arma::colvec(m, arma::fill::zeros), covar_t,num_its_temp); + // arma::mat temp_draws(m, num_its_temp); + // // Rcout << "Line 4478. i= "<< i << ".\n"; + // + // for ( arma::uword i = 0; i < temp_draws.n_cols; ++i ) { + // temp_draws.col(i) = preds_temp_arma + Y.col(i) * U[i]; + // } + // Rcout << "Line 4483. i= "<< i << ".\n"; + + + + + //arma::uword m = num_test_obs; + arma::vec U = arma::chi2rnd(nu+num_obs,num_its_temp); + //arma::vec U = Rcpp::rchisq(num_its_temp,nu+num_obs); + U = sqrt((nu+num_obs) / U); + //arma::mat Y = arma::mvnrnd( arma::colvec(m, arma::fill::zeros), covar_t,num_its_temp); + arma::mat Y = arma::randn(num_its_temp, num_test_obs); + arma::mat temp_LY = Y*arma::chol(covar_t); + temp_LY.each_col() %= U; + arma::mat temp_draws = arma::repmat(preds_temp_arma, 1, num_its_temp).t() + temp_LY; + + + + // arma::mat temp_draws(m, num_its_temp); + // // Rcout << "Line 4478. i= "<< i << ".\n"; + // + // for ( arma::uword i = 0; i < temp_draws.n_cols; ++i ) { + // temp_draws.col(i) = preds_temp_arma + Y.col(i) * U[i]; + // } + + // Rcout << "number of rows of preds_temp_arma = " << preds_temp_arma.n_rows << ".\n"; + // Rcout << "number of rows of temp_LY = " << temp_LY.n_rows << ".\n"; + // + // Rcout << "number of rows of draws_for_preds = " << draws_for_preds.n_rows << ".\n"; + // Rcout << "number of rows of temp_draws = " << temp_draws.n_rows << ".\n"; + // + draws_for_preds = join_cols(draws_for_preds,temp_draws); + + } + + //arma::colvec predicted_values; + // Rcout << "Line 4491"; + + //arma::colvec predicted_values; + + //arma::mat M1(preds_all_models.begin(), preds_all_models.nrow(), preds_all_models.ncol(), false); + arma::colvec predicted_values=sum(weighted_preds_all_models_arma,1); + + //NumericMatrix draws_wrapped= wrap(draws_for_preds); + NumericMatrix output(3, num_test_obs); + //NumericVector probs_for_quantiles = NumericVector::create(lower_prob, 0.5, upper_prob); + std::vector probs_for_quantiles {lower_prob, 0.5, upper_prob}; + + + + typedef std::vector stdvec; + + for(int i=0;i tempcol= arma::conv_to::from(draws_for_preds.col(i)); + std::vector tempquant= Quantile2(tempcol, probs_for_quantiles); + NumericVector tempforoutput = wrap(tempquant); + output(_,i)= tempforoutput; + + } + + for(int i=0;i +#endif + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List pred_ints_lin_alg_parallel_outsamp(List overall_sum_trees, + List overall_sum_mat, + NumericVector y, + NumericVector BIC_weights, + int num_iter,int burnin,int num_obs,int num_test_obs, + double a,double sigma,double mu_mu,double nu, + double lambda,//List resids, + NumericMatrix test_data, double lower_prob, double upper_prob, int num_cores){ + + + List termobs_testdata_overall= get_termobs_testdata_overall(overall_sum_trees,test_data); + + + //NumericMatrix preds_all_models(num_test_obs,BIC_weights.size()); + arma::mat preds_all_models_arma(num_test_obs,BIC_weights.size()); + arma::mat weighted_preds_all_models_arma(num_test_obs,BIC_weights.size()); + + // for all sums of trees + + NumericVector BICi=-0.5*BIC_weights; + double max_BIC=max(BICi); + + + NumericVector post_weights(BIC_weights.size()); + + for(int k=0;k(y_scaled); + arma::mat y_arma(num_obs,1); + y_arma.col(0)=yvec; + arma::mat yty=y_arma.t()*y_arma; + + + //#pragma omp parallel num_threads(1) + //#pragma omp for + for(int i=0;i probs_for_quantiles {lower_prob, 0.5, upper_prob}; + + + + typedef std::vector stdvec; + + for(int i=0;i tempcol= arma::conv_to::from(draws_for_preds.col(i)); + std::vector tempquant= Quantile2(tempcol, probs_for_quantiles); + NumericVector tempforoutput = wrap(tempquant); + output(_,i)= tempforoutput; + + } + + for(int i=0;i +#endif +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List pred_ints_lin_alg_fields_outsamp(List overall_sum_trees, + List overall_sum_mat, + NumericVector y, + NumericVector BIC_weights, + int num_iter,int burnin,int num_obs,int num_test_obs, + double a,double sigma,double mu_mu,double nu, + double lambda,//List resids, + NumericMatrix test_data, double lower_prob, double upper_prob, int num_cores){ + + + //List termobs_testdata_overall= get_termobs_testdata_overall(overall_sum_trees,test_data); + + arma::field>> termobs_testdata_overallF=get_termobs_testdata_fields_overall(overall_sum_trees,test_data); + + //NumericMatrix preds_all_models(num_test_obs,BIC_weights.size()); + arma::mat preds_all_models_arma(num_test_obs,BIC_weights.size()); + arma::mat weighted_preds_all_models_arma(num_test_obs,BIC_weights.size()); + + // for all sums of trees + + NumericVector BICi=-0.5*BIC_weights; + double max_BIC=max(BICi); + + + NumericVector post_weights(BIC_weights.size()); + + for(int k=0;k(post_weights); + //int num_models= BIC_weights.size(); + + IntegerVector num_its_to_sample = RcppArmadillo::rmultinom(num_iter,post_weights); + IntegerVector num_its_sum = cumsum(num_its_to_sample); + arma::vec num_its_to_sample_arma = as(num_its_to_sample); + arma::vec num_its_sum_arma = as(num_its_sum); + + //arma::mat draws_for_preds(0,num_test_obs); + //arma::mat draws_for_preds(num_iter,num_test_obs); + arma::mat draws_for_preds(num_iter,num_test_obs); + + + NumericVector y_scaled=scale_response(min(y),max(y),-0.5,0.5,y); + arma::vec yvec=Rcpp::as(y_scaled); + arma::mat y_arma(num_obs,1); + y_arma.col(0)=yvec; + //get exponent + //double expon=(n+nu)/2; + //get y^Tpsi^{-1}y + // arma::mat psi_inv=psi.i(); + + //Rcout << "Line 5649.\n"; + + arma::mat yty=y_arma.t()*y_arma; + + arma::mat I_test(num_test_obs,num_test_obs); + I_test=I_test.eye(); + + //create field (armadillo list) of models + //each model is a field (armadillo list) of trees represented by matrices + arma::field> modelsF(overall_sum_trees.size()); + for(int i=0;i treesF(temp_tree_list.size()); + for(int q=0;q(temp_tree_list[q]); + treesF(q)=temp_tree_mat; + } + //Rcout << "Line 5669.\n"; + + modelsF(i)=treesF; + } + + + arma::field> matsF(overall_sum_mat.size()); + for(int i=0;i treesF(temp_tree_list.size()); + for(int q=0;q(temp_tree_list[q]); + treesF(q)=temp_tree_mat; + } + matsF(i)=treesF; + } + + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(int i=0;i tree_list = modelsF(i); + + arma::mat W(num_obs,0); + int upsilon=0; + for(unsigned int j=0;j0){ + break; + } + } + + //end find_term_obs + + + //assign term_obs to the correct index of J + //NumericVector term_obs2=as(wrap(term_obs)); + //NumericVector obs_col(obs_to_nodes_temp.nrow()); + arma::vec obs_col= arma::zeros(tree_matrix_temp.n_rows); + //Rcout << "Line 5747.\n"; + obs_col.elem(term_obs)= arma::ones(term_obs.n_elem); + //Rcout << "Line 5749.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(i)= colmat; + } + + + + + W.insert_cols(upsilon,Jmat); + upsilon+=b_j; + } + + + //Rcout << "Line 5759.\n"; + + + //arma::mat W_tilde=get_W_test(overall_sum_trees[i],termobs_testdata_overall[i],num_test_obs); + + //////////////////////////////////////// + + arma::mat W_tilde(num_test_obs,0); + int upsilon2=0; + for(unsigned int j=0;j curr_termobs=(termobs_testdata_overallF(i))(j); + + //begin find termnodes + //NumericVector tree_term_nodes=find_term_nodes(curr_tree); + + //arma::mat arma_tree(tree_table.begin(),tree_table.nrow(), tree_table.ncol(), false); + arma::vec colmat=curr_tree.col(4); + arma::uvec term_nodes=arma::find(colmat==-1); + term_nodes=term_nodes+1; + + //return(wrap(term_nodes)); + //Rcout << "Line 5782.\n"; + + //end find termnodes + + int b_j=term_nodes.size(); + //will make J as we go in BART-BMA no need to create it again here.... + //arma::mat Jmat=get_J_test(curr_termobs,tree_term_nodes,n); + //arma::mat Jmat=get_J_test(curr_termobs,term_nodes,num_test_obs); + + //begin J test function + arma::mat Jmat(num_test_obs, term_nodes.n_elem); + Jmat.zeros(); + + //for each terminal node get the observations associated with it and set column + for(unsigned int q=0;q(num_test_obs); + //Rcout << "Line 5809.\n"; + obs_col.elem(term_obs2)=arma::ones(term_obs2.n_elem); + //Rcout << "Line 5811.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(q)= colmat; + } + //return(Jmat); + + //end J test function + + W_tilde.insert_cols(upsilon2,Jmat); + upsilon2+=b_j; + } + + //////////////////////////////////////// + //Rcout << "Line 5819.\n"; + + + double b=W.n_cols; + + + //get t(y)inv(psi)J + arma::mat ytW=y_arma.t()*W; + + + //get t(J)inv(psi)J + arma::mat WtW=W.t()*W; + + + + + + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + + + //arma::mat L_mat = arma::chol(sec_term,"lower"); + + //arma::mat L_inv = arma::inv(L_mat); + //arma::mat L_inv = arma::inv(arma::chol(sec_term)); + //arma::mat L_inv = arma::inv((L_mat)); + //arma::mat L_inv_t = arma::trans(arma::inv(trimatu(L_mat))); + //arma::mat L_inv_t = arma::trans(arma::inv((L_mat))); + + + + //arma::mat sec_term_inv=sec_term.i(); + arma::mat sec_term_inv=inv_sympd(sec_term); + + + //get t(J)inv(psi)y + arma::mat third_term=W.t()*y_arma; + + + //arma::mat coeff = solve(L_mat.t(), solve(L_mat, Wmat.t()*y_arma)); + //arma::mat coeff = L_inv.t()*L_inv*Wmat.t()*y_arma; + + + + //get m^TV^{-1}m + arma::mat mvm= ytW*sec_term_inv*third_term; + + + + arma::mat w_tilde_M_inv = W_tilde*sec_term_inv; + + //Rcout << "Line 4151"; + + + + // //Obtain (lower triangular?) matrix t(L) by Cholesky decomposition such that sec_term_inv=L*t(L) + // arma::mat rooti = arma::trans(arma::inv(trimatu(arma::chol(sec_term)))); + // //obtain the log of the root of the determinant + // double rootisum = arma::sum(log(rooti.diag())); + // + // arma::mat LtWtY= rooti*(Wmat.t()*y); + // + // arma::mat rel=(b/2)*log(a)+rootisum -expon*log(nu*lambda - arma::sum(LtWtY%LtWtY) +yty); + // + // + + + // arma::mat rooti = arma::trans(arma::inv(trimatu(arma::chol(sec_term)))); + // arma::mat LtWtY= rooti*(Wmat.t()*y); + + + + arma::vec preds_temp_arma= w_tilde_M_inv*third_term; + + // arma::vec preds_temp_arma= W_tilde*sec_term_inv*(Wmat.t())*y_arma; + + //arma::vec preds_temp_arma= W_tilde*coeff; + //arma::mat mvm= coeff*y_arma; + //arma::mat mvm= y_arma.t()*Wmat*coeff; + + + + + + arma::mat temp_for_scal = ((nu*lambda+yty-mvm)/(nu+num_obs)); + double temp_scal= as_scalar(temp_for_scal) ; + //Rcout << "Line 4156"; + //arma::mat covar_t=temp_scal*(I_test+w_tilde_M_inv*(W_tilde.t())); + arma::mat covar_t=temp_scal*(I_test+w_tilde_M_inv*(W_tilde.t())); + + + //arma::mat W_tilde_L_inv_t= W_tilde*L_inv_t; + //arma::mat covar_t=temp_scal*(I_test+W_tilde_L_inv_t*(W_tilde_L_inv_t.t())); + + + + // Rcout << "Line 4459. i= "<< i << ".\n"; + + + + //double weight=exp(BICi[i]-(max_BIC+log(sum(exp(BICi-max_BIC))))); + + weighted_preds_all_models_arma.col(i)=preds_temp_arma*post_weights_arma[i]; + + preds_all_models_arma.col(i)=preds_temp_arma; + + // Rcout << "Line 4468. i= "<< i << ".\n"; + + + //int num_its_to_sample = round(weight*(num_iter)); + + int num_its_temp = num_its_to_sample_arma[i]; + + // arma::uword m = num_test_obs; + // arma::vec U = arma::chi2rnd(nu+num_obs,num_its_temp); + // U = sqrt(nu+num_obs / U); + // arma::mat Y = arma::mvnrnd( arma::colvec(m, arma::fill::zeros), covar_t,num_its_temp); + // arma::mat temp_draws(m, num_its_temp); + // // Rcout << "Line 4478. i= "<< i << ".\n"; + // + // for ( arma::uword i = 0; i < temp_draws.n_cols; ++i ) { + // temp_draws.col(i) = preds_temp_arma + Y.col(i) * U[i]; + // } + // Rcout << "Line 4483. i= "<< i << ".\n"; + + + + + //arma::uword m = num_test_obs; + arma::vec U = arma::chi2rnd(nu+num_obs,num_its_temp); + //arma::vec U = Rcpp::rchisq(num_its_temp,nu+num_obs); + U = sqrt((nu+num_obs) / U); + //arma::mat Y = arma::mvnrnd( arma::colvec(m, arma::fill::zeros), covar_t,num_its_temp); + arma::mat Y = arma::randn(num_its_temp, num_test_obs); + arma::mat temp_LY = Y*arma::chol(covar_t); + temp_LY.each_col() %= U; + arma::mat temp_draws = arma::repmat(preds_temp_arma, 1, num_its_temp).t() + temp_LY; + + + + // arma::mat temp_draws(m, num_its_temp); + // // Rcout << "Line 4478. i= "<< i << ".\n"; + // + // for ( arma::uword i = 0; i < temp_draws.n_cols; ++i ) { + // temp_draws.col(i) = preds_temp_arma + Y.col(i) * U[i]; + // } + + // Rcout << "number of rows of preds_temp_arma = " << preds_temp_arma.n_rows << ".\n"; + // Rcout << "number of rows of temp_LY = " << temp_LY.n_rows << ".\n"; + // + // Rcout << "number of rows of draws_for_preds = " << draws_for_preds.n_rows << ".\n"; + // Rcout << "number of rows of temp_draws = " << temp_draws.n_rows << ".\n"; + // + + + //draws_for_preds = join_cols(draws_for_preds,temp_draws); + + //draws_for_preds = join_cols(draws_for_preds,temp_draws); + if(i==0){ + draws_for_preds.rows(0,num_its_sum_arma[i]-1)=temp_draws; + }else{ + draws_for_preds.rows(num_its_sum_arma[i-1],num_its_sum_arma[i]-1)=temp_draws; + } + + } + +#pragma omp barrier + + //arma::colvec predicted_values; + // Rcout << "Line 4491"; + + //arma::mat M1(preds_all_models.begin(), preds_all_models.nrow(), preds_all_models.ncol(), false); + arma::colvec predicted_values=sum(weighted_preds_all_models_arma,1); + + //NumericMatrix draws_wrapped= wrap(draws_for_preds); + NumericMatrix output(3, num_test_obs); + //NumericVector probs_for_quantiles = NumericVector::create(lower_prob, 0.5, upper_prob); + std::vector probs_for_quantiles {lower_prob, 0.5, upper_prob}; + + + + // Rcout << "Line 4492"; + //Rcout << "probs_for_quantiles = " << probs_for_quantiles << ".\n"; + typedef std::vector stdvec; + + for(int i=0;i tempcol= arma::conv_to::from(draws_for_preds.col(i)); + std::vector tempquant= Quantile2(tempcol, probs_for_quantiles); + NumericVector tempforoutput = wrap(tempquant); + output(_,i)= tempforoutput; + + } + + for(int i=0;i +#endif +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List pred_ints_chol_parallel_outsamp(List overall_sum_trees, + List overall_sum_mat, + NumericVector y, + NumericVector BIC_weights, + int num_iter,int burnin,int num_obs,int num_test_obs, + double a,double sigma,double mu_mu,double nu, + double lambda,//List resids, + NumericMatrix test_data, double lower_prob, + double upper_prob, int num_cores){ + + + //List termobs_testdata_overall= get_termobs_testdata_overall(overall_sum_trees,test_data); + arma::field>> termobs_testdata_overallF=get_termobs_testdata_fields_overall(overall_sum_trees,test_data); + + + //NumericMatrix preds_all_models(num_test_obs,BIC_weights.size()); + arma::mat preds_all_models_arma(num_test_obs,BIC_weights.size()); + arma::mat weighted_preds_all_models_arma(num_test_obs,BIC_weights.size()); + + // for all sums of trees + + NumericVector BICi=-0.5*BIC_weights; + double max_BIC=max(BICi); + + + NumericVector post_weights(BIC_weights.size()); + + for(int k=0;k(post_weights); + + //int num_models= BIC_weights.size(); + + IntegerVector num_its_to_sample = RcppArmadillo::rmultinom(num_iter,post_weights); + IntegerVector num_its_sum = cumsum(num_its_to_sample); + + arma::vec num_its_to_sample_arma = as(num_its_to_sample); + arma::vec num_its_sum_arma = as(num_its_sum); + + //arma::mat draws_for_preds(0,num_test_obs); + arma::mat draws_for_preds(num_iter,num_test_obs); + + NumericVector y_scaled=scale_response(min(y),max(y),-0.5,0.5,y); + arma::vec yvec=Rcpp::as(y_scaled); + arma::mat y_arma(num_obs,1); + y_arma.col(0)=yvec; + //get exponent + //double expon=(n+nu)/2; + //get y^Tpsi^{-1}y + // arma::mat psi_inv=psi.i(); + + //Rcout << "Line 5649.\n"; + + arma::mat yty=y_arma.t()*y_arma; + + arma::mat I_test(num_test_obs,num_test_obs); + I_test=I_test.eye(); + + + + arma::field> modelsF(overall_sum_trees.size()); + for(int i=0;i treesF(temp_tree_list.size()); + for(int q=0;q(temp_tree_list[q]); + treesF(q)=temp_tree_mat; + } + //Rcout << "Line 5669.\n"; + + modelsF(i)=treesF; + } + + + arma::field> matsF(overall_sum_mat.size()); + for(int i=0;i treesF(temp_tree_list.size()); + for(int q=0;q(temp_tree_list[q]); + treesF(q)=temp_tree_mat; + } + matsF(i)=treesF; + } + + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(int i=0;i tree_list = modelsF(i); + + arma::mat W(num_obs,0); + int upsilon=0; + for(unsigned int j=0;j0){ + break; + } + } + + //end find_term_obs + + + //assign term_obs to the correct index of J + //NumericVector term_obs2=as(wrap(term_obs)); + //NumericVector obs_col(obs_to_nodes_temp.nrow()); + arma::vec obs_col= arma::zeros(tree_matrix_temp.n_rows); + //Rcout << "Line 5747.\n"; + obs_col.elem(term_obs)= arma::ones(term_obs.n_elem); + //Rcout << "Line 5749.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(i)= colmat; + } + + + + + W.insert_cols(upsilon,Jmat); + upsilon+=b_j; + } + + + //Rcout << "Line 6224.\n"; + + + //arma::mat W_tilde=get_W_test(overall_sum_trees[i],termobs_testdata_overall[i],num_test_obs); + + //////////////////////////////////////// + + arma::mat W_tilde(num_test_obs,0); + int upsilon2=0; + for(unsigned int j=0;j curr_termobs=(termobs_testdata_overallF(i))(j); + + //begin find termnodes + //NumericVector tree_term_nodes=find_term_nodes(curr_tree); + + //arma::mat arma_tree(tree_table.begin(),tree_table.nrow(), tree_table.ncol(), false); + arma::vec colmat=curr_tree.col(4); + arma::uvec term_nodes=arma::find(colmat==-1); + term_nodes=term_nodes+1; + + //return(wrap(term_nodes)); + //Rcout << "Line 5782.\n"; + + //end find termnodes + + int b_j=term_nodes.size(); + //will make J as we go in BART-BMA no need to create it again here.... + //arma::mat Jmat=get_J_test(curr_termobs,tree_term_nodes,n); + //arma::mat Jmat=get_J_test(curr_termobs,term_nodes,num_test_obs); + + //begin J test function + arma::mat Jmat(num_test_obs, term_nodes.n_elem); + Jmat.zeros(); + + //for each terminal node get the observations associated with it and set column + for(unsigned int q=0;q(num_test_obs); + //Rcout << "Line 5809.\n"; + obs_col.elem(term_obs2)=arma::ones(term_obs2.n_elem); + //Rcout << "Line 5811.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(q)= colmat; + } + //return(Jmat); + + //end J test function + + W_tilde.insert_cols(upsilon2,Jmat); + upsilon2+=b_j; + } + + //////////////////////////////////////// + //Rcout << "Line 5819.\n"; + + + double b=W.n_cols; + //arma::vec yvec=Rcpp::as(y); + //arma::mat y_arma(num_obs,1); + //y_arma.col(0)=yvec; + //get exponent + //double expon=(n+nu)/2; + //get y^Tpsi^{-1}y + // arma::mat psi_inv=psi.i(); + + + //arma::mat yty=y_arma.t()*y_arma; + + //get t(y)inv(psi)J + arma::mat ytW=y_arma.t()*W; + + + //get t(J)inv(psi)J + arma::mat WtW=W.t()*W; + + + + + + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + + + arma::mat L_mat = arma::chol(sec_term,"lower"); + + //arma::mat L_inv = arma::inv(L_mat); + //arma::mat L_inv = arma::inv(arma::chol(sec_term)); + //arma::mat L_inv = arma::inv((L_mat)); + //arma::mat L_inv_t = arma::trans(arma::inv(trimatu(L_mat))); + arma::mat L_inv_t = arma::trans(arma::inv((L_mat))); + + + + //arma::mat sec_term_inv=sec_term.i(); + //arma::mat sec_term_inv=inv_sympd(sec_term); + + + //get t(J)inv(psi)y + // arma::mat third_term=Wmat.t()*y_arma; + + + arma::mat coeff = solve(L_mat.t(), solve(L_mat, W.t()*y_arma)); + //arma::mat coeff = L_inv.t()*L_inv*Wmat.t()*y_arma; + + + + //get m^TV^{-1}m + //arma::mat mvm= ytW*sec_term_inv*third_term; + + + + //arma::mat w_tilde_M_inv = W_tilde*sec_term_inv; + + //Rcout << "Line 6348.\n"; + + + + // //Obtain (lower triangular?) matrix t(L) by Cholesky decomposition such that sec_term_inv=L*t(L) + // arma::mat rooti = arma::trans(arma::inv(trimatu(arma::chol(sec_term)))); + // //obtain the log of the root of the determinant + // double rootisum = arma::sum(log(rooti.diag())); + // + // arma::mat LtWtY= rooti*(Wmat.t()*y); + // + // arma::mat rel=(b/2)*log(a)+rootisum -expon*log(nu*lambda - arma::sum(LtWtY%LtWtY) +yty); + // + // + + + // arma::mat rooti = arma::trans(arma::inv(trimatu(arma::chol(sec_term)))); + // arma::mat LtWtY= rooti*(Wmat.t()*y); + + + + //arma::vec preds_temp_arma= w_tilde_M_inv*third_term; + + // arma::vec preds_temp_arma= W_tilde*sec_term_inv*(Wmat.t())*y_arma; + + arma::vec preds_temp_arma= W_tilde*coeff; + //arma::mat mvm= coeff*y_arma; + arma::mat mvm= y_arma.t()*W*coeff; + + + + //arma::mat I_test(num_test_obs,num_test_obs); + //I_test=I_test.eye(); + + arma::mat temp_for_scal = ((nu*lambda+yty-mvm)/(nu+num_obs)); + double temp_scal= as_scalar(temp_for_scal) ; + //Rcout << "Line 4156"; + //arma::mat covar_t=temp_scal*(I_test+w_tilde_M_inv*(W_tilde.t())); + //arma::mat covar_t=temp_scal*(I_test+w_tilde_M_inv*(W_tilde.t())); + + + arma::mat W_tilde_L_inv_t= W_tilde*L_inv_t; + arma::mat covar_t=temp_scal*(I_test+W_tilde_L_inv_t*(W_tilde_L_inv_t.t())); + + + + //Rcout << "Line 6394. i= "<< i << ".\n"; + + + + //double weight=exp(BICi[i]-(max_BIC+log(sum(exp(BICi-max_BIC))))); + + weighted_preds_all_models_arma.col(i)=preds_temp_arma*post_weights_arma[i]; + preds_all_models_arma.col(i)=preds_temp_arma; + + //Rcout << "Line 6403. i= "<< i << ".\n"; + + + //int num_its_to_sample = round(weight*(num_iter)); + + int num_its_temp = num_its_to_sample_arma[i]; + + // arma::uword m = num_test_obs; + // arma::vec U = arma::chi2rnd(nu+num_obs,num_its_temp); + // U = sqrt(nu+num_obs / U); + // arma::mat Y = arma::mvnrnd( arma::colvec(m, arma::fill::zeros), covar_t,num_its_temp); + // arma::mat temp_draws(m, num_its_temp); + // // Rcout << "Line 4478. i= "<< i << ".\n"; + // + // for ( arma::uword i = 0; i < temp_draws.n_cols; ++i ) { + // temp_draws.col(i) = preds_temp_arma + Y.col(i) * U[i]; + // } + // Rcout << "Line 4483. i= "<< i << ".\n"; + + + + + //arma::uword m = num_test_obs; + arma::vec U = arma::chi2rnd(nu+num_obs,num_its_temp); + //arma::vec U = Rcpp::rchisq(num_its_temp,nu+num_obs); + U = sqrt((nu+num_obs) / U); + //arma::mat Y = arma::mvnrnd( arma::colvec(m, arma::fill::zeros), covar_t,num_its_temp); + arma::mat Y = arma::randn(num_its_temp, num_test_obs); + arma::mat temp_LY = Y*arma::chol(covar_t); + temp_LY.each_col() %= U; + arma::mat temp_draws = arma::repmat(preds_temp_arma, 1, num_its_temp).t() + temp_LY; + + //Rcout << "Line 6435. i= "<< i << ".\n"; + + + // arma::mat temp_draws(m, num_its_temp); + // // Rcout << "Line 4478. i= "<< i << ".\n"; + // + // for ( arma::uword i = 0; i < temp_draws.n_cols; ++i ) { + // temp_draws.col(i) = preds_temp_arma + Y.col(i) * U[i]; + // } + + // Rcout << "number of rows of preds_temp_arma = " << preds_temp_arma.n_rows << ".\n"; + // Rcout << "number of rows of temp_LY = " << temp_LY.n_rows << ".\n"; + // + // Rcout << "number of rows of draws_for_preds = " << draws_for_preds.n_rows << ".\n"; + // Rcout << "number of rows of temp_draws = " << temp_draws.n_rows << ".\n"; + // + //draws_for_preds = join_cols(draws_for_preds,temp_draws); + + if(i==0){ + draws_for_preds.rows(0,num_its_sum_arma[i]-1)=temp_draws; + }else{ + draws_for_preds.rows(num_its_sum_arma[i-1],num_its_sum_arma[i]-1)=temp_draws; + } + + //Rcout << "Line 6459. i= "<< i << ".\n"; + + + } + + //arma::colvec predicted_values; + //Rcout << "Line 6463.\n"; + + //arma::colvec predicted_values; + + //arma::mat M1(preds_all_models.begin(), preds_all_models.nrow(), preds_all_models.ncol(), false); + arma::colvec predicted_values=sum(weighted_preds_all_models_arma,1); + + //NumericMatrix draws_wrapped= wrap(draws_for_preds); + NumericMatrix output(3, num_test_obs); + //NumericVector probs_for_quantiles = NumericVector::create(lower_prob, 0.5, upper_prob); + std::vector probs_for_quantiles {lower_prob, 0.5, upper_prob}; + + + + typedef std::vector stdvec; + + for(int i=0;i tempcol= arma::conv_to::from(draws_for_preds.col(i)); + std::vector tempquant= Quantile2(tempcol, probs_for_quantiles); + NumericVector tempforoutput = wrap(tempquant); + output(_,i)= tempforoutput; + + } + + for(int i=0;i +#endif +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List mean_vars_lin_alg_parallel_outsamp(List overall_sum_trees, + List overall_sum_mat, + NumericVector y, + NumericVector BIC_weights, + int num_iter,int burnin,int num_obs,int num_test_obs, + double a,double sigma,double mu_mu,double nu, + double lambda,//List resids, + NumericMatrix test_data, int num_cores){ + + //NumericVector y_scaled=scale_response(min(y),max(y),-0.5,0.5,y); + + //List termobs_testdata_overall= get_termobs_testdata_overall(overall_sum_trees,test_data); + arma::field>> termobs_testdata_overallF=get_termobs_testdata_fields_overall(overall_sum_trees,test_data); + + //NumericMatrix preds_all_models(num_test_obs,BIC_weights.size()); + arma::mat preds_all_models_arma(num_test_obs,BIC_weights.size()); + arma::mat weighted_preds_all_models_arma(num_test_obs,BIC_weights.size()); + // for all sums of trees + + NumericVector BICi=-0.5*BIC_weights; + double max_BIC=max(BICi); + + //create field (armadillo list) of models + //each model is a field (armadillo list) of trees represented by matrices + arma::field> modelsF(overall_sum_trees.size()); + for(int i=0;i treesF(temp_tree_list.size()); + for(int q=0;q(temp_tree_list[q]); + treesF(q)=temp_tree_mat; + } + //Rcout << "Line 5669.\n"; + + modelsF(i)=treesF; + } + + + arma::field> matsF(overall_sum_mat.size()); + for(int i=0;i treesF(temp_tree_list.size()); + for(int q=0;q(temp_tree_list[q]); + treesF(q)=temp_tree_mat; + } + matsF(i)=treesF; + } + + + + //List covar_matrices(BIC_weights.size()); + //NumericVector model_weights(BIC_weights.size()); + arma::vec model_weightsv(BIC_weights.size()); + arma::field covar_matricesF(BIC_weights.size()); + + + NumericVector y_scaled=scale_response(min(y),max(y),-0.5,0.5,y); + arma::vec yvec=Rcpp::as(y_scaled); + arma::mat y_arma(num_obs,1); + y_arma.col(0)=yvec; + //get exponent + //double expon=(n+nu)/2; + //get y^Tpsi^{-1}y + // arma::mat psi_inv=psi.i(); + + + arma::mat yty=y_arma.t()*y_arma; + + + arma::mat I_test(num_test_obs,num_test_obs); + I_test=I_test.eye(); + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(int i=0;i tree_list = modelsF(i); + + arma::mat W(num_obs,0); + int upsilon=0; + for(unsigned int j=0;j0){ + break; + } + } + + //end find_term_obs + + + //assign term_obs to the correct index of J + //NumericVector term_obs2=as(wrap(term_obs)); + //NumericVector obs_col(obs_to_nodes_temp.nrow()); + arma::vec obs_col= arma::zeros(tree_matrix_temp.n_rows); + //Rcout << "Line 5747.\n"; + obs_col.elem(term_obs)= arma::ones(term_obs.n_elem); + //Rcout << "Line 5749.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(i)= colmat; + } + + + + + W.insert_cols(upsilon,Jmat); + upsilon+=b_j; + } + + + //Rcout << "Line 5759.\n"; + + + //arma::mat W_tilde=get_W_test(overall_sum_trees[i],termobs_testdata_overall[i],num_test_obs); + + //////////////////////////////////////// + + arma::mat W_tilde(num_test_obs,0); + int upsilon2=0; + for(unsigned int j=0;j curr_termobs=(termobs_testdata_overallF(i))(j); + + //begin find termnodes + //NumericVector tree_term_nodes=find_term_nodes(curr_tree); + + //arma::mat arma_tree(tree_table.begin(),tree_table.nrow(), tree_table.ncol(), false); + arma::vec colmat=curr_tree.col(4); + arma::uvec term_nodes=arma::find(colmat==-1); + term_nodes=term_nodes+1; + + //return(wrap(term_nodes)); + //Rcout << "Line 5782.\n"; + + //end find termnodes + + int b_j=term_nodes.size(); + //will make J as we go in BART-BMA no need to create it again here.... + //arma::mat Jmat=get_J_test(curr_termobs,tree_term_nodes,n); + //arma::mat Jmat=get_J_test(curr_termobs,term_nodes,num_test_obs); + + //begin J test function + arma::mat Jmat(num_test_obs, term_nodes.n_elem); + Jmat.zeros(); + + //for each terminal node get the observations associated with it and set column + for(unsigned int q=0;q(num_test_obs); + //Rcout << "Line 5809.\n"; + obs_col.elem(term_obs2)=arma::ones(term_obs2.n_elem); + //Rcout << "Line 5811.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(q)= colmat; + } + //return(Jmat); + + //end J test function + + W_tilde.insert_cols(upsilon2,Jmat); + upsilon2+=b_j; + } + + //////////////////////////////////////// + + double b=W.n_cols; + + + //get t(y)inv(psi)J + arma::mat ytW=y_arma.t()*W; + + + //get t(J)inv(psi)J + arma::mat WtW=W.t()*W; + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + //arma::mat sec_term_inv=sec_term.i(); + arma::mat sec_term_inv=inv_sympd(sec_term); + //get t(J)inv(psi)y + arma::mat third_term=W.t()*y_arma; + + //get m^TV^{-1}m + arma::mat mvm= ytW*sec_term_inv*third_term; + + arma::mat w_tilde_M_inv = W_tilde*sec_term_inv; + + //Rcout << "Line 4151"; + + //arma::vec preds_temp_arma= w_tilde_M_inv*third_term; + arma::vec preds_temp_arma= W_tilde*sec_term_inv*(W.t())*y_arma; + + + + arma::mat temp_for_scal = ((nu*lambda+yty-mvm)/(nu+num_obs)); + double temp_scal= as_scalar(temp_for_scal) ; + //Rcout << "Line 4156"; + arma::mat covar_t=temp_scal*(I_test+w_tilde_M_inv*(W_tilde.t())); + + + //Rcout << "Line 4162"; + + + double weight=exp(BICi[i]-(max_BIC+log(sum(exp(BICi-max_BIC))))); + //Rcout << "Line 4167"; + + model_weightsv[i]=weight; + //Rcout << "Line 4170"; + + weighted_preds_all_models_arma.col(i)=preds_temp_arma*weight; + preds_all_models_arma.col(i)=preds_temp_arma; + //Rcout << "Line 4173"; + + covar_matricesF(i)= covar_t; + //Rcout << "Line 4176"; + + + + + } +#pragma omp barrier + + //arma::colvec predicted_values; + + //arma::mat M1(preds_all_models.begin(), preds_all_models.nrow(), preds_all_models.ncol(), false); + arma::colvec predicted_values=sum(weighted_preds_all_models_arma,1); + + + //NumericVector orig_preds=get_original(min(y),max(y),-0.5,0.5,wrap(predicted_values)) ; + + + List ret(4); + ret[0]=wrap(predicted_values); + ret[1]=wrap(model_weightsv); + ret[2]=wrap(preds_all_models_arma.t()); + ret[3]=wrap(covar_matricesF); + + return(ret); +} +//###########################################################################################################################// + +// [[Rcpp::plugins(openmp)]] +// Protect against compilers without OpenMP +#ifdef _OPENMP +#include +#endif + + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List pred_ints_ITE_outsamp_par(List overall_sum_trees, + List overall_sum_mat, + NumericVector y, + NumericVector BIC_weights,//double min_possible,double max_possible, + int num_obs,int num_test_obs, + double a,double sigma,double mu_mu,double nu, + double lambda,//List resids, + NumericMatrix test_data, double lower_prob, double upper_prob, int num_cores, + double root_alg_precision, + NumericMatrix training_data){ + + + //List termobs_testdata_overall= get_termobs_testdata_overall(overall_sum_trees,test_data); + + NumericVector onevec1(training_data.nrow(),1.0); + NumericMatrix Test1data= cbind(onevec1,test_data); + + NumericVector zerovec1(training_data.nrow(),0.0); + NumericMatrix Test0data= cbind(zerovec1,test_data); + + arma::field>> termobs_testdata1_overallF=get_termobs_testdata_fields_overall(overall_sum_trees,Test1data); + arma::field>> termobs_testdata0_overallF=get_termobs_testdata_fields_overall(overall_sum_trees,Test0data); + + //NumericMatrix preds_all_models(num_test_obs,BIC_weights.size()); + arma::mat preds_all_models_arma(num_test_obs,BIC_weights.size()); + arma::mat weighted_preds_all_models_arma(num_test_obs,BIC_weights.size()); + arma::mat t_vars_arma(num_test_obs,BIC_weights.size()); + + arma::vec cate_means_arma(BIC_weights.size()); + arma::vec cate_means_weighted_arma(BIC_weights.size()); + + arma::vec cate_vars_arma(BIC_weights.size()); + + + // for all sums of trees + + NumericVector BICi=-0.5*BIC_weights; + double max_BIC=max(BICi); + + + NumericVector post_weights(BIC_weights.size()); + + for(int k=0;k(post_weights); + + //int num_models= BIC_weights.size(); + + + arma::vec averagingvec=(1/double(num_test_obs))*arma::ones(num_test_obs); + + + + NumericVector y_scaled=scale_response(min(y),max(y),-0.5,0.5,y); + arma::vec yvec=Rcpp::as(y_scaled); + arma::mat y_arma(num_obs,1); + y_arma.col(0)=yvec; + arma::mat yty=y_arma.t()*y_arma; + + + //arma::mat I_test(num_test_obs,num_test_obs); + //I_test=I_test.eye(); + + //create field (armadillo list) of models + //each model is a field (armadillo list) of trees represented by matrices + arma::field> modelsF(overall_sum_trees.size()); + for(int i=0;i treesF(temp_tree_list.size()); + for(int q=0;q(temp_tree_list[q]); + treesF(q)=temp_tree_mat; + } + //Rcout << "Line 5669.\n"; + + modelsF(i)=treesF; + } + + + arma::field> matsF(overall_sum_mat.size()); + for(int i=0;i treesF(temp_tree_list.size()); + for(int q=0;q(temp_tree_list[q]); + treesF(q)=temp_tree_mat; + } + matsF(i)=treesF; + } + + + + + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(int i=0;i tree_list = modelsF(i); + + arma::mat W(num_obs,0); + int upsilon=0; + for(unsigned int j=0;j0){ + break; + } + } + + //end find_term_obs + + + //assign term_obs to the correct index of J + //NumericVector term_obs2=as(wrap(term_obs)); + //NumericVector obs_col(obs_to_nodes_temp.nrow()); + arma::vec obs_col= arma::zeros(tree_matrix_temp.n_rows); + //Rcout << "Line 5747.\n"; + obs_col.elem(term_obs)= arma::ones(term_obs.n_elem); + //Rcout << "Line 5749.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(i)= colmat; + } + + + + + W.insert_cols(upsilon,Jmat); + upsilon+=b_j; + } + + + //Rcout << "Line 5759.\n"; + + + //arma::mat W_tilde=get_W_test(overall_sum_trees[i],termobs_testdata_overall[i],num_test_obs); + + + //////////////////////////////////////// + + arma::mat W_tilde1(num_test_obs,0); + int upsilon2=0; + for(unsigned int j=0;j curr_termobs=(termobs_testdata1_overallF(i))(j); + + //begin find termnodes + //NumericVector tree_term_nodes=find_term_nodes(curr_tree); + + //arma::mat arma_tree(tree_table.begin(),tree_table.nrow(), tree_table.ncol(), false); + arma::vec colmat=curr_tree.col(4); + arma::uvec term_nodes=arma::find(colmat==-1); + term_nodes=term_nodes+1; + + //return(wrap(term_nodes)); + //Rcout << "Line 5782.\n"; + + //end find termnodes + + int b_j=term_nodes.size(); + //will make J as we go in BART-BMA no need to create it again here.... + //arma::mat Jmat=get_J_test(curr_termobs,tree_term_nodes,n); + //arma::mat Jmat=get_J_test(curr_termobs,term_nodes,num_test_obs); + + //begin J test function + arma::mat Jmat(num_test_obs, term_nodes.n_elem); + Jmat.zeros(); + + //for each terminal node get the observations associated with it and set column + for(unsigned int q=0;q(num_test_obs); + //Rcout << "Line 5809.\n"; + obs_col.elem(term_obs2)=arma::ones(term_obs2.n_elem); + //Rcout << "Line 5811.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(q)= colmat; + } + //return(Jmat); + + //end J test function + + W_tilde1.insert_cols(upsilon2,Jmat); + upsilon2+=b_j; + } + + //////////////////////////////////////// + + //////////////////////////////////////// + + arma::mat W_tilde0(num_test_obs,0); + int upsilon3=0; + for(unsigned int j=0;j curr_termobs=(termobs_testdata0_overallF(i))(j); + + //begin find termnodes + //NumericVector tree_term_nodes=find_term_nodes(curr_tree); + + //arma::mat arma_tree(tree_table.begin(),tree_table.nrow(), tree_table.ncol(), false); + arma::vec colmat=curr_tree.col(4); + arma::uvec term_nodes=arma::find(colmat==-1); + term_nodes=term_nodes+1; + + //return(wrap(term_nodes)); + //Rcout << "Line 5782.\n"; + + //end find termnodes + + int b_j=term_nodes.size(); + //will make J as we go in BART-BMA no need to create it again here.... + //arma::mat Jmat=get_J_test(curr_termobs,tree_term_nodes,n); + //arma::mat Jmat=get_J_test(curr_termobs,term_nodes,num_test_obs); + + //begin J test function + arma::mat Jmat(num_test_obs, term_nodes.n_elem); + Jmat.zeros(); + + //for each terminal node get the observations associated with it and set column + for(unsigned int q=0;q(num_test_obs); + //Rcout << "Line 5809.\n"; + obs_col.elem(term_obs2)=arma::ones(term_obs2.n_elem); + //Rcout << "Line 5811.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(q)= colmat; + } + //return(Jmat); + + //end J test function + + W_tilde0.insert_cols(upsilon3,Jmat); + upsilon3+=b_j; + } + + //////////////////////////////////////// + + //Rcout << "Line 5819.\n"; + + + double b=W.n_cols; + + + //get t(y)inv(psi)J + arma::mat ytW=y_arma.t()*W; + + + //get t(J)inv(psi)J + arma::mat WtW=W.t()*W; + + + + + + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + + + //arma::mat L_mat = arma::chol(sec_term,"lower"); + + //arma::mat L_inv = arma::inv(L_mat); + //arma::mat L_inv = arma::inv(arma::chol(sec_term)); + //arma::mat L_inv = arma::inv((L_mat)); + //arma::mat L_inv_t = arma::trans(arma::inv(trimatu(L_mat))); + //arma::mat L_inv_t = arma::trans(arma::inv((L_mat))); + + + + //arma::mat sec_term_inv=sec_term.i(); + arma::mat sec_term_inv=inv_sympd(sec_term); + + + //get t(J)inv(psi)y + arma::mat third_term=W.t()*y_arma; + + + //arma::mat coeff = solve(L_mat.t(), solve(L_mat, Wmat.t()*y_arma)); + //arma::mat coeff = L_inv.t()*L_inv*Wmat.t()*y_arma; + + + + //get m^TV^{-1}m + arma::mat mvm= ytW*sec_term_inv*third_term; + + arma::mat Treat_diff = W_tilde1-W_tilde0; + + arma::mat w_tilde_M_inv = Treat_diff*sec_term_inv; + + //Rcout << "Line 4151"; + + + + // //Obtain (lower triangular?) matrix t(L) by Cholesky decomposition such that sec_term_inv=L*t(L) + // arma::mat rooti = arma::trans(arma::inv(trimatu(arma::chol(sec_term)))); + // //obtain the log of the root of the determinant + // double rootisum = arma::sum(log(rooti.diag())); + // + // arma::mat LtWtY= rooti*(Wmat.t()*y); + // + // arma::mat rel=(b/2)*log(a)+rootisum -expon*log(nu*lambda - arma::sum(LtWtY%LtWtY) +yty); + // + // + + + // arma::mat rooti = arma::trans(arma::inv(trimatu(arma::chol(sec_term)))); + // arma::mat LtWtY= rooti*(Wmat.t()*y); + + + + arma::vec preds_temp_arma= w_tilde_M_inv*third_term; + + // arma::vec preds_temp_arma= W_tilde*sec_term_inv*(Wmat.t())*y_arma; + + //arma::vec preds_temp_arma= W_tilde*coeff; + //arma::mat mvm= coeff*y_arma; + //arma::mat mvm= y_arma.t()*Wmat*coeff; + + + + + + arma::mat temp_for_scal = ((nu*lambda+yty-mvm)/(nu+num_obs)); + double temp_scal= as_scalar(temp_for_scal) ; + //Rcout << "Line 4156"; + //arma::mat covar_t=temp_scal*(I_test+w_tilde_M_inv*(W_tilde.t())); + arma::mat covar_t=temp_scal*(w_tilde_M_inv*(Treat_diff.t())); + + arma::mat catevartemp=temp_scal*(averagingvec.t()*w_tilde_M_inv*(Treat_diff.t())*averagingvec); + + //arma::mat W_tilde_L_inv_t= W_tilde*L_inv_t; + //arma::mat covar_t=temp_scal*(I_test+W_tilde_L_inv_t*(W_tilde_L_inv_t.t())); + + + + // Rcout << "Line 4459. i= "<< i << ".\n"; + + + + //double weight=exp(BICi[i]-(max_BIC+log(sum(exp(BICi-max_BIC))))); + + weighted_preds_all_models_arma.col(i)=preds_temp_arma*post_weights_arma[i]; + + preds_all_models_arma.col(i)=preds_temp_arma; + t_vars_arma.col(i)=covar_t.diag(); + + cate_means_arma(i)=as_scalar(averagingvec.t()*preds_temp_arma); + cate_means_weighted_arma(i)=cate_means_arma(i)*post_weights_arma(i); + + cate_vars_arma(i)=as_scalar(catevartemp); + + } + + //} +#pragma omp barrier + + //arma::colvec predicted_values; + + //arma::mat M1(preds_all_models.begin(), preds_all_models.nrow(), preds_all_models.ncol(), false); + arma::colvec predicted_values=sum(weighted_preds_all_models_arma,1); + + double cate_pred=sum(cate_means_weighted_arma); + + //NumericMatrix draws_wrapped= wrap(draws_for_preds); + arma::mat output(3, num_test_obs); + //NumericVector probs_for_quantiles = NumericVector::create(lower_prob, 0.5, upper_prob); + + //std::vector probs_for_quantiles {lower_prob, 0.5, upper_prob}; + arma::mat cate_ints(3, 1); + + + + typedef std::vector stdvec; + std::vector weights_vec= as(post_weights); + + boost::math::students_t dist2(nu+num_obs); + double lq_tstandard= boost::math::quantile(dist2,lower_prob); + double med_tstandard= boost::math::quantile(dist2,0.5); //This is just 0 ?? + double uq_tstandard= boost::math::quantile(dist2,upper_prob); + + + if(weights_vec.size()==1){ + + cate_ints(0,0)= cate_means_arma(0)+sqrt(cate_vars_arma(0))*lq_tstandard; + cate_ints(1,0)= cate_means_arma(0)+sqrt(cate_vars_arma(0))*med_tstandard; + cate_ints(2,0)= cate_means_arma(0)+sqrt(cate_vars_arma(0))*uq_tstandard; + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(int i=0;i tempmeans= arma::conv_to::from(preds_all_models_arma.row(i)); + std::vector tempvars= arma::conv_to::from(t_vars_arma.row(i)); + + //boost::math::students_t dist2(nu+num_obs); + + //Rcout << "Line 13812 tempvars" << t_vars_arma.row(i) << ".\n"; + //Rcout << "tempmeans" << preds_all_models_arma.row(i) << ".\n"; + + + output(0,i)= tempmeans[0]+sqrt(tempvars[0])*lq_tstandard; + output(1,i)= tempmeans[0]+sqrt(tempvars[0])*med_tstandard; + output(2,i)= tempmeans[0]+sqrt(tempvars[0])*uq_tstandard; + + + } +#pragma omp barrier + }else{ + std::vector tempmeans_cate= arma::conv_to::from(cate_means_arma); + std::vector tempvars_cate= arma::conv_to::from(cate_vars_arma); + + std::vector bounds_lQ_cate = mixt_find_boundsQ( nu+num_obs, tempmeans_cate, tempvars_cate, lq_tstandard); + + //Rcout << "line 13828 cate_vars_arma = " << cate_vars_arma << ".\n"; + //Rcout << "cate_means_arma = " << cate_means_arma << ".\n"; + + //Rcout << "bounds_lQ_cate[0] = " << bounds_lQ_cate[0] << ".\n"; + //Rcout << "bounds_lQ_cate[1] = " << bounds_lQ_cate[1] << ".\n"; + + cate_ints(0,0)= rootmixt(nu+num_obs, + bounds_lQ_cate[0]-0.0001, + bounds_lQ_cate[1]+0.0001, + tempmeans_cate, + tempvars_cate, + weights_vec, lower_prob,root_alg_precision); + + std::vector bounds_med_cate = mixt_find_boundsQ( nu+num_obs, tempmeans_cate, tempvars_cate, med_tstandard); + + //Rcout << "bounds_lQ_cate[0] = " << bounds_lQ_cate[0] << ".\n"; + //Rcout << "bounds_lQ_cate[1] = " << bounds_lQ_cate[1] << ".\n"; + + cate_ints(1,0)= rootmixt(nu+num_obs, + bounds_med_cate[0]-0.0001, + bounds_med_cate[1]+0.0001, + tempmeans_cate, + tempvars_cate, + weights_vec, 0.5, root_alg_precision); + + std::vector bounds_uQ_cate = mixt_find_boundsQ( nu+num_obs, tempmeans_cate, tempvars_cate, uq_tstandard); + + //Rcout << "bounds_lQ_cate[0] = " << bounds_lQ_cate[0] << ".\n"; + //Rcout << "bounds_lQ_cate[1] = " << bounds_lQ_cate[1] << ".\n"; + + cate_ints(2,0)= rootmixt(nu+num_obs, + bounds_uQ_cate[0]-0.0001, + bounds_uQ_cate[1]+0.0001, + tempmeans_cate, + tempvars_cate, + weights_vec, upper_prob, root_alg_precision); + + //Rcout << "line 13871 cate_ints = " << cate_ints << ".\n"; + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(int i=0;i tempmeans= arma::conv_to::from(preds_all_models_arma.row(i)); + std::vector tempvars= arma::conv_to::from(t_vars_arma.row(i)); + + //Rcout << "Line 13859. tempvars" << t_vars_arma.row(i) << ".\n"; + //Rcout << "tempmeans" << preds_all_models_arma.row(i) << ".\n"; + + std::vector bounds_lQ = mixt_find_boundsQ( nu+num_obs, tempmeans, tempvars, lq_tstandard); + + output(0,i)=rootmixt(nu+num_obs, + bounds_lQ[0]-0.0001, + bounds_lQ[1]+0.0001, + tempmeans, + tempvars, + weights_vec, lower_prob,root_alg_precision); + + + std::vector bounds_med = mixt_find_boundsQ( nu+num_obs, tempmeans, tempvars, med_tstandard); + + output(1,i)=rootmixt(nu+num_obs, + bounds_med[0]-0.0001, + bounds_med[1]+0.0001, + tempmeans, + tempvars, + weights_vec, 0.5,root_alg_precision); + + std::vector bounds_uQ = mixt_find_boundsQ( nu+num_obs, tempmeans, tempvars, uq_tstandard); + + output(2,i)=rootmixt(nu+num_obs, + bounds_uQ[0]-0.0001, + bounds_uQ[1]+0.0001, + tempmeans, + tempvars, + weights_vec, upper_prob,root_alg_precision); + + + } +#pragma omp barrier + } + + arma::mat output_rescaled(output.n_rows, output.n_cols); + + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(unsigned int i=0;i +#endif + + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List pred_ints_ITE_insamp_par(List overall_sum_trees, + List overall_sum_mat, + NumericVector y, + NumericVector BIC_weights,//double min_possible,double max_possible, + int num_obs,//int num_test_obs, + double a,double sigma, + double mu_mu,double nu, + double lambda,//List resids,NumericMatrix test_data, + double lower_prob, double upper_prob, + int num_cores, + double root_alg_precision, + NumericMatrix training_data){ + + + //List termobs_testdata_overall= get_termobs_testdata_overall(overall_sum_trees,test_data); + + NumericVector onevec1(training_data.nrow(),1.0); + NumericMatrix Training1data= cbind(onevec1,training_data); + + NumericVector zerovec1(training_data.nrow(),0.0); + NumericMatrix Training0data= cbind(zerovec1,training_data); + + arma::field>> termobs_testdata1_overallF=get_termobs_testdata_fields_overall(overall_sum_trees,Training1data); + arma::field>> termobs_testdata0_overallF=get_termobs_testdata_fields_overall(overall_sum_trees,Training0data); + + //NumericMatrix preds_all_models(num_test_obs,BIC_weights.size()); + arma::mat preds_all_models_arma(num_obs,BIC_weights.size()); + arma::mat weighted_preds_all_models_arma(num_obs,BIC_weights.size()); + arma::mat t_vars_arma(num_obs,BIC_weights.size()); + + arma::vec cate_means_arma(BIC_weights.size()); + arma::vec cate_means_weighted_arma(BIC_weights.size()); + + arma::vec cate_vars_arma(BIC_weights.size()); + + + // for all sums of trees + + NumericVector BICi=-0.5*BIC_weights; + double max_BIC=max(BICi); + + + NumericVector post_weights(BIC_weights.size()); + + for(int k=0;k(post_weights); + + //int num_models= BIC_weights.size(); + + + arma::vec averagingvec=(1/double(num_obs))*arma::ones(num_obs); + + + + NumericVector y_scaled=scale_response(min(y),max(y),-0.5,0.5,y); + arma::vec yvec=Rcpp::as(y_scaled); + arma::mat y_arma(num_obs,1); + y_arma.col(0)=yvec; + arma::mat yty=y_arma.t()*y_arma; + + + //arma::mat I_test(num_test_obs,num_test_obs); + //I_test=I_test.eye(); + + //create field (armadillo list) of models + //each model is a field (armadillo list) of trees represented by matrices + arma::field> modelsF(overall_sum_trees.size()); + for(int i=0;i treesF(temp_tree_list.size()); + for(int q=0;q(temp_tree_list[q]); + treesF(q)=temp_tree_mat; + } + //Rcout << "Line 5669.\n"; + + modelsF(i)=treesF; + } + + + arma::field> matsF(overall_sum_mat.size()); + for(int i=0;i treesF(temp_tree_list.size()); + for(int q=0;q(temp_tree_list[q]); + treesF(q)=temp_tree_mat; + } + matsF(i)=treesF; + } + + + + + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(int i=0;i tree_list = modelsF(i); + + arma::mat W(num_obs,0); + int upsilon=0; + for(unsigned int j=0;j0){ + break; + } + } + + //end find_term_obs + + + //assign term_obs to the correct index of J + //NumericVector term_obs2=as(wrap(term_obs)); + //NumericVector obs_col(obs_to_nodes_temp.nrow()); + arma::vec obs_col= arma::zeros(tree_matrix_temp.n_rows); + //Rcout << "Line 5747.\n"; + obs_col.elem(term_obs)= arma::ones(term_obs.n_elem); + //Rcout << "Line 5749.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(i)= colmat; + } + + + + + W.insert_cols(upsilon,Jmat); + upsilon+=b_j; + } + + + //Rcout << "Line 5759.\n"; + + + //arma::mat W_tilde=get_W_test(overall_sum_trees[i],termobs_testdata_overall[i],num_test_obs); + + + //////////////////////////////////////// + + arma::mat W_tilde1(num_obs,0); + int upsilon2=0; + for(unsigned int j=0;j curr_termobs=(termobs_testdata1_overallF(i))(j); + + //begin find termnodes + //NumericVector tree_term_nodes=find_term_nodes(curr_tree); + + //arma::mat arma_tree(tree_table.begin(),tree_table.nrow(), tree_table.ncol(), false); + arma::vec colmat=curr_tree.col(4); + arma::uvec term_nodes=arma::find(colmat==-1); + term_nodes=term_nodes+1; + + //return(wrap(term_nodes)); + //Rcout << "Line 5782.\n"; + + //end find termnodes + + int b_j=term_nodes.size(); + //will make J as we go in BART-BMA no need to create it again here.... + //arma::mat Jmat=get_J_test(curr_termobs,tree_term_nodes,n); + //arma::mat Jmat=get_J_test(curr_termobs,term_nodes,num_test_obs); + + //begin J test function + arma::mat Jmat(num_obs, term_nodes.n_elem); + Jmat.zeros(); + + //for each terminal node get the observations associated with it and set column + for(unsigned int q=0;q(num_obs); + //Rcout << "Line 5809.\n"; + obs_col.elem(term_obs2)=arma::ones(term_obs2.n_elem); + //Rcout << "Line 5811.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(q)= colmat; + } + //return(Jmat); + + //end J test function + + W_tilde1.insert_cols(upsilon2,Jmat); + upsilon2+=b_j; + } + + //////////////////////////////////////// + + //////////////////////////////////////// + + arma::mat W_tilde0(num_obs,0); + int upsilon3=0; + for(unsigned int j=0;j curr_termobs=(termobs_testdata0_overallF(i))(j); + + //begin find termnodes + //NumericVector tree_term_nodes=find_term_nodes(curr_tree); + + //arma::mat arma_tree(tree_table.begin(),tree_table.nrow(), tree_table.ncol(), false); + arma::vec colmat=curr_tree.col(4); + arma::uvec term_nodes=arma::find(colmat==-1); + term_nodes=term_nodes+1; + + //return(wrap(term_nodes)); + //Rcout << "Line 5782.\n"; + + //end find termnodes + + int b_j=term_nodes.size(); + //will make J as we go in BART-BMA no need to create it again here.... + //arma::mat Jmat=get_J_test(curr_termobs,tree_term_nodes,n); + //arma::mat Jmat=get_J_test(curr_termobs,term_nodes,num_test_obs); + + //begin J test function + arma::mat Jmat(num_obs, term_nodes.n_elem); + Jmat.zeros(); + + //for each terminal node get the observations associated with it and set column + for(unsigned int q=0;q(num_obs); + //Rcout << "Line 5809.\n"; + obs_col.elem(term_obs2)=arma::ones(term_obs2.n_elem); + //Rcout << "Line 5811.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(q)= colmat; + } + //return(Jmat); + + //end J test function + + W_tilde0.insert_cols(upsilon3,Jmat); + upsilon3+=b_j; + } + + //////////////////////////////////////// + + //Rcout << "Line 5819.\n"; + + + double b=W.n_cols; + + + //get t(y)inv(psi)J + arma::mat ytW=y_arma.t()*W; + + + //get t(J)inv(psi)J + arma::mat WtW=W.t()*W; + + + + + + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + + + //arma::mat L_mat = arma::chol(sec_term,"lower"); + + //arma::mat L_inv = arma::inv(L_mat); + //arma::mat L_inv = arma::inv(arma::chol(sec_term)); + //arma::mat L_inv = arma::inv((L_mat)); + //arma::mat L_inv_t = arma::trans(arma::inv(trimatu(L_mat))); + //arma::mat L_inv_t = arma::trans(arma::inv((L_mat))); + + + + //arma::mat sec_term_inv=sec_term.i(); + arma::mat sec_term_inv=inv_sympd(sec_term); + + + //get t(J)inv(psi)y + arma::mat third_term=W.t()*y_arma; + + + //arma::mat coeff = solve(L_mat.t(), solve(L_mat, Wmat.t()*y_arma)); + //arma::mat coeff = L_inv.t()*L_inv*Wmat.t()*y_arma; + + + + //get m^TV^{-1}m + arma::mat mvm= ytW*sec_term_inv*third_term; + + arma::mat Treat_diff = W_tilde1-W_tilde0; + + arma::mat w_tilde_M_inv = Treat_diff*sec_term_inv; + + //Rcout << "Line 4151"; + + + + // //Obtain (lower triangular?) matrix t(L) by Cholesky decomposition such that sec_term_inv=L*t(L) + // arma::mat rooti = arma::trans(arma::inv(trimatu(arma::chol(sec_term)))); + // //obtain the log of the root of the determinant + // double rootisum = arma::sum(log(rooti.diag())); + // + // arma::mat LtWtY= rooti*(Wmat.t()*y); + // + // arma::mat rel=(b/2)*log(a)+rootisum -expon*log(nu*lambda - arma::sum(LtWtY%LtWtY) +yty); + // + // + + + // arma::mat rooti = arma::trans(arma::inv(trimatu(arma::chol(sec_term)))); + // arma::mat LtWtY= rooti*(Wmat.t()*y); + + + + arma::vec preds_temp_arma= w_tilde_M_inv*third_term; + + // arma::vec preds_temp_arma= W_tilde*sec_term_inv*(Wmat.t())*y_arma; + + //arma::vec preds_temp_arma= W_tilde*coeff; + //arma::mat mvm= coeff*y_arma; + //arma::mat mvm= y_arma.t()*Wmat*coeff; + + + + + + arma::mat temp_for_scal = ((nu*lambda+yty-mvm)/(nu+num_obs)); + double temp_scal= as_scalar(temp_for_scal) ; + //Rcout << "Line 4156"; + //arma::mat covar_t=temp_scal*(I_test+w_tilde_M_inv*(W_tilde.t())); + arma::mat covar_t=temp_scal*(w_tilde_M_inv*(Treat_diff.t())); + + arma::mat catevartemp=temp_scal*(averagingvec.t()*w_tilde_M_inv*(Treat_diff.t())*averagingvec); + + //arma::mat W_tilde_L_inv_t= W_tilde*L_inv_t; + //arma::mat covar_t=temp_scal*(I_test+W_tilde_L_inv_t*(W_tilde_L_inv_t.t())); + + + + // Rcout << "Line 4459. i= "<< i << ".\n"; + + + + //double weight=exp(BICi[i]-(max_BIC+log(sum(exp(BICi-max_BIC))))); + + weighted_preds_all_models_arma.col(i)=preds_temp_arma*post_weights_arma(i); + + preds_all_models_arma.col(i)=preds_temp_arma; + t_vars_arma.col(i)=covar_t.diag(); + + cate_means_arma(i)=as_scalar(averagingvec.t()*preds_temp_arma); + cate_means_weighted_arma(i)=cate_means_arma(i)*post_weights_arma(i); + + cate_vars_arma(i)=as_scalar(catevartemp); + + } + + //} +#pragma omp barrier + + //arma::colvec predicted_values; + + //arma::mat M1(preds_all_models.begin(), preds_all_models.nrow(), preds_all_models.ncol(), false); + arma::colvec predicted_values=sum(weighted_preds_all_models_arma,1); + + double cate_pred=sum(cate_means_weighted_arma); + + //NumericMatrix draws_wrapped= wrap(draws_for_preds); + arma::mat output(3, num_obs); + //NumericVector probs_for_quantiles = NumericVector::create(lower_prob, 0.5, upper_prob); + + //std::vector probs_for_quantiles {lower_prob, 0.5, upper_prob}; + arma::mat cate_ints(3, 1); + + + + typedef std::vector stdvec; + std::vector weights_vec= as(post_weights); + + boost::math::students_t dist2(nu+num_obs); + double lq_tstandard= boost::math::quantile(dist2,lower_prob); + double med_tstandard= boost::math::quantile(dist2,0.5); //This is just 0 ?? + double uq_tstandard= boost::math::quantile(dist2,upper_prob); + + //Rcout << "cate_vars_arma line 13795" << cate_vars_arma << ".\n"; + //Rcout << "cate_means_arma" << cate_means_arma << ".\n"; + + if(weights_vec.size()==1){ + + cate_ints(0,0)= cate_means_arma(0)+sqrt(cate_vars_arma(0))*lq_tstandard; + cate_ints(1,0)= cate_means_arma(0)+sqrt(cate_vars_arma(0))*med_tstandard; + cate_ints(2,0)= cate_means_arma(0)+sqrt(cate_vars_arma(0))*uq_tstandard; + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(int i=0;i tempmeans= arma::conv_to::from(preds_all_models_arma.row(i)); + std::vector tempvars= arma::conv_to::from(t_vars_arma.row(i)); + + //boost::math::students_t dist2(nu+num_obs); + + //Rcout << "Line 13812 tempvars" << t_vars_arma.row(i) << ".\n"; + //Rcout << "tempmeans" << preds_all_models_arma.row(i) << ".\n"; + + + output(0,i)= tempmeans[0]+sqrt(tempvars[0])*lq_tstandard; + output(1,i)= tempmeans[0]+sqrt(tempvars[0])*med_tstandard; + output(2,i)= tempmeans[0]+sqrt(tempvars[0])*uq_tstandard; + + + } +#pragma omp barrier + }else{ + std::vector tempmeans_cate= arma::conv_to::from(cate_means_arma); + std::vector tempvars_cate= arma::conv_to::from(cate_vars_arma); + + std::vector bounds_lQ_cate = mixt_find_boundsQ( nu+num_obs, tempmeans_cate, tempvars_cate, lq_tstandard); + + //Rcout << "line 13828 cate_vars_arma = " << cate_vars_arma << ".\n"; + //Rcout << "cate_means_arma = " << cate_means_arma << ".\n"; + + //Rcout << "bounds_lQ_cate[0] = " << bounds_lQ_cate[0] << ".\n"; + //Rcout << "bounds_lQ_cate[1] = " << bounds_lQ_cate[1] << ".\n"; + + cate_ints(0,0)= rootmixt(nu+num_obs, + bounds_lQ_cate[0]-0.0001, + bounds_lQ_cate[1]+0.0001, + tempmeans_cate, + tempvars_cate, + weights_vec, lower_prob,root_alg_precision); + + std::vector bounds_med_cate = mixt_find_boundsQ( nu+num_obs, tempmeans_cate, tempvars_cate, med_tstandard); + + //Rcout << "bounds_lQ_cate[0] = " << bounds_lQ_cate[0] << ".\n"; + //Rcout << "bounds_lQ_cate[1] = " << bounds_lQ_cate[1] << ".\n"; + + cate_ints(1,0)= rootmixt(nu+num_obs, + bounds_med_cate[0]-0.0001, + bounds_med_cate[1]+0.0001, + tempmeans_cate, + tempvars_cate, + weights_vec, 0.5, root_alg_precision); + + std::vector bounds_uQ_cate = mixt_find_boundsQ( nu+num_obs, tempmeans_cate, tempvars_cate, uq_tstandard); + + //Rcout << "bounds_lQ_cate[0] = " << bounds_lQ_cate[0] << ".\n"; + //Rcout << "bounds_lQ_cate[1] = " << bounds_lQ_cate[1] << ".\n"; + + cate_ints(2,0)= rootmixt(nu+num_obs, + bounds_uQ_cate[0]-0.0001, + bounds_uQ_cate[1]+0.0001, + tempmeans_cate, + tempvars_cate, + weights_vec, upper_prob, root_alg_precision); + + //Rcout << "line 13871 cate_ints = " << cate_ints << ".\n"; + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(int i=0;i tempmeans= arma::conv_to::from(preds_all_models_arma.row(i)); + std::vector tempvars= arma::conv_to::from(t_vars_arma.row(i)); + + //Rcout << "Line 13859. tempvars" << t_vars_arma.row(i) << ".\n"; + //Rcout << "tempmeans" << preds_all_models_arma.row(i) << ".\n"; + + std::vector bounds_lQ = mixt_find_boundsQ( nu+num_obs, tempmeans, tempvars, lq_tstandard); + + output(0,i)=rootmixt(nu+num_obs, + bounds_lQ[0]-0.0001, + bounds_lQ[1]+0.0001, + tempmeans, + tempvars, + weights_vec, lower_prob,root_alg_precision); + + + std::vector bounds_med = mixt_find_boundsQ( nu+num_obs, tempmeans, tempvars, med_tstandard); + + output(1,i)=rootmixt(nu+num_obs, + bounds_med[0]-0.0001, + bounds_med[1]+0.0001, + tempmeans, + tempvars, + weights_vec, 0.5,root_alg_precision); + + std::vector bounds_uQ = mixt_find_boundsQ( nu+num_obs, tempmeans, tempvars, uq_tstandard); + + output(2,i)=rootmixt(nu+num_obs, + bounds_uQ[0]-0.0001, + bounds_uQ[1]+0.0001, + tempmeans, + tempvars, + weights_vec, upper_prob,root_alg_precision); + + + } +#pragma omp barrier + } + arma::mat output_rescaled(output.n_rows, output.n_cols); + + double min_y = min(y); + double max_y = max(y); + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(unsigned int i=0;i +#endif + + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List pred_ints_ITE_CATT_outsamp_par(List overall_sum_trees, + List overall_sum_mat, + NumericVector y, + NumericVector BIC_weights,//double min_possible,double max_possible, + int num_obs,int num_test_obs, + double a,double sigma,double mu_mu,double nu, + double lambda,//List resids, + NumericMatrix test_data, double lower_prob, double upper_prob, int num_cores, + double root_alg_precision, + NumericMatrix training_data,NumericVector ztest){ + + + //List termobs_testdata_overall= get_termobs_testdata_overall(overall_sum_trees,test_data); + + NumericVector onevec1(training_data.nrow(),1.0); + NumericMatrix Test1data= cbind(onevec1,test_data); + + NumericVector zerovec1(training_data.nrow(),0.0); + NumericMatrix Test0data= cbind(zerovec1,test_data); + + arma::field>> termobs_testdata1_overallF=get_termobs_testdata_fields_overall(overall_sum_trees,Test1data); + arma::field>> termobs_testdata0_overallF=get_termobs_testdata_fields_overall(overall_sum_trees,Test0data); + + //NumericMatrix preds_all_models(num_test_obs,BIC_weights.size()); + arma::mat preds_all_models_arma(num_test_obs,BIC_weights.size()); + arma::mat weighted_preds_all_models_arma(num_test_obs,BIC_weights.size()); + arma::mat t_vars_arma(num_test_obs,BIC_weights.size()); + + arma::vec cate_means_arma(BIC_weights.size()); + arma::vec cate_means_weighted_arma(BIC_weights.size()); + arma::vec cate_vars_arma(BIC_weights.size()); + + arma::vec catt_means_arma(BIC_weights.size()); + arma::vec catt_means_weighted_arma(BIC_weights.size()); + arma::vec catt_vars_arma(BIC_weights.size()); + + arma::vec catnt_means_arma(BIC_weights.size()); + arma::vec catnt_means_weighted_arma(BIC_weights.size()); + arma::vec catnt_vars_arma(BIC_weights.size()); + + // for all sums of trees + + NumericVector BICi=-0.5*BIC_weights; + double max_BIC=max(BICi); + + + NumericVector post_weights(BIC_weights.size()); + + for(int k=0;k(post_weights); + arma::vec ztest_arma = as(ztest); + + + //int num_models= BIC_weights.size(); + + + arma::vec averagingvec=(1/double(num_test_obs))*arma::ones(num_test_obs); + + arma::vec catt_averagingvec=(1/arma::sum(ztest_arma))*ztest_arma; + arma::vec catnt_averagingvec=(1/(num_test_obs-arma::sum(ztest_arma)))*(1-ztest_arma); + + + NumericVector y_scaled=scale_response(min(y),max(y),-0.5,0.5,y); + arma::vec yvec=Rcpp::as(y_scaled); + arma::mat y_arma(num_obs,1); + y_arma.col(0)=yvec; + arma::mat yty=y_arma.t()*y_arma; + + + //arma::mat I_test(num_test_obs,num_test_obs); + //I_test=I_test.eye(); + + //create field (armadillo list) of models + //each model is a field (armadillo list) of trees represented by matrices + arma::field> modelsF(overall_sum_trees.size()); + for(int i=0;i treesF(temp_tree_list.size()); + for(int q=0;q(temp_tree_list[q]); + treesF(q)=temp_tree_mat; + } + //Rcout << "Line 5669.\n"; + + modelsF(i)=treesF; + } + + + arma::field> matsF(overall_sum_mat.size()); + for(int i=0;i treesF(temp_tree_list.size()); + for(int q=0;q(temp_tree_list[q]); + treesF(q)=temp_tree_mat; + } + matsF(i)=treesF; + } + + + + + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(int i=0;i tree_list = modelsF(i); + + arma::mat W(num_obs,0); + int upsilon=0; + for(unsigned int j=0;j0){ + break; + } + } + + //end find_term_obs + + + //assign term_obs to the correct index of J + //NumericVector term_obs2=as(wrap(term_obs)); + //NumericVector obs_col(obs_to_nodes_temp.nrow()); + arma::vec obs_col= arma::zeros(tree_matrix_temp.n_rows); + //Rcout << "Line 5747.\n"; + obs_col.elem(term_obs)= arma::ones(term_obs.n_elem); + //Rcout << "Line 5749.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(i)= colmat; + } + + + + + W.insert_cols(upsilon,Jmat); + upsilon+=b_j; + } + + + //Rcout << "Line 5759.\n"; + + + //arma::mat W_tilde=get_W_test(overall_sum_trees[i],termobs_testdata_overall[i],num_test_obs); + + + //////////////////////////////////////// + + arma::mat W_tilde1(num_test_obs,0); + int upsilon2=0; + for(unsigned int j=0;j curr_termobs=(termobs_testdata1_overallF(i))(j); + + //begin find termnodes + //NumericVector tree_term_nodes=find_term_nodes(curr_tree); + + //arma::mat arma_tree(tree_table.begin(),tree_table.nrow(), tree_table.ncol(), false); + arma::vec colmat=curr_tree.col(4); + arma::uvec term_nodes=arma::find(colmat==-1); + term_nodes=term_nodes+1; + + //return(wrap(term_nodes)); + //Rcout << "Line 5782.\n"; + + //end find termnodes + + int b_j=term_nodes.size(); + //will make J as we go in BART-BMA no need to create it again here.... + //arma::mat Jmat=get_J_test(curr_termobs,tree_term_nodes,n); + //arma::mat Jmat=get_J_test(curr_termobs,term_nodes,num_test_obs); + + //begin J test function + arma::mat Jmat(num_test_obs, term_nodes.n_elem); + Jmat.zeros(); + + //for each terminal node get the observations associated with it and set column + for(unsigned int q=0;q(num_test_obs); + //Rcout << "Line 5809.\n"; + obs_col.elem(term_obs2)=arma::ones(term_obs2.n_elem); + //Rcout << "Line 5811.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(q)= colmat; + } + //return(Jmat); + + //end J test function + + W_tilde1.insert_cols(upsilon2,Jmat); + upsilon2+=b_j; + } + + //////////////////////////////////////// + + //////////////////////////////////////// + + arma::mat W_tilde0(num_test_obs,0); + int upsilon3=0; + for(unsigned int j=0;j curr_termobs=(termobs_testdata0_overallF(i))(j); + + //begin find termnodes + //NumericVector tree_term_nodes=find_term_nodes(curr_tree); + + //arma::mat arma_tree(tree_table.begin(),tree_table.nrow(), tree_table.ncol(), false); + arma::vec colmat=curr_tree.col(4); + arma::uvec term_nodes=arma::find(colmat==-1); + term_nodes=term_nodes+1; + + //return(wrap(term_nodes)); + //Rcout << "Line 5782.\n"; + + //end find termnodes + + int b_j=term_nodes.size(); + //will make J as we go in BART-BMA no need to create it again here.... + //arma::mat Jmat=get_J_test(curr_termobs,tree_term_nodes,n); + //arma::mat Jmat=get_J_test(curr_termobs,term_nodes,num_test_obs); + + //begin J test function + arma::mat Jmat(num_test_obs, term_nodes.n_elem); + Jmat.zeros(); + + //for each terminal node get the observations associated with it and set column + for(unsigned int q=0;q(num_test_obs); + //Rcout << "Line 5809.\n"; + obs_col.elem(term_obs2)=arma::ones(term_obs2.n_elem); + //Rcout << "Line 5811.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(q)= colmat; + } + //return(Jmat); + + //end J test function + + W_tilde0.insert_cols(upsilon3,Jmat); + upsilon3+=b_j; + } + + //////////////////////////////////////// + + //Rcout << "Line 5819.\n"; + + + double b=W.n_cols; + + + //get t(y)inv(psi)J + arma::mat ytW=y_arma.t()*W; + + + //get t(J)inv(psi)J + arma::mat WtW=W.t()*W; + + + + + + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + + + //arma::mat L_mat = arma::chol(sec_term,"lower"); + + //arma::mat L_inv = arma::inv(L_mat); + //arma::mat L_inv = arma::inv(arma::chol(sec_term)); + //arma::mat L_inv = arma::inv((L_mat)); + //arma::mat L_inv_t = arma::trans(arma::inv(trimatu(L_mat))); + //arma::mat L_inv_t = arma::trans(arma::inv((L_mat))); + + + + //arma::mat sec_term_inv=sec_term.i(); + arma::mat sec_term_inv=inv_sympd(sec_term); + + + //get t(J)inv(psi)y + arma::mat third_term=W.t()*y_arma; + + + //arma::mat coeff = solve(L_mat.t(), solve(L_mat, Wmat.t()*y_arma)); + //arma::mat coeff = L_inv.t()*L_inv*Wmat.t()*y_arma; + + + + //get m^TV^{-1}m + arma::mat mvm= ytW*sec_term_inv*third_term; + + arma::mat Treat_diff = W_tilde1-W_tilde0; + + arma::mat w_tilde_M_inv = Treat_diff*sec_term_inv; + + //Rcout << "Line 4151"; + + + + // //Obtain (lower triangular?) matrix t(L) by Cholesky decomposition such that sec_term_inv=L*t(L) + // arma::mat rooti = arma::trans(arma::inv(trimatu(arma::chol(sec_term)))); + // //obtain the log of the root of the determinant + // double rootisum = arma::sum(log(rooti.diag())); + // + // arma::mat LtWtY= rooti*(Wmat.t()*y); + // + // arma::mat rel=(b/2)*log(a)+rootisum -expon*log(nu*lambda - arma::sum(LtWtY%LtWtY) +yty); + // + // + + + // arma::mat rooti = arma::trans(arma::inv(trimatu(arma::chol(sec_term)))); + // arma::mat LtWtY= rooti*(Wmat.t()*y); + + + + arma::vec preds_temp_arma= w_tilde_M_inv*third_term; + + // arma::vec preds_temp_arma= W_tilde*sec_term_inv*(Wmat.t())*y_arma; + + //arma::vec preds_temp_arma= W_tilde*coeff; + //arma::mat mvm= coeff*y_arma; + //arma::mat mvm= y_arma.t()*Wmat*coeff; + + + + + + arma::mat temp_for_scal = ((nu*lambda+yty-mvm)/(nu+num_obs)); + double temp_scal= as_scalar(temp_for_scal) ; + //Rcout << "Line 4156"; + //arma::mat covar_t=temp_scal*(I_test+w_tilde_M_inv*(W_tilde.t())); + arma::mat covar_t=temp_scal*(w_tilde_M_inv*(Treat_diff.t())); + + arma::mat catevartemp=temp_scal*(averagingvec.t()*w_tilde_M_inv*(Treat_diff.t())*averagingvec); + + arma::mat cattvartemp=temp_scal*(catt_averagingvec.t()*w_tilde_M_inv*(Treat_diff.t())*catt_averagingvec); + arma::mat catntvartemp=temp_scal*(catnt_averagingvec.t()*w_tilde_M_inv*(Treat_diff.t())*catnt_averagingvec); + + //arma::mat W_tilde_L_inv_t= W_tilde*L_inv_t; + //arma::mat covar_t=temp_scal*(I_test+W_tilde_L_inv_t*(W_tilde_L_inv_t.t())); + + + + // Rcout << "Line 4459. i= "<< i << ".\n"; + + + + //double weight=exp(BICi[i]-(max_BIC+log(sum(exp(BICi-max_BIC))))); + + weighted_preds_all_models_arma.col(i)=preds_temp_arma*post_weights_arma[i]; + + preds_all_models_arma.col(i)=preds_temp_arma; + t_vars_arma.col(i)=covar_t.diag(); + + cate_means_arma(i)=as_scalar(averagingvec.t()*preds_temp_arma); + cate_means_weighted_arma(i)=cate_means_arma(i)*post_weights_arma(i); + + cate_vars_arma(i)=as_scalar(catevartemp); + + + catt_means_arma(i)=as_scalar(catt_averagingvec.t()*preds_temp_arma); + catt_means_weighted_arma(i)=catt_means_arma(i)*post_weights_arma(i); + + catt_vars_arma(i)=as_scalar(cattvartemp); + + + catnt_means_arma(i)=as_scalar(catnt_averagingvec.t()*preds_temp_arma); + catnt_means_weighted_arma(i)=catnt_means_arma(i)*post_weights_arma(i); + + catnt_vars_arma(i)=as_scalar(catntvartemp); + + + } + + //} +#pragma omp barrier + + //arma::colvec predicted_values; + + //arma::mat M1(preds_all_models.begin(), preds_all_models.nrow(), preds_all_models.ncol(), false); + arma::colvec predicted_values=sum(weighted_preds_all_models_arma,1); + + double cate_pred=sum(cate_means_weighted_arma); + double catt_pred=sum(catt_means_weighted_arma); + double catnt_pred=sum(catnt_means_weighted_arma); + + //NumericMatrix draws_wrapped= wrap(draws_for_preds); + arma::mat output(3, num_test_obs); + //NumericVector probs_for_quantiles = NumericVector::create(lower_prob, 0.5, upper_prob); + + //std::vector probs_for_quantiles {lower_prob, 0.5, upper_prob}; + arma::mat cate_ints(3, 1); + arma::mat catt_ints(3, 1); + arma::mat catnt_ints(3, 1); + + + + typedef std::vector stdvec; + std::vector weights_vec= as(post_weights); + + boost::math::students_t dist2(nu+num_obs); + double lq_tstandard= boost::math::quantile(dist2,lower_prob); + double med_tstandard= boost::math::quantile(dist2,0.5); //This is just 0 ?? + double uq_tstandard= boost::math::quantile(dist2,upper_prob); + + + if(weights_vec.size()==1){ + + cate_ints(0,0)= cate_means_arma(0)+sqrt(cate_vars_arma(0))*lq_tstandard; + cate_ints(1,0)= cate_means_arma(0)+sqrt(cate_vars_arma(0))*med_tstandard; + cate_ints(2,0)= cate_means_arma(0)+sqrt(cate_vars_arma(0))*uq_tstandard; + + catt_ints(0,0)= catt_means_arma(0)+sqrt(catt_vars_arma(0))*lq_tstandard; + catt_ints(1,0)= catt_means_arma(0)+sqrt(catt_vars_arma(0))*med_tstandard; + catt_ints(2,0)= catt_means_arma(0)+sqrt(catt_vars_arma(0))*uq_tstandard; + + catnt_ints(0,0)= catnt_means_arma(0)+sqrt(catnt_vars_arma(0))*lq_tstandard; + catnt_ints(1,0)= catnt_means_arma(0)+sqrt(catnt_vars_arma(0))*med_tstandard; + catnt_ints(2,0)= catnt_means_arma(0)+sqrt(catnt_vars_arma(0))*uq_tstandard; + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(int i=0;i tempmeans= arma::conv_to::from(preds_all_models_arma.row(i)); + std::vector tempvars= arma::conv_to::from(t_vars_arma.row(i)); + + //boost::math::students_t dist2(nu+num_obs); + + //Rcout << "Line 13812 tempvars" << t_vars_arma.row(i) << ".\n"; + //Rcout << "tempmeans" << preds_all_models_arma.row(i) << ".\n"; + + + output(0,i)= tempmeans[0]+sqrt(tempvars[0])*lq_tstandard; + output(1,i)= tempmeans[0]+sqrt(tempvars[0])*med_tstandard; + output(2,i)= tempmeans[0]+sqrt(tempvars[0])*uq_tstandard; + + + } +#pragma omp barrier + }else{ + std::vector tempmeans_cate= arma::conv_to::from(cate_means_arma); + std::vector tempvars_cate= arma::conv_to::from(cate_vars_arma); + + std::vector bounds_lQ_cate = mixt_find_boundsQ( nu+num_obs, tempmeans_cate, tempvars_cate, lq_tstandard); + + //Rcout << "line 13828 cate_vars_arma = " << cate_vars_arma << ".\n"; + //Rcout << "cate_means_arma = " << cate_means_arma << ".\n"; + + //Rcout << "bounds_lQ_cate[0] = " << bounds_lQ_cate[0] << ".\n"; + //Rcout << "bounds_lQ_cate[1] = " << bounds_lQ_cate[1] << ".\n"; + + cate_ints(0,0)= rootmixt(nu+num_obs, + bounds_lQ_cate[0]-0.0001, + bounds_lQ_cate[1]+0.0001, + tempmeans_cate, + tempvars_cate, + weights_vec, lower_prob,root_alg_precision); + + std::vector bounds_med_cate = mixt_find_boundsQ( nu+num_obs, tempmeans_cate, tempvars_cate, med_tstandard); + + //Rcout << "bounds_lQ_cate[0] = " << bounds_lQ_cate[0] << ".\n"; + //Rcout << "bounds_lQ_cate[1] = " << bounds_lQ_cate[1] << ".\n"; + + cate_ints(1,0)= rootmixt(nu+num_obs, + bounds_med_cate[0]-0.0001, + bounds_med_cate[1]+0.0001, + tempmeans_cate, + tempvars_cate, + weights_vec, 0.5, root_alg_precision); + + std::vector bounds_uQ_cate = mixt_find_boundsQ( nu+num_obs, tempmeans_cate, tempvars_cate, uq_tstandard); + + //Rcout << "bounds_lQ_cate[0] = " << bounds_lQ_cate[0] << ".\n"; + //Rcout << "bounds_lQ_cate[1] = " << bounds_lQ_cate[1] << ".\n"; + + cate_ints(2,0)= rootmixt(nu+num_obs, + bounds_uQ_cate[0]-0.0001, + bounds_uQ_cate[1]+0.0001, + tempmeans_cate, + tempvars_cate, + weights_vec, upper_prob, root_alg_precision); + + //Rcout << "line 13871 cate_ints = " << cate_ints << ".\n"; + + + // + + std::vector tempmeans_catt= arma::conv_to::from(catt_means_arma); + std::vector tempvars_catt= arma::conv_to::from(catt_vars_arma); + + std::vector bounds_lQ_catt = mixt_find_boundsQ( nu+num_obs, tempmeans_catt, tempvars_catt, lq_tstandard); + + + catt_ints(0,0)= rootmixt(nu+num_obs, + bounds_lQ_catt[0]-0.0001, + bounds_lQ_catt[1]+0.0001, + tempmeans_catt, + tempvars_catt, + weights_vec, lower_prob,root_alg_precision); + + std::vector bounds_med_catt = mixt_find_boundsQ( nu+num_obs, tempmeans_catt, tempvars_catt, med_tstandard); + + //Rcout << "bounds_lQ_catt[0] = " << bounds_lQ_catt[0] << ".\n"; + //Rcout << "bounds_lQ_catt[1] = " << bounds_lQ_catt[1] << ".\n"; + + catt_ints(1,0)= rootmixt(nu+num_obs, + bounds_med_catt[0]-0.0001, + bounds_med_catt[1]+0.0001, + tempmeans_catt, + tempvars_catt, + weights_vec, 0.5, root_alg_precision); + + std::vector bounds_uQ_catt = mixt_find_boundsQ( nu+num_obs, tempmeans_catt, tempvars_catt, uq_tstandard); + + //Rcout << "bounds_lQ_catt[0] = " << bounds_lQ_catt[0] << ".\n"; + //Rcout << "bounds_lQ_catt[1] = " << bounds_lQ_catt[1] << ".\n"; + + catt_ints(2,0)= rootmixt(nu+num_obs, + bounds_uQ_catt[0]-0.0001, + bounds_uQ_catt[1]+0.0001, + tempmeans_catt, + tempvars_catt, + weights_vec, upper_prob, root_alg_precision); + + + + // + + + std::vector tempmeans_catnt= arma::conv_to::from(catnt_means_arma); + std::vector tempvars_catnt= arma::conv_to::from(catnt_vars_arma); + + std::vector bounds_lQ_catnt = mixt_find_boundsQ( nu+num_obs, tempmeans_catnt, tempvars_catnt, lq_tstandard); + + + + catnt_ints(0,0)= rootmixt(nu+num_obs, + bounds_lQ_catnt[0]-0.0001, + bounds_lQ_catnt[1]+0.0001, + tempmeans_catnt, + tempvars_catnt, + weights_vec, lower_prob,root_alg_precision); + + std::vector bounds_med_catnt = mixt_find_boundsQ( nu+num_obs, tempmeans_catnt, tempvars_catnt, med_tstandard); + + //Rcout << "bounds_lQ_catnt[0] = " << bounds_lQ_catnt[0] << ".\n"; + //Rcout << "bounds_lQ_catnt[1] = " << bounds_lQ_catnt[1] << ".\n"; + + catnt_ints(1,0)= rootmixt(nu+num_obs, + bounds_med_catnt[0]-0.0001, + bounds_med_catnt[1]+0.0001, + tempmeans_catnt, + tempvars_catnt, + weights_vec, 0.5, root_alg_precision); + + std::vector bounds_uQ_catnt = mixt_find_boundsQ( nu+num_obs, tempmeans_catnt, tempvars_catnt, uq_tstandard); + + //Rcout << "bounds_lQ_catnt[0] = " << bounds_lQ_catnt[0] << ".\n"; + //Rcout << "bounds_lQ_catnt[1] = " << bounds_lQ_catnt[1] << ".\n"; + + catnt_ints(2,0)= rootmixt(nu+num_obs, + bounds_uQ_catnt[0]-0.0001, + bounds_uQ_catnt[1]+0.0001, + tempmeans_catnt, + tempvars_catnt, + weights_vec, upper_prob, root_alg_precision); + + + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(int i=0;i tempmeans= arma::conv_to::from(preds_all_models_arma.row(i)); + std::vector tempvars= arma::conv_to::from(t_vars_arma.row(i)); + + //Rcout << "Line 13859. tempvars" << t_vars_arma.row(i) << ".\n"; + //Rcout << "tempmeans" << preds_all_models_arma.row(i) << ".\n"; + + std::vector bounds_lQ = mixt_find_boundsQ( nu+num_obs, tempmeans, tempvars, lq_tstandard); + + output(0,i)=rootmixt(nu+num_obs, + bounds_lQ[0]-0.0001, + bounds_lQ[1]+0.0001, + tempmeans, + tempvars, + weights_vec, lower_prob,root_alg_precision); + + + std::vector bounds_med = mixt_find_boundsQ( nu+num_obs, tempmeans, tempvars, med_tstandard); + + output(1,i)=rootmixt(nu+num_obs, + bounds_med[0]-0.0001, + bounds_med[1]+0.0001, + tempmeans, + tempvars, + weights_vec, 0.5,root_alg_precision); + + std::vector bounds_uQ = mixt_find_boundsQ( nu+num_obs, tempmeans, tempvars, uq_tstandard); + + output(2,i)=rootmixt(nu+num_obs, + bounds_uQ[0]-0.0001, + bounds_uQ[1]+0.0001, + tempmeans, + tempvars, + weights_vec, upper_prob,root_alg_precision); + + + } +#pragma omp barrier + } + + arma::mat output_rescaled(output.n_rows, output.n_cols); + + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(unsigned int i=0;i +#endif + + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List pred_ints_ITE_CATT_insamp_par(List overall_sum_trees, + List overall_sum_mat, + NumericVector y, + NumericVector BIC_weights,//double min_possible,double max_possible, + int num_obs,//int num_test_obs, + double a,double sigma, + double mu_mu,double nu, + double lambda,//List resids,NumericMatrix test_data, + double lower_prob, double upper_prob, + int num_cores, + double root_alg_precision, + NumericMatrix training_data, + NumericVector ztrain){ + + + //List termobs_testdata_overall= get_termobs_testdata_overall(overall_sum_trees,test_data); + + NumericVector onevec1(training_data.nrow(),1.0); + NumericMatrix Training1data= cbind(onevec1,training_data); + + NumericVector zerovec1(training_data.nrow(),0.0); + NumericMatrix Training0data= cbind(zerovec1,training_data); + + arma::field>> termobs_testdata1_overallF=get_termobs_testdata_fields_overall(overall_sum_trees,Training1data); + arma::field>> termobs_testdata0_overallF=get_termobs_testdata_fields_overall(overall_sum_trees,Training0data); + + //NumericMatrix preds_all_models(num_test_obs,BIC_weights.size()); + arma::mat preds_all_models_arma(num_obs,BIC_weights.size()); + arma::mat weighted_preds_all_models_arma(num_obs,BIC_weights.size()); + arma::mat t_vars_arma(num_obs,BIC_weights.size()); + + arma::vec cate_means_arma(BIC_weights.size()); + arma::vec cate_means_weighted_arma(BIC_weights.size()); + arma::vec cate_vars_arma(BIC_weights.size()); + + arma::vec catt_means_arma(BIC_weights.size()); + arma::vec catt_means_weighted_arma(BIC_weights.size()); + arma::vec catt_vars_arma(BIC_weights.size()); + + arma::vec catnt_means_arma(BIC_weights.size()); + arma::vec catnt_means_weighted_arma(BIC_weights.size()); + arma::vec catnt_vars_arma(BIC_weights.size()); + + // for all sums of trees + + NumericVector BICi=-0.5*BIC_weights; + double max_BIC=max(BICi); + + + NumericVector post_weights(BIC_weights.size()); + + for(int k=0;k(post_weights); + + + arma::vec ztrain_arma = as(ztrain); + + + + arma::vec averagingvec=(1/double(num_obs))*arma::ones(num_obs); + + arma::vec catt_averagingvec=(1/arma::sum(ztrain_arma))*ztrain_arma; + arma::vec catnt_averagingvec=(1/(num_obs-arma::sum(ztrain_arma)))*(1-ztrain_arma); + + + NumericVector y_scaled=scale_response(min(y),max(y),-0.5,0.5,y); + arma::vec yvec=Rcpp::as(y_scaled); + arma::mat y_arma(num_obs,1); + y_arma.col(0)=yvec; + arma::mat yty=y_arma.t()*y_arma; + + + //arma::mat I_test(num_test_obs,num_test_obs); + //I_test=I_test.eye(); + + //create field (armadillo list) of models + //each model is a field (armadillo list) of trees represented by matrices + arma::field> modelsF(overall_sum_trees.size()); + for(int i=0;i treesF(temp_tree_list.size()); + for(int q=0;q(temp_tree_list[q]); + treesF(q)=temp_tree_mat; + } + //Rcout << "Line 5669.\n"; + + modelsF(i)=treesF; + } + + + arma::field> matsF(overall_sum_mat.size()); + for(int i=0;i treesF(temp_tree_list.size()); + for(int q=0;q(temp_tree_list[q]); + treesF(q)=temp_tree_mat; + } + matsF(i)=treesF; + } + + + + + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(int i=0;i tree_list = modelsF(i); + + arma::mat W(num_obs,0); + int upsilon=0; + for(unsigned int j=0;j0){ + break; + } + } + + //end find_term_obs + + + //assign term_obs to the correct index of J + //NumericVector term_obs2=as(wrap(term_obs)); + //NumericVector obs_col(obs_to_nodes_temp.nrow()); + arma::vec obs_col= arma::zeros(tree_matrix_temp.n_rows); + //Rcout << "Line 5747.\n"; + obs_col.elem(term_obs)= arma::ones(term_obs.n_elem); + //Rcout << "Line 5749.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(i)= colmat; + } + + + + + W.insert_cols(upsilon,Jmat); + upsilon+=b_j; + } + + + //Rcout << "Line 5759.\n"; + + + //arma::mat W_tilde=get_W_test(overall_sum_trees[i],termobs_testdata_overall[i],num_test_obs); + + + //////////////////////////////////////// + + arma::mat W_tilde1(num_obs,0); + int upsilon2=0; + for(unsigned int j=0;j curr_termobs=(termobs_testdata1_overallF(i))(j); + + //begin find termnodes + //NumericVector tree_term_nodes=find_term_nodes(curr_tree); + + //arma::mat arma_tree(tree_table.begin(),tree_table.nrow(), tree_table.ncol(), false); + arma::vec colmat=curr_tree.col(4); + arma::uvec term_nodes=arma::find(colmat==-1); + term_nodes=term_nodes+1; + + //return(wrap(term_nodes)); + //Rcout << "Line 5782.\n"; + + //end find termnodes + + int b_j=term_nodes.size(); + //will make J as we go in BART-BMA no need to create it again here.... + //arma::mat Jmat=get_J_test(curr_termobs,tree_term_nodes,n); + //arma::mat Jmat=get_J_test(curr_termobs,term_nodes,num_test_obs); + + //begin J test function + arma::mat Jmat(num_obs, term_nodes.n_elem); + Jmat.zeros(); + + //for each terminal node get the observations associated with it and set column + for(unsigned int q=0;q(num_obs); + //Rcout << "Line 5809.\n"; + obs_col.elem(term_obs2)=arma::ones(term_obs2.n_elem); + //Rcout << "Line 5811.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(q)= colmat; + } + //return(Jmat); + + //end J test function + + W_tilde1.insert_cols(upsilon2,Jmat); + upsilon2+=b_j; + } + + //////////////////////////////////////// + + //////////////////////////////////////// + + arma::mat W_tilde0(num_obs,0); + int upsilon3=0; + for(unsigned int j=0;j curr_termobs=(termobs_testdata0_overallF(i))(j); + + //begin find termnodes + //NumericVector tree_term_nodes=find_term_nodes(curr_tree); + + //arma::mat arma_tree(tree_table.begin(),tree_table.nrow(), tree_table.ncol(), false); + arma::vec colmat=curr_tree.col(4); + arma::uvec term_nodes=arma::find(colmat==-1); + term_nodes=term_nodes+1; + + //return(wrap(term_nodes)); + //Rcout << "Line 5782.\n"; + + //end find termnodes + + int b_j=term_nodes.size(); + //will make J as we go in BART-BMA no need to create it again here.... + //arma::mat Jmat=get_J_test(curr_termobs,tree_term_nodes,n); + //arma::mat Jmat=get_J_test(curr_termobs,term_nodes,num_test_obs); + + //begin J test function + arma::mat Jmat(num_obs, term_nodes.n_elem); + Jmat.zeros(); + + //for each terminal node get the observations associated with it and set column + for(unsigned int q=0;q(num_obs); + //Rcout << "Line 5809.\n"; + obs_col.elem(term_obs2)=arma::ones(term_obs2.n_elem); + //Rcout << "Line 5811.\n"; + //arma::vec colmat=Rcpp::as(obs_col); + Jmat.col(q)= obs_col; + + // arma::vec colmat=arma::zeros(Jmat.n_rows) ;// colmattest(Jmat.n_rows,0); + // colmat.elem(term_obs).fill(1); + // Jmat.col(q)= colmat; + } + //return(Jmat); + + //end J test function + + W_tilde0.insert_cols(upsilon3,Jmat); + upsilon3+=b_j; + } + + //////////////////////////////////////// + + //Rcout << "Line 5819.\n"; + + + double b=W.n_cols; + + + //get t(y)inv(psi)J + arma::mat ytW=y_arma.t()*W; + + + //get t(J)inv(psi)J + arma::mat WtW=W.t()*W; + + + + + + //get jpsij +aI + arma::mat aI(b,b); + aI=a*aI.eye(); + arma::mat sec_term=WtW+aI; + + + //arma::mat L_mat = arma::chol(sec_term,"lower"); + + //arma::mat L_inv = arma::inv(L_mat); + //arma::mat L_inv = arma::inv(arma::chol(sec_term)); + //arma::mat L_inv = arma::inv((L_mat)); + //arma::mat L_inv_t = arma::trans(arma::inv(trimatu(L_mat))); + //arma::mat L_inv_t = arma::trans(arma::inv((L_mat))); + + + + //arma::mat sec_term_inv=sec_term.i(); + arma::mat sec_term_inv=inv_sympd(sec_term); + + + //get t(J)inv(psi)y + arma::mat third_term=W.t()*y_arma; + + + //arma::mat coeff = solve(L_mat.t(), solve(L_mat, Wmat.t()*y_arma)); + //arma::mat coeff = L_inv.t()*L_inv*Wmat.t()*y_arma; + + + + //get m^TV^{-1}m + arma::mat mvm= ytW*sec_term_inv*third_term; + + arma::mat Treat_diff = W_tilde1-W_tilde0; + + arma::mat w_tilde_M_inv = Treat_diff*sec_term_inv; + + //Rcout << "Line 4151"; + + + + // //Obtain (lower triangular?) matrix t(L) by Cholesky decomposition such that sec_term_inv=L*t(L) + // arma::mat rooti = arma::trans(arma::inv(trimatu(arma::chol(sec_term)))); + // //obtain the log of the root of the determinant + // double rootisum = arma::sum(log(rooti.diag())); + // + // arma::mat LtWtY= rooti*(Wmat.t()*y); + // + // arma::mat rel=(b/2)*log(a)+rootisum -expon*log(nu*lambda - arma::sum(LtWtY%LtWtY) +yty); + // + // + + + // arma::mat rooti = arma::trans(arma::inv(trimatu(arma::chol(sec_term)))); + // arma::mat LtWtY= rooti*(Wmat.t()*y); + + + + arma::vec preds_temp_arma= w_tilde_M_inv*third_term; + + // arma::vec preds_temp_arma= W_tilde*sec_term_inv*(Wmat.t())*y_arma; + + //arma::vec preds_temp_arma= W_tilde*coeff; + //arma::mat mvm= coeff*y_arma; + //arma::mat mvm= y_arma.t()*Wmat*coeff; + + + + + + arma::mat temp_for_scal = ((nu*lambda+yty-mvm)/(nu+num_obs)); + double temp_scal= as_scalar(temp_for_scal) ; + //Rcout << "Line 4156"; + //arma::mat covar_t=temp_scal*(I_test+w_tilde_M_inv*(W_tilde.t())); + arma::mat covar_t=temp_scal*(w_tilde_M_inv*(Treat_diff.t())); + + arma::mat catevartemp=temp_scal*(averagingvec.t()*w_tilde_M_inv*(Treat_diff.t())*averagingvec); + arma::mat cattvartemp=temp_scal*(catt_averagingvec.t()*w_tilde_M_inv*(Treat_diff.t())*catt_averagingvec); + arma::mat catntvartemp=temp_scal*(catnt_averagingvec.t()*w_tilde_M_inv*(Treat_diff.t())*catnt_averagingvec); + + //arma::mat W_tilde_L_inv_t= W_tilde*L_inv_t; + //arma::mat covar_t=temp_scal*(I_test+W_tilde_L_inv_t*(W_tilde_L_inv_t.t())); + + + + // Rcout << "Line 4459. i= "<< i << ".\n"; + + + + //double weight=exp(BICi[i]-(max_BIC+log(sum(exp(BICi-max_BIC))))); + + weighted_preds_all_models_arma.col(i)=preds_temp_arma*post_weights_arma[i]; + + preds_all_models_arma.col(i)=preds_temp_arma; + t_vars_arma.col(i)=covar_t.diag(); + + cate_means_arma(i)=as_scalar(averagingvec.t()*preds_temp_arma); + cate_means_weighted_arma(i)=cate_means_arma(i)*post_weights_arma(i); + + cate_vars_arma(i)=as_scalar(catevartemp); + + + catt_means_arma(i)=as_scalar(catt_averagingvec.t()*preds_temp_arma); + catt_means_weighted_arma(i)=catt_means_arma(i)*post_weights_arma(i); + + catt_vars_arma(i)=as_scalar(cattvartemp); + + + catnt_means_arma(i)=as_scalar(catnt_averagingvec.t()*preds_temp_arma); + catnt_means_weighted_arma(i)=catnt_means_arma(i)*post_weights_arma(i); + + catnt_vars_arma(i)=as_scalar(catntvartemp); + + } + + //} +#pragma omp barrier + + //arma::colvec predicted_values; + + //arma::mat M1(preds_all_models.begin(), preds_all_models.nrow(), preds_all_models.ncol(), false); + arma::colvec predicted_values=sum(weighted_preds_all_models_arma,1); + + double cate_pred=sum(cate_means_weighted_arma); + double catt_pred=sum(catt_means_weighted_arma); + double catnt_pred=sum(catnt_means_weighted_arma); + + //NumericMatrix draws_wrapped= wrap(draws_for_preds); + arma::mat output(3, num_obs); + //NumericVector probs_for_quantiles = NumericVector::create(lower_prob, 0.5, upper_prob); + + //std::vector probs_for_quantiles {lower_prob, 0.5, upper_prob}; + arma::mat cate_ints(3, 1); + arma::mat catt_ints(3, 1); + arma::mat catnt_ints(3, 1); + + + typedef std::vector stdvec; + std::vector weights_vec= as(post_weights); + + boost::math::students_t dist2(nu+num_obs); + double lq_tstandard= boost::math::quantile(dist2,lower_prob); + double med_tstandard= boost::math::quantile(dist2,0.5); //This is just 0 ?? + double uq_tstandard= boost::math::quantile(dist2,upper_prob); + + //Rcout << "cate_vars_arma line 13795" << cate_vars_arma << ".\n"; + //Rcout << "cate_means_arma" << cate_means_arma << ".\n"; + + if(weights_vec.size()==1){ + + cate_ints(0,0)= cate_means_arma(0)+sqrt(cate_vars_arma(0))*lq_tstandard; + cate_ints(1,0)= cate_means_arma(0)+sqrt(cate_vars_arma(0))*med_tstandard; + cate_ints(2,0)= cate_means_arma(0)+sqrt(cate_vars_arma(0))*uq_tstandard; + + catt_ints(0,0)= catt_means_arma(0)+sqrt(catt_vars_arma(0))*lq_tstandard; + catt_ints(1,0)= catt_means_arma(0)+sqrt(catt_vars_arma(0))*med_tstandard; + catt_ints(2,0)= catt_means_arma(0)+sqrt(catt_vars_arma(0))*uq_tstandard; + + catnt_ints(0,0)= catnt_means_arma(0)+sqrt(catnt_vars_arma(0))*lq_tstandard; + catnt_ints(1,0)= catnt_means_arma(0)+sqrt(catnt_vars_arma(0))*med_tstandard; + catnt_ints(2,0)= catnt_means_arma(0)+sqrt(catnt_vars_arma(0))*uq_tstandard; + + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(int i=0;i tempmeans= arma::conv_to::from(preds_all_models_arma.row(i)); + std::vector tempvars= arma::conv_to::from(t_vars_arma.row(i)); + + //boost::math::students_t dist2(nu+num_obs); + + //Rcout << "Line 13812 tempvars" << t_vars_arma.row(i) << ".\n"; + //Rcout << "tempmeans" << preds_all_models_arma.row(i) << ".\n"; + + + output(0,i)= tempmeans[0]+sqrt(tempvars[0])*lq_tstandard; + output(1,i)= tempmeans[0]+sqrt(tempvars[0])*med_tstandard; + output(2,i)= tempmeans[0]+sqrt(tempvars[0])*uq_tstandard; + + + } +#pragma omp barrier + }else{ + std::vector tempmeans_cate= arma::conv_to::from(cate_means_arma); + std::vector tempvars_cate= arma::conv_to::from(cate_vars_arma); + + std::vector bounds_lQ_cate = mixt_find_boundsQ( nu+num_obs, tempmeans_cate, tempvars_cate, lq_tstandard); + + //Rcout << "line 13828 cate_vars_arma = " << cate_vars_arma << ".\n"; + //Rcout << "cate_means_arma = " << cate_means_arma << ".\n"; + + //Rcout << "bounds_lQ_cate[0] = " << bounds_lQ_cate[0] << ".\n"; + //Rcout << "bounds_lQ_cate[1] = " << bounds_lQ_cate[1] << ".\n"; + + cate_ints(0,0)= rootmixt(nu+num_obs, + bounds_lQ_cate[0]-0.0001, + bounds_lQ_cate[1]+0.0001, + tempmeans_cate, + tempvars_cate, + weights_vec, lower_prob,root_alg_precision); + + std::vector bounds_med_cate = mixt_find_boundsQ( nu+num_obs, tempmeans_cate, tempvars_cate, med_tstandard); + + //Rcout << "bounds_lQ_cate[0] = " << bounds_lQ_cate[0] << ".\n"; + //Rcout << "bounds_lQ_cate[1] = " << bounds_lQ_cate[1] << ".\n"; + + cate_ints(1,0)= rootmixt(nu+num_obs, + bounds_med_cate[0]-0.0001, + bounds_med_cate[1]+0.0001, + tempmeans_cate, + tempvars_cate, + weights_vec, 0.5, root_alg_precision); + + std::vector bounds_uQ_cate = mixt_find_boundsQ( nu+num_obs, tempmeans_cate, tempvars_cate, uq_tstandard); + + //Rcout << "bounds_lQ_cate[0] = " << bounds_lQ_cate[0] << ".\n"; + //Rcout << "bounds_lQ_cate[1] = " << bounds_lQ_cate[1] << ".\n"; + + cate_ints(2,0)= rootmixt(nu+num_obs, + bounds_uQ_cate[0]-0.0001, + bounds_uQ_cate[1]+0.0001, + tempmeans_cate, + tempvars_cate, + weights_vec, upper_prob, root_alg_precision); + + //Rcout << "line 13871 cate_ints = " << cate_ints << ".\n"; + + + + + // + + std::vector tempmeans_catt= arma::conv_to::from(catt_means_arma); + std::vector tempvars_catt= arma::conv_to::from(catt_vars_arma); + + std::vector bounds_lQ_catt = mixt_find_boundsQ( nu+num_obs, tempmeans_catt, tempvars_catt, lq_tstandard); + + + catt_ints(0,0)= rootmixt(nu+num_obs, + bounds_lQ_catt[0]-0.0001, + bounds_lQ_catt[1]+0.0001, + tempmeans_catt, + tempvars_catt, + weights_vec, lower_prob,root_alg_precision); + + std::vector bounds_med_catt = mixt_find_boundsQ( nu+num_obs, tempmeans_catt, tempvars_catt, med_tstandard); + + //Rcout << "bounds_lQ_catt[0] = " << bounds_lQ_catt[0] << ".\n"; + //Rcout << "bounds_lQ_catt[1] = " << bounds_lQ_catt[1] << ".\n"; + + catt_ints(1,0)= rootmixt(nu+num_obs, + bounds_med_catt[0]-0.0001, + bounds_med_catt[1]+0.0001, + tempmeans_catt, + tempvars_catt, + weights_vec, 0.5, root_alg_precision); + + std::vector bounds_uQ_catt = mixt_find_boundsQ( nu+num_obs, tempmeans_catt, tempvars_catt, uq_tstandard); + + //Rcout << "bounds_lQ_catt[0] = " << bounds_lQ_catt[0] << ".\n"; + //Rcout << "bounds_lQ_catt[1] = " << bounds_lQ_catt[1] << ".\n"; + + catt_ints(2,0)= rootmixt(nu+num_obs, + bounds_uQ_catt[0]-0.0001, + bounds_uQ_catt[1]+0.0001, + tempmeans_catt, + tempvars_catt, + weights_vec, upper_prob, root_alg_precision); + + + + // + + + std::vector tempmeans_catnt= arma::conv_to::from(catnt_means_arma); + std::vector tempvars_catnt= arma::conv_to::from(catnt_vars_arma); + + std::vector bounds_lQ_catnt = mixt_find_boundsQ( nu+num_obs, tempmeans_catnt, tempvars_catnt, lq_tstandard); + + + + catnt_ints(0,0)= rootmixt(nu+num_obs, + bounds_lQ_catnt[0]-0.0001, + bounds_lQ_catnt[1]+0.0001, + tempmeans_catnt, + tempvars_catnt, + weights_vec, lower_prob,root_alg_precision); + + std::vector bounds_med_catnt = mixt_find_boundsQ( nu+num_obs, tempmeans_catnt, tempvars_catnt, med_tstandard); + + //Rcout << "bounds_lQ_catnt[0] = " << bounds_lQ_catnt[0] << ".\n"; + //Rcout << "bounds_lQ_catnt[1] = " << bounds_lQ_catnt[1] << ".\n"; + + catnt_ints(1,0)= rootmixt(nu+num_obs, + bounds_med_catnt[0]-0.0001, + bounds_med_catnt[1]+0.0001, + tempmeans_catnt, + tempvars_catnt, + weights_vec, 0.5, root_alg_precision); + + std::vector bounds_uQ_catnt = mixt_find_boundsQ( nu+num_obs, tempmeans_catnt, tempvars_catnt, uq_tstandard); + + //Rcout << "bounds_lQ_catnt[0] = " << bounds_lQ_catnt[0] << ".\n"; + //Rcout << "bounds_lQ_catnt[1] = " << bounds_lQ_catnt[1] << ".\n"; + + catnt_ints(2,0)= rootmixt(nu+num_obs, + bounds_uQ_catnt[0]-0.0001, + bounds_uQ_catnt[1]+0.0001, + tempmeans_catnt, + tempvars_catnt, + weights_vec, upper_prob, root_alg_precision); + + + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(int i=0;i tempmeans= arma::conv_to::from(preds_all_models_arma.row(i)); + std::vector tempvars= arma::conv_to::from(t_vars_arma.row(i)); + + //Rcout << "Line 13859. tempvars" << t_vars_arma.row(i) << ".\n"; + //Rcout << "tempmeans" << preds_all_models_arma.row(i) << ".\n"; + + std::vector bounds_lQ = mixt_find_boundsQ( nu+num_obs, tempmeans, tempvars, lq_tstandard); + + output(0,i)=rootmixt(nu+num_obs, + bounds_lQ[0]-0.0001, + bounds_lQ[1]+0.0001, + tempmeans, + tempvars, + weights_vec, lower_prob,root_alg_precision); + + + std::vector bounds_med = mixt_find_boundsQ( nu+num_obs, tempmeans, tempvars, med_tstandard); + + output(1,i)=rootmixt(nu+num_obs, + bounds_med[0]-0.0001, + bounds_med[1]+0.0001, + tempmeans, + tempvars, + weights_vec, 0.5,root_alg_precision); + + std::vector bounds_uQ = mixt_find_boundsQ( nu+num_obs, tempmeans, tempvars, uq_tstandard); + + output(2,i)=rootmixt(nu+num_obs, + bounds_uQ[0]-0.0001, + bounds_uQ[1]+0.0001, + tempmeans, + tempvars, + weights_vec, upper_prob,root_alg_precision); + + + } +#pragma omp barrier + } + + arma::mat output_rescaled(output.n_rows, output.n_cols); + + +#pragma omp parallel num_threads(num_cores) +#pragma omp for + for(unsigned int i=0;i +#include +using namespace Rcpp; +// [[Rcpp::export]] + +NumericVector find_term_nodes_gs(NumericMatrix tree_table){ + //NumericVector terminal_nodes; + arma::mat arma_tree(tree_table.begin(),tree_table.nrow(), tree_table.ncol(), false); + + arma::vec colmat=arma_tree.col(4); + arma::uvec term_nodes=arma::find(colmat==-1); + term_nodes=term_nodes+1; + return(wrap(term_nodes)); +} + +//################################################################################################################################// +//[[Rcpp::depends(RcppArmadillo)]] +//[[Rcpp::export]] + +NumericVector find_term_obs_gs(NumericMatrix tree_matrix_temp,double terminal_node){ + //NumericVector term_obs; + arma::mat arma_tree_mat(tree_matrix_temp.begin(),tree_matrix_temp.nrow(), tree_matrix_temp.ncol(), false); + arma::uvec term_obs; + + for(int j=0;j0){ + break; + } + } + + return(wrap(term_obs)); +} +//################################################################################################################################// +// [[Rcpp::export]] +NumericVector calc_rowsums(NumericMatrix predictions){ + arma::mat M1(predictions.begin(), predictions.nrow(), predictions.ncol(), false); + arma::colvec predicted_values=sum(M1,1); + return(wrap(predicted_values)); +} +//############################################################################################// +// [[Rcpp::export]] +NumericVector calculate_resids(NumericMatrix predictions,NumericVector response){ + NumericVector resids=response.size(); + NumericVector row_sums=calc_rowsums(predictions); + resids=response - row_sums; + return(resids); +} + +//################################################################################################################################// +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] +List update_Gibbs_mean_var(//NumericMatrix tree_table, + NumericVector resids,double a,double sigma,double mu_mu,IntegerVector terminal_nodes, List term_obs_tree){ + List update_params(2); + + NumericVector mu_ij(terminal_nodes.size()); + NumericVector Tj(terminal_nodes.size()); + NumericVector new_mean(terminal_nodes.size()); + NumericVector new_var(terminal_nodes.size()); + + for(int k=0;k< terminal_nodes.size();k++){ + //update the node means + //find which observations are assigned to terminal node k in tree i + + IntegerVector term_obs=term_obs_tree[k]; + + //get the number of observations in node k + NumericVector temp_obs=resids[term_obs]; + mu_ij[k]=std::accumulate(temp_obs.begin(),temp_obs.end(), 0.0); + Tj[k]=term_obs.size(); + + new_mean[k]=(mu_ij[k]+a*mu_mu)/(Tj[k]+a); + + new_var[k]=(1/((1/(pow(sigma,2)))*(Tj[k]+a))); + + //NumericVector temp; + //term_obs=temp; + + } + + update_params[0]=new_mean; + update_params[1]=new_var; + + return(update_params); +} +//################################################################################################################################// + +// [[Rcpp::export]] +double update_sigma(double a1,double b,NumericVector resids,int n){ + NumericVector sq_resid=resids*resids; + double ssr= std::accumulate(sq_resid.begin(),sq_resid.end(),0.0); + double shape=(a1+n/2); + double rate =((ssr/2)+(1/b)); + RNGScope scope; + //double tau =shape/rate; + + double tau =R::rgamma(shape,1/rate); + double sigma = sqrt((1/tau)); + return sigma; +} + +//################################################################################################################################// +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +NumericVector find_node_means(NumericMatrix sum_tree, NumericVector term_nodes){ + NumericVector means=sum_tree(_,5); + arma::vec node_means = as(means); + arma::uvec arma_term_nodes=as(term_nodes); + arma_term_nodes=arma_term_nodes-1; + arma::vec final_means=node_means.elem(arma_term_nodes); + return(wrap(final_means)); + +} +//################################################################################################################################// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +List get_tree_info(List overall_sum_trees,List overall_sum_mat,int num_obs){ + List overall_term_nodes_trees(overall_sum_trees.size()); + List overall_term_obs_trees(overall_sum_trees.size()); + List overall_predictions(overall_sum_trees.size()); + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + List sum_tree=overall_sum_trees[i]; + + List sum_tree_mat=overall_sum_mat[i]; + + //save all info in list of list format the same as the trees. + + List term_nodes_trees(sum_tree.size()); + List term_obs_trees(sum_tree.size()); + NumericMatrix predictions(num_obs,sum_tree.size()); + + for(int k =0;k(predy); + M.shed_col(i); + NumericMatrix s=as(wrap(M)); + return(s); +} +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// [[Rcpp::export]] +NumericVector get_new_mean(IntegerVector terminal_nodes,List new_mean_var){ + NumericVector node_means; + for(int k=0;k +using namespace Rcpp; + +// [[Rcpp::export]] + +List get_tree_info_test_data(NumericMatrix test_data,NumericMatrix tree_data) { + // Function to make predictions from test data, given a single tree and the terminal node predictions, this function will be called + //for each tree accepted in Occam's Window. + + //test_data is a nxp matrix with the same variable names as the training data the model was built on + + //tree_data is the tree table with the tree information i.e. split points and split variables and terminal node mean values + + //term_node_means is a vector storing the terminal node mean values + + arma::mat arma_tree(tree_data.begin(), tree_data.nrow(), tree_data.ncol(), false); + arma::mat testd(test_data.begin(), test_data.nrow(), test_data.ncol(), false); + //NumericVector internal_nodes=find_internal_nodes_gs(tree_data); + NumericVector terminal_nodes=find_term_nodes_gs(tree_data); + //arma::vec arma_terminal_nodes=Rcpp::as(terminal_nodes); + NumericVector tree_predictions; + + //now for each internal node find the observations that belong to the terminal nodes + + NumericVector predictions(test_data.nrow()); + List term_obs(terminal_nodes.size()); + if(terminal_nodes.size()==1){ + double nodemean=tree_data(terminal_nodes[0]-1,5); // let nodemean equal tree_data row terminal_nodes[i]^th row , 6th column. The minus 1 is because terminal nodes consists of indices starting at 1, but need indices to start at 0. + predictions=rep(nodemean,test_data.nrow()); + IntegerVector temp_obsvec = seq_len(test_data.nrow())-1; + term_obs[0]= temp_obsvec; + } + else{ + for(int i=0;i temp_split); + } + + arma::uvec temp_pred_indices; + arma::vec data_subset = testd.col(split); + data_subset=data_subset.elem(pred_indices); + + //now loop through each row of node_split_mat + int n=node_split_mat.n_rows; + + for(int j=1;j split_p); + } + pred_indices=pred_indices.elem(temp_pred_indices); + + if(pred_indices.size()==0){ + continue; + } + + } + + double nodemean=tree_data(terminal_nodes[i]-1,5); + IntegerVector predind=as(wrap(pred_indices)); + predictions[predind]= nodemean; + term_obs[i]=predind; + } + } + List ret(3); + ret[0] = terminal_nodes; + ret[1] = term_obs; + ret[2] = predictions; + return(ret); +} +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +// [[Rcpp::depends(RcppArmadillo)]] +// [[Rcpp::export]] + +List get_tree_info_testdata_overall(List overall_sum_trees,int num_obs,NumericMatrix test_data){ + + List overall_term_nodes_trees(overall_sum_trees.size()); + List overall_term_obs_trees(overall_sum_trees.size()); + List overall_predictions(overall_sum_trees.size()); + + for(int i=0;i(s)){ + //if current set of trees contains more than one tree...usually does! + List sum_tree=overall_sum_trees[i]; + //save all info in list of list format the same as the trees. + List term_nodes_trees(sum_tree.size()); + List term_obs_trees(sum_tree.size()); + NumericMatrix predictions(num_obs,sum_tree.size()); + + for(int k=0;k=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + List overall_term_test_obs_trees=test_tree_info[1]; + + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=tree_info[2]; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + //List prediction_list(overall_sum_trees.size()); + //List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + //List predictive_dist_train_list(overall_sum_trees.size()); + //List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List predictive_dist_test_list(overall_sum_trees.size()); + List predictive_dist_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + //NumericMatrix post_predictions(num_iter,num_obs); + //NumericMatrix post_predictions_PI(num_iter,num_obs); + //NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions_PI(num_iter,num_test_obs); + NumericMatrix post_test_predictions_orig_PI(num_iter,num_test_obs); + //NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + NumericMatrix sum_new_test_predictions(num_test_obs,sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + // List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + // List overall_term_test_obs_trees=test_tree_info[1]; + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=tree_info[2]; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + //List prediction_list(overall_sum_trees.size()); + //List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + //List predictive_dist_train_list(overall_sum_trees.size()); + List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + //List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + //NumericMatrix post_predictions(num_iter,num_obs); + //NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_predictions_PI(num_iter,num_obs); + NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + //NumericMatrix sum_new_test_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + List overall_term_test_obs_trees=test_tree_info[1]; + + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=tree_info[2]; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + //List prediction_list(overall_sum_trees.size()); + //List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + //List predictive_dist_train_list(overall_sum_trees.size()); + //List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List predictive_dist_test_list(overall_sum_trees.size()); + List predictive_dist_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + //NumericMatrix post_predictions(num_iter,num_obs); + //NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_predictions_PI(num_iter,num_obs); + //NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions_PI(num_iter,num_test_obs); + NumericMatrix post_test_predictions_orig_PI(num_iter,num_test_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + NumericMatrix sum_new_test_predictions(num_test_obs,sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + // List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + // List overall_term_test_obs_trees=test_tree_info[1]; + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=tree_info[2]; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + //List prediction_list(overall_sum_trees.size()); + //List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + //List predictive_dist_train_list(overall_sum_trees.size()); + List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + //List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + //NumericMatrix post_predictions(num_iter,num_obs); + //NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_predictions_PI(num_iter,num_obs); + NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + //NumericMatrix sum_new_test_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + List overall_term_test_obs_trees=test_tree_info[1]; + + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=tree_info[2]; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + //List prediction_list(overall_sum_trees.size()); + //List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + List prediction_test_list_orig(overall_sum_trees.size()); + //List predictive_dist_train_list(overall_sum_trees.size()); + //List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List predictive_dist_test_list(overall_sum_trees.size()); + List predictive_dist_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + //NumericMatrix post_predictions(num_iter,num_obs); + //NumericMatrix post_predictions_PI(num_iter,num_obs); + //NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions_PI(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig_PI(num_iter,num_test_obs); + //NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + NumericMatrix sum_new_test_predictions(num_test_obs,sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + // List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + // List overall_term_test_obs_trees=test_tree_info[1]; + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=tree_info[2]; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + //List prediction_list(overall_sum_trees.size()); + List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + //List predictive_dist_train_list(overall_sum_trees.size()); + //List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + //List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + //NumericMatrix post_predictions(num_iter,num_obs); + NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_predictions_PI(num_iter,num_obs); + //NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + //NumericMatrix sum_new_test_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + List overall_term_test_obs_trees=test_tree_info[1]; + + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=tree_info[2]; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + //List prediction_list(overall_sum_trees.size()); + //List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + List prediction_test_list_orig(overall_sum_trees.size()); + //List predictive_dist_train_list(overall_sum_trees.size()); + //List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List predictive_dist_test_list(overall_sum_trees.size()); + //List predictive_dist_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + //NumericMatrix post_predictions(num_iter,num_obs); + //NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_predictions_PI(num_iter,num_obs); + //NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions_PI(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig_PI(num_iter,num_test_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + NumericMatrix sum_new_test_predictions(num_test_obs,sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + // List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + // List overall_term_test_obs_trees=test_tree_info[1]; + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=tree_info[2]; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + //List prediction_list(overall_sum_trees.size()); + List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + //List predictive_dist_train_list(overall_sum_trees.size()); + //List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + //List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + //NumericMatrix post_predictions(num_iter,num_obs); + NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_predictions_PI(num_iter,num_obs); + //NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + //NumericMatrix sum_new_test_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + //List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + //List overall_term_test_obs_trees=test_tree_info[1]; + + + + //get terminal observations (i.e. node indices) for all observation in treated group + List test_tree_info_all_T=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,all_treated_data); + List overall_term_test_obs_trees_all_T=test_tree_info_all_T[1]; + + //get terminal observations (i.e. node indices) for all observation in control group + List test_tree_info_all_C=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,all_control_data); + List overall_term_test_obs_trees_all_C=test_tree_info_all_C[1]; + + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=tree_info[2]; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + List prediction_list(overall_sum_trees.size()); + List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + List ITE_test_list(overall_sum_trees.size()); + List predictive_dist_train_list(overall_sum_trees.size()); + List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List predictive_dist_test_list(overall_sum_trees.size()); + List ITE_dist_test_list(overall_sum_trees.size()); + //List predictive_dist_test_list_orig(overall_sum_trees.size()); + List ITE_dist_test_list_orig(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + List ITE_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + //List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_term_test_obs_all_T=overall_term_test_obs_trees_all_T[i]; + List sum_term_test_obs_all_C=overall_term_test_obs_trees_all_C[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + NumericMatrix post_predictions(num_iter,num_obs); + NumericMatrix post_predictions_PI(num_iter,num_obs); + NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions_PI(num_iter,num_test_obs); + NumericMatrix post_test_ITEs_PI(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig_PI(num_iter,num_test_obs); + NumericMatrix post_test_ITEs_orig_PI(num_iter,num_test_obs); + NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + NumericMatrix post_test_ITEs(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix post_test_ITEs_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + //NumericMatrix sum_new_test_predictions(num_test_obs,sum_predictions.ncol()); + NumericMatrix sum_new_test_predictions_all_T(num_test_obs,sum_predictions.ncol()); + NumericMatrix sum_new_test_predictions_all_C(num_test_obs,sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + // List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + // List overall_term_test_obs_trees=test_tree_info[1]; + + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=tree_info[2]; + + + List tree_info_all_T=get_tree_info_testdata_overall(overall_sum_trees,num_obs,all_treated_data); + List overall_term_obs_trees_all_T=tree_info_all_T[1]; + + //get terminal observations (i.e. node indices) for all observation in control group + List tree_info_all_C=get_tree_info_testdata_overall(overall_sum_trees,num_obs,all_control_data); + List overall_term_obs_trees_all_C=tree_info_all_C[1]; + + + + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + List prediction_list(overall_sum_trees.size()); + List prediction_list_orig(overall_sum_trees.size()); + List ITE_list(overall_sum_trees.size()); + List ITE_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + List predictive_dist_train_list(overall_sum_trees.size()); + List predictive_dist_train_list_orig(overall_sum_trees.size()); + List ITE_dist_train_list(overall_sum_trees.size()); + List ITE_dist_train_list_orig(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + List sum_term_obs_all_T=overall_term_obs_trees_all_T[i]; + List sum_term_obs_all_C=overall_term_obs_trees_all_C[i]; + //List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + NumericMatrix post_predictions(num_iter,num_obs); + NumericMatrix post_ITEs(num_iter,num_obs); + NumericMatrix post_predictions_PI(num_iter,num_obs); + NumericMatrix post_ITEs_PI(num_iter,num_obs); + NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + NumericMatrix post_ITEs_orig_PI(num_iter,num_obs); + NumericMatrix post_predictions_orig(num_iter,num_obs); + NumericMatrix post_ITEs_orig(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + NumericMatrix sum_new_predictions_all_T(sum_predictions.nrow(),sum_predictions.ncol()); + NumericMatrix sum_new_predictions_all_C(sum_predictions.nrow(),sum_predictions.ncol()); + //NumericMatrix sum_new_test_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + //List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + //List overall_term_test_obs_trees=test_tree_info[1]; + + + + //get terminal observations (i.e. node indices) for all observation in treated group + List test_tree_info_all_T=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,all_treated_data); + List overall_term_test_obs_trees_all_T=test_tree_info_all_T[1]; + + //get terminal observations (i.e. node indices) for all observation in control group + List test_tree_info_all_C=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,all_control_data); + List overall_term_test_obs_trees_all_C=test_tree_info_all_C[1]; + + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=tree_info[2]; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + List prediction_list(overall_sum_trees.size()); + List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + List ITE_test_list(overall_sum_trees.size()); + List predictive_dist_train_list(overall_sum_trees.size()); + List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List predictive_dist_test_list(overall_sum_trees.size()); + List ITE_dist_test_list(overall_sum_trees.size()); + //List predictive_dist_test_list_orig(overall_sum_trees.size()); + List ITE_dist_test_list_orig(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + List ITE_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + //List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_term_test_obs_all_T=overall_term_test_obs_trees_all_T[i]; + List sum_term_test_obs_all_C=overall_term_test_obs_trees_all_C[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + NumericMatrix post_predictions(num_iter,num_obs); + NumericMatrix post_predictions_PI(num_iter,num_obs); + NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions_PI(num_iter,num_test_obs); + NumericMatrix post_test_ITEs_PI(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig_PI(num_iter,num_test_obs); + NumericMatrix post_test_ITEs_orig_PI(num_iter,num_test_obs); + NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + NumericMatrix post_test_ITEs(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix post_test_ITEs_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + //NumericMatrix sum_new_test_predictions(num_test_obs,sum_predictions.ncol()); + NumericMatrix sum_new_test_predictions_all_T(num_test_obs,sum_predictions.ncol()); + NumericMatrix sum_new_test_predictions_all_C(num_test_obs,sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + // List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + // List overall_term_test_obs_trees=test_tree_info[1]; + + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=tree_info[2]; + + + List tree_info_all_T=get_tree_info_testdata_overall(overall_sum_trees,num_obs,all_treated_data); + List overall_term_obs_trees_all_T=tree_info_all_T[1]; + + //get terminal observations (i.e. node indices) for all observation in control group + List tree_info_all_C=get_tree_info_testdata_overall(overall_sum_trees,num_obs,all_control_data); + List overall_term_obs_trees_all_C=tree_info_all_C[1]; + + + + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + List prediction_list(overall_sum_trees.size()); + List prediction_list_orig(overall_sum_trees.size()); + List ITE_list(overall_sum_trees.size()); + List ITE_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + List predictive_dist_train_list(overall_sum_trees.size()); + List predictive_dist_train_list_orig(overall_sum_trees.size()); + List ITE_dist_train_list(overall_sum_trees.size()); + List ITE_dist_train_list_orig(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + List sum_term_obs_all_T=overall_term_obs_trees_all_T[i]; + List sum_term_obs_all_C=overall_term_obs_trees_all_C[i]; + //List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + NumericMatrix post_predictions(num_iter,num_obs); + NumericMatrix post_ITEs(num_iter,num_obs); + NumericMatrix post_predictions_PI(num_iter,num_obs); + NumericMatrix post_ITEs_PI(num_iter,num_obs); + NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + NumericMatrix post_ITEs_orig_PI(num_iter,num_obs); + NumericMatrix post_predictions_orig(num_iter,num_obs); + NumericMatrix post_ITEs_orig(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + NumericMatrix sum_new_predictions_all_T(sum_predictions.nrow(),sum_predictions.ncol()); + NumericMatrix sum_new_predictions_all_C(sum_predictions.nrow(),sum_predictions.ncol()); + //NumericMatrix sum_new_test_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + List overall_term_test_obs_trees=test_tree_info[1]; + + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=new_pred_list; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + //List prediction_list(overall_sum_trees.size()); + //List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + //List predictive_dist_train_list(overall_sum_trees.size()); + //List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List predictive_dist_test_list(overall_sum_trees.size()); + List predictive_dist_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + //NumericMatrix post_predictions(num_iter,num_obs); + //NumericMatrix post_predictions_PI(num_iter,num_obs); + //NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions_PI(num_iter,num_test_obs); + NumericMatrix post_test_predictions_orig_PI(num_iter,num_test_obs); + //NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + NumericMatrix sum_new_test_predictions(num_test_obs,sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + // List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + // List overall_term_test_obs_trees=test_tree_info[1]; + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=new_pred_list; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + //List prediction_list(overall_sum_trees.size()); + //List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + //List predictive_dist_train_list(overall_sum_trees.size()); + List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + //List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + //NumericMatrix post_predictions(num_iter,num_obs); + //NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_predictions_PI(num_iter,num_obs); + NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + //NumericMatrix sum_new_test_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + List overall_term_test_obs_trees=test_tree_info[1]; + + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=new_pred_list; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + //List prediction_list(overall_sum_trees.size()); + //List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + //List predictive_dist_train_list(overall_sum_trees.size()); + //List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List predictive_dist_test_list(overall_sum_trees.size()); + List predictive_dist_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + //NumericMatrix post_predictions(num_iter,num_obs); + //NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_predictions_PI(num_iter,num_obs); + //NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions_PI(num_iter,num_test_obs); + NumericMatrix post_test_predictions_orig_PI(num_iter,num_test_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + NumericMatrix sum_new_test_predictions(num_test_obs,sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + // List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + // List overall_term_test_obs_trees=test_tree_info[1]; + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=new_pred_list; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + //List prediction_list(overall_sum_trees.size()); + //List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + //List predictive_dist_train_list(overall_sum_trees.size()); + List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + //List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + //NumericMatrix post_predictions(num_iter,num_obs); + //NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_predictions_PI(num_iter,num_obs); + NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + //NumericMatrix sum_new_test_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + List overall_term_test_obs_trees=test_tree_info[1]; + + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=new_pred_list; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + //List prediction_list(overall_sum_trees.size()); + //List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + List prediction_test_list_orig(overall_sum_trees.size()); + //List predictive_dist_train_list(overall_sum_trees.size()); + //List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List predictive_dist_test_list(overall_sum_trees.size()); + List predictive_dist_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + //NumericMatrix post_predictions(num_iter,num_obs); + //NumericMatrix post_predictions_PI(num_iter,num_obs); + //NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions_PI(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig_PI(num_iter,num_test_obs); + //NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + NumericMatrix sum_new_test_predictions(num_test_obs,sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + // List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + // List overall_term_test_obs_trees=test_tree_info[1]; + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=new_pred_list; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + //List prediction_list(overall_sum_trees.size()); + List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + //List predictive_dist_train_list(overall_sum_trees.size()); + //List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + //List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + //NumericMatrix post_predictions(num_iter,num_obs); + NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_predictions_PI(num_iter,num_obs); + //NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + //NumericMatrix sum_new_test_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + List overall_term_test_obs_trees=test_tree_info[1]; + + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=new_pred_list; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + //List prediction_list(overall_sum_trees.size()); + //List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + List prediction_test_list_orig(overall_sum_trees.size()); + //List predictive_dist_train_list(overall_sum_trees.size()); + //List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List predictive_dist_test_list(overall_sum_trees.size()); + //List predictive_dist_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + //NumericMatrix post_predictions(num_iter,num_obs); + //NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_predictions_PI(num_iter,num_obs); + //NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions_PI(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig_PI(num_iter,num_test_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + NumericMatrix sum_new_test_predictions(num_test_obs,sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + // List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + // List overall_term_test_obs_trees=test_tree_info[1]; + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=new_pred_list; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + //List prediction_list(overall_sum_trees.size()); + List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + //List predictive_dist_train_list(overall_sum_trees.size()); + //List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + //List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + //NumericMatrix post_predictions(num_iter,num_obs); + NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_predictions_PI(num_iter,num_obs); + //NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + //NumericMatrix sum_new_test_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + //List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + //List overall_term_test_obs_trees=test_tree_info[1]; + + + + //get terminal observations (i.e. node indices) for all observation in treated group + List test_tree_info_all_T=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,all_treated_data); + List overall_term_test_obs_trees_all_T=test_tree_info_all_T[1]; + + //get terminal observations (i.e. node indices) for all observation in control group + List test_tree_info_all_C=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,all_control_data); + List overall_term_test_obs_trees_all_C=test_tree_info_all_C[1]; + + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=new_pred_list; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + List prediction_list(overall_sum_trees.size()); + List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + List ITE_test_list(overall_sum_trees.size()); + List predictive_dist_train_list(overall_sum_trees.size()); + List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List predictive_dist_test_list(overall_sum_trees.size()); + List ITE_dist_test_list(overall_sum_trees.size()); + //List predictive_dist_test_list_orig(overall_sum_trees.size()); + List ITE_dist_test_list_orig(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + List ITE_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + //List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_term_test_obs_all_T=overall_term_test_obs_trees_all_T[i]; + List sum_term_test_obs_all_C=overall_term_test_obs_trees_all_C[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + NumericMatrix post_predictions(num_iter,num_obs); + NumericMatrix post_predictions_PI(num_iter,num_obs); + NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions_PI(num_iter,num_test_obs); + NumericMatrix post_test_ITEs_PI(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig_PI(num_iter,num_test_obs); + NumericMatrix post_test_ITEs_orig_PI(num_iter,num_test_obs); + NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + NumericMatrix post_test_ITEs(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix post_test_ITEs_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + //NumericMatrix sum_new_test_predictions(num_test_obs,sum_predictions.ncol()); + NumericMatrix sum_new_test_predictions_all_T(num_test_obs,sum_predictions.ncol()); + NumericMatrix sum_new_test_predictions_all_C(num_test_obs,sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + // List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + // List overall_term_test_obs_trees=test_tree_info[1]; + + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=new_pred_list; + + + List tree_info_all_T=get_tree_info_testdata_overall(overall_sum_trees,num_obs,all_treated_data); + List overall_term_obs_trees_all_T=tree_info_all_T[1]; + + //get terminal observations (i.e. node indices) for all observation in control group + List tree_info_all_C=get_tree_info_testdata_overall(overall_sum_trees,num_obs,all_control_data); + List overall_term_obs_trees_all_C=tree_info_all_C[1]; + + + + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + List prediction_list(overall_sum_trees.size()); + List prediction_list_orig(overall_sum_trees.size()); + List ITE_list(overall_sum_trees.size()); + List ITE_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + List predictive_dist_train_list(overall_sum_trees.size()); + List predictive_dist_train_list_orig(overall_sum_trees.size()); + List ITE_dist_train_list(overall_sum_trees.size()); + List ITE_dist_train_list_orig(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + List sum_term_obs_all_T=overall_term_obs_trees_all_T[i]; + List sum_term_obs_all_C=overall_term_obs_trees_all_C[i]; + //List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + NumericMatrix post_predictions(num_iter,num_obs); + NumericMatrix post_ITEs(num_iter,num_obs); + NumericMatrix post_predictions_PI(num_iter,num_obs); + NumericMatrix post_ITEs_PI(num_iter,num_obs); + NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + NumericMatrix post_ITEs_orig_PI(num_iter,num_obs); + NumericMatrix post_predictions_orig(num_iter,num_obs); + NumericMatrix post_ITEs_orig(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + NumericMatrix sum_new_predictions_all_T(sum_predictions.nrow(),sum_predictions.ncol()); + NumericMatrix sum_new_predictions_all_C(sum_predictions.nrow(),sum_predictions.ncol()); + //NumericMatrix sum_new_test_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + //List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + //List overall_term_test_obs_trees=test_tree_info[1]; + + + + //get terminal observations (i.e. node indices) for all observation in treated group + List test_tree_info_all_T=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,all_treated_data); + List overall_term_test_obs_trees_all_T=test_tree_info_all_T[1]; + + //get terminal observations (i.e. node indices) for all observation in control group + List test_tree_info_all_C=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,all_control_data); + List overall_term_test_obs_trees_all_C=test_tree_info_all_C[1]; + + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=new_pred_list; + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + List prediction_list(overall_sum_trees.size()); + List prediction_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + List ITE_test_list(overall_sum_trees.size()); + List predictive_dist_train_list(overall_sum_trees.size()); + List predictive_dist_train_list_orig(overall_sum_trees.size()); + //List predictive_dist_test_list(overall_sum_trees.size()); + List ITE_dist_test_list(overall_sum_trees.size()); + //List predictive_dist_test_list_orig(overall_sum_trees.size()); + List ITE_dist_test_list_orig(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + List ITE_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + //List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_term_test_obs_all_T=overall_term_test_obs_trees_all_T[i]; + List sum_term_test_obs_all_C=overall_term_test_obs_trees_all_C[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + NumericMatrix post_predictions(num_iter,num_obs); + NumericMatrix post_predictions_PI(num_iter,num_obs); + NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + //NumericMatrix post_test_predictions_PI(num_iter,num_test_obs); + NumericMatrix post_test_ITEs_PI(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig_PI(num_iter,num_test_obs); + NumericMatrix post_test_ITEs_orig_PI(num_iter,num_test_obs); + NumericMatrix post_predictions_orig(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + NumericMatrix post_test_ITEs(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix post_test_ITEs_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + //NumericMatrix sum_new_test_predictions(num_test_obs,sum_predictions.ncol()); + NumericMatrix sum_new_test_predictions_all_T(num_test_obs,sum_predictions.ncol()); + NumericMatrix sum_new_test_predictions_all_C(num_test_obs,sum_predictions.ncol()); + + for(int j=0;j=num_iter){ + throw std::range_error("Number of iterations has to be greater than the number of burn-in samples"); + } + //get tree info just once don't repeat for each iteration: + //need terminal nodes, terminal node means and observations terminal nodes refer to + List tree_info=get_tree_info(overall_sum_trees,overall_sum_mat,num_obs); + // List test_tree_info=get_tree_info_testdata_overall(overall_sum_trees,num_test_obs,test_data); + // List overall_term_test_obs_trees=test_tree_info[1]; + + double a1=nu/2; + double b=2/(nu*lambda); + double sigma2=sigma; + double sigma_init=sigma2; + List overall_term_nodes_trees=tree_info[0]; + List overall_term_obs_trees=tree_info[1]; + List overall_predictions=new_pred_list; + + + List tree_info_all_T=get_tree_info_testdata_overall(overall_sum_trees,num_obs,all_treated_data); + List overall_term_obs_trees_all_T=tree_info_all_T[1]; + + //get terminal observations (i.e. node indices) for all observation in control group + List tree_info_all_C=get_tree_info_testdata_overall(overall_sum_trees,num_obs,all_control_data); + List overall_term_obs_trees_all_C=tree_info_all_C[1]; + + + + NumericVector y_scaled=scale_response_gs(min(y),max(y),-0.5,0.5,y); + List prediction_list(overall_sum_trees.size()); + List prediction_list_orig(overall_sum_trees.size()); + List ITE_list(overall_sum_trees.size()); + List ITE_list_orig(overall_sum_trees.size()); + //List prediction_test_list(overall_sum_trees.size()); + List predictive_dist_train_list(overall_sum_trees.size()); + List predictive_dist_train_list_orig(overall_sum_trees.size()); + List ITE_dist_train_list(overall_sum_trees.size()); + List ITE_dist_train_list_orig(overall_sum_trees.size()); + //List prediction_test_list_orig(overall_sum_trees.size()); + //List overall_sum_trees1=clone(overall_sum_trees); + //List overall_sum_mat1=clone(overall_sum_mat); + List sigma_chains(overall_sum_trees.size()); + //int one_tree=0; + for(int i=0;i(s)){ + //if current set of trees contains more than one tree + //List sum_tree=overall_sum_trees1[i]; + //List sum_tree_mat=overall_sum_mat1[i]; + List sum_term_nodes=overall_term_nodes_trees[i]; + List sum_term_obs=overall_term_obs_trees[i]; + List sum_term_obs_all_T=overall_term_obs_trees_all_T[i]; + List sum_term_obs_all_C=overall_term_obs_trees_all_C[i]; + //List sum_term_test_obs=overall_term_test_obs_trees[i]; + List sum_resids0=resids[i]; + List sum_resids=clone(sum_resids0); + NumericMatrix tree_predictions=overall_predictions[i]; + sum_predictions=clone(tree_predictions); + NumericMatrix post_predictions(num_iter,num_obs); + NumericMatrix post_ITEs(num_iter,num_obs); + NumericMatrix post_predictions_PI(num_iter,num_obs); + NumericMatrix post_ITEs_PI(num_iter,num_obs); + NumericMatrix post_predictions_orig_PI(num_iter,num_obs); + NumericMatrix post_ITEs_orig_PI(num_iter,num_obs); + NumericMatrix post_predictions_orig(num_iter,num_obs); + NumericMatrix post_ITEs_orig(num_iter,num_obs); + //NumericMatrix post_test_predictions(num_iter,num_test_obs); + //NumericMatrix post_test_predictions_orig(num_iter,num_test_obs); + NumericMatrix sum_new_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + NumericMatrix sum_new_predictions_all_T(sum_predictions.nrow(),sum_predictions.ncol()); + NumericMatrix sum_new_predictions_all_C(sum_predictions.nrow(),sum_predictions.ncol()); + //NumericMatrix sum_new_test_predictions(sum_predictions.nrow(),sum_predictions.ncol()); + + for(int j=0;j do not edit by hand +// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 + +#include +#include + +using namespace Rcpp; + +// find_internal_nodes_pred +NumericVector find_internal_nodes_pred(NumericMatrix treetable); +RcppExport SEXP _bartBMA_find_internal_nodes_pred(SEXP treetableSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type treetable(treetableSEXP); + rcpp_result_gen = Rcpp::wrap(find_internal_nodes_pred(treetable)); + return rcpp_result_gen; +END_RCPP +} +// find_term_nodes_pred +NumericVector find_term_nodes_pred(NumericMatrix tree_table); +RcppExport SEXP _bartBMA_find_term_nodes_pred(SEXP tree_tableSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type tree_table(tree_tableSEXP); + rcpp_result_gen = Rcpp::wrap(find_term_nodes_pred(tree_table)); + return rcpp_result_gen; +END_RCPP +} +// get_original_pred +NumericVector get_original_pred(double low, double high, double sp_low, double sp_high, NumericVector sum_preds); +RcppExport SEXP _bartBMA_get_original_pred(SEXP lowSEXP, SEXP highSEXP, SEXP sp_lowSEXP, SEXP sp_highSEXP, SEXP sum_predsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type low(lowSEXP); + Rcpp::traits::input_parameter< double >::type high(highSEXP); + Rcpp::traits::input_parameter< double >::type sp_low(sp_lowSEXP); + Rcpp::traits::input_parameter< double >::type sp_high(sp_highSEXP); + Rcpp::traits::input_parameter< NumericVector >::type sum_preds(sum_predsSEXP); + rcpp_result_gen = Rcpp::wrap(get_original_pred(low, high, sp_low, sp_high, sum_preds)); + return rcpp_result_gen; +END_RCPP +} +// bartBMA_get_testdata_term_obs_pred +NumericVector bartBMA_get_testdata_term_obs_pred(NumericMatrix test_data, NumericMatrix tree_data, NumericVector term_node_means); +RcppExport SEXP _bartBMA_bartBMA_get_testdata_term_obs_pred(SEXP test_dataSEXP, SEXP tree_dataSEXP, SEXP term_node_meansSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type tree_data(tree_dataSEXP); + Rcpp::traits::input_parameter< NumericVector >::type term_node_means(term_node_meansSEXP); + rcpp_result_gen = Rcpp::wrap(bartBMA_get_testdata_term_obs_pred(test_data, tree_data, term_node_means)); + return rcpp_result_gen; +END_RCPP +} +// get_BART_BMA_test_predictions +List get_BART_BMA_test_predictions(NumericMatrix test_data, NumericVector BIC, List sum_trees, NumericVector y_minmax); +RcppExport SEXP _bartBMA_get_BART_BMA_test_predictions(SEXP test_dataSEXP, SEXP BICSEXP, SEXP sum_treesSEXP, SEXP y_minmaxSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC(BICSEXP); + Rcpp::traits::input_parameter< List >::type sum_trees(sum_treesSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y_minmax(y_minmaxSEXP); + rcpp_result_gen = Rcpp::wrap(get_BART_BMA_test_predictions(test_data, BIC, sum_trees, y_minmax)); + return rcpp_result_gen; +END_RCPP +} +// get_imp_vars +NumericVector get_imp_vars(NumericVector split_vars, int num_col, NumericVector current_vars); +RcppExport SEXP _bartBMA_get_imp_vars(SEXP split_varsSEXP, SEXP num_colSEXP, SEXP current_varsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type split_vars(split_varsSEXP); + Rcpp::traits::input_parameter< int >::type num_col(num_colSEXP); + Rcpp::traits::input_parameter< NumericVector >::type current_vars(current_varsSEXP); + rcpp_result_gen = Rcpp::wrap(get_imp_vars(split_vars, num_col, current_vars)); + return rcpp_result_gen; +END_RCPP +} +// get_weighted_var_imp +List get_weighted_var_imp(int num_vars, NumericVector BIC, List sum_trees); +RcppExport SEXP _bartBMA_get_weighted_var_imp(SEXP num_varsSEXP, SEXP BICSEXP, SEXP sum_treesSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< int >::type num_vars(num_varsSEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC(BICSEXP); + Rcpp::traits::input_parameter< List >::type sum_trees(sum_treesSEXP); + rcpp_result_gen = Rcpp::wrap(get_weighted_var_imp(num_vars, BIC, sum_trees)); + return rcpp_result_gen; +END_RCPP +} +// csample_num +IntegerVector csample_num(IntegerVector x, int size, bool replace, NumericVector prob); +RcppExport SEXP _bartBMA_csample_num(SEXP xSEXP, SEXP sizeSEXP, SEXP replaceSEXP, SEXP probSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< IntegerVector >::type x(xSEXP); + Rcpp::traits::input_parameter< int >::type size(sizeSEXP); + Rcpp::traits::input_parameter< bool >::type replace(replaceSEXP); + Rcpp::traits::input_parameter< NumericVector >::type prob(probSEXP); + rcpp_result_gen = Rcpp::wrap(csample_num(x, size, replace, prob)); + return rcpp_result_gen; +END_RCPP +} +// add_rows +NumericMatrix add_rows(NumericMatrix prior_tree_table_temp, int grow_node); +RcppExport SEXP _bartBMA_add_rows(SEXP prior_tree_table_tempSEXP, SEXP grow_nodeSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type prior_tree_table_temp(prior_tree_table_tempSEXP); + Rcpp::traits::input_parameter< int >::type grow_node(grow_nodeSEXP); + rcpp_result_gen = Rcpp::wrap(add_rows(prior_tree_table_temp, grow_node)); + return rcpp_result_gen; +END_RCPP +} +// addcol +NumericMatrix addcol(NumericMatrix prior_tree_matrix_temp, int grow_node, NumericVector ld_obs, NumericVector rd_obs); +RcppExport SEXP _bartBMA_addcol(SEXP prior_tree_matrix_tempSEXP, SEXP grow_nodeSEXP, SEXP ld_obsSEXP, SEXP rd_obsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type prior_tree_matrix_temp(prior_tree_matrix_tempSEXP); + Rcpp::traits::input_parameter< int >::type grow_node(grow_nodeSEXP); + Rcpp::traits::input_parameter< NumericVector >::type ld_obs(ld_obsSEXP); + Rcpp::traits::input_parameter< NumericVector >::type rd_obs(rd_obsSEXP); + rcpp_result_gen = Rcpp::wrap(addcol(prior_tree_matrix_temp, grow_node, ld_obs, rd_obs)); + return rcpp_result_gen; +END_RCPP +} +// set_daughter_to_end_tree +NumericMatrix set_daughter_to_end_tree(int grow_node, NumericMatrix prior_tree_table_temp, double left_daughter); +RcppExport SEXP _bartBMA_set_daughter_to_end_tree(SEXP grow_nodeSEXP, SEXP prior_tree_table_tempSEXP, SEXP left_daughterSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< int >::type grow_node(grow_nodeSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type prior_tree_table_temp(prior_tree_table_tempSEXP); + Rcpp::traits::input_parameter< double >::type left_daughter(left_daughterSEXP); + rcpp_result_gen = Rcpp::wrap(set_daughter_to_end_tree(grow_node, prior_tree_table_temp, left_daughter)); + return rcpp_result_gen; +END_RCPP +} +// set_daughter_to_end_mat +NumericMatrix set_daughter_to_end_mat(int d, NumericMatrix prior_tree_matrix_temp, double left_daughter, NumericVector ld_obs, NumericVector rd_obs); +RcppExport SEXP _bartBMA_set_daughter_to_end_mat(SEXP dSEXP, SEXP prior_tree_matrix_tempSEXP, SEXP left_daughterSEXP, SEXP ld_obsSEXP, SEXP rd_obsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< int >::type d(dSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type prior_tree_matrix_temp(prior_tree_matrix_tempSEXP); + Rcpp::traits::input_parameter< double >::type left_daughter(left_daughterSEXP); + Rcpp::traits::input_parameter< NumericVector >::type ld_obs(ld_obsSEXP); + Rcpp::traits::input_parameter< NumericVector >::type rd_obs(rd_obsSEXP); + rcpp_result_gen = Rcpp::wrap(set_daughter_to_end_mat(d, prior_tree_matrix_temp, left_daughter, ld_obs, rd_obs)); + return rcpp_result_gen; +END_RCPP +} +// remove_zero +NumericVector remove_zero(NumericVector nodes_at_depth); +RcppExport SEXP _bartBMA_remove_zero(SEXP nodes_at_depthSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type nodes_at_depth(nodes_at_depthSEXP); + rcpp_result_gen = Rcpp::wrap(remove_zero(nodes_at_depth)); + return rcpp_result_gen; +END_RCPP +} +// order_intvec_ +IntegerVector order_intvec_(IntegerVector x); +RcppExport SEXP _bartBMA_order_intvec_(SEXP xSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< IntegerVector >::type x(xSEXP); + rcpp_result_gen = Rcpp::wrap(order_intvec_(x)); + return rcpp_result_gen; +END_RCPP +} +// get_gnp +NumericVector get_gnp(NumericVector nodes_at_depth, int grow_node); +RcppExport SEXP _bartBMA_get_gnp(SEXP nodes_at_depthSEXP, SEXP grow_nodeSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type nodes_at_depth(nodes_at_depthSEXP); + Rcpp::traits::input_parameter< int >::type grow_node(grow_nodeSEXP); + rcpp_result_gen = Rcpp::wrap(get_gnp(nodes_at_depth, grow_node)); + return rcpp_result_gen; +END_RCPP +} +// find_term_nodes +NumericVector find_term_nodes(NumericMatrix tree_table); +RcppExport SEXP _bartBMA_find_term_nodes(SEXP tree_tableSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type tree_table(tree_tableSEXP); + rcpp_result_gen = Rcpp::wrap(find_term_nodes(tree_table)); + return rcpp_result_gen; +END_RCPP +} +// find_term_obs +arma::uvec find_term_obs(NumericMatrix tree_matrix_temp, double terminal_node); +RcppExport SEXP _bartBMA_find_term_obs(SEXP tree_matrix_tempSEXP, SEXP terminal_nodeSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type tree_matrix_temp(tree_matrix_tempSEXP); + Rcpp::traits::input_parameter< double >::type terminal_node(terminal_nodeSEXP); + rcpp_result_gen = Rcpp::wrap(find_term_obs(tree_matrix_temp, terminal_node)); + return rcpp_result_gen; +END_RCPP +} +// likelihood_function +double likelihood_function(NumericVector y_temp, NumericMatrix treetable_temp, NumericMatrix obs_to_nodes_temp, double a, double mu, double nu, double lambda); +RcppExport SEXP _bartBMA_likelihood_function(SEXP y_tempSEXP, SEXP treetable_tempSEXP, SEXP obs_to_nodes_tempSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type y_temp(y_tempSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type treetable_temp(treetable_tempSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type obs_to_nodes_temp(obs_to_nodes_tempSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + rcpp_result_gen = Rcpp::wrap(likelihood_function(y_temp, treetable_temp, obs_to_nodes_temp, a, mu, nu, lambda)); + return rcpp_result_gen; +END_RCPP +} +// find_internal_nodes +arma::uvec find_internal_nodes(NumericMatrix treetable); +RcppExport SEXP _bartBMA_find_internal_nodes(SEXP treetableSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type treetable(treetableSEXP); + rcpp_result_gen = Rcpp::wrap(find_internal_nodes(treetable)); + return rcpp_result_gen; +END_RCPP +} +// find_prev_nonterm +double find_prev_nonterm(arma::uvec find_nonterm, NumericVector prev); +RcppExport SEXP _bartBMA_find_prev_nonterm(SEXP find_nontermSEXP, SEXP prevSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< arma::uvec >::type find_nonterm(find_nontermSEXP); + Rcpp::traits::input_parameter< NumericVector >::type prev(prevSEXP); + rcpp_result_gen = Rcpp::wrap(find_prev_nonterm(find_nonterm, prev)); + return rcpp_result_gen; +END_RCPP +} +// find_nodes_to_update +arma::uvec find_nodes_to_update(arma::uvec all_ld, double left_daughter); +RcppExport SEXP _bartBMA_find_nodes_to_update(SEXP all_ldSEXP, SEXP left_daughterSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< arma::uvec >::type all_ld(all_ldSEXP); + Rcpp::traits::input_parameter< double >::type left_daughter(left_daughterSEXP); + rcpp_result_gen = Rcpp::wrap(find_nodes_to_update(all_ld, left_daughter)); + return rcpp_result_gen; +END_RCPP +} +// set_tree_to_middle +NumericMatrix set_tree_to_middle(NumericVector node_to_update, NumericMatrix prior_tree_table_temp, int grow_node, double left_daughter); +RcppExport SEXP _bartBMA_set_tree_to_middle(SEXP node_to_updateSEXP, SEXP prior_tree_table_tempSEXP, SEXP grow_nodeSEXP, SEXP left_daughterSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type node_to_update(node_to_updateSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type prior_tree_table_temp(prior_tree_table_tempSEXP); + Rcpp::traits::input_parameter< int >::type grow_node(grow_nodeSEXP); + Rcpp::traits::input_parameter< double >::type left_daughter(left_daughterSEXP); + rcpp_result_gen = Rcpp::wrap(set_tree_to_middle(node_to_update, prior_tree_table_temp, grow_node, left_daughter)); + return rcpp_result_gen; +END_RCPP +} +// update_grow_obs +NumericMatrix update_grow_obs(NumericMatrix prior_tree_matrix_temp, double grow_node, double left_daughter, int d, NumericVector ld_obs, NumericVector rd_obs); +RcppExport SEXP _bartBMA_update_grow_obs(SEXP prior_tree_matrix_tempSEXP, SEXP grow_nodeSEXP, SEXP left_daughterSEXP, SEXP dSEXP, SEXP ld_obsSEXP, SEXP rd_obsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type prior_tree_matrix_temp(prior_tree_matrix_tempSEXP); + Rcpp::traits::input_parameter< double >::type grow_node(grow_nodeSEXP); + Rcpp::traits::input_parameter< double >::type left_daughter(left_daughterSEXP); + Rcpp::traits::input_parameter< int >::type d(dSEXP); + Rcpp::traits::input_parameter< NumericVector >::type ld_obs(ld_obsSEXP); + Rcpp::traits::input_parameter< NumericVector >::type rd_obs(rd_obsSEXP); + rcpp_result_gen = Rcpp::wrap(update_grow_obs(prior_tree_matrix_temp, grow_node, left_daughter, d, ld_obs, rd_obs)); + return rcpp_result_gen; +END_RCPP +} +// find_obs_to_update_grow +NumericMatrix find_obs_to_update_grow(NumericMatrix prior_tree_matrix_temp, double left_daughter, int d, NumericVector ld_obs, NumericVector rd_obs); +RcppExport SEXP _bartBMA_find_obs_to_update_grow(SEXP prior_tree_matrix_tempSEXP, SEXP left_daughterSEXP, SEXP dSEXP, SEXP ld_obsSEXP, SEXP rd_obsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type prior_tree_matrix_temp(prior_tree_matrix_tempSEXP); + Rcpp::traits::input_parameter< double >::type left_daughter(left_daughterSEXP); + Rcpp::traits::input_parameter< int >::type d(dSEXP); + Rcpp::traits::input_parameter< NumericVector >::type ld_obs(ld_obsSEXP); + Rcpp::traits::input_parameter< NumericVector >::type rd_obs(rd_obsSEXP); + rcpp_result_gen = Rcpp::wrap(find_obs_to_update_grow(prior_tree_matrix_temp, left_daughter, d, ld_obs, rd_obs)); + return rcpp_result_gen; +END_RCPP +} +// get_daughter_obs +List get_daughter_obs(arma::mat& xmat, NumericVector obs_to_update, int split_var, double split_point); +RcppExport SEXP _bartBMA_get_daughter_obs(SEXP xmatSEXP, SEXP obs_to_updateSEXP, SEXP split_varSEXP, SEXP split_pointSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< arma::mat& >::type xmat(xmatSEXP); + Rcpp::traits::input_parameter< NumericVector >::type obs_to_update(obs_to_updateSEXP); + Rcpp::traits::input_parameter< int >::type split_var(split_varSEXP); + Rcpp::traits::input_parameter< double >::type split_point(split_pointSEXP); + rcpp_result_gen = Rcpp::wrap(get_daughter_obs(xmat, obs_to_update, split_var, split_point)); + return rcpp_result_gen; +END_RCPP +} +// find_term_cols +int find_term_cols(NumericMatrix tree_matrix_temp, int terminal_node); +RcppExport SEXP _bartBMA_find_term_cols(SEXP tree_matrix_tempSEXP, SEXP terminal_nodeSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type tree_matrix_temp(tree_matrix_tempSEXP); + Rcpp::traits::input_parameter< int >::type terminal_node(terminal_nodeSEXP); + rcpp_result_gen = Rcpp::wrap(find_term_cols(tree_matrix_temp, terminal_node)); + return rcpp_result_gen; +END_RCPP +} +// get_grow_obs +NumericVector get_grow_obs(arma::mat& xmat, NumericVector grow_obs, int split_var); +RcppExport SEXP _bartBMA_get_grow_obs(SEXP xmatSEXP, SEXP grow_obsSEXP, SEXP split_varSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< arma::mat& >::type xmat(xmatSEXP); + Rcpp::traits::input_parameter< NumericVector >::type grow_obs(grow_obsSEXP); + Rcpp::traits::input_parameter< int >::type split_var(split_varSEXP); + rcpp_result_gen = Rcpp::wrap(get_grow_obs(xmat, grow_obs, split_var)); + return rcpp_result_gen; +END_RCPP +} +// grow_tree +List grow_tree(arma::mat& xmat, NumericMatrix prior_tree_matrix, int grow_node, NumericMatrix prior_tree_table, int splitvar, double splitpoint, NumericVector grow_obs, int d); +RcppExport SEXP _bartBMA_grow_tree(SEXP xmatSEXP, SEXP prior_tree_matrixSEXP, SEXP grow_nodeSEXP, SEXP prior_tree_tableSEXP, SEXP splitvarSEXP, SEXP splitpointSEXP, SEXP grow_obsSEXP, SEXP dSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< arma::mat& >::type xmat(xmatSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type prior_tree_matrix(prior_tree_matrixSEXP); + Rcpp::traits::input_parameter< int >::type grow_node(grow_nodeSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type prior_tree_table(prior_tree_tableSEXP); + Rcpp::traits::input_parameter< int >::type splitvar(splitvarSEXP); + Rcpp::traits::input_parameter< double >::type splitpoint(splitpointSEXP); + Rcpp::traits::input_parameter< NumericVector >::type grow_obs(grow_obsSEXP); + Rcpp::traits::input_parameter< int >::type d(dSEXP); + rcpp_result_gen = Rcpp::wrap(grow_tree(xmat, prior_tree_matrix, grow_node, prior_tree_table, splitvar, splitpoint, grow_obs, d)); + return rcpp_result_gen; +END_RCPP +} +// set_daughter +NumericMatrix set_daughter(int left_daughter, int right_daughter, IntegerVector ld_obs, IntegerVector rd_obs, NumericMatrix tree_matrix_temp, double term_cols); +RcppExport SEXP _bartBMA_set_daughter(SEXP left_daughterSEXP, SEXP right_daughterSEXP, SEXP ld_obsSEXP, SEXP rd_obsSEXP, SEXP tree_matrix_tempSEXP, SEXP term_colsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< int >::type left_daughter(left_daughterSEXP); + Rcpp::traits::input_parameter< int >::type right_daughter(right_daughterSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type ld_obs(ld_obsSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type rd_obs(rd_obsSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type tree_matrix_temp(tree_matrix_tempSEXP); + Rcpp::traits::input_parameter< double >::type term_cols(term_colsSEXP); + rcpp_result_gen = Rcpp::wrap(set_daughter(left_daughter, right_daughter, ld_obs, rd_obs, tree_matrix_temp, term_cols)); + return rcpp_result_gen; +END_RCPP +} +// order_ +IntegerVector order_(NumericVector x); +RcppExport SEXP _bartBMA_order_(SEXP xSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type x(xSEXP); + rcpp_result_gen = Rcpp::wrap(order_(x)); + return rcpp_result_gen; +END_RCPP +} +// orderforOW +IntegerVector orderforOW(NumericVector x); +RcppExport SEXP _bartBMA_orderforOW(SEXP xSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type x(xSEXP); + rcpp_result_gen = Rcpp::wrap(orderforOW(x)); + return rcpp_result_gen; +END_RCPP +} +// secondKindStirlingNumber +double secondKindStirlingNumber(int n, int k); +RcppExport SEXP _bartBMA_secondKindStirlingNumber(SEXP nSEXP, SEXP kSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< int >::type n(nSEXP); + Rcpp::traits::input_parameter< int >::type k(kSEXP); + rcpp_result_gen = Rcpp::wrap(secondKindStirlingNumber(n, k)); + return rcpp_result_gen; +END_RCPP +} +// get_tree_prior +double get_tree_prior(double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, NumericMatrix tree_table, NumericMatrix tree_matrix, double alpha, double beta); +RcppExport SEXP _bartBMA_get_tree_prior(SEXP spike_treeSEXP, SEXP s_t_hyperpriorSEXP, SEXP p_s_tSEXP, SEXP a_s_tSEXP, SEXP b_s_tSEXP, SEXP num_obsSEXP, SEXP num_varsSEXP, SEXP lambda_poissonSEXP, SEXP tree_tableSEXP, SEXP tree_matrixSEXP, SEXP alphaSEXP, SEXP betaSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type spike_tree(spike_treeSEXP); + Rcpp::traits::input_parameter< int >::type s_t_hyperprior(s_t_hyperpriorSEXP); + Rcpp::traits::input_parameter< double >::type p_s_t(p_s_tSEXP); + Rcpp::traits::input_parameter< double >::type a_s_t(a_s_tSEXP); + Rcpp::traits::input_parameter< double >::type b_s_t(b_s_tSEXP); + Rcpp::traits::input_parameter< double >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type num_vars(num_varsSEXP); + Rcpp::traits::input_parameter< double >::type lambda_poisson(lambda_poissonSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type tree_table(tree_tableSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type tree_matrix(tree_matrixSEXP); + Rcpp::traits::input_parameter< double >::type alpha(alphaSEXP); + Rcpp::traits::input_parameter< double >::type beta(betaSEXP); + rcpp_result_gen = Rcpp::wrap(get_tree_prior(spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, tree_table, tree_matrix, alpha, beta)); + return rcpp_result_gen; +END_RCPP +} +// start_tree +NumericMatrix start_tree(double start_mean, double start_sd); +RcppExport SEXP _bartBMA_start_tree(SEXP start_meanSEXP, SEXP start_sdSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type start_mean(start_meanSEXP); + Rcpp::traits::input_parameter< double >::type start_sd(start_sdSEXP); + rcpp_result_gen = Rcpp::wrap(start_tree(start_mean, start_sd)); + return rcpp_result_gen; +END_RCPP +} +// start_tree2 +NumericMatrix start_tree2(); +RcppExport SEXP _bartBMA_start_tree2() { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + rcpp_result_gen = Rcpp::wrap(start_tree2()); + return rcpp_result_gen; +END_RCPP +} +// start_matrix +NumericMatrix start_matrix(int n); +RcppExport SEXP _bartBMA_start_matrix(SEXP nSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< int >::type n(nSEXP); + rcpp_result_gen = Rcpp::wrap(start_matrix(n)); + return rcpp_result_gen; +END_RCPP +} +// evaluate_model_occams_window +List evaluate_model_occams_window(NumericVector tree_lik, double lowest_BIC, double c, List tree_list, List tree_mat_list, IntegerVector tree_parent); +RcppExport SEXP _bartBMA_evaluate_model_occams_window(SEXP tree_likSEXP, SEXP lowest_BICSEXP, SEXP cSEXP, SEXP tree_listSEXP, SEXP tree_mat_listSEXP, SEXP tree_parentSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type tree_lik(tree_likSEXP); + Rcpp::traits::input_parameter< double >::type lowest_BIC(lowest_BICSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< List >::type tree_list(tree_listSEXP); + Rcpp::traits::input_parameter< List >::type tree_mat_list(tree_mat_listSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type tree_parent(tree_parentSEXP); + rcpp_result_gen = Rcpp::wrap(evaluate_model_occams_window(tree_lik, lowest_BIC, c, tree_list, tree_mat_list, tree_parent)); + return rcpp_result_gen; +END_RCPP +} +// evaluate_model_occams_window_exact +List evaluate_model_occams_window_exact(NumericVector tree_lik, double lowest_BIC, double c, List tree_list, List tree_mat_list, IntegerVector tree_parent, List tree_pred_list); +RcppExport SEXP _bartBMA_evaluate_model_occams_window_exact(SEXP tree_likSEXP, SEXP lowest_BICSEXP, SEXP cSEXP, SEXP tree_listSEXP, SEXP tree_mat_listSEXP, SEXP tree_parentSEXP, SEXP tree_pred_listSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type tree_lik(tree_likSEXP); + Rcpp::traits::input_parameter< double >::type lowest_BIC(lowest_BICSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< List >::type tree_list(tree_listSEXP); + Rcpp::traits::input_parameter< List >::type tree_mat_list(tree_mat_listSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type tree_parent(tree_parentSEXP); + Rcpp::traits::input_parameter< List >::type tree_pred_list(tree_pred_listSEXP); + rcpp_result_gen = Rcpp::wrap(evaluate_model_occams_window_exact(tree_lik, lowest_BIC, c, tree_list, tree_mat_list, tree_parent, tree_pred_list)); + return rcpp_result_gen; +END_RCPP +} +// get_testdata_term_obs +NumericVector get_testdata_term_obs(NumericMatrix test_data, NumericMatrix tree_data); +RcppExport SEXP _bartBMA_get_testdata_term_obs(SEXP test_dataSEXP, SEXP tree_dataSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type tree_data(tree_dataSEXP); + rcpp_result_gen = Rcpp::wrap(get_testdata_term_obs(test_data, tree_data)); + return rcpp_result_gen; +END_RCPP +} +// get_initial_resids +List get_initial_resids(NumericMatrix test_data, List List_of_lists_tree_tables, NumericVector ytrain); +RcppExport SEXP _bartBMA_get_initial_resids(SEXP test_dataSEXP, SEXP List_of_lists_tree_tablesSEXP, SEXP ytrainSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< List >::type List_of_lists_tree_tables(List_of_lists_tree_tablesSEXP); + Rcpp::traits::input_parameter< NumericVector >::type ytrain(ytrainSEXP); + rcpp_result_gen = Rcpp::wrap(get_initial_resids(test_data, List_of_lists_tree_tables, ytrain)); + return rcpp_result_gen; +END_RCPP +} +// resize +List resize(const List& x, int n); +RcppExport SEXP _bartBMA_resize(SEXP xSEXP, SEXP nSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< const List& >::type x(xSEXP); + Rcpp::traits::input_parameter< int >::type n(nSEXP); + rcpp_result_gen = Rcpp::wrap(resize(x, n)); + return rcpp_result_gen; +END_RCPP +} +// resize_bigger +List resize_bigger(const List& x, int n); +RcppExport SEXP _bartBMA_resize_bigger(SEXP xSEXP, SEXP nSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< const List& >::type x(xSEXP); + Rcpp::traits::input_parameter< int >::type n(nSEXP); + rcpp_result_gen = Rcpp::wrap(resize_bigger(x, n)); + return rcpp_result_gen; +END_RCPP +} +// J +arma::mat J(NumericMatrix obs_to_nodes_temp, NumericVector tree_term_nodes); +RcppExport SEXP _bartBMA_J(SEXP obs_to_nodes_tempSEXP, SEXP tree_term_nodesSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type obs_to_nodes_temp(obs_to_nodes_tempSEXP); + Rcpp::traits::input_parameter< NumericVector >::type tree_term_nodes(tree_term_nodesSEXP); + rcpp_result_gen = Rcpp::wrap(J(obs_to_nodes_temp, tree_term_nodes)); + return rcpp_result_gen; +END_RCPP +} +// mu_vector +NumericVector mu_vector(List sum_treetable, int n); +RcppExport SEXP _bartBMA_mu_vector(SEXP sum_treetableSEXP, SEXP nSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type sum_treetable(sum_treetableSEXP); + Rcpp::traits::input_parameter< int >::type n(nSEXP); + rcpp_result_gen = Rcpp::wrap(mu_vector(sum_treetable, n)); + return rcpp_result_gen; +END_RCPP +} +// W +arma::mat W(List sum_treetable, List sum_obs_to_nodes, int n); +RcppExport SEXP _bartBMA_W(SEXP sum_treetableSEXP, SEXP sum_obs_to_nodesSEXP, SEXP nSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type sum_treetable(sum_treetableSEXP); + Rcpp::traits::input_parameter< List >::type sum_obs_to_nodes(sum_obs_to_nodesSEXP); + Rcpp::traits::input_parameter< int >::type n(nSEXP); + rcpp_result_gen = Rcpp::wrap(W(sum_treetable, sum_obs_to_nodes, n)); + return rcpp_result_gen; +END_RCPP +} +// likelihood_function2 +double likelihood_function2(NumericVector y_temp, NumericMatrix treetable_temp, NumericMatrix obs_to_nodes_temp, double a, double mu, double nu, double lambda); +RcppExport SEXP _bartBMA_likelihood_function2(SEXP y_tempSEXP, SEXP treetable_tempSEXP, SEXP obs_to_nodes_tempSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type y_temp(y_tempSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type treetable_temp(treetable_tempSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type obs_to_nodes_temp(obs_to_nodes_tempSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + rcpp_result_gen = Rcpp::wrap(likelihood_function2(y_temp, treetable_temp, obs_to_nodes_temp, a, mu, nu, lambda)); + return rcpp_result_gen; +END_RCPP +} +// likelihood_function2_exact +List likelihood_function2_exact(NumericVector y_temp, NumericMatrix treetable_temp, NumericMatrix obs_to_nodes_temp, double a, double mu, double nu, double lambda); +RcppExport SEXP _bartBMA_likelihood_function2_exact(SEXP y_tempSEXP, SEXP treetable_tempSEXP, SEXP obs_to_nodes_tempSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type y_temp(y_tempSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type treetable_temp(treetable_tempSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type obs_to_nodes_temp(obs_to_nodes_tempSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + rcpp_result_gen = Rcpp::wrap(likelihood_function2_exact(y_temp, treetable_temp, obs_to_nodes_temp, a, mu, nu, lambda)); + return rcpp_result_gen; +END_RCPP +} +// sumtree_likelihood_function +double sumtree_likelihood_function(NumericVector y_temp, List sum_treetable, List sum_obs_to_nodes, int n, double a, double nu, double lambda); +RcppExport SEXP _bartBMA_sumtree_likelihood_function(SEXP y_tempSEXP, SEXP sum_treetableSEXP, SEXP sum_obs_to_nodesSEXP, SEXP nSEXP, SEXP aSEXP, SEXP nuSEXP, SEXP lambdaSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type y_temp(y_tempSEXP); + Rcpp::traits::input_parameter< List >::type sum_treetable(sum_treetableSEXP); + Rcpp::traits::input_parameter< List >::type sum_obs_to_nodes(sum_obs_to_nodesSEXP); + Rcpp::traits::input_parameter< int >::type n(nSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + rcpp_result_gen = Rcpp::wrap(sumtree_likelihood_function(y_temp, sum_treetable, sum_obs_to_nodes, n, a, nu, lambda)); + return rcpp_result_gen; +END_RCPP +} +// sumtree_likelihood_function2 +double sumtree_likelihood_function2(NumericVector y_temp, List sum_treetable, List sum_obs_to_nodes, int n, double a, double nu, double lambda); +RcppExport SEXP _bartBMA_sumtree_likelihood_function2(SEXP y_tempSEXP, SEXP sum_treetableSEXP, SEXP sum_obs_to_nodesSEXP, SEXP nSEXP, SEXP aSEXP, SEXP nuSEXP, SEXP lambdaSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type y_temp(y_tempSEXP); + Rcpp::traits::input_parameter< List >::type sum_treetable(sum_treetableSEXP); + Rcpp::traits::input_parameter< List >::type sum_obs_to_nodes(sum_obs_to_nodesSEXP); + Rcpp::traits::input_parameter< int >::type n(nSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + rcpp_result_gen = Rcpp::wrap(sumtree_likelihood_function2(y_temp, sum_treetable, sum_obs_to_nodes, n, a, nu, lambda)); + return rcpp_result_gen; +END_RCPP +} +// sumtree_likelihood_function2_exact +List sumtree_likelihood_function2_exact(NumericVector y_temp, List sum_treetable, List sum_obs_to_nodes, int n, double a, double nu, double lambda); +RcppExport SEXP _bartBMA_sumtree_likelihood_function2_exact(SEXP y_tempSEXP, SEXP sum_treetableSEXP, SEXP sum_obs_to_nodesSEXP, SEXP nSEXP, SEXP aSEXP, SEXP nuSEXP, SEXP lambdaSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type y_temp(y_tempSEXP); + Rcpp::traits::input_parameter< List >::type sum_treetable(sum_treetableSEXP); + Rcpp::traits::input_parameter< List >::type sum_obs_to_nodes(sum_obs_to_nodesSEXP); + Rcpp::traits::input_parameter< int >::type n(nSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + rcpp_result_gen = Rcpp::wrap(sumtree_likelihood_function2_exact(y_temp, sum_treetable, sum_obs_to_nodes, n, a, nu, lambda)); + return rcpp_result_gen; +END_RCPP +} +// sumtree_likelihood_function3 +double sumtree_likelihood_function3(NumericVector y_temp, List sum_treetable, List sum_obs_to_nodes, int n, double a, double nu, double lambda); +RcppExport SEXP _bartBMA_sumtree_likelihood_function3(SEXP y_tempSEXP, SEXP sum_treetableSEXP, SEXP sum_obs_to_nodesSEXP, SEXP nSEXP, SEXP aSEXP, SEXP nuSEXP, SEXP lambdaSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type y_temp(y_tempSEXP); + Rcpp::traits::input_parameter< List >::type sum_treetable(sum_treetableSEXP); + Rcpp::traits::input_parameter< List >::type sum_obs_to_nodes(sum_obs_to_nodesSEXP); + Rcpp::traits::input_parameter< int >::type n(nSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + rcpp_result_gen = Rcpp::wrap(sumtree_likelihood_function3(y_temp, sum_treetable, sum_obs_to_nodes, n, a, nu, lambda)); + return rcpp_result_gen; +END_RCPP +} +// sumtree_likelihood_function4 +double sumtree_likelihood_function4(NumericVector y_temp, List sum_treetable, List sum_obs_to_nodes, int n, double a, double nu, double lambda); +RcppExport SEXP _bartBMA_sumtree_likelihood_function4(SEXP y_tempSEXP, SEXP sum_treetableSEXP, SEXP sum_obs_to_nodesSEXP, SEXP nSEXP, SEXP aSEXP, SEXP nuSEXP, SEXP lambdaSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type y_temp(y_tempSEXP); + Rcpp::traits::input_parameter< List >::type sum_treetable(sum_treetableSEXP); + Rcpp::traits::input_parameter< List >::type sum_obs_to_nodes(sum_obs_to_nodesSEXP); + Rcpp::traits::input_parameter< int >::type n(nSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + rcpp_result_gen = Rcpp::wrap(sumtree_likelihood_function4(y_temp, sum_treetable, sum_obs_to_nodes, n, a, nu, lambda)); + return rcpp_result_gen; +END_RCPP +} +// get_best_split +List get_best_split(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, NumericVector resids, arma::mat& data, NumericMatrix treetable, NumericMatrix tree_mat, double a, double mu, double nu, double lambda, double c, double lowest_BIC, int parent, NumericMatrix cp_mat, double alpha, double beta, int maxOWsize, unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split); +RcppExport SEXP _bartBMA_get_best_split(SEXP less_greedySEXP, SEXP spike_treeSEXP, SEXP s_t_hyperpriorSEXP, SEXP p_s_tSEXP, SEXP a_s_tSEXP, SEXP b_s_tSEXP, SEXP num_obsSEXP, SEXP num_varsSEXP, SEXP lambda_poissonSEXP, SEXP residsSEXP, SEXP dataSEXP, SEXP treetableSEXP, SEXP tree_matSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP cSEXP, SEXP lowest_BICSEXP, SEXP parentSEXP, SEXP cp_matSEXP, SEXP alphaSEXP, SEXP betaSEXP, SEXP maxOWsizeSEXP, SEXP min_num_obs_for_splitSEXP, SEXP min_num_obs_after_splitSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type less_greedy(less_greedySEXP); + Rcpp::traits::input_parameter< double >::type spike_tree(spike_treeSEXP); + Rcpp::traits::input_parameter< int >::type s_t_hyperprior(s_t_hyperpriorSEXP); + Rcpp::traits::input_parameter< double >::type p_s_t(p_s_tSEXP); + Rcpp::traits::input_parameter< double >::type a_s_t(a_s_tSEXP); + Rcpp::traits::input_parameter< double >::type b_s_t(b_s_tSEXP); + Rcpp::traits::input_parameter< double >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type num_vars(num_varsSEXP); + Rcpp::traits::input_parameter< double >::type lambda_poisson(lambda_poissonSEXP); + Rcpp::traits::input_parameter< NumericVector >::type resids(residsSEXP); + Rcpp::traits::input_parameter< arma::mat& >::type data(dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type treetable(treetableSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type tree_mat(tree_matSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< double >::type lowest_BIC(lowest_BICSEXP); + Rcpp::traits::input_parameter< int >::type parent(parentSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type cp_mat(cp_matSEXP); + Rcpp::traits::input_parameter< double >::type alpha(alphaSEXP); + Rcpp::traits::input_parameter< double >::type beta(betaSEXP); + Rcpp::traits::input_parameter< int >::type maxOWsize(maxOWsizeSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_for_split(min_num_obs_for_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_after_split(min_num_obs_after_splitSEXP); + rcpp_result_gen = Rcpp::wrap(get_best_split(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, resids, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_mat, alpha, beta, maxOWsize, min_num_obs_for_split, min_num_obs_after_split)); + return rcpp_result_gen; +END_RCPP +} +// get_best_split_2 +List get_best_split_2(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, NumericVector resids, arma::mat& data, NumericMatrix treetable, NumericMatrix tree_mat, double a, double mu, double nu, double lambda, double c, double lowest_BIC, int parent, List cp_matlist, double alpha, double beta, int maxOWsize, unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split); +RcppExport SEXP _bartBMA_get_best_split_2(SEXP less_greedySEXP, SEXP spike_treeSEXP, SEXP s_t_hyperpriorSEXP, SEXP p_s_tSEXP, SEXP a_s_tSEXP, SEXP b_s_tSEXP, SEXP num_obsSEXP, SEXP num_varsSEXP, SEXP lambda_poissonSEXP, SEXP residsSEXP, SEXP dataSEXP, SEXP treetableSEXP, SEXP tree_matSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP cSEXP, SEXP lowest_BICSEXP, SEXP parentSEXP, SEXP cp_matlistSEXP, SEXP alphaSEXP, SEXP betaSEXP, SEXP maxOWsizeSEXP, SEXP min_num_obs_for_splitSEXP, SEXP min_num_obs_after_splitSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type less_greedy(less_greedySEXP); + Rcpp::traits::input_parameter< double >::type spike_tree(spike_treeSEXP); + Rcpp::traits::input_parameter< int >::type s_t_hyperprior(s_t_hyperpriorSEXP); + Rcpp::traits::input_parameter< double >::type p_s_t(p_s_tSEXP); + Rcpp::traits::input_parameter< double >::type a_s_t(a_s_tSEXP); + Rcpp::traits::input_parameter< double >::type b_s_t(b_s_tSEXP); + Rcpp::traits::input_parameter< double >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type num_vars(num_varsSEXP); + Rcpp::traits::input_parameter< double >::type lambda_poisson(lambda_poissonSEXP); + Rcpp::traits::input_parameter< NumericVector >::type resids(residsSEXP); + Rcpp::traits::input_parameter< arma::mat& >::type data(dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type treetable(treetableSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type tree_mat(tree_matSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< double >::type lowest_BIC(lowest_BICSEXP); + Rcpp::traits::input_parameter< int >::type parent(parentSEXP); + Rcpp::traits::input_parameter< List >::type cp_matlist(cp_matlistSEXP); + Rcpp::traits::input_parameter< double >::type alpha(alphaSEXP); + Rcpp::traits::input_parameter< double >::type beta(betaSEXP); + Rcpp::traits::input_parameter< int >::type maxOWsize(maxOWsizeSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_for_split(min_num_obs_for_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_after_split(min_num_obs_after_splitSEXP); + rcpp_result_gen = Rcpp::wrap(get_best_split_2(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, resids, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_matlist, alpha, beta, maxOWsize, min_num_obs_for_split, min_num_obs_after_split)); + return rcpp_result_gen; +END_RCPP +} +// get_best_split_sum +List get_best_split_sum(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, arma::mat& data, NumericMatrix treetable, NumericMatrix tree_mat, double a, double mu, double nu, double lambda, double c, double lowest_BIC, int parent, NumericMatrix cp_mat, double alpha, double beta, int maxOWsize, List sum_trees, List sum_trees_mat, NumericVector y_scaled, IntegerVector parent2, int i, unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split); +RcppExport SEXP _bartBMA_get_best_split_sum(SEXP less_greedySEXP, SEXP spike_treeSEXP, SEXP s_t_hyperpriorSEXP, SEXP p_s_tSEXP, SEXP a_s_tSEXP, SEXP b_s_tSEXP, SEXP num_obsSEXP, SEXP num_varsSEXP, SEXP lambda_poissonSEXP, SEXP dataSEXP, SEXP treetableSEXP, SEXP tree_matSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP cSEXP, SEXP lowest_BICSEXP, SEXP parentSEXP, SEXP cp_matSEXP, SEXP alphaSEXP, SEXP betaSEXP, SEXP maxOWsizeSEXP, SEXP sum_treesSEXP, SEXP sum_trees_matSEXP, SEXP y_scaledSEXP, SEXP parent2SEXP, SEXP iSEXP, SEXP min_num_obs_for_splitSEXP, SEXP min_num_obs_after_splitSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type less_greedy(less_greedySEXP); + Rcpp::traits::input_parameter< double >::type spike_tree(spike_treeSEXP); + Rcpp::traits::input_parameter< int >::type s_t_hyperprior(s_t_hyperpriorSEXP); + Rcpp::traits::input_parameter< double >::type p_s_t(p_s_tSEXP); + Rcpp::traits::input_parameter< double >::type a_s_t(a_s_tSEXP); + Rcpp::traits::input_parameter< double >::type b_s_t(b_s_tSEXP); + Rcpp::traits::input_parameter< double >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type num_vars(num_varsSEXP); + Rcpp::traits::input_parameter< double >::type lambda_poisson(lambda_poissonSEXP); + Rcpp::traits::input_parameter< arma::mat& >::type data(dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type treetable(treetableSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type tree_mat(tree_matSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< double >::type lowest_BIC(lowest_BICSEXP); + Rcpp::traits::input_parameter< int >::type parent(parentSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type cp_mat(cp_matSEXP); + Rcpp::traits::input_parameter< double >::type alpha(alphaSEXP); + Rcpp::traits::input_parameter< double >::type beta(betaSEXP); + Rcpp::traits::input_parameter< int >::type maxOWsize(maxOWsizeSEXP); + Rcpp::traits::input_parameter< List >::type sum_trees(sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type sum_trees_mat(sum_trees_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y_scaled(y_scaledSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type parent2(parent2SEXP); + Rcpp::traits::input_parameter< int >::type i(iSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_for_split(min_num_obs_for_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_after_split(min_num_obs_after_splitSEXP); + rcpp_result_gen = Rcpp::wrap(get_best_split_sum(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_mat, alpha, beta, maxOWsize, sum_trees, sum_trees_mat, y_scaled, parent2, i, min_num_obs_for_split, min_num_obs_after_split)); + return rcpp_result_gen; +END_RCPP +} +// get_best_split_sum_2 +List get_best_split_sum_2(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, arma::mat& data, NumericMatrix treetable, NumericMatrix tree_mat, double a, double mu, double nu, double lambda, double c, double lowest_BIC, int parent, List cp_matlist, double alpha, double beta, int maxOWsize, List sum_trees, List sum_trees_mat, NumericVector y_scaled, IntegerVector parent2, int i, unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split); +RcppExport SEXP _bartBMA_get_best_split_sum_2(SEXP less_greedySEXP, SEXP spike_treeSEXP, SEXP s_t_hyperpriorSEXP, SEXP p_s_tSEXP, SEXP a_s_tSEXP, SEXP b_s_tSEXP, SEXP num_obsSEXP, SEXP num_varsSEXP, SEXP lambda_poissonSEXP, SEXP dataSEXP, SEXP treetableSEXP, SEXP tree_matSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP cSEXP, SEXP lowest_BICSEXP, SEXP parentSEXP, SEXP cp_matlistSEXP, SEXP alphaSEXP, SEXP betaSEXP, SEXP maxOWsizeSEXP, SEXP sum_treesSEXP, SEXP sum_trees_matSEXP, SEXP y_scaledSEXP, SEXP parent2SEXP, SEXP iSEXP, SEXP min_num_obs_for_splitSEXP, SEXP min_num_obs_after_splitSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type less_greedy(less_greedySEXP); + Rcpp::traits::input_parameter< double >::type spike_tree(spike_treeSEXP); + Rcpp::traits::input_parameter< int >::type s_t_hyperprior(s_t_hyperpriorSEXP); + Rcpp::traits::input_parameter< double >::type p_s_t(p_s_tSEXP); + Rcpp::traits::input_parameter< double >::type a_s_t(a_s_tSEXP); + Rcpp::traits::input_parameter< double >::type b_s_t(b_s_tSEXP); + Rcpp::traits::input_parameter< double >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type num_vars(num_varsSEXP); + Rcpp::traits::input_parameter< double >::type lambda_poisson(lambda_poissonSEXP); + Rcpp::traits::input_parameter< arma::mat& >::type data(dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type treetable(treetableSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type tree_mat(tree_matSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< double >::type lowest_BIC(lowest_BICSEXP); + Rcpp::traits::input_parameter< int >::type parent(parentSEXP); + Rcpp::traits::input_parameter< List >::type cp_matlist(cp_matlistSEXP); + Rcpp::traits::input_parameter< double >::type alpha(alphaSEXP); + Rcpp::traits::input_parameter< double >::type beta(betaSEXP); + Rcpp::traits::input_parameter< int >::type maxOWsize(maxOWsizeSEXP); + Rcpp::traits::input_parameter< List >::type sum_trees(sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type sum_trees_mat(sum_trees_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y_scaled(y_scaledSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type parent2(parent2SEXP); + Rcpp::traits::input_parameter< int >::type i(iSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_for_split(min_num_obs_for_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_after_split(min_num_obs_after_splitSEXP); + rcpp_result_gen = Rcpp::wrap(get_best_split_sum_2(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_matlist, alpha, beta, maxOWsize, sum_trees, sum_trees_mat, y_scaled, parent2, i, min_num_obs_for_split, min_num_obs_after_split)); + return rcpp_result_gen; +END_RCPP +} +// get_best_split_exact +List get_best_split_exact(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, NumericVector resids, arma::mat& data, NumericMatrix treetable, NumericMatrix tree_mat, double a, double mu, double nu, double lambda, double c, double lowest_BIC, int parent, NumericMatrix cp_mat, double alpha, double beta, int maxOWsize, unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split); +RcppExport SEXP _bartBMA_get_best_split_exact(SEXP less_greedySEXP, SEXP spike_treeSEXP, SEXP s_t_hyperpriorSEXP, SEXP p_s_tSEXP, SEXP a_s_tSEXP, SEXP b_s_tSEXP, SEXP num_obsSEXP, SEXP num_varsSEXP, SEXP lambda_poissonSEXP, SEXP residsSEXP, SEXP dataSEXP, SEXP treetableSEXP, SEXP tree_matSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP cSEXP, SEXP lowest_BICSEXP, SEXP parentSEXP, SEXP cp_matSEXP, SEXP alphaSEXP, SEXP betaSEXP, SEXP maxOWsizeSEXP, SEXP min_num_obs_for_splitSEXP, SEXP min_num_obs_after_splitSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type less_greedy(less_greedySEXP); + Rcpp::traits::input_parameter< double >::type spike_tree(spike_treeSEXP); + Rcpp::traits::input_parameter< int >::type s_t_hyperprior(s_t_hyperpriorSEXP); + Rcpp::traits::input_parameter< double >::type p_s_t(p_s_tSEXP); + Rcpp::traits::input_parameter< double >::type a_s_t(a_s_tSEXP); + Rcpp::traits::input_parameter< double >::type b_s_t(b_s_tSEXP); + Rcpp::traits::input_parameter< double >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type num_vars(num_varsSEXP); + Rcpp::traits::input_parameter< double >::type lambda_poisson(lambda_poissonSEXP); + Rcpp::traits::input_parameter< NumericVector >::type resids(residsSEXP); + Rcpp::traits::input_parameter< arma::mat& >::type data(dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type treetable(treetableSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type tree_mat(tree_matSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< double >::type lowest_BIC(lowest_BICSEXP); + Rcpp::traits::input_parameter< int >::type parent(parentSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type cp_mat(cp_matSEXP); + Rcpp::traits::input_parameter< double >::type alpha(alphaSEXP); + Rcpp::traits::input_parameter< double >::type beta(betaSEXP); + Rcpp::traits::input_parameter< int >::type maxOWsize(maxOWsizeSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_for_split(min_num_obs_for_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_after_split(min_num_obs_after_splitSEXP); + rcpp_result_gen = Rcpp::wrap(get_best_split_exact(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, resids, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_mat, alpha, beta, maxOWsize, min_num_obs_for_split, min_num_obs_after_split)); + return rcpp_result_gen; +END_RCPP +} +// get_best_split_2_exact +List get_best_split_2_exact(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, NumericVector resids, arma::mat& data, NumericMatrix treetable, NumericMatrix tree_mat, double a, double mu, double nu, double lambda, double c, double lowest_BIC, int parent, List cp_matlist, double alpha, double beta, int maxOWsize, unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split); +RcppExport SEXP _bartBMA_get_best_split_2_exact(SEXP less_greedySEXP, SEXP spike_treeSEXP, SEXP s_t_hyperpriorSEXP, SEXP p_s_tSEXP, SEXP a_s_tSEXP, SEXP b_s_tSEXP, SEXP num_obsSEXP, SEXP num_varsSEXP, SEXP lambda_poissonSEXP, SEXP residsSEXP, SEXP dataSEXP, SEXP treetableSEXP, SEXP tree_matSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP cSEXP, SEXP lowest_BICSEXP, SEXP parentSEXP, SEXP cp_matlistSEXP, SEXP alphaSEXP, SEXP betaSEXP, SEXP maxOWsizeSEXP, SEXP min_num_obs_for_splitSEXP, SEXP min_num_obs_after_splitSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type less_greedy(less_greedySEXP); + Rcpp::traits::input_parameter< double >::type spike_tree(spike_treeSEXP); + Rcpp::traits::input_parameter< int >::type s_t_hyperprior(s_t_hyperpriorSEXP); + Rcpp::traits::input_parameter< double >::type p_s_t(p_s_tSEXP); + Rcpp::traits::input_parameter< double >::type a_s_t(a_s_tSEXP); + Rcpp::traits::input_parameter< double >::type b_s_t(b_s_tSEXP); + Rcpp::traits::input_parameter< double >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type num_vars(num_varsSEXP); + Rcpp::traits::input_parameter< double >::type lambda_poisson(lambda_poissonSEXP); + Rcpp::traits::input_parameter< NumericVector >::type resids(residsSEXP); + Rcpp::traits::input_parameter< arma::mat& >::type data(dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type treetable(treetableSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type tree_mat(tree_matSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< double >::type lowest_BIC(lowest_BICSEXP); + Rcpp::traits::input_parameter< int >::type parent(parentSEXP); + Rcpp::traits::input_parameter< List >::type cp_matlist(cp_matlistSEXP); + Rcpp::traits::input_parameter< double >::type alpha(alphaSEXP); + Rcpp::traits::input_parameter< double >::type beta(betaSEXP); + Rcpp::traits::input_parameter< int >::type maxOWsize(maxOWsizeSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_for_split(min_num_obs_for_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_after_split(min_num_obs_after_splitSEXP); + rcpp_result_gen = Rcpp::wrap(get_best_split_2_exact(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, resids, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_matlist, alpha, beta, maxOWsize, min_num_obs_for_split, min_num_obs_after_split)); + return rcpp_result_gen; +END_RCPP +} +// get_best_split_sum_exact +List get_best_split_sum_exact(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, arma::mat& data, NumericMatrix treetable, NumericMatrix tree_mat, double a, double mu, double nu, double lambda, double c, double lowest_BIC, int parent, NumericMatrix cp_mat, double alpha, double beta, int maxOWsize, List sum_trees, List sum_trees_mat, NumericVector y_scaled, IntegerVector parent2, int i, unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split); +RcppExport SEXP _bartBMA_get_best_split_sum_exact(SEXP less_greedySEXP, SEXP spike_treeSEXP, SEXP s_t_hyperpriorSEXP, SEXP p_s_tSEXP, SEXP a_s_tSEXP, SEXP b_s_tSEXP, SEXP num_obsSEXP, SEXP num_varsSEXP, SEXP lambda_poissonSEXP, SEXP dataSEXP, SEXP treetableSEXP, SEXP tree_matSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP cSEXP, SEXP lowest_BICSEXP, SEXP parentSEXP, SEXP cp_matSEXP, SEXP alphaSEXP, SEXP betaSEXP, SEXP maxOWsizeSEXP, SEXP sum_treesSEXP, SEXP sum_trees_matSEXP, SEXP y_scaledSEXP, SEXP parent2SEXP, SEXP iSEXP, SEXP min_num_obs_for_splitSEXP, SEXP min_num_obs_after_splitSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type less_greedy(less_greedySEXP); + Rcpp::traits::input_parameter< double >::type spike_tree(spike_treeSEXP); + Rcpp::traits::input_parameter< int >::type s_t_hyperprior(s_t_hyperpriorSEXP); + Rcpp::traits::input_parameter< double >::type p_s_t(p_s_tSEXP); + Rcpp::traits::input_parameter< double >::type a_s_t(a_s_tSEXP); + Rcpp::traits::input_parameter< double >::type b_s_t(b_s_tSEXP); + Rcpp::traits::input_parameter< double >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type num_vars(num_varsSEXP); + Rcpp::traits::input_parameter< double >::type lambda_poisson(lambda_poissonSEXP); + Rcpp::traits::input_parameter< arma::mat& >::type data(dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type treetable(treetableSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type tree_mat(tree_matSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< double >::type lowest_BIC(lowest_BICSEXP); + Rcpp::traits::input_parameter< int >::type parent(parentSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type cp_mat(cp_matSEXP); + Rcpp::traits::input_parameter< double >::type alpha(alphaSEXP); + Rcpp::traits::input_parameter< double >::type beta(betaSEXP); + Rcpp::traits::input_parameter< int >::type maxOWsize(maxOWsizeSEXP); + Rcpp::traits::input_parameter< List >::type sum_trees(sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type sum_trees_mat(sum_trees_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y_scaled(y_scaledSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type parent2(parent2SEXP); + Rcpp::traits::input_parameter< int >::type i(iSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_for_split(min_num_obs_for_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_after_split(min_num_obs_after_splitSEXP); + rcpp_result_gen = Rcpp::wrap(get_best_split_sum_exact(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_mat, alpha, beta, maxOWsize, sum_trees, sum_trees_mat, y_scaled, parent2, i, min_num_obs_for_split, min_num_obs_after_split)); + return rcpp_result_gen; +END_RCPP +} +// get_best_split_sum_2_exact +List get_best_split_sum_2_exact(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, arma::mat& data, NumericMatrix treetable, NumericMatrix tree_mat, double a, double mu, double nu, double lambda, double c, double lowest_BIC, int parent, List cp_matlist, double alpha, double beta, int maxOWsize, List sum_trees, List sum_trees_mat, NumericVector y_scaled, IntegerVector parent2, int i, unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split); +RcppExport SEXP _bartBMA_get_best_split_sum_2_exact(SEXP less_greedySEXP, SEXP spike_treeSEXP, SEXP s_t_hyperpriorSEXP, SEXP p_s_tSEXP, SEXP a_s_tSEXP, SEXP b_s_tSEXP, SEXP num_obsSEXP, SEXP num_varsSEXP, SEXP lambda_poissonSEXP, SEXP dataSEXP, SEXP treetableSEXP, SEXP tree_matSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP cSEXP, SEXP lowest_BICSEXP, SEXP parentSEXP, SEXP cp_matlistSEXP, SEXP alphaSEXP, SEXP betaSEXP, SEXP maxOWsizeSEXP, SEXP sum_treesSEXP, SEXP sum_trees_matSEXP, SEXP y_scaledSEXP, SEXP parent2SEXP, SEXP iSEXP, SEXP min_num_obs_for_splitSEXP, SEXP min_num_obs_after_splitSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type less_greedy(less_greedySEXP); + Rcpp::traits::input_parameter< double >::type spike_tree(spike_treeSEXP); + Rcpp::traits::input_parameter< int >::type s_t_hyperprior(s_t_hyperpriorSEXP); + Rcpp::traits::input_parameter< double >::type p_s_t(p_s_tSEXP); + Rcpp::traits::input_parameter< double >::type a_s_t(a_s_tSEXP); + Rcpp::traits::input_parameter< double >::type b_s_t(b_s_tSEXP); + Rcpp::traits::input_parameter< double >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type num_vars(num_varsSEXP); + Rcpp::traits::input_parameter< double >::type lambda_poisson(lambda_poissonSEXP); + Rcpp::traits::input_parameter< arma::mat& >::type data(dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type treetable(treetableSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type tree_mat(tree_matSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< double >::type lowest_BIC(lowest_BICSEXP); + Rcpp::traits::input_parameter< int >::type parent(parentSEXP); + Rcpp::traits::input_parameter< List >::type cp_matlist(cp_matlistSEXP); + Rcpp::traits::input_parameter< double >::type alpha(alphaSEXP); + Rcpp::traits::input_parameter< double >::type beta(betaSEXP); + Rcpp::traits::input_parameter< int >::type maxOWsize(maxOWsizeSEXP); + Rcpp::traits::input_parameter< List >::type sum_trees(sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type sum_trees_mat(sum_trees_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y_scaled(y_scaledSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type parent2(parent2SEXP); + Rcpp::traits::input_parameter< int >::type i(iSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_for_split(min_num_obs_for_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_after_split(min_num_obs_after_splitSEXP); + rcpp_result_gen = Rcpp::wrap(get_best_split_sum_2_exact(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, data, treetable, tree_mat, a, mu, nu, lambda, c, lowest_BIC, parent, cp_matlist, alpha, beta, maxOWsize, sum_trees, sum_trees_mat, y_scaled, parent2, i, min_num_obs_for_split, min_num_obs_after_split)); + return rcpp_result_gen; +END_RCPP +} +// update_mean_var +NumericVector update_mean_var(NumericMatrix tree_table, NumericMatrix tree_matrix, NumericVector resids, double a); +RcppExport SEXP _bartBMA_update_mean_var(SEXP tree_tableSEXP, SEXP tree_matrixSEXP, SEXP residsSEXP, SEXP aSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type tree_table(tree_tableSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type tree_matrix(tree_matrixSEXP); + Rcpp::traits::input_parameter< NumericVector >::type resids(residsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + rcpp_result_gen = Rcpp::wrap(update_mean_var(tree_table, tree_matrix, resids, a)); + return rcpp_result_gen; +END_RCPP +} +// update_predictions +List update_predictions(NumericMatrix tree_table, NumericMatrix tree_matrix, NumericVector new_mean, int n); +RcppExport SEXP _bartBMA_update_predictions(SEXP tree_tableSEXP, SEXP tree_matrixSEXP, SEXP new_meanSEXP, SEXP nSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type tree_table(tree_tableSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type tree_matrix(tree_matrixSEXP); + Rcpp::traits::input_parameter< NumericVector >::type new_mean(new_meanSEXP); + Rcpp::traits::input_parameter< int >::type n(nSEXP); + rcpp_result_gen = Rcpp::wrap(update_predictions(tree_table, tree_matrix, new_mean, n)); + return rcpp_result_gen; +END_RCPP +} +// subsetter +NumericVector subsetter(NumericVector a, LogicalVector b); +RcppExport SEXP _bartBMA_subsetter(SEXP aSEXP, SEXP bSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type a(aSEXP); + Rcpp::traits::input_parameter< LogicalVector >::type b(bSEXP); + rcpp_result_gen = Rcpp::wrap(subsetter(a, b)); + return rcpp_result_gen; +END_RCPP +} +// order_inc_ +IntegerVector order_inc_(NumericVector x); +RcppExport SEXP _bartBMA_order_inc_(SEXP xSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type x(xSEXP); + rcpp_result_gen = Rcpp::wrap(order_inc_(x)); + return rcpp_result_gen; +END_RCPP +} +// min_which2 +List min_which2(NumericVector array, int n, double minout, int whichout); +RcppExport SEXP _bartBMA_min_which2(SEXP arraySEXP, SEXP nSEXP, SEXP minoutSEXP, SEXP whichoutSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type array(arraySEXP); + Rcpp::traits::input_parameter< int >::type n(nSEXP); + Rcpp::traits::input_parameter< double >::type minout(minoutSEXP); + Rcpp::traits::input_parameter< int >::type whichout(whichoutSEXP); + rcpp_result_gen = Rcpp::wrap(min_which2(array, n, minout, whichout)); + return rcpp_result_gen; +END_RCPP +} +// mll_meanvar2 +double mll_meanvar2(double x, double x2, int n); +RcppExport SEXP _bartBMA_mll_meanvar2(SEXP xSEXP, SEXP x2SEXP, SEXP nSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type x(xSEXP); + Rcpp::traits::input_parameter< double >::type x2(x2SEXP); + Rcpp::traits::input_parameter< int >::type n(nSEXP); + rcpp_result_gen = Rcpp::wrap(mll_meanvar2(x, x2, n)); + return rcpp_result_gen; +END_RCPP +} +// PELT_meanvar_norm2 +IntegerVector PELT_meanvar_norm2(NumericVector resp, double pen); +RcppExport SEXP _bartBMA_PELT_meanvar_norm2(SEXP respSEXP, SEXP penSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type resp(respSEXP); + Rcpp::traits::input_parameter< double >::type pen(penSEXP); + rcpp_result_gen = Rcpp::wrap(PELT_meanvar_norm2(resp, pen)); + return rcpp_result_gen; +END_RCPP +} +// SS +double SS(arma::vec x, arma::vec y, double split); +RcppExport SEXP _bartBMA_SS(SEXP xSEXP, SEXP ySEXP, SEXP splitSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< arma::vec >::type x(xSEXP); + Rcpp::traits::input_parameter< arma::vec >::type y(ySEXP); + Rcpp::traits::input_parameter< double >::type split(splitSEXP); + rcpp_result_gen = Rcpp::wrap(SS(x, y, split)); + return rcpp_result_gen; +END_RCPP +} +// gridCP +List gridCP(arma::vec x, arma::vec y, int gridSize); +RcppExport SEXP _bartBMA_gridCP(SEXP xSEXP, SEXP ySEXP, SEXP gridSizeSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< arma::vec >::type x(xSEXP); + Rcpp::traits::input_parameter< arma::vec >::type y(ySEXP); + Rcpp::traits::input_parameter< int >::type gridSize(gridSizeSEXP); + rcpp_result_gen = Rcpp::wrap(gridCP(x, y, gridSize)); + return rcpp_result_gen; +END_RCPP +} +// gridCP_arma +arma::field gridCP_arma(arma::vec x, arma::vec y, int gridSize); +RcppExport SEXP _bartBMA_gridCP_arma(SEXP xSEXP, SEXP ySEXP, SEXP gridSizeSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< arma::vec >::type x(xSEXP); + Rcpp::traits::input_parameter< arma::vec >::type y(ySEXP); + Rcpp::traits::input_parameter< int >::type gridSize(gridSizeSEXP); + rcpp_result_gen = Rcpp::wrap(gridCP_arma(x, y, gridSize)); + return rcpp_result_gen; +END_RCPP +} +// make_gridpoint_cpmat +List make_gridpoint_cpmat(NumericMatrix data, NumericVector resp, int gridsize, int num_cp); +RcppExport SEXP _bartBMA_make_gridpoint_cpmat(SEXP dataSEXP, SEXP respSEXP, SEXP gridsizeSEXP, SEXP num_cpSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type data(dataSEXP); + Rcpp::traits::input_parameter< NumericVector >::type resp(respSEXP); + Rcpp::traits::input_parameter< int >::type gridsize(gridsizeSEXP); + Rcpp::traits::input_parameter< int >::type num_cp(num_cpSEXP); + rcpp_result_gen = Rcpp::wrap(make_gridpoint_cpmat(data, resp, gridsize, num_cp)); + return rcpp_result_gen; +END_RCPP +} +// make_gridpoint_cpmat_arma +List make_gridpoint_cpmat_arma(arma::mat data, arma::vec resp, int gridsize, int num_cp); +RcppExport SEXP _bartBMA_make_gridpoint_cpmat_arma(SEXP dataSEXP, SEXP respSEXP, SEXP gridsizeSEXP, SEXP num_cpSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< arma::mat >::type data(dataSEXP); + Rcpp::traits::input_parameter< arma::vec >::type resp(respSEXP); + Rcpp::traits::input_parameter< int >::type gridsize(gridsizeSEXP); + Rcpp::traits::input_parameter< int >::type num_cp(num_cpSEXP); + rcpp_result_gen = Rcpp::wrap(make_gridpoint_cpmat_arma(data, resp, gridsize, num_cp)); + return rcpp_result_gen; +END_RCPP +} +// make_pelt_cpmat +List make_pelt_cpmat(NumericMatrix data, NumericVector resp, double pen, int num_cp); +RcppExport SEXP _bartBMA_make_pelt_cpmat(SEXP dataSEXP, SEXP respSEXP, SEXP penSEXP, SEXP num_cpSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type data(dataSEXP); + Rcpp::traits::input_parameter< NumericVector >::type resp(respSEXP); + Rcpp::traits::input_parameter< double >::type pen(penSEXP); + Rcpp::traits::input_parameter< int >::type num_cp(num_cpSEXP); + rcpp_result_gen = Rcpp::wrap(make_pelt_cpmat(data, resp, pen, num_cp)); + return rcpp_result_gen; +END_RCPP +} +// get_best_trees +List get_best_trees(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, arma::mat& D1, NumericMatrix resids, double a, double mu, double nu, double lambda, double c, double sigma_mu, List tree_table, List tree_mat, double lowest_BIC, IntegerVector parent, List cp_mat_list, NumericMatrix test_data, double alpha, double beta, bool is_test_data, double pen, int num_cp, bool split_rule_node, bool gridpoint, int maxOWsize, int num_splits, int gridsize, bool zero_split, unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split); +RcppExport SEXP _bartBMA_get_best_trees(SEXP less_greedySEXP, SEXP spike_treeSEXP, SEXP s_t_hyperpriorSEXP, SEXP p_s_tSEXP, SEXP a_s_tSEXP, SEXP b_s_tSEXP, SEXP num_obsSEXP, SEXP num_varsSEXP, SEXP lambda_poissonSEXP, SEXP D1SEXP, SEXP residsSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP cSEXP, SEXP sigma_muSEXP, SEXP tree_tableSEXP, SEXP tree_matSEXP, SEXP lowest_BICSEXP, SEXP parentSEXP, SEXP cp_mat_listSEXP, SEXP test_dataSEXP, SEXP alphaSEXP, SEXP betaSEXP, SEXP is_test_dataSEXP, SEXP penSEXP, SEXP num_cpSEXP, SEXP split_rule_nodeSEXP, SEXP gridpointSEXP, SEXP maxOWsizeSEXP, SEXP num_splitsSEXP, SEXP gridsizeSEXP, SEXP zero_splitSEXP, SEXP min_num_obs_for_splitSEXP, SEXP min_num_obs_after_splitSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type less_greedy(less_greedySEXP); + Rcpp::traits::input_parameter< double >::type spike_tree(spike_treeSEXP); + Rcpp::traits::input_parameter< int >::type s_t_hyperprior(s_t_hyperpriorSEXP); + Rcpp::traits::input_parameter< double >::type p_s_t(p_s_tSEXP); + Rcpp::traits::input_parameter< double >::type a_s_t(a_s_tSEXP); + Rcpp::traits::input_parameter< double >::type b_s_t(b_s_tSEXP); + Rcpp::traits::input_parameter< double >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type num_vars(num_varsSEXP); + Rcpp::traits::input_parameter< double >::type lambda_poisson(lambda_poissonSEXP); + Rcpp::traits::input_parameter< arma::mat& >::type D1(D1SEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type resids(residsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< double >::type sigma_mu(sigma_muSEXP); + Rcpp::traits::input_parameter< List >::type tree_table(tree_tableSEXP); + Rcpp::traits::input_parameter< List >::type tree_mat(tree_matSEXP); + Rcpp::traits::input_parameter< double >::type lowest_BIC(lowest_BICSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type parent(parentSEXP); + Rcpp::traits::input_parameter< List >::type cp_mat_list(cp_mat_listSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< double >::type alpha(alphaSEXP); + Rcpp::traits::input_parameter< double >::type beta(betaSEXP); + Rcpp::traits::input_parameter< bool >::type is_test_data(is_test_dataSEXP); + Rcpp::traits::input_parameter< double >::type pen(penSEXP); + Rcpp::traits::input_parameter< int >::type num_cp(num_cpSEXP); + Rcpp::traits::input_parameter< bool >::type split_rule_node(split_rule_nodeSEXP); + Rcpp::traits::input_parameter< bool >::type gridpoint(gridpointSEXP); + Rcpp::traits::input_parameter< int >::type maxOWsize(maxOWsizeSEXP); + Rcpp::traits::input_parameter< int >::type num_splits(num_splitsSEXP); + Rcpp::traits::input_parameter< int >::type gridsize(gridsizeSEXP); + Rcpp::traits::input_parameter< bool >::type zero_split(zero_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_for_split(min_num_obs_for_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_after_split(min_num_obs_after_splitSEXP); + rcpp_result_gen = Rcpp::wrap(get_best_trees(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split)); + return rcpp_result_gen; +END_RCPP +} +// get_best_trees_update_splits +List get_best_trees_update_splits(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, arma::mat& D1, NumericMatrix resids, double a, double mu, double nu, double lambda, double c, double sigma_mu, List tree_table, List tree_mat, double lowest_BIC, IntegerVector parent, List cp_mat_list, NumericMatrix test_data, double alpha, double beta, bool is_test_data, double pen, int num_cp, bool split_rule_node, bool gridpoint, int maxOWsize, int num_splits, int gridsize, bool zero_split, unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split); +RcppExport SEXP _bartBMA_get_best_trees_update_splits(SEXP less_greedySEXP, SEXP spike_treeSEXP, SEXP s_t_hyperpriorSEXP, SEXP p_s_tSEXP, SEXP a_s_tSEXP, SEXP b_s_tSEXP, SEXP num_obsSEXP, SEXP num_varsSEXP, SEXP lambda_poissonSEXP, SEXP D1SEXP, SEXP residsSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP cSEXP, SEXP sigma_muSEXP, SEXP tree_tableSEXP, SEXP tree_matSEXP, SEXP lowest_BICSEXP, SEXP parentSEXP, SEXP cp_mat_listSEXP, SEXP test_dataSEXP, SEXP alphaSEXP, SEXP betaSEXP, SEXP is_test_dataSEXP, SEXP penSEXP, SEXP num_cpSEXP, SEXP split_rule_nodeSEXP, SEXP gridpointSEXP, SEXP maxOWsizeSEXP, SEXP num_splitsSEXP, SEXP gridsizeSEXP, SEXP zero_splitSEXP, SEXP min_num_obs_for_splitSEXP, SEXP min_num_obs_after_splitSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type less_greedy(less_greedySEXP); + Rcpp::traits::input_parameter< double >::type spike_tree(spike_treeSEXP); + Rcpp::traits::input_parameter< int >::type s_t_hyperprior(s_t_hyperpriorSEXP); + Rcpp::traits::input_parameter< double >::type p_s_t(p_s_tSEXP); + Rcpp::traits::input_parameter< double >::type a_s_t(a_s_tSEXP); + Rcpp::traits::input_parameter< double >::type b_s_t(b_s_tSEXP); + Rcpp::traits::input_parameter< double >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type num_vars(num_varsSEXP); + Rcpp::traits::input_parameter< double >::type lambda_poisson(lambda_poissonSEXP); + Rcpp::traits::input_parameter< arma::mat& >::type D1(D1SEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type resids(residsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< double >::type sigma_mu(sigma_muSEXP); + Rcpp::traits::input_parameter< List >::type tree_table(tree_tableSEXP); + Rcpp::traits::input_parameter< List >::type tree_mat(tree_matSEXP); + Rcpp::traits::input_parameter< double >::type lowest_BIC(lowest_BICSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type parent(parentSEXP); + Rcpp::traits::input_parameter< List >::type cp_mat_list(cp_mat_listSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< double >::type alpha(alphaSEXP); + Rcpp::traits::input_parameter< double >::type beta(betaSEXP); + Rcpp::traits::input_parameter< bool >::type is_test_data(is_test_dataSEXP); + Rcpp::traits::input_parameter< double >::type pen(penSEXP); + Rcpp::traits::input_parameter< int >::type num_cp(num_cpSEXP); + Rcpp::traits::input_parameter< bool >::type split_rule_node(split_rule_nodeSEXP); + Rcpp::traits::input_parameter< bool >::type gridpoint(gridpointSEXP); + Rcpp::traits::input_parameter< int >::type maxOWsize(maxOWsizeSEXP); + Rcpp::traits::input_parameter< int >::type num_splits(num_splitsSEXP); + Rcpp::traits::input_parameter< int >::type gridsize(gridsizeSEXP); + Rcpp::traits::input_parameter< bool >::type zero_split(zero_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_for_split(min_num_obs_for_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_after_split(min_num_obs_after_splitSEXP); + rcpp_result_gen = Rcpp::wrap(get_best_trees_update_splits(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split)); + return rcpp_result_gen; +END_RCPP +} +// get_best_trees_sum +List get_best_trees_sum(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, arma::mat& D1, NumericMatrix resids, double a, double mu, double nu, double lambda, double c, double sigma_mu, List tree_table, List tree_mat, double lowest_BIC, IntegerVector parent, List cp_mat_list, IntegerVector err_list, NumericMatrix test_data, double alpha, double beta, bool is_test_data, double pen, int num_cp, bool split_rule_node, bool gridpoint, int maxOWsize, List prev_sum_trees, List prev_sum_trees_mat, NumericVector y_scaled, int num_splits, int gridsize, bool zero_split, unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split); +RcppExport SEXP _bartBMA_get_best_trees_sum(SEXP less_greedySEXP, SEXP spike_treeSEXP, SEXP s_t_hyperpriorSEXP, SEXP p_s_tSEXP, SEXP a_s_tSEXP, SEXP b_s_tSEXP, SEXP num_obsSEXP, SEXP num_varsSEXP, SEXP lambda_poissonSEXP, SEXP D1SEXP, SEXP residsSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP cSEXP, SEXP sigma_muSEXP, SEXP tree_tableSEXP, SEXP tree_matSEXP, SEXP lowest_BICSEXP, SEXP parentSEXP, SEXP cp_mat_listSEXP, SEXP err_listSEXP, SEXP test_dataSEXP, SEXP alphaSEXP, SEXP betaSEXP, SEXP is_test_dataSEXP, SEXP penSEXP, SEXP num_cpSEXP, SEXP split_rule_nodeSEXP, SEXP gridpointSEXP, SEXP maxOWsizeSEXP, SEXP prev_sum_treesSEXP, SEXP prev_sum_trees_matSEXP, SEXP y_scaledSEXP, SEXP num_splitsSEXP, SEXP gridsizeSEXP, SEXP zero_splitSEXP, SEXP min_num_obs_for_splitSEXP, SEXP min_num_obs_after_splitSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type less_greedy(less_greedySEXP); + Rcpp::traits::input_parameter< double >::type spike_tree(spike_treeSEXP); + Rcpp::traits::input_parameter< int >::type s_t_hyperprior(s_t_hyperpriorSEXP); + Rcpp::traits::input_parameter< double >::type p_s_t(p_s_tSEXP); + Rcpp::traits::input_parameter< double >::type a_s_t(a_s_tSEXP); + Rcpp::traits::input_parameter< double >::type b_s_t(b_s_tSEXP); + Rcpp::traits::input_parameter< double >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type num_vars(num_varsSEXP); + Rcpp::traits::input_parameter< double >::type lambda_poisson(lambda_poissonSEXP); + Rcpp::traits::input_parameter< arma::mat& >::type D1(D1SEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type resids(residsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< double >::type sigma_mu(sigma_muSEXP); + Rcpp::traits::input_parameter< List >::type tree_table(tree_tableSEXP); + Rcpp::traits::input_parameter< List >::type tree_mat(tree_matSEXP); + Rcpp::traits::input_parameter< double >::type lowest_BIC(lowest_BICSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type parent(parentSEXP); + Rcpp::traits::input_parameter< List >::type cp_mat_list(cp_mat_listSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type err_list(err_listSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< double >::type alpha(alphaSEXP); + Rcpp::traits::input_parameter< double >::type beta(betaSEXP); + Rcpp::traits::input_parameter< bool >::type is_test_data(is_test_dataSEXP); + Rcpp::traits::input_parameter< double >::type pen(penSEXP); + Rcpp::traits::input_parameter< int >::type num_cp(num_cpSEXP); + Rcpp::traits::input_parameter< bool >::type split_rule_node(split_rule_nodeSEXP); + Rcpp::traits::input_parameter< bool >::type gridpoint(gridpointSEXP); + Rcpp::traits::input_parameter< int >::type maxOWsize(maxOWsizeSEXP); + Rcpp::traits::input_parameter< List >::type prev_sum_trees(prev_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type prev_sum_trees_mat(prev_sum_trees_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y_scaled(y_scaledSEXP); + Rcpp::traits::input_parameter< int >::type num_splits(num_splitsSEXP); + Rcpp::traits::input_parameter< int >::type gridsize(gridsizeSEXP); + Rcpp::traits::input_parameter< bool >::type zero_split(zero_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_for_split(min_num_obs_for_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_after_split(min_num_obs_after_splitSEXP); + rcpp_result_gen = Rcpp::wrap(get_best_trees_sum(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, err_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, prev_sum_trees, prev_sum_trees_mat, y_scaled, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split)); + return rcpp_result_gen; +END_RCPP +} +// get_best_trees_sum_update_splits +List get_best_trees_sum_update_splits(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, arma::mat& D1, NumericMatrix resids, double a, double mu, double nu, double lambda, double c, double sigma_mu, List tree_table, List tree_mat, double lowest_BIC, IntegerVector parent, List cp_mat_list, IntegerVector err_list, NumericMatrix test_data, double alpha, double beta, bool is_test_data, double pen, int num_cp, bool split_rule_node, bool gridpoint, int maxOWsize, List prev_sum_trees, List prev_sum_trees_mat, NumericVector y_scaled, int num_splits, int gridsize, bool zero_split, unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split); +RcppExport SEXP _bartBMA_get_best_trees_sum_update_splits(SEXP less_greedySEXP, SEXP spike_treeSEXP, SEXP s_t_hyperpriorSEXP, SEXP p_s_tSEXP, SEXP a_s_tSEXP, SEXP b_s_tSEXP, SEXP num_obsSEXP, SEXP num_varsSEXP, SEXP lambda_poissonSEXP, SEXP D1SEXP, SEXP residsSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP cSEXP, SEXP sigma_muSEXP, SEXP tree_tableSEXP, SEXP tree_matSEXP, SEXP lowest_BICSEXP, SEXP parentSEXP, SEXP cp_mat_listSEXP, SEXP err_listSEXP, SEXP test_dataSEXP, SEXP alphaSEXP, SEXP betaSEXP, SEXP is_test_dataSEXP, SEXP penSEXP, SEXP num_cpSEXP, SEXP split_rule_nodeSEXP, SEXP gridpointSEXP, SEXP maxOWsizeSEXP, SEXP prev_sum_treesSEXP, SEXP prev_sum_trees_matSEXP, SEXP y_scaledSEXP, SEXP num_splitsSEXP, SEXP gridsizeSEXP, SEXP zero_splitSEXP, SEXP min_num_obs_for_splitSEXP, SEXP min_num_obs_after_splitSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type less_greedy(less_greedySEXP); + Rcpp::traits::input_parameter< double >::type spike_tree(spike_treeSEXP); + Rcpp::traits::input_parameter< int >::type s_t_hyperprior(s_t_hyperpriorSEXP); + Rcpp::traits::input_parameter< double >::type p_s_t(p_s_tSEXP); + Rcpp::traits::input_parameter< double >::type a_s_t(a_s_tSEXP); + Rcpp::traits::input_parameter< double >::type b_s_t(b_s_tSEXP); + Rcpp::traits::input_parameter< double >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type num_vars(num_varsSEXP); + Rcpp::traits::input_parameter< double >::type lambda_poisson(lambda_poissonSEXP); + Rcpp::traits::input_parameter< arma::mat& >::type D1(D1SEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type resids(residsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< double >::type sigma_mu(sigma_muSEXP); + Rcpp::traits::input_parameter< List >::type tree_table(tree_tableSEXP); + Rcpp::traits::input_parameter< List >::type tree_mat(tree_matSEXP); + Rcpp::traits::input_parameter< double >::type lowest_BIC(lowest_BICSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type parent(parentSEXP); + Rcpp::traits::input_parameter< List >::type cp_mat_list(cp_mat_listSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type err_list(err_listSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< double >::type alpha(alphaSEXP); + Rcpp::traits::input_parameter< double >::type beta(betaSEXP); + Rcpp::traits::input_parameter< bool >::type is_test_data(is_test_dataSEXP); + Rcpp::traits::input_parameter< double >::type pen(penSEXP); + Rcpp::traits::input_parameter< int >::type num_cp(num_cpSEXP); + Rcpp::traits::input_parameter< bool >::type split_rule_node(split_rule_nodeSEXP); + Rcpp::traits::input_parameter< bool >::type gridpoint(gridpointSEXP); + Rcpp::traits::input_parameter< int >::type maxOWsize(maxOWsizeSEXP); + Rcpp::traits::input_parameter< List >::type prev_sum_trees(prev_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type prev_sum_trees_mat(prev_sum_trees_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y_scaled(y_scaledSEXP); + Rcpp::traits::input_parameter< int >::type num_splits(num_splitsSEXP); + Rcpp::traits::input_parameter< int >::type gridsize(gridsizeSEXP); + Rcpp::traits::input_parameter< bool >::type zero_split(zero_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_for_split(min_num_obs_for_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_after_split(min_num_obs_after_splitSEXP); + rcpp_result_gen = Rcpp::wrap(get_best_trees_sum_update_splits(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, err_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, prev_sum_trees, prev_sum_trees_mat, y_scaled, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split)); + return rcpp_result_gen; +END_RCPP +} +// get_best_trees_exact +List get_best_trees_exact(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, arma::mat& D1, NumericMatrix resids, double a, double mu, double nu, double lambda, double c, double sigma_mu, List tree_table, List tree_mat, double lowest_BIC, IntegerVector parent, List cp_mat_list, NumericMatrix test_data, double alpha, double beta, bool is_test_data, double pen, int num_cp, bool split_rule_node, bool gridpoint, int maxOWsize, int num_splits, int gridsize, bool zero_split, unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split); +RcppExport SEXP _bartBMA_get_best_trees_exact(SEXP less_greedySEXP, SEXP spike_treeSEXP, SEXP s_t_hyperpriorSEXP, SEXP p_s_tSEXP, SEXP a_s_tSEXP, SEXP b_s_tSEXP, SEXP num_obsSEXP, SEXP num_varsSEXP, SEXP lambda_poissonSEXP, SEXP D1SEXP, SEXP residsSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP cSEXP, SEXP sigma_muSEXP, SEXP tree_tableSEXP, SEXP tree_matSEXP, SEXP lowest_BICSEXP, SEXP parentSEXP, SEXP cp_mat_listSEXP, SEXP test_dataSEXP, SEXP alphaSEXP, SEXP betaSEXP, SEXP is_test_dataSEXP, SEXP penSEXP, SEXP num_cpSEXP, SEXP split_rule_nodeSEXP, SEXP gridpointSEXP, SEXP maxOWsizeSEXP, SEXP num_splitsSEXP, SEXP gridsizeSEXP, SEXP zero_splitSEXP, SEXP min_num_obs_for_splitSEXP, SEXP min_num_obs_after_splitSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type less_greedy(less_greedySEXP); + Rcpp::traits::input_parameter< double >::type spike_tree(spike_treeSEXP); + Rcpp::traits::input_parameter< int >::type s_t_hyperprior(s_t_hyperpriorSEXP); + Rcpp::traits::input_parameter< double >::type p_s_t(p_s_tSEXP); + Rcpp::traits::input_parameter< double >::type a_s_t(a_s_tSEXP); + Rcpp::traits::input_parameter< double >::type b_s_t(b_s_tSEXP); + Rcpp::traits::input_parameter< double >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type num_vars(num_varsSEXP); + Rcpp::traits::input_parameter< double >::type lambda_poisson(lambda_poissonSEXP); + Rcpp::traits::input_parameter< arma::mat& >::type D1(D1SEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type resids(residsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< double >::type sigma_mu(sigma_muSEXP); + Rcpp::traits::input_parameter< List >::type tree_table(tree_tableSEXP); + Rcpp::traits::input_parameter< List >::type tree_mat(tree_matSEXP); + Rcpp::traits::input_parameter< double >::type lowest_BIC(lowest_BICSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type parent(parentSEXP); + Rcpp::traits::input_parameter< List >::type cp_mat_list(cp_mat_listSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< double >::type alpha(alphaSEXP); + Rcpp::traits::input_parameter< double >::type beta(betaSEXP); + Rcpp::traits::input_parameter< bool >::type is_test_data(is_test_dataSEXP); + Rcpp::traits::input_parameter< double >::type pen(penSEXP); + Rcpp::traits::input_parameter< int >::type num_cp(num_cpSEXP); + Rcpp::traits::input_parameter< bool >::type split_rule_node(split_rule_nodeSEXP); + Rcpp::traits::input_parameter< bool >::type gridpoint(gridpointSEXP); + Rcpp::traits::input_parameter< int >::type maxOWsize(maxOWsizeSEXP); + Rcpp::traits::input_parameter< int >::type num_splits(num_splitsSEXP); + Rcpp::traits::input_parameter< int >::type gridsize(gridsizeSEXP); + Rcpp::traits::input_parameter< bool >::type zero_split(zero_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_for_split(min_num_obs_for_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_after_split(min_num_obs_after_splitSEXP); + rcpp_result_gen = Rcpp::wrap(get_best_trees_exact(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split)); + return rcpp_result_gen; +END_RCPP +} +// get_best_trees_update_splits_exact +List get_best_trees_update_splits_exact(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, arma::mat& D1, NumericMatrix resids, double a, double mu, double nu, double lambda, double c, double sigma_mu, List tree_table, List tree_mat, double lowest_BIC, IntegerVector parent, List cp_mat_list, NumericMatrix test_data, double alpha, double beta, bool is_test_data, double pen, int num_cp, bool split_rule_node, bool gridpoint, int maxOWsize, int num_splits, int gridsize, bool zero_split, unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split); +RcppExport SEXP _bartBMA_get_best_trees_update_splits_exact(SEXP less_greedySEXP, SEXP spike_treeSEXP, SEXP s_t_hyperpriorSEXP, SEXP p_s_tSEXP, SEXP a_s_tSEXP, SEXP b_s_tSEXP, SEXP num_obsSEXP, SEXP num_varsSEXP, SEXP lambda_poissonSEXP, SEXP D1SEXP, SEXP residsSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP cSEXP, SEXP sigma_muSEXP, SEXP tree_tableSEXP, SEXP tree_matSEXP, SEXP lowest_BICSEXP, SEXP parentSEXP, SEXP cp_mat_listSEXP, SEXP test_dataSEXP, SEXP alphaSEXP, SEXP betaSEXP, SEXP is_test_dataSEXP, SEXP penSEXP, SEXP num_cpSEXP, SEXP split_rule_nodeSEXP, SEXP gridpointSEXP, SEXP maxOWsizeSEXP, SEXP num_splitsSEXP, SEXP gridsizeSEXP, SEXP zero_splitSEXP, SEXP min_num_obs_for_splitSEXP, SEXP min_num_obs_after_splitSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type less_greedy(less_greedySEXP); + Rcpp::traits::input_parameter< double >::type spike_tree(spike_treeSEXP); + Rcpp::traits::input_parameter< int >::type s_t_hyperprior(s_t_hyperpriorSEXP); + Rcpp::traits::input_parameter< double >::type p_s_t(p_s_tSEXP); + Rcpp::traits::input_parameter< double >::type a_s_t(a_s_tSEXP); + Rcpp::traits::input_parameter< double >::type b_s_t(b_s_tSEXP); + Rcpp::traits::input_parameter< double >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type num_vars(num_varsSEXP); + Rcpp::traits::input_parameter< double >::type lambda_poisson(lambda_poissonSEXP); + Rcpp::traits::input_parameter< arma::mat& >::type D1(D1SEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type resids(residsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< double >::type sigma_mu(sigma_muSEXP); + Rcpp::traits::input_parameter< List >::type tree_table(tree_tableSEXP); + Rcpp::traits::input_parameter< List >::type tree_mat(tree_matSEXP); + Rcpp::traits::input_parameter< double >::type lowest_BIC(lowest_BICSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type parent(parentSEXP); + Rcpp::traits::input_parameter< List >::type cp_mat_list(cp_mat_listSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< double >::type alpha(alphaSEXP); + Rcpp::traits::input_parameter< double >::type beta(betaSEXP); + Rcpp::traits::input_parameter< bool >::type is_test_data(is_test_dataSEXP); + Rcpp::traits::input_parameter< double >::type pen(penSEXP); + Rcpp::traits::input_parameter< int >::type num_cp(num_cpSEXP); + Rcpp::traits::input_parameter< bool >::type split_rule_node(split_rule_nodeSEXP); + Rcpp::traits::input_parameter< bool >::type gridpoint(gridpointSEXP); + Rcpp::traits::input_parameter< int >::type maxOWsize(maxOWsizeSEXP); + Rcpp::traits::input_parameter< int >::type num_splits(num_splitsSEXP); + Rcpp::traits::input_parameter< int >::type gridsize(gridsizeSEXP); + Rcpp::traits::input_parameter< bool >::type zero_split(zero_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_for_split(min_num_obs_for_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_after_split(min_num_obs_after_splitSEXP); + rcpp_result_gen = Rcpp::wrap(get_best_trees_update_splits_exact(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split)); + return rcpp_result_gen; +END_RCPP +} +// get_best_trees_sum_exact +List get_best_trees_sum_exact(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, arma::mat& D1, NumericMatrix resids, double a, double mu, double nu, double lambda, double c, double sigma_mu, List tree_table, List tree_mat, double lowest_BIC, IntegerVector parent, List cp_mat_list, IntegerVector err_list, NumericMatrix test_data, double alpha, double beta, bool is_test_data, double pen, int num_cp, bool split_rule_node, bool gridpoint, int maxOWsize, List prev_sum_trees, List prev_sum_trees_mat, NumericVector y_scaled, int num_splits, int gridsize, bool zero_split, unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split); +RcppExport SEXP _bartBMA_get_best_trees_sum_exact(SEXP less_greedySEXP, SEXP spike_treeSEXP, SEXP s_t_hyperpriorSEXP, SEXP p_s_tSEXP, SEXP a_s_tSEXP, SEXP b_s_tSEXP, SEXP num_obsSEXP, SEXP num_varsSEXP, SEXP lambda_poissonSEXP, SEXP D1SEXP, SEXP residsSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP cSEXP, SEXP sigma_muSEXP, SEXP tree_tableSEXP, SEXP tree_matSEXP, SEXP lowest_BICSEXP, SEXP parentSEXP, SEXP cp_mat_listSEXP, SEXP err_listSEXP, SEXP test_dataSEXP, SEXP alphaSEXP, SEXP betaSEXP, SEXP is_test_dataSEXP, SEXP penSEXP, SEXP num_cpSEXP, SEXP split_rule_nodeSEXP, SEXP gridpointSEXP, SEXP maxOWsizeSEXP, SEXP prev_sum_treesSEXP, SEXP prev_sum_trees_matSEXP, SEXP y_scaledSEXP, SEXP num_splitsSEXP, SEXP gridsizeSEXP, SEXP zero_splitSEXP, SEXP min_num_obs_for_splitSEXP, SEXP min_num_obs_after_splitSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type less_greedy(less_greedySEXP); + Rcpp::traits::input_parameter< double >::type spike_tree(spike_treeSEXP); + Rcpp::traits::input_parameter< int >::type s_t_hyperprior(s_t_hyperpriorSEXP); + Rcpp::traits::input_parameter< double >::type p_s_t(p_s_tSEXP); + Rcpp::traits::input_parameter< double >::type a_s_t(a_s_tSEXP); + Rcpp::traits::input_parameter< double >::type b_s_t(b_s_tSEXP); + Rcpp::traits::input_parameter< double >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type num_vars(num_varsSEXP); + Rcpp::traits::input_parameter< double >::type lambda_poisson(lambda_poissonSEXP); + Rcpp::traits::input_parameter< arma::mat& >::type D1(D1SEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type resids(residsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< double >::type sigma_mu(sigma_muSEXP); + Rcpp::traits::input_parameter< List >::type tree_table(tree_tableSEXP); + Rcpp::traits::input_parameter< List >::type tree_mat(tree_matSEXP); + Rcpp::traits::input_parameter< double >::type lowest_BIC(lowest_BICSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type parent(parentSEXP); + Rcpp::traits::input_parameter< List >::type cp_mat_list(cp_mat_listSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type err_list(err_listSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< double >::type alpha(alphaSEXP); + Rcpp::traits::input_parameter< double >::type beta(betaSEXP); + Rcpp::traits::input_parameter< bool >::type is_test_data(is_test_dataSEXP); + Rcpp::traits::input_parameter< double >::type pen(penSEXP); + Rcpp::traits::input_parameter< int >::type num_cp(num_cpSEXP); + Rcpp::traits::input_parameter< bool >::type split_rule_node(split_rule_nodeSEXP); + Rcpp::traits::input_parameter< bool >::type gridpoint(gridpointSEXP); + Rcpp::traits::input_parameter< int >::type maxOWsize(maxOWsizeSEXP); + Rcpp::traits::input_parameter< List >::type prev_sum_trees(prev_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type prev_sum_trees_mat(prev_sum_trees_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y_scaled(y_scaledSEXP); + Rcpp::traits::input_parameter< int >::type num_splits(num_splitsSEXP); + Rcpp::traits::input_parameter< int >::type gridsize(gridsizeSEXP); + Rcpp::traits::input_parameter< bool >::type zero_split(zero_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_for_split(min_num_obs_for_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_after_split(min_num_obs_after_splitSEXP); + rcpp_result_gen = Rcpp::wrap(get_best_trees_sum_exact(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, err_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, prev_sum_trees, prev_sum_trees_mat, y_scaled, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split)); + return rcpp_result_gen; +END_RCPP +} +// get_best_trees_sum_update_splits_exact +List get_best_trees_sum_update_splits_exact(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, arma::mat& D1, NumericMatrix resids, double a, double mu, double nu, double lambda, double c, double sigma_mu, List tree_table, List tree_mat, double lowest_BIC, IntegerVector parent, List cp_mat_list, IntegerVector err_list, NumericMatrix test_data, double alpha, double beta, bool is_test_data, double pen, int num_cp, bool split_rule_node, bool gridpoint, int maxOWsize, List prev_sum_trees, List prev_sum_trees_mat, NumericVector y_scaled, int num_splits, int gridsize, bool zero_split, unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split); +RcppExport SEXP _bartBMA_get_best_trees_sum_update_splits_exact(SEXP less_greedySEXP, SEXP spike_treeSEXP, SEXP s_t_hyperpriorSEXP, SEXP p_s_tSEXP, SEXP a_s_tSEXP, SEXP b_s_tSEXP, SEXP num_obsSEXP, SEXP num_varsSEXP, SEXP lambda_poissonSEXP, SEXP D1SEXP, SEXP residsSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP cSEXP, SEXP sigma_muSEXP, SEXP tree_tableSEXP, SEXP tree_matSEXP, SEXP lowest_BICSEXP, SEXP parentSEXP, SEXP cp_mat_listSEXP, SEXP err_listSEXP, SEXP test_dataSEXP, SEXP alphaSEXP, SEXP betaSEXP, SEXP is_test_dataSEXP, SEXP penSEXP, SEXP num_cpSEXP, SEXP split_rule_nodeSEXP, SEXP gridpointSEXP, SEXP maxOWsizeSEXP, SEXP prev_sum_treesSEXP, SEXP prev_sum_trees_matSEXP, SEXP y_scaledSEXP, SEXP num_splitsSEXP, SEXP gridsizeSEXP, SEXP zero_splitSEXP, SEXP min_num_obs_for_splitSEXP, SEXP min_num_obs_after_splitSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type less_greedy(less_greedySEXP); + Rcpp::traits::input_parameter< double >::type spike_tree(spike_treeSEXP); + Rcpp::traits::input_parameter< int >::type s_t_hyperprior(s_t_hyperpriorSEXP); + Rcpp::traits::input_parameter< double >::type p_s_t(p_s_tSEXP); + Rcpp::traits::input_parameter< double >::type a_s_t(a_s_tSEXP); + Rcpp::traits::input_parameter< double >::type b_s_t(b_s_tSEXP); + Rcpp::traits::input_parameter< double >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type num_vars(num_varsSEXP); + Rcpp::traits::input_parameter< double >::type lambda_poisson(lambda_poissonSEXP); + Rcpp::traits::input_parameter< arma::mat& >::type D1(D1SEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type resids(residsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< double >::type sigma_mu(sigma_muSEXP); + Rcpp::traits::input_parameter< List >::type tree_table(tree_tableSEXP); + Rcpp::traits::input_parameter< List >::type tree_mat(tree_matSEXP); + Rcpp::traits::input_parameter< double >::type lowest_BIC(lowest_BICSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type parent(parentSEXP); + Rcpp::traits::input_parameter< List >::type cp_mat_list(cp_mat_listSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type err_list(err_listSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< double >::type alpha(alphaSEXP); + Rcpp::traits::input_parameter< double >::type beta(betaSEXP); + Rcpp::traits::input_parameter< bool >::type is_test_data(is_test_dataSEXP); + Rcpp::traits::input_parameter< double >::type pen(penSEXP); + Rcpp::traits::input_parameter< int >::type num_cp(num_cpSEXP); + Rcpp::traits::input_parameter< bool >::type split_rule_node(split_rule_nodeSEXP); + Rcpp::traits::input_parameter< bool >::type gridpoint(gridpointSEXP); + Rcpp::traits::input_parameter< int >::type maxOWsize(maxOWsizeSEXP); + Rcpp::traits::input_parameter< List >::type prev_sum_trees(prev_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type prev_sum_trees_mat(prev_sum_trees_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y_scaled(y_scaledSEXP); + Rcpp::traits::input_parameter< int >::type num_splits(num_splitsSEXP); + Rcpp::traits::input_parameter< int >::type gridsize(gridsizeSEXP); + Rcpp::traits::input_parameter< bool >::type zero_split(zero_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_for_split(min_num_obs_for_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_after_split(min_num_obs_after_splitSEXP); + rcpp_result_gen = Rcpp::wrap(get_best_trees_sum_update_splits_exact(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, D1, resids, a, mu, nu, lambda, c, sigma_mu, tree_table, tree_mat, lowest_BIC, parent, cp_mat_list, err_list, test_data, alpha, beta, is_test_data, pen, num_cp, split_rule_node, gridpoint, maxOWsize, prev_sum_trees, prev_sum_trees_mat, y_scaled, num_splits, gridsize, zero_split, min_num_obs_for_split, min_num_obs_after_split)); + return rcpp_result_gen; +END_RCPP +} +// scale_response +NumericVector scale_response(double a, double b, double c, double d, NumericVector y); +RcppExport SEXP _bartBMA_scale_response(SEXP aSEXP, SEXP bSEXP, SEXP cSEXP, SEXP dSEXP, SEXP ySEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type b(bSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< double >::type d(dSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + rcpp_result_gen = Rcpp::wrap(scale_response(a, b, c, d, y)); + return rcpp_result_gen; +END_RCPP +} +// get_original +NumericVector get_original(double low, double high, double sp_low, double sp_high, NumericVector sum_preds); +RcppExport SEXP _bartBMA_get_original(SEXP lowSEXP, SEXP highSEXP, SEXP sp_lowSEXP, SEXP sp_highSEXP, SEXP sum_predsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type low(lowSEXP); + Rcpp::traits::input_parameter< double >::type high(highSEXP); + Rcpp::traits::input_parameter< double >::type sp_low(sp_lowSEXP); + Rcpp::traits::input_parameter< double >::type sp_high(sp_highSEXP); + Rcpp::traits::input_parameter< NumericVector >::type sum_preds(sum_predsSEXP); + rcpp_result_gen = Rcpp::wrap(get_original(low, high, sp_low, sp_high, sum_preds)); + return rcpp_result_gen; +END_RCPP +} +// get_original_arma +arma::vec get_original_arma(double low, double high, double sp_low, double sp_high, arma::vec sum_preds); +RcppExport SEXP _bartBMA_get_original_arma(SEXP lowSEXP, SEXP highSEXP, SEXP sp_lowSEXP, SEXP sp_highSEXP, SEXP sum_predsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type low(lowSEXP); + Rcpp::traits::input_parameter< double >::type high(highSEXP); + Rcpp::traits::input_parameter< double >::type sp_low(sp_lowSEXP); + Rcpp::traits::input_parameter< double >::type sp_high(sp_highSEXP); + Rcpp::traits::input_parameter< arma::vec >::type sum_preds(sum_predsSEXP); + rcpp_result_gen = Rcpp::wrap(get_original_arma(low, high, sp_low, sp_high, sum_preds)); + return rcpp_result_gen; +END_RCPP +} +// get_original_TE_arma +arma::vec get_original_TE_arma(double low, double high, double sp_low, double sp_high, arma::vec sum_preds); +RcppExport SEXP _bartBMA_get_original_TE_arma(SEXP lowSEXP, SEXP highSEXP, SEXP sp_lowSEXP, SEXP sp_highSEXP, SEXP sum_predsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type low(lowSEXP); + Rcpp::traits::input_parameter< double >::type high(highSEXP); + Rcpp::traits::input_parameter< double >::type sp_low(sp_lowSEXP); + Rcpp::traits::input_parameter< double >::type sp_high(sp_highSEXP); + Rcpp::traits::input_parameter< arma::vec >::type sum_preds(sum_predsSEXP); + rcpp_result_gen = Rcpp::wrap(get_original_TE_arma(low, high, sp_low, sp_high, sum_preds)); + return rcpp_result_gen; +END_RCPP +} +// get_original_TE_double +double get_original_TE_double(double low, double high, double sp_low, double sp_high, double sum_preds); +RcppExport SEXP _bartBMA_get_original_TE_double(SEXP lowSEXP, SEXP highSEXP, SEXP sp_lowSEXP, SEXP sp_highSEXP, SEXP sum_predsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type low(lowSEXP); + Rcpp::traits::input_parameter< double >::type high(highSEXP); + Rcpp::traits::input_parameter< double >::type sp_low(sp_lowSEXP); + Rcpp::traits::input_parameter< double >::type sp_high(sp_highSEXP); + Rcpp::traits::input_parameter< double >::type sum_preds(sum_predsSEXP); + rcpp_result_gen = Rcpp::wrap(get_original_TE_double(low, high, sp_low, sp_high, sum_preds)); + return rcpp_result_gen; +END_RCPP +} +// get_termobs_test_data +List get_termobs_test_data(NumericMatrix test_data, NumericMatrix tree_data); +RcppExport SEXP _bartBMA_get_termobs_test_data(SEXP test_dataSEXP, SEXP tree_dataSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type tree_data(tree_dataSEXP); + rcpp_result_gen = Rcpp::wrap(get_termobs_test_data(test_data, tree_data)); + return rcpp_result_gen; +END_RCPP +} +// get_termobs_test_data_fields +arma::field get_termobs_test_data_fields(NumericMatrix test_data, NumericMatrix tree_data); +RcppExport SEXP _bartBMA_get_termobs_test_data_fields(SEXP test_dataSEXP, SEXP tree_dataSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type tree_data(tree_dataSEXP); + rcpp_result_gen = Rcpp::wrap(get_termobs_test_data_fields(test_data, tree_data)); + return rcpp_result_gen; +END_RCPP +} +// get_termobs_testdata_overall +List get_termobs_testdata_overall(List overall_sum_trees, NumericMatrix test_data); +RcppExport SEXP _bartBMA_get_termobs_testdata_overall(SEXP overall_sum_treesSEXP, SEXP test_dataSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + rcpp_result_gen = Rcpp::wrap(get_termobs_testdata_overall(overall_sum_trees, test_data)); + return rcpp_result_gen; +END_RCPP +} +// get_J_test +arma::mat get_J_test(List curr_termobs, NumericVector tree_term_nodes, int n); +RcppExport SEXP _bartBMA_get_J_test(SEXP curr_termobsSEXP, SEXP tree_term_nodesSEXP, SEXP nSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type curr_termobs(curr_termobsSEXP); + Rcpp::traits::input_parameter< NumericVector >::type tree_term_nodes(tree_term_nodesSEXP); + Rcpp::traits::input_parameter< int >::type n(nSEXP); + rcpp_result_gen = Rcpp::wrap(get_J_test(curr_termobs, tree_term_nodes, n)); + return rcpp_result_gen; +END_RCPP +} +// get_W_test +arma::mat get_W_test(List sum_treetable, List termobs_testdata_onemodel, int n); +RcppExport SEXP _bartBMA_get_W_test(SEXP sum_treetableSEXP, SEXP termobs_testdata_onemodelSEXP, SEXP nSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type sum_treetable(sum_treetableSEXP); + Rcpp::traits::input_parameter< List >::type termobs_testdata_onemodel(termobs_testdata_onemodelSEXP); + Rcpp::traits::input_parameter< int >::type n(nSEXP); + rcpp_result_gen = Rcpp::wrap(get_W_test(sum_treetable, termobs_testdata_onemodel, n)); + return rcpp_result_gen; +END_RCPP +} +// preds_bbma_lin_alg_outsamp +NumericVector preds_bbma_lin_alg_outsamp(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, NumericMatrix test_data); +RcppExport SEXP _bartBMA_preds_bbma_lin_alg_outsamp(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP test_dataSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + rcpp_result_gen = Rcpp::wrap(preds_bbma_lin_alg_outsamp(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data)); + return rcpp_result_gen; +END_RCPP +} +// preds_bbma_lin_alg_insamp +NumericVector preds_bbma_lin_alg_insamp(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, double a, double sigma, double mu_mu, double nu, double lambda); +RcppExport SEXP _bartBMA_preds_bbma_lin_alg_insamp(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + rcpp_result_gen = Rcpp::wrap(preds_bbma_lin_alg_insamp(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda)); + return rcpp_result_gen; +END_RCPP +} +// mean_vars_lin_alg_outsamp +List mean_vars_lin_alg_outsamp(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, NumericMatrix test_data); +RcppExport SEXP _bartBMA_mean_vars_lin_alg_outsamp(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP test_dataSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + rcpp_result_gen = Rcpp::wrap(mean_vars_lin_alg_outsamp(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data)); + return rcpp_result_gen; +END_RCPP +} +// mean_vars_lin_alg_insamp +List mean_vars_lin_alg_insamp(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda); +RcppExport SEXP _bartBMA_mean_vars_lin_alg_insamp(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + rcpp_result_gen = Rcpp::wrap(mean_vars_lin_alg_insamp(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda)); + return rcpp_result_gen; +END_RCPP +} +// BART_BMA_sumLikelihood +List BART_BMA_sumLikelihood(double less_greedy, double spike_tree, int s_t_hyperprior, double p_s_t, double a_s_t, double b_s_t, double num_obs, double num_vars, double lambda_poisson, NumericMatrix data, NumericVector y, double start_mean, double start_sd, double a, double mu, double nu, double lambda, double c, double sigma_mu, double pen, int num_cp, NumericMatrix test_data, int num_rounds, double alpha, double beta, bool split_rule_node, bool gridpoint, int maxOWsize, int num_splits, int gridsize, bool zero_split, bool only_max_num_trees, unsigned int min_num_obs_for_split, unsigned int min_num_obs_after_split, int exact_residuals); +RcppExport SEXP _bartBMA_BART_BMA_sumLikelihood(SEXP less_greedySEXP, SEXP spike_treeSEXP, SEXP s_t_hyperpriorSEXP, SEXP p_s_tSEXP, SEXP a_s_tSEXP, SEXP b_s_tSEXP, SEXP num_obsSEXP, SEXP num_varsSEXP, SEXP lambda_poissonSEXP, SEXP dataSEXP, SEXP ySEXP, SEXP start_meanSEXP, SEXP start_sdSEXP, SEXP aSEXP, SEXP muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP cSEXP, SEXP sigma_muSEXP, SEXP penSEXP, SEXP num_cpSEXP, SEXP test_dataSEXP, SEXP num_roundsSEXP, SEXP alphaSEXP, SEXP betaSEXP, SEXP split_rule_nodeSEXP, SEXP gridpointSEXP, SEXP maxOWsizeSEXP, SEXP num_splitsSEXP, SEXP gridsizeSEXP, SEXP zero_splitSEXP, SEXP only_max_num_treesSEXP, SEXP min_num_obs_for_splitSEXP, SEXP min_num_obs_after_splitSEXP, SEXP exact_residualsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type less_greedy(less_greedySEXP); + Rcpp::traits::input_parameter< double >::type spike_tree(spike_treeSEXP); + Rcpp::traits::input_parameter< int >::type s_t_hyperprior(s_t_hyperpriorSEXP); + Rcpp::traits::input_parameter< double >::type p_s_t(p_s_tSEXP); + Rcpp::traits::input_parameter< double >::type a_s_t(a_s_tSEXP); + Rcpp::traits::input_parameter< double >::type b_s_t(b_s_tSEXP); + Rcpp::traits::input_parameter< double >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type num_vars(num_varsSEXP); + Rcpp::traits::input_parameter< double >::type lambda_poisson(lambda_poissonSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type data(dataSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< double >::type start_mean(start_meanSEXP); + Rcpp::traits::input_parameter< double >::type start_sd(start_sdSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type mu(muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< double >::type sigma_mu(sigma_muSEXP); + Rcpp::traits::input_parameter< double >::type pen(penSEXP); + Rcpp::traits::input_parameter< int >::type num_cp(num_cpSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< int >::type num_rounds(num_roundsSEXP); + Rcpp::traits::input_parameter< double >::type alpha(alphaSEXP); + Rcpp::traits::input_parameter< double >::type beta(betaSEXP); + Rcpp::traits::input_parameter< bool >::type split_rule_node(split_rule_nodeSEXP); + Rcpp::traits::input_parameter< bool >::type gridpoint(gridpointSEXP); + Rcpp::traits::input_parameter< int >::type maxOWsize(maxOWsizeSEXP); + Rcpp::traits::input_parameter< int >::type num_splits(num_splitsSEXP); + Rcpp::traits::input_parameter< int >::type gridsize(gridsizeSEXP); + Rcpp::traits::input_parameter< bool >::type zero_split(zero_splitSEXP); + Rcpp::traits::input_parameter< bool >::type only_max_num_trees(only_max_num_treesSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_for_split(min_num_obs_for_splitSEXP); + Rcpp::traits::input_parameter< unsigned int >::type min_num_obs_after_split(min_num_obs_after_splitSEXP); + Rcpp::traits::input_parameter< int >::type exact_residuals(exact_residualsSEXP); + rcpp_result_gen = Rcpp::wrap(BART_BMA_sumLikelihood(less_greedy, spike_tree, s_t_hyperprior, p_s_t, a_s_t, b_s_t, num_obs, num_vars, lambda_poisson, data, y, start_mean, start_sd, a, mu, nu, lambda, c, sigma_mu, pen, num_cp, test_data, num_rounds, alpha, beta, split_rule_node, gridpoint, maxOWsize, num_splits, gridsize, zero_split, only_max_num_trees, min_num_obs_for_split, min_num_obs_after_split, exact_residuals)); + return rcpp_result_gen; +END_RCPP +} +// Quantile +Rcpp::NumericVector Quantile(Rcpp::NumericVector x, Rcpp::NumericVector probs); +RcppExport SEXP _bartBMA_Quantile(SEXP xSEXP, SEXP probsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< Rcpp::NumericVector >::type x(xSEXP); + Rcpp::traits::input_parameter< Rcpp::NumericVector >::type probs(probsSEXP); + rcpp_result_gen = Rcpp::wrap(Quantile(x, probs)); + return rcpp_result_gen; +END_RCPP +} +// mixt_eval_cdf +double mixt_eval_cdf(double x_val, double d_o_f, std::vector mean_vec, std::vector var_vec, std::vector weights_vec, double quant_val); +RcppExport SEXP _bartBMA_mixt_eval_cdf(SEXP x_valSEXP, SEXP d_o_fSEXP, SEXP mean_vecSEXP, SEXP var_vecSEXP, SEXP weights_vecSEXP, SEXP quant_valSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type x_val(x_valSEXP); + Rcpp::traits::input_parameter< double >::type d_o_f(d_o_fSEXP); + Rcpp::traits::input_parameter< std::vector >::type mean_vec(mean_vecSEXP); + Rcpp::traits::input_parameter< std::vector >::type var_vec(var_vecSEXP); + Rcpp::traits::input_parameter< std::vector >::type weights_vec(weights_vecSEXP); + Rcpp::traits::input_parameter< double >::type quant_val(quant_valSEXP); + rcpp_result_gen = Rcpp::wrap(mixt_eval_cdf(x_val, d_o_f, mean_vec, var_vec, weights_vec, quant_val)); + return rcpp_result_gen; +END_RCPP +} +// rootmixt +double rootmixt(double d_o_f, double a, double b, std::vector mean_vec, std::vector var_vec, std::vector weights_vec, double quant_val, double root_alg_precision); +RcppExport SEXP _bartBMA_rootmixt(SEXP d_o_fSEXP, SEXP aSEXP, SEXP bSEXP, SEXP mean_vecSEXP, SEXP var_vecSEXP, SEXP weights_vecSEXP, SEXP quant_valSEXP, SEXP root_alg_precisionSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type d_o_f(d_o_fSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type b(bSEXP); + Rcpp::traits::input_parameter< std::vector >::type mean_vec(mean_vecSEXP); + Rcpp::traits::input_parameter< std::vector >::type var_vec(var_vecSEXP); + Rcpp::traits::input_parameter< std::vector >::type weights_vec(weights_vecSEXP); + Rcpp::traits::input_parameter< double >::type quant_val(quant_valSEXP); + Rcpp::traits::input_parameter< double >::type root_alg_precision(root_alg_precisionSEXP); + rcpp_result_gen = Rcpp::wrap(rootmixt(d_o_f, a, b, mean_vec, var_vec, weights_vec, quant_val, root_alg_precision)); + return rcpp_result_gen; +END_RCPP +} +// pred_ints_exact_outsamp +List pred_ints_exact_outsamp(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, NumericMatrix test_data, double lower_prob, double upper_prob, int num_cores, double root_alg_precision); +RcppExport SEXP _bartBMA_pred_ints_exact_outsamp(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP test_dataSEXP, SEXP lower_probSEXP, SEXP upper_probSEXP, SEXP num_coresSEXP, SEXP root_alg_precisionSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< double >::type lower_prob(lower_probSEXP); + Rcpp::traits::input_parameter< double >::type upper_prob(upper_probSEXP); + Rcpp::traits::input_parameter< int >::type num_cores(num_coresSEXP); + Rcpp::traits::input_parameter< double >::type root_alg_precision(root_alg_precisionSEXP); + rcpp_result_gen = Rcpp::wrap(pred_ints_exact_outsamp(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores, root_alg_precision)); + return rcpp_result_gen; +END_RCPP +} +// pred_ints_exact_outsamp_par +List pred_ints_exact_outsamp_par(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, NumericMatrix test_data, double lower_prob, double upper_prob, int num_cores, double root_alg_precision); +RcppExport SEXP _bartBMA_pred_ints_exact_outsamp_par(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP test_dataSEXP, SEXP lower_probSEXP, SEXP upper_probSEXP, SEXP num_coresSEXP, SEXP root_alg_precisionSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< double >::type lower_prob(lower_probSEXP); + Rcpp::traits::input_parameter< double >::type upper_prob(upper_probSEXP); + Rcpp::traits::input_parameter< int >::type num_cores(num_coresSEXP); + Rcpp::traits::input_parameter< double >::type root_alg_precision(root_alg_precisionSEXP); + rcpp_result_gen = Rcpp::wrap(pred_ints_exact_outsamp_par(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores, root_alg_precision)); + return rcpp_result_gen; +END_RCPP +} +// pred_ints_lin_alg_outsamp +List pred_ints_lin_alg_outsamp(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, NumericMatrix test_data, double lower_prob, double upper_prob); +RcppExport SEXP _bartBMA_pred_ints_lin_alg_outsamp(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP test_dataSEXP, SEXP lower_probSEXP, SEXP upper_probSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< double >::type lower_prob(lower_probSEXP); + Rcpp::traits::input_parameter< double >::type upper_prob(upper_probSEXP); + rcpp_result_gen = Rcpp::wrap(pred_ints_lin_alg_outsamp(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob)); + return rcpp_result_gen; +END_RCPP +} +// pred_ints_lin_alg_insamp +List pred_ints_lin_alg_insamp(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, double a, double sigma, double mu_mu, double nu, double lambda, double lower_prob, double upper_prob); +RcppExport SEXP _bartBMA_pred_ints_lin_alg_insamp(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP lower_probSEXP, SEXP upper_probSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type lower_prob(lower_probSEXP); + Rcpp::traits::input_parameter< double >::type upper_prob(upper_probSEXP); + rcpp_result_gen = Rcpp::wrap(pred_ints_lin_alg_insamp(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, lower_prob, upper_prob)); + return rcpp_result_gen; +END_RCPP +} +// pred_ints_chol_attempt_outsamp +List pred_ints_chol_attempt_outsamp(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, NumericMatrix test_data, double lower_prob, double upper_prob); +RcppExport SEXP _bartBMA_pred_ints_chol_attempt_outsamp(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP test_dataSEXP, SEXP lower_probSEXP, SEXP upper_probSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< double >::type lower_prob(lower_probSEXP); + Rcpp::traits::input_parameter< double >::type upper_prob(upper_probSEXP); + rcpp_result_gen = Rcpp::wrap(pred_ints_chol_attempt_outsamp(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob)); + return rcpp_result_gen; +END_RCPP +} +// pred_ints_lin_alg_parallel_outsamp +List pred_ints_lin_alg_parallel_outsamp(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, NumericMatrix test_data, double lower_prob, double upper_prob, int num_cores); +RcppExport SEXP _bartBMA_pred_ints_lin_alg_parallel_outsamp(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP test_dataSEXP, SEXP lower_probSEXP, SEXP upper_probSEXP, SEXP num_coresSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< double >::type lower_prob(lower_probSEXP); + Rcpp::traits::input_parameter< double >::type upper_prob(upper_probSEXP); + Rcpp::traits::input_parameter< int >::type num_cores(num_coresSEXP); + rcpp_result_gen = Rcpp::wrap(pred_ints_lin_alg_parallel_outsamp(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores)); + return rcpp_result_gen; +END_RCPP +} +// pred_ints_lin_alg_fields_outsamp +List pred_ints_lin_alg_fields_outsamp(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, NumericMatrix test_data, double lower_prob, double upper_prob, int num_cores); +RcppExport SEXP _bartBMA_pred_ints_lin_alg_fields_outsamp(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP test_dataSEXP, SEXP lower_probSEXP, SEXP upper_probSEXP, SEXP num_coresSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< double >::type lower_prob(lower_probSEXP); + Rcpp::traits::input_parameter< double >::type upper_prob(upper_probSEXP); + Rcpp::traits::input_parameter< int >::type num_cores(num_coresSEXP); + rcpp_result_gen = Rcpp::wrap(pred_ints_lin_alg_fields_outsamp(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores)); + return rcpp_result_gen; +END_RCPP +} +// pred_ints_chol_parallel_outsamp +List pred_ints_chol_parallel_outsamp(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, NumericMatrix test_data, double lower_prob, double upper_prob, int num_cores); +RcppExport SEXP _bartBMA_pred_ints_chol_parallel_outsamp(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP test_dataSEXP, SEXP lower_probSEXP, SEXP upper_probSEXP, SEXP num_coresSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< double >::type lower_prob(lower_probSEXP); + Rcpp::traits::input_parameter< double >::type upper_prob(upper_probSEXP); + Rcpp::traits::input_parameter< int >::type num_cores(num_coresSEXP); + rcpp_result_gen = Rcpp::wrap(pred_ints_chol_parallel_outsamp(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores)); + return rcpp_result_gen; +END_RCPP +} +// mean_vars_lin_alg_parallel_outsamp +List mean_vars_lin_alg_parallel_outsamp(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, NumericMatrix test_data, int num_cores); +RcppExport SEXP _bartBMA_mean_vars_lin_alg_parallel_outsamp(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP test_dataSEXP, SEXP num_coresSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< int >::type num_cores(num_coresSEXP); + rcpp_result_gen = Rcpp::wrap(mean_vars_lin_alg_parallel_outsamp(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, num_cores)); + return rcpp_result_gen; +END_RCPP +} +// pred_ints_ITE_outsamp_par +List pred_ints_ITE_outsamp_par(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, NumericMatrix test_data, double lower_prob, double upper_prob, int num_cores, double root_alg_precision, NumericMatrix training_data); +RcppExport SEXP _bartBMA_pred_ints_ITE_outsamp_par(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP test_dataSEXP, SEXP lower_probSEXP, SEXP upper_probSEXP, SEXP num_coresSEXP, SEXP root_alg_precisionSEXP, SEXP training_dataSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< double >::type lower_prob(lower_probSEXP); + Rcpp::traits::input_parameter< double >::type upper_prob(upper_probSEXP); + Rcpp::traits::input_parameter< int >::type num_cores(num_coresSEXP); + Rcpp::traits::input_parameter< double >::type root_alg_precision(root_alg_precisionSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type training_data(training_dataSEXP); + rcpp_result_gen = Rcpp::wrap(pred_ints_ITE_outsamp_par(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores, root_alg_precision, training_data)); + return rcpp_result_gen; +END_RCPP +} +// pred_ints_ITE_insamp_par +List pred_ints_ITE_insamp_par(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_obs, double a, double sigma, double mu_mu, double nu, double lambda, double lower_prob, double upper_prob, int num_cores, double root_alg_precision, NumericMatrix training_data); +RcppExport SEXP _bartBMA_pred_ints_ITE_insamp_par(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP lower_probSEXP, SEXP upper_probSEXP, SEXP num_coresSEXP, SEXP root_alg_precisionSEXP, SEXP training_dataSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type lower_prob(lower_probSEXP); + Rcpp::traits::input_parameter< double >::type upper_prob(upper_probSEXP); + Rcpp::traits::input_parameter< int >::type num_cores(num_coresSEXP); + Rcpp::traits::input_parameter< double >::type root_alg_precision(root_alg_precisionSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type training_data(training_dataSEXP); + rcpp_result_gen = Rcpp::wrap(pred_ints_ITE_insamp_par(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_obs, a, sigma, mu_mu, nu, lambda, lower_prob, upper_prob, num_cores, root_alg_precision, training_data)); + return rcpp_result_gen; +END_RCPP +} +// pred_ints_ITE_CATT_outsamp_par +List pred_ints_ITE_CATT_outsamp_par(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, NumericMatrix test_data, double lower_prob, double upper_prob, int num_cores, double root_alg_precision, NumericMatrix training_data, NumericVector ztest); +RcppExport SEXP _bartBMA_pred_ints_ITE_CATT_outsamp_par(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP test_dataSEXP, SEXP lower_probSEXP, SEXP upper_probSEXP, SEXP num_coresSEXP, SEXP root_alg_precisionSEXP, SEXP training_dataSEXP, SEXP ztestSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< double >::type lower_prob(lower_probSEXP); + Rcpp::traits::input_parameter< double >::type upper_prob(upper_probSEXP); + Rcpp::traits::input_parameter< int >::type num_cores(num_coresSEXP); + Rcpp::traits::input_parameter< double >::type root_alg_precision(root_alg_precisionSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type training_data(training_dataSEXP); + Rcpp::traits::input_parameter< NumericVector >::type ztest(ztestSEXP); + rcpp_result_gen = Rcpp::wrap(pred_ints_ITE_CATT_outsamp_par(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, test_data, lower_prob, upper_prob, num_cores, root_alg_precision, training_data, ztest)); + return rcpp_result_gen; +END_RCPP +} +// pred_ints_ITE_CATT_insamp_par +List pred_ints_ITE_CATT_insamp_par(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_obs, double a, double sigma, double mu_mu, double nu, double lambda, double lower_prob, double upper_prob, int num_cores, double root_alg_precision, NumericMatrix training_data, NumericVector ztrain); +RcppExport SEXP _bartBMA_pred_ints_ITE_CATT_insamp_par(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP lower_probSEXP, SEXP upper_probSEXP, SEXP num_coresSEXP, SEXP root_alg_precisionSEXP, SEXP training_dataSEXP, SEXP ztrainSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< double >::type lower_prob(lower_probSEXP); + Rcpp::traits::input_parameter< double >::type upper_prob(upper_probSEXP); + Rcpp::traits::input_parameter< int >::type num_cores(num_coresSEXP); + Rcpp::traits::input_parameter< double >::type root_alg_precision(root_alg_precisionSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type training_data(training_dataSEXP); + Rcpp::traits::input_parameter< NumericVector >::type ztrain(ztrainSEXP); + rcpp_result_gen = Rcpp::wrap(pred_ints_ITE_CATT_insamp_par(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_obs, a, sigma, mu_mu, nu, lambda, lower_prob, upper_prob, num_cores, root_alg_precision, training_data, ztrain)); + return rcpp_result_gen; +END_RCPP +} +// find_term_nodes_gs +NumericVector find_term_nodes_gs(NumericMatrix tree_table); +RcppExport SEXP _bartBMA_find_term_nodes_gs(SEXP tree_tableSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type tree_table(tree_tableSEXP); + rcpp_result_gen = Rcpp::wrap(find_term_nodes_gs(tree_table)); + return rcpp_result_gen; +END_RCPP +} +// find_term_obs_gs +NumericVector find_term_obs_gs(NumericMatrix tree_matrix_temp, double terminal_node); +RcppExport SEXP _bartBMA_find_term_obs_gs(SEXP tree_matrix_tempSEXP, SEXP terminal_nodeSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type tree_matrix_temp(tree_matrix_tempSEXP); + Rcpp::traits::input_parameter< double >::type terminal_node(terminal_nodeSEXP); + rcpp_result_gen = Rcpp::wrap(find_term_obs_gs(tree_matrix_temp, terminal_node)); + return rcpp_result_gen; +END_RCPP +} +// calc_rowsums +NumericVector calc_rowsums(NumericMatrix predictions); +RcppExport SEXP _bartBMA_calc_rowsums(SEXP predictionsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type predictions(predictionsSEXP); + rcpp_result_gen = Rcpp::wrap(calc_rowsums(predictions)); + return rcpp_result_gen; +END_RCPP +} +// calculate_resids +NumericVector calculate_resids(NumericMatrix predictions, NumericVector response); +RcppExport SEXP _bartBMA_calculate_resids(SEXP predictionsSEXP, SEXP responseSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type predictions(predictionsSEXP); + Rcpp::traits::input_parameter< NumericVector >::type response(responseSEXP); + rcpp_result_gen = Rcpp::wrap(calculate_resids(predictions, response)); + return rcpp_result_gen; +END_RCPP +} +// update_Gibbs_mean_var +List update_Gibbs_mean_var(NumericVector resids, double a, double sigma, double mu_mu, IntegerVector terminal_nodes, List term_obs_tree); +RcppExport SEXP _bartBMA_update_Gibbs_mean_var(SEXP residsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP terminal_nodesSEXP, SEXP term_obs_treeSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type resids(residsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type terminal_nodes(terminal_nodesSEXP); + Rcpp::traits::input_parameter< List >::type term_obs_tree(term_obs_treeSEXP); + rcpp_result_gen = Rcpp::wrap(update_Gibbs_mean_var(resids, a, sigma, mu_mu, terminal_nodes, term_obs_tree)); + return rcpp_result_gen; +END_RCPP +} +// update_sigma +double update_sigma(double a1, double b, NumericVector resids, int n); +RcppExport SEXP _bartBMA_update_sigma(SEXP a1SEXP, SEXP bSEXP, SEXP residsSEXP, SEXP nSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type a1(a1SEXP); + Rcpp::traits::input_parameter< double >::type b(bSEXP); + Rcpp::traits::input_parameter< NumericVector >::type resids(residsSEXP); + Rcpp::traits::input_parameter< int >::type n(nSEXP); + rcpp_result_gen = Rcpp::wrap(update_sigma(a1, b, resids, n)); + return rcpp_result_gen; +END_RCPP +} +// find_node_means +NumericVector find_node_means(NumericMatrix sum_tree, NumericVector term_nodes); +RcppExport SEXP _bartBMA_find_node_means(SEXP sum_treeSEXP, SEXP term_nodesSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type sum_tree(sum_treeSEXP); + Rcpp::traits::input_parameter< NumericVector >::type term_nodes(term_nodesSEXP); + rcpp_result_gen = Rcpp::wrap(find_node_means(sum_tree, term_nodes)); + return rcpp_result_gen; +END_RCPP +} +// get_tree_info +List get_tree_info(List overall_sum_trees, List overall_sum_mat, int num_obs); +RcppExport SEXP _bartBMA_get_tree_info(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP num_obsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + rcpp_result_gen = Rcpp::wrap(get_tree_info(overall_sum_trees, overall_sum_mat, num_obs)); + return rcpp_result_gen; +END_RCPP +} +// remove_curr_col +NumericMatrix remove_curr_col(NumericMatrix predy, int i); +RcppExport SEXP _bartBMA_remove_curr_col(SEXP predySEXP, SEXP iSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type predy(predySEXP); + Rcpp::traits::input_parameter< int >::type i(iSEXP); + rcpp_result_gen = Rcpp::wrap(remove_curr_col(predy, i)); + return rcpp_result_gen; +END_RCPP +} +// get_new_mean +NumericVector get_new_mean(IntegerVector terminal_nodes, List new_mean_var); +RcppExport SEXP _bartBMA_get_new_mean(SEXP terminal_nodesSEXP, SEXP new_mean_varSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< IntegerVector >::type terminal_nodes(terminal_nodesSEXP); + Rcpp::traits::input_parameter< List >::type new_mean_var(new_mean_varSEXP); + rcpp_result_gen = Rcpp::wrap(get_new_mean(terminal_nodes, new_mean_var)); + return rcpp_result_gen; +END_RCPP +} +// update_predictions_gs +List update_predictions_gs(NumericVector new_mean, NumericVector new_var, int n, IntegerVector terminal_nodes, List term_obs_tree); +RcppExport SEXP _bartBMA_update_predictions_gs(SEXP new_meanSEXP, SEXP new_varSEXP, SEXP nSEXP, SEXP terminal_nodesSEXP, SEXP term_obs_treeSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type new_mean(new_meanSEXP); + Rcpp::traits::input_parameter< NumericVector >::type new_var(new_varSEXP); + Rcpp::traits::input_parameter< int >::type n(nSEXP); + Rcpp::traits::input_parameter< IntegerVector >::type terminal_nodes(terminal_nodesSEXP); + Rcpp::traits::input_parameter< List >::type term_obs_tree(term_obs_treeSEXP); + rcpp_result_gen = Rcpp::wrap(update_predictions_gs(new_mean, new_var, n, terminal_nodes, term_obs_tree)); + return rcpp_result_gen; +END_RCPP +} +// scale_response_gs +NumericVector scale_response_gs(double a, double b, double c, double d, NumericVector y); +RcppExport SEXP _bartBMA_scale_response_gs(SEXP aSEXP, SEXP bSEXP, SEXP cSEXP, SEXP dSEXP, SEXP ySEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type b(bSEXP); + Rcpp::traits::input_parameter< double >::type c(cSEXP); + Rcpp::traits::input_parameter< double >::type d(dSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + rcpp_result_gen = Rcpp::wrap(scale_response_gs(a, b, c, d, y)); + return rcpp_result_gen; +END_RCPP +} +// get_original_gs +NumericVector get_original_gs(double low, double high, double sp_low, double sp_high, NumericVector sum_preds); +RcppExport SEXP _bartBMA_get_original_gs(SEXP lowSEXP, SEXP highSEXP, SEXP sp_lowSEXP, SEXP sp_highSEXP, SEXP sum_predsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< double >::type low(lowSEXP); + Rcpp::traits::input_parameter< double >::type high(highSEXP); + Rcpp::traits::input_parameter< double >::type sp_low(sp_lowSEXP); + Rcpp::traits::input_parameter< double >::type sp_high(sp_highSEXP); + Rcpp::traits::input_parameter< NumericVector >::type sum_preds(sum_predsSEXP); + rcpp_result_gen = Rcpp::wrap(get_original_gs(low, high, sp_low, sp_high, sum_preds)); + return rcpp_result_gen; +END_RCPP +} +// find_internal_nodes_gs +NumericVector find_internal_nodes_gs(NumericMatrix treetable); +RcppExport SEXP _bartBMA_find_internal_nodes_gs(SEXP treetableSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type treetable(treetableSEXP); + rcpp_result_gen = Rcpp::wrap(find_internal_nodes_gs(treetable)); + return rcpp_result_gen; +END_RCPP +} +// get_tree_info_test_data +List get_tree_info_test_data(NumericMatrix test_data, NumericMatrix tree_data); +RcppExport SEXP _bartBMA_get_tree_info_test_data(SEXP test_dataSEXP, SEXP tree_dataSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type tree_data(tree_dataSEXP); + rcpp_result_gen = Rcpp::wrap(get_tree_info_test_data(test_data, tree_data)); + return rcpp_result_gen; +END_RCPP +} +// get_tree_info_testdata_overall +List get_tree_info_testdata_overall(List overall_sum_trees, int num_obs, NumericMatrix test_data); +RcppExport SEXP _bartBMA_get_tree_info_testdata_overall(SEXP overall_sum_treesSEXP, SEXP num_obsSEXP, SEXP test_dataSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + rcpp_result_gen = Rcpp::wrap(get_tree_info_testdata_overall(overall_sum_trees, num_obs, test_data)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler +List gibbs_sampler(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, NumericMatrix test_data); +RcppExport SEXP _bartBMA_gibbs_sampler(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP test_dataSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler2 +List gibbs_sampler2(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids); +RcppExport SEXP _bartBMA_gibbs_sampler2(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler2(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler_no_update +List gibbs_sampler_no_update(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, NumericMatrix test_data); +RcppExport SEXP _bartBMA_gibbs_sampler_no_update(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP test_dataSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler_no_update(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler_no_update2 +List gibbs_sampler_no_update2(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids); +RcppExport SEXP _bartBMA_gibbs_sampler_no_update2(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler_no_update2(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler_exp +List gibbs_sampler_exp(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, NumericMatrix test_data); +RcppExport SEXP _bartBMA_gibbs_sampler_exp(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP test_dataSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler_exp(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler2_exp +List gibbs_sampler2_exp(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids); +RcppExport SEXP _bartBMA_gibbs_sampler2_exp(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler2_exp(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler_no_update_exp +List gibbs_sampler_no_update_exp(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, NumericMatrix test_data); +RcppExport SEXP _bartBMA_gibbs_sampler_no_update_exp(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP test_dataSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler_no_update_exp(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler_no_update2_exp +List gibbs_sampler_no_update2_exp(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids); +RcppExport SEXP _bartBMA_gibbs_sampler_no_update2_exp(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler_no_update2_exp(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler_ITE +List gibbs_sampler_ITE(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, NumericMatrix all_treated_data, NumericMatrix all_control_data); +RcppExport SEXP _bartBMA_gibbs_sampler_ITE(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP all_treated_dataSEXP, SEXP all_control_dataSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type all_treated_data(all_treated_dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type all_control_data(all_control_dataSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler_ITE(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler_ITE2 +List gibbs_sampler_ITE2(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, NumericMatrix all_treated_data, NumericMatrix all_control_data); +RcppExport SEXP _bartBMA_gibbs_sampler_ITE2(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP all_treated_dataSEXP, SEXP all_control_dataSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type all_treated_data(all_treated_dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type all_control_data(all_control_dataSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler_ITE2(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler_ITE_no_update +List gibbs_sampler_ITE_no_update(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, NumericMatrix all_treated_data, NumericMatrix all_control_data); +RcppExport SEXP _bartBMA_gibbs_sampler_ITE_no_update(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP all_treated_dataSEXP, SEXP all_control_dataSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type all_treated_data(all_treated_dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type all_control_data(all_control_dataSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler_ITE_no_update(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler_ITE_no_update2 +List gibbs_sampler_ITE_no_update2(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, NumericMatrix all_treated_data, NumericMatrix all_control_data); +RcppExport SEXP _bartBMA_gibbs_sampler_ITE_no_update2(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP all_treated_dataSEXP, SEXP all_control_dataSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type all_treated_data(all_treated_dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type all_control_data(all_control_dataSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler_ITE_no_update2(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler_new_inits +List gibbs_sampler_new_inits(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, NumericMatrix test_data, List new_pred_list); +RcppExport SEXP _bartBMA_gibbs_sampler_new_inits(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP test_dataSEXP, SEXP new_pred_listSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< List >::type new_pred_list(new_pred_listSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler_new_inits(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data, new_pred_list)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler2_new_inits +List gibbs_sampler2_new_inits(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, List new_pred_list); +RcppExport SEXP _bartBMA_gibbs_sampler2_new_inits(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP new_pred_listSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< List >::type new_pred_list(new_pred_listSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler2_new_inits(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, new_pred_list)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler_no_update_new_inits +List gibbs_sampler_no_update_new_inits(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, NumericMatrix test_data, List new_pred_list); +RcppExport SEXP _bartBMA_gibbs_sampler_no_update_new_inits(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP test_dataSEXP, SEXP new_pred_listSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< List >::type new_pred_list(new_pred_listSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler_no_update_new_inits(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data, new_pred_list)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler_no_update2_new_inits +List gibbs_sampler_no_update2_new_inits(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, List new_pred_list); +RcppExport SEXP _bartBMA_gibbs_sampler_no_update2_new_inits(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP new_pred_listSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< List >::type new_pred_list(new_pred_listSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler_no_update2_new_inits(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, new_pred_list)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler_exp_new_inits +List gibbs_sampler_exp_new_inits(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, NumericMatrix test_data, List new_pred_list); +RcppExport SEXP _bartBMA_gibbs_sampler_exp_new_inits(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP test_dataSEXP, SEXP new_pred_listSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< List >::type new_pred_list(new_pred_listSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler_exp_new_inits(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data, new_pred_list)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler2_exp_new_inits +List gibbs_sampler2_exp_new_inits(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, List new_pred_list); +RcppExport SEXP _bartBMA_gibbs_sampler2_exp_new_inits(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP new_pred_listSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< List >::type new_pred_list(new_pred_listSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler2_exp_new_inits(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, new_pred_list)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler_no_update_exp_new_inits +List gibbs_sampler_no_update_exp_new_inits(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, NumericMatrix test_data, List new_pred_list); +RcppExport SEXP _bartBMA_gibbs_sampler_no_update_exp_new_inits(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP test_dataSEXP, SEXP new_pred_listSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type test_data(test_dataSEXP); + Rcpp::traits::input_parameter< List >::type new_pred_list(new_pred_listSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler_no_update_exp_new_inits(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, test_data, new_pred_list)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler_no_update2_exp_new_inits +List gibbs_sampler_no_update2_exp_new_inits(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, List new_pred_list); +RcppExport SEXP _bartBMA_gibbs_sampler_no_update2_exp_new_inits(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP new_pred_listSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< List >::type new_pred_list(new_pred_listSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler_no_update2_exp_new_inits(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, new_pred_list)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler_ITE_new_inits +List gibbs_sampler_ITE_new_inits(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, NumericMatrix all_treated_data, NumericMatrix all_control_data, List new_pred_list); +RcppExport SEXP _bartBMA_gibbs_sampler_ITE_new_inits(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP all_treated_dataSEXP, SEXP all_control_dataSEXP, SEXP new_pred_listSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type all_treated_data(all_treated_dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type all_control_data(all_control_dataSEXP); + Rcpp::traits::input_parameter< List >::type new_pred_list(new_pred_listSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler_ITE_new_inits(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data, new_pred_list)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler_ITE2_new_inits +List gibbs_sampler_ITE2_new_inits(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, NumericMatrix all_treated_data, NumericMatrix all_control_data, List new_pred_list); +RcppExport SEXP _bartBMA_gibbs_sampler_ITE2_new_inits(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP all_treated_dataSEXP, SEXP all_control_dataSEXP, SEXP new_pred_listSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type all_treated_data(all_treated_dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type all_control_data(all_control_dataSEXP); + Rcpp::traits::input_parameter< List >::type new_pred_list(new_pred_listSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler_ITE2_new_inits(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data, new_pred_list)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler_ITE_no_update_new_inits +List gibbs_sampler_ITE_no_update_new_inits(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, int num_test_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, NumericMatrix all_treated_data, NumericMatrix all_control_data, List new_pred_list); +RcppExport SEXP _bartBMA_gibbs_sampler_ITE_no_update_new_inits(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP num_test_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP all_treated_dataSEXP, SEXP all_control_dataSEXP, SEXP new_pred_listSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< int >::type num_test_obs(num_test_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type all_treated_data(all_treated_dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type all_control_data(all_control_dataSEXP); + Rcpp::traits::input_parameter< List >::type new_pred_list(new_pred_listSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler_ITE_no_update_new_inits(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, num_test_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data, new_pred_list)); + return rcpp_result_gen; +END_RCPP +} +// gibbs_sampler_ITE_no_update2_new_inits +List gibbs_sampler_ITE_no_update2_new_inits(List overall_sum_trees, List overall_sum_mat, NumericVector y, NumericVector BIC_weights, int num_iter, int burnin, int num_obs, double a, double sigma, double mu_mu, double nu, double lambda, List resids, NumericMatrix all_treated_data, NumericMatrix all_control_data, List new_pred_list); +RcppExport SEXP _bartBMA_gibbs_sampler_ITE_no_update2_new_inits(SEXP overall_sum_treesSEXP, SEXP overall_sum_matSEXP, SEXP ySEXP, SEXP BIC_weightsSEXP, SEXP num_iterSEXP, SEXP burninSEXP, SEXP num_obsSEXP, SEXP aSEXP, SEXP sigmaSEXP, SEXP mu_muSEXP, SEXP nuSEXP, SEXP lambdaSEXP, SEXP residsSEXP, SEXP all_treated_dataSEXP, SEXP all_control_dataSEXP, SEXP new_pred_listSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< List >::type overall_sum_trees(overall_sum_treesSEXP); + Rcpp::traits::input_parameter< List >::type overall_sum_mat(overall_sum_matSEXP); + Rcpp::traits::input_parameter< NumericVector >::type y(ySEXP); + Rcpp::traits::input_parameter< NumericVector >::type BIC_weights(BIC_weightsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type burnin(burninSEXP); + Rcpp::traits::input_parameter< int >::type num_obs(num_obsSEXP); + Rcpp::traits::input_parameter< double >::type a(aSEXP); + Rcpp::traits::input_parameter< double >::type sigma(sigmaSEXP); + Rcpp::traits::input_parameter< double >::type mu_mu(mu_muSEXP); + Rcpp::traits::input_parameter< double >::type nu(nuSEXP); + Rcpp::traits::input_parameter< double >::type lambda(lambdaSEXP); + Rcpp::traits::input_parameter< List >::type resids(residsSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type all_treated_data(all_treated_dataSEXP); + Rcpp::traits::input_parameter< NumericMatrix >::type all_control_data(all_control_dataSEXP); + Rcpp::traits::input_parameter< List >::type new_pred_list(new_pred_listSEXP); + rcpp_result_gen = Rcpp::wrap(gibbs_sampler_ITE_no_update2_new_inits(overall_sum_trees, overall_sum_mat, y, BIC_weights, num_iter, burnin, num_obs, a, sigma, mu_mu, nu, lambda, resids, all_treated_data, all_control_data, new_pred_list)); + return rcpp_result_gen; +END_RCPP +} + +static const R_CallMethodDef CallEntries[] = { + {"_bartBMA_find_internal_nodes_pred", (DL_FUNC) &_bartBMA_find_internal_nodes_pred, 1}, + {"_bartBMA_find_term_nodes_pred", (DL_FUNC) &_bartBMA_find_term_nodes_pred, 1}, + {"_bartBMA_get_original_pred", (DL_FUNC) &_bartBMA_get_original_pred, 5}, + {"_bartBMA_bartBMA_get_testdata_term_obs_pred", (DL_FUNC) &_bartBMA_bartBMA_get_testdata_term_obs_pred, 3}, + {"_bartBMA_get_BART_BMA_test_predictions", (DL_FUNC) &_bartBMA_get_BART_BMA_test_predictions, 4}, + {"_bartBMA_get_imp_vars", (DL_FUNC) &_bartBMA_get_imp_vars, 3}, + {"_bartBMA_get_weighted_var_imp", (DL_FUNC) &_bartBMA_get_weighted_var_imp, 3}, + {"_bartBMA_csample_num", (DL_FUNC) &_bartBMA_csample_num, 4}, + {"_bartBMA_add_rows", (DL_FUNC) &_bartBMA_add_rows, 2}, + {"_bartBMA_addcol", (DL_FUNC) &_bartBMA_addcol, 4}, + {"_bartBMA_set_daughter_to_end_tree", (DL_FUNC) &_bartBMA_set_daughter_to_end_tree, 3}, + {"_bartBMA_set_daughter_to_end_mat", (DL_FUNC) &_bartBMA_set_daughter_to_end_mat, 5}, + {"_bartBMA_remove_zero", (DL_FUNC) &_bartBMA_remove_zero, 1}, + {"_bartBMA_order_intvec_", (DL_FUNC) &_bartBMA_order_intvec_, 1}, + {"_bartBMA_get_gnp", (DL_FUNC) &_bartBMA_get_gnp, 2}, + {"_bartBMA_find_term_nodes", (DL_FUNC) &_bartBMA_find_term_nodes, 1}, + {"_bartBMA_find_term_obs", (DL_FUNC) &_bartBMA_find_term_obs, 2}, + {"_bartBMA_likelihood_function", (DL_FUNC) &_bartBMA_likelihood_function, 7}, + {"_bartBMA_find_internal_nodes", (DL_FUNC) &_bartBMA_find_internal_nodes, 1}, + {"_bartBMA_find_prev_nonterm", (DL_FUNC) &_bartBMA_find_prev_nonterm, 2}, + {"_bartBMA_find_nodes_to_update", (DL_FUNC) &_bartBMA_find_nodes_to_update, 2}, + {"_bartBMA_set_tree_to_middle", (DL_FUNC) &_bartBMA_set_tree_to_middle, 4}, + {"_bartBMA_update_grow_obs", (DL_FUNC) &_bartBMA_update_grow_obs, 6}, + {"_bartBMA_find_obs_to_update_grow", (DL_FUNC) &_bartBMA_find_obs_to_update_grow, 5}, + {"_bartBMA_get_daughter_obs", (DL_FUNC) &_bartBMA_get_daughter_obs, 4}, + {"_bartBMA_find_term_cols", (DL_FUNC) &_bartBMA_find_term_cols, 2}, + {"_bartBMA_get_grow_obs", (DL_FUNC) &_bartBMA_get_grow_obs, 3}, + {"_bartBMA_grow_tree", (DL_FUNC) &_bartBMA_grow_tree, 8}, + {"_bartBMA_set_daughter", (DL_FUNC) &_bartBMA_set_daughter, 6}, + {"_bartBMA_order_", (DL_FUNC) &_bartBMA_order_, 1}, + {"_bartBMA_orderforOW", (DL_FUNC) &_bartBMA_orderforOW, 1}, + {"_bartBMA_secondKindStirlingNumber", (DL_FUNC) &_bartBMA_secondKindStirlingNumber, 2}, + {"_bartBMA_get_tree_prior", (DL_FUNC) &_bartBMA_get_tree_prior, 12}, + {"_bartBMA_start_tree", (DL_FUNC) &_bartBMA_start_tree, 2}, + {"_bartBMA_start_tree2", (DL_FUNC) &_bartBMA_start_tree2, 0}, + {"_bartBMA_start_matrix", (DL_FUNC) &_bartBMA_start_matrix, 1}, + {"_bartBMA_evaluate_model_occams_window", (DL_FUNC) &_bartBMA_evaluate_model_occams_window, 6}, + {"_bartBMA_evaluate_model_occams_window_exact", (DL_FUNC) &_bartBMA_evaluate_model_occams_window_exact, 7}, + {"_bartBMA_get_testdata_term_obs", (DL_FUNC) &_bartBMA_get_testdata_term_obs, 2}, + {"_bartBMA_get_initial_resids", (DL_FUNC) &_bartBMA_get_initial_resids, 3}, + {"_bartBMA_resize", (DL_FUNC) &_bartBMA_resize, 2}, + {"_bartBMA_resize_bigger", (DL_FUNC) &_bartBMA_resize_bigger, 2}, + {"_bartBMA_J", (DL_FUNC) &_bartBMA_J, 2}, + {"_bartBMA_mu_vector", (DL_FUNC) &_bartBMA_mu_vector, 2}, + {"_bartBMA_W", (DL_FUNC) &_bartBMA_W, 3}, + {"_bartBMA_likelihood_function2", (DL_FUNC) &_bartBMA_likelihood_function2, 7}, + {"_bartBMA_likelihood_function2_exact", (DL_FUNC) &_bartBMA_likelihood_function2_exact, 7}, + {"_bartBMA_sumtree_likelihood_function", (DL_FUNC) &_bartBMA_sumtree_likelihood_function, 7}, + {"_bartBMA_sumtree_likelihood_function2", (DL_FUNC) &_bartBMA_sumtree_likelihood_function2, 7}, + {"_bartBMA_sumtree_likelihood_function2_exact", (DL_FUNC) &_bartBMA_sumtree_likelihood_function2_exact, 7}, + {"_bartBMA_sumtree_likelihood_function3", (DL_FUNC) &_bartBMA_sumtree_likelihood_function3, 7}, + {"_bartBMA_sumtree_likelihood_function4", (DL_FUNC) &_bartBMA_sumtree_likelihood_function4, 7}, + {"_bartBMA_get_best_split", (DL_FUNC) &_bartBMA_get_best_split, 26}, + {"_bartBMA_get_best_split_2", (DL_FUNC) &_bartBMA_get_best_split_2, 26}, + {"_bartBMA_get_best_split_sum", (DL_FUNC) &_bartBMA_get_best_split_sum, 30}, + {"_bartBMA_get_best_split_sum_2", (DL_FUNC) &_bartBMA_get_best_split_sum_2, 30}, + {"_bartBMA_get_best_split_exact", (DL_FUNC) &_bartBMA_get_best_split_exact, 26}, + {"_bartBMA_get_best_split_2_exact", (DL_FUNC) &_bartBMA_get_best_split_2_exact, 26}, + {"_bartBMA_get_best_split_sum_exact", (DL_FUNC) &_bartBMA_get_best_split_sum_exact, 30}, + {"_bartBMA_get_best_split_sum_2_exact", (DL_FUNC) &_bartBMA_get_best_split_sum_2_exact, 30}, + {"_bartBMA_update_mean_var", (DL_FUNC) &_bartBMA_update_mean_var, 4}, + {"_bartBMA_update_predictions", (DL_FUNC) &_bartBMA_update_predictions, 4}, + {"_bartBMA_subsetter", (DL_FUNC) &_bartBMA_subsetter, 2}, + {"_bartBMA_order_inc_", (DL_FUNC) &_bartBMA_order_inc_, 1}, + {"_bartBMA_min_which2", (DL_FUNC) &_bartBMA_min_which2, 4}, + {"_bartBMA_mll_meanvar2", (DL_FUNC) &_bartBMA_mll_meanvar2, 3}, + {"_bartBMA_PELT_meanvar_norm2", (DL_FUNC) &_bartBMA_PELT_meanvar_norm2, 2}, + {"_bartBMA_SS", (DL_FUNC) &_bartBMA_SS, 3}, + {"_bartBMA_gridCP", (DL_FUNC) &_bartBMA_gridCP, 3}, + {"_bartBMA_gridCP_arma", (DL_FUNC) &_bartBMA_gridCP_arma, 3}, + {"_bartBMA_make_gridpoint_cpmat", (DL_FUNC) &_bartBMA_make_gridpoint_cpmat, 4}, + {"_bartBMA_make_gridpoint_cpmat_arma", (DL_FUNC) &_bartBMA_make_gridpoint_cpmat_arma, 4}, + {"_bartBMA_make_pelt_cpmat", (DL_FUNC) &_bartBMA_make_pelt_cpmat, 4}, + {"_bartBMA_get_best_trees", (DL_FUNC) &_bartBMA_get_best_trees, 36}, + {"_bartBMA_get_best_trees_update_splits", (DL_FUNC) &_bartBMA_get_best_trees_update_splits, 36}, + {"_bartBMA_get_best_trees_sum", (DL_FUNC) &_bartBMA_get_best_trees_sum, 40}, + {"_bartBMA_get_best_trees_sum_update_splits", (DL_FUNC) &_bartBMA_get_best_trees_sum_update_splits, 40}, + {"_bartBMA_get_best_trees_exact", (DL_FUNC) &_bartBMA_get_best_trees_exact, 36}, + {"_bartBMA_get_best_trees_update_splits_exact", (DL_FUNC) &_bartBMA_get_best_trees_update_splits_exact, 36}, + {"_bartBMA_get_best_trees_sum_exact", (DL_FUNC) &_bartBMA_get_best_trees_sum_exact, 40}, + {"_bartBMA_get_best_trees_sum_update_splits_exact", (DL_FUNC) &_bartBMA_get_best_trees_sum_update_splits_exact, 40}, + {"_bartBMA_scale_response", (DL_FUNC) &_bartBMA_scale_response, 5}, + {"_bartBMA_get_original", (DL_FUNC) &_bartBMA_get_original, 5}, + {"_bartBMA_get_original_arma", (DL_FUNC) &_bartBMA_get_original_arma, 5}, + {"_bartBMA_get_original_TE_arma", (DL_FUNC) &_bartBMA_get_original_TE_arma, 5}, + {"_bartBMA_get_original_TE_double", (DL_FUNC) &_bartBMA_get_original_TE_double, 5}, + {"_bartBMA_get_termobs_test_data", (DL_FUNC) &_bartBMA_get_termobs_test_data, 2}, + {"_bartBMA_get_termobs_test_data_fields", (DL_FUNC) &_bartBMA_get_termobs_test_data_fields, 2}, + {"_bartBMA_get_termobs_testdata_overall", (DL_FUNC) &_bartBMA_get_termobs_testdata_overall, 2}, + {"_bartBMA_get_J_test", (DL_FUNC) &_bartBMA_get_J_test, 3}, + {"_bartBMA_get_W_test", (DL_FUNC) &_bartBMA_get_W_test, 3}, + {"_bartBMA_preds_bbma_lin_alg_outsamp", (DL_FUNC) &_bartBMA_preds_bbma_lin_alg_outsamp, 14}, + {"_bartBMA_preds_bbma_lin_alg_insamp", (DL_FUNC) &_bartBMA_preds_bbma_lin_alg_insamp, 12}, + {"_bartBMA_mean_vars_lin_alg_outsamp", (DL_FUNC) &_bartBMA_mean_vars_lin_alg_outsamp, 14}, + {"_bartBMA_mean_vars_lin_alg_insamp", (DL_FUNC) &_bartBMA_mean_vars_lin_alg_insamp, 13}, + {"_bartBMA_BART_BMA_sumLikelihood", (DL_FUNC) &_bartBMA_BART_BMA_sumLikelihood, 35}, + {"_bartBMA_Quantile", (DL_FUNC) &_bartBMA_Quantile, 2}, + {"_bartBMA_mixt_eval_cdf", (DL_FUNC) &_bartBMA_mixt_eval_cdf, 6}, + {"_bartBMA_rootmixt", (DL_FUNC) &_bartBMA_rootmixt, 8}, + {"_bartBMA_pred_ints_exact_outsamp", (DL_FUNC) &_bartBMA_pred_ints_exact_outsamp, 16}, + {"_bartBMA_pred_ints_exact_outsamp_par", (DL_FUNC) &_bartBMA_pred_ints_exact_outsamp_par, 16}, + {"_bartBMA_pred_ints_lin_alg_outsamp", (DL_FUNC) &_bartBMA_pred_ints_lin_alg_outsamp, 16}, + {"_bartBMA_pred_ints_lin_alg_insamp", (DL_FUNC) &_bartBMA_pred_ints_lin_alg_insamp, 14}, + {"_bartBMA_pred_ints_chol_attempt_outsamp", (DL_FUNC) &_bartBMA_pred_ints_chol_attempt_outsamp, 16}, + {"_bartBMA_pred_ints_lin_alg_parallel_outsamp", (DL_FUNC) &_bartBMA_pred_ints_lin_alg_parallel_outsamp, 17}, + {"_bartBMA_pred_ints_lin_alg_fields_outsamp", (DL_FUNC) &_bartBMA_pred_ints_lin_alg_fields_outsamp, 17}, + {"_bartBMA_pred_ints_chol_parallel_outsamp", (DL_FUNC) &_bartBMA_pred_ints_chol_parallel_outsamp, 17}, + {"_bartBMA_mean_vars_lin_alg_parallel_outsamp", (DL_FUNC) &_bartBMA_mean_vars_lin_alg_parallel_outsamp, 15}, + {"_bartBMA_pred_ints_ITE_outsamp_par", (DL_FUNC) &_bartBMA_pred_ints_ITE_outsamp_par, 17}, + {"_bartBMA_pred_ints_ITE_insamp_par", (DL_FUNC) &_bartBMA_pred_ints_ITE_insamp_par, 15}, + {"_bartBMA_pred_ints_ITE_CATT_outsamp_par", (DL_FUNC) &_bartBMA_pred_ints_ITE_CATT_outsamp_par, 18}, + {"_bartBMA_pred_ints_ITE_CATT_insamp_par", (DL_FUNC) &_bartBMA_pred_ints_ITE_CATT_insamp_par, 16}, + {"_bartBMA_find_term_nodes_gs", (DL_FUNC) &_bartBMA_find_term_nodes_gs, 1}, + {"_bartBMA_find_term_obs_gs", (DL_FUNC) &_bartBMA_find_term_obs_gs, 2}, + {"_bartBMA_calc_rowsums", (DL_FUNC) &_bartBMA_calc_rowsums, 1}, + {"_bartBMA_calculate_resids", (DL_FUNC) &_bartBMA_calculate_resids, 2}, + {"_bartBMA_update_Gibbs_mean_var", (DL_FUNC) &_bartBMA_update_Gibbs_mean_var, 6}, + {"_bartBMA_update_sigma", (DL_FUNC) &_bartBMA_update_sigma, 4}, + {"_bartBMA_find_node_means", (DL_FUNC) &_bartBMA_find_node_means, 2}, + {"_bartBMA_get_tree_info", (DL_FUNC) &_bartBMA_get_tree_info, 3}, + {"_bartBMA_remove_curr_col", (DL_FUNC) &_bartBMA_remove_curr_col, 2}, + {"_bartBMA_get_new_mean", (DL_FUNC) &_bartBMA_get_new_mean, 2}, + {"_bartBMA_update_predictions_gs", (DL_FUNC) &_bartBMA_update_predictions_gs, 5}, + {"_bartBMA_scale_response_gs", (DL_FUNC) &_bartBMA_scale_response_gs, 5}, + {"_bartBMA_get_original_gs", (DL_FUNC) &_bartBMA_get_original_gs, 5}, + {"_bartBMA_find_internal_nodes_gs", (DL_FUNC) &_bartBMA_find_internal_nodes_gs, 1}, + {"_bartBMA_get_tree_info_test_data", (DL_FUNC) &_bartBMA_get_tree_info_test_data, 2}, + {"_bartBMA_get_tree_info_testdata_overall", (DL_FUNC) &_bartBMA_get_tree_info_testdata_overall, 3}, + {"_bartBMA_gibbs_sampler", (DL_FUNC) &_bartBMA_gibbs_sampler, 15}, + {"_bartBMA_gibbs_sampler2", (DL_FUNC) &_bartBMA_gibbs_sampler2, 13}, + {"_bartBMA_gibbs_sampler_no_update", (DL_FUNC) &_bartBMA_gibbs_sampler_no_update, 15}, + {"_bartBMA_gibbs_sampler_no_update2", (DL_FUNC) &_bartBMA_gibbs_sampler_no_update2, 13}, + {"_bartBMA_gibbs_sampler_exp", (DL_FUNC) &_bartBMA_gibbs_sampler_exp, 15}, + {"_bartBMA_gibbs_sampler2_exp", (DL_FUNC) &_bartBMA_gibbs_sampler2_exp, 13}, + {"_bartBMA_gibbs_sampler_no_update_exp", (DL_FUNC) &_bartBMA_gibbs_sampler_no_update_exp, 15}, + {"_bartBMA_gibbs_sampler_no_update2_exp", (DL_FUNC) &_bartBMA_gibbs_sampler_no_update2_exp, 13}, + {"_bartBMA_gibbs_sampler_ITE", (DL_FUNC) &_bartBMA_gibbs_sampler_ITE, 16}, + {"_bartBMA_gibbs_sampler_ITE2", (DL_FUNC) &_bartBMA_gibbs_sampler_ITE2, 15}, + {"_bartBMA_gibbs_sampler_ITE_no_update", (DL_FUNC) &_bartBMA_gibbs_sampler_ITE_no_update, 16}, + {"_bartBMA_gibbs_sampler_ITE_no_update2", (DL_FUNC) &_bartBMA_gibbs_sampler_ITE_no_update2, 15}, + {"_bartBMA_gibbs_sampler_new_inits", (DL_FUNC) &_bartBMA_gibbs_sampler_new_inits, 16}, + {"_bartBMA_gibbs_sampler2_new_inits", (DL_FUNC) &_bartBMA_gibbs_sampler2_new_inits, 14}, + {"_bartBMA_gibbs_sampler_no_update_new_inits", (DL_FUNC) &_bartBMA_gibbs_sampler_no_update_new_inits, 16}, + {"_bartBMA_gibbs_sampler_no_update2_new_inits", (DL_FUNC) &_bartBMA_gibbs_sampler_no_update2_new_inits, 14}, + {"_bartBMA_gibbs_sampler_exp_new_inits", (DL_FUNC) &_bartBMA_gibbs_sampler_exp_new_inits, 16}, + {"_bartBMA_gibbs_sampler2_exp_new_inits", (DL_FUNC) &_bartBMA_gibbs_sampler2_exp_new_inits, 14}, + {"_bartBMA_gibbs_sampler_no_update_exp_new_inits", (DL_FUNC) &_bartBMA_gibbs_sampler_no_update_exp_new_inits, 16}, + {"_bartBMA_gibbs_sampler_no_update2_exp_new_inits", (DL_FUNC) &_bartBMA_gibbs_sampler_no_update2_exp_new_inits, 14}, + {"_bartBMA_gibbs_sampler_ITE_new_inits", (DL_FUNC) &_bartBMA_gibbs_sampler_ITE_new_inits, 17}, + {"_bartBMA_gibbs_sampler_ITE2_new_inits", (DL_FUNC) &_bartBMA_gibbs_sampler_ITE2_new_inits, 16}, + {"_bartBMA_gibbs_sampler_ITE_no_update_new_inits", (DL_FUNC) &_bartBMA_gibbs_sampler_ITE_no_update_new_inits, 17}, + {"_bartBMA_gibbs_sampler_ITE_no_update2_new_inits", (DL_FUNC) &_bartBMA_gibbs_sampler_ITE_no_update2_new_inits, 16}, + {NULL, NULL, 0} +}; + +RcppExport void R_init_bartBMA(DllInfo *dll) { + R_registerRoutines(dll, NULL, CallEntries, NULL, NULL); + R_useDynamicSymbols(dll, FALSE); +} diff --git a/src/bbma_preds_samples.cpp b/src/bbma_preds_samples.cpp new file mode 100644 index 0000000..e69de29