Esempio n. 1
0
// [[Rcpp::export]]
NumericVector attribs() {
  NumericVector out = NumericVector::create(1, 2, 3);

  out.names() = CharacterVector::create("a", "b", "c");
  out.attr("my-attr") = "my-value";
  out.attr("class") = "my-class";

  return out;
}
Esempio n. 2
0
// [[Rcpp::export]]
std::unordered_map<std::string, Node*> internal_huffman_map(NumericVector probabilities) {
  std::unordered_map<std::string, Node*> map(probabilities.size());
  CharacterVector items = probabilities.names();
  visitNodes(internal_huffman_encode(probabilities), "", [&items, &map] (Node *n, string s) {
    if (n->index >= 0) {
      map[(const string) items[n->index]] = n;
    }
  });
  return map;
}
Esempio n. 3
0
// [[Rcpp::export]]
Environment huffman_encode(NumericVector probabilities) {
  CharacterVector items = probabilities.names();
  Environment env = Environment::empty_env().new_child(true);
  
  visitNodes(internal_huffman_encode(probabilities), "", [&items, &env] (Node *n, string s) {
    if (n->index >= 0) {
      env.assign((const string) items[n->index], s);
    }
    delete n;
  });
  return env;
}
Esempio n. 4
0
// [[Rcpp::export]]
NumericVector mse_compare_cpp(double b, colvec p, mat Ve, 
              mat Vz, int n, int n_reps){
//Function to run n_reps of the simulation study and calculate the MSE
//of various estimators

  colvec ols(n_reps);
  colvec tsls(n_reps);
  colvec fmsc(n_reps);
  colvec DHW90(n_reps);
  colvec DHW95(n_reps);
  colvec AVG(n_reps);

  for(int i = 0; i < n_reps; i++){
    
    dgp_OLS_IV sim_data(b, p, Ve, Vz, n);
    fmsc_OLS_IV sim_results(sim_data.x, sim_data.y, sim_data.z);

    ols(i) = sim_results.b_ols();
    tsls(i) = sim_results.b_tsls();
    fmsc(i) = sim_results.b_fmsc();
    DHW90(i) = sim_results.b_DHW(0.1);
    DHW95(i) = sim_results.b_DHW(0.05);
    AVG(i) = sim_results.b_AVG();
    
  }
  
  double const trim_frac = 0; //Change this if you want trimmed MSE
  
  double MSE_ols = MSE_trim(ols, b, trim_frac);
  double MSE_tsls = MSE_trim(tsls, b, trim_frac);
  double MSE_fmsc = MSE_trim(fmsc, b, trim_frac);
  double MSE_DHW90 = MSE_trim(DHW90, b, trim_frac);
  double MSE_DHW95 = MSE_trim(DHW95, b, trim_frac);
  double MSE_star = MSE_trim(AVG, b, trim_frac);
  
  //Create and return vector of results
  NumericVector out = NumericVector::create(MSE_ols, MSE_tsls, MSE_fmsc, 
                        MSE_star, MSE_DHW90, MSE_DHW95);
  out.names() = CharacterVector::create("OLS", "TSLS", "FMSC", 
                        "AVG", "DHW90", "DHW95");
  return(out);  

  
}
Esempio n. 5
0
// [[Rcpp::export]]
List CIs_compare_default_cpp(double p , double r, int n, 
                                        int n_reps){
//Function to test the confidence interval code with the same parameter
//values as in mse_compare_default_cpp
//In this specification there are three instruments (z1, z2, z3)
//which ensures that the finite-sample RMSE of the 2SLS estimator exists.
//Further, the design sets
//  var(x) = 1
//  corr(e,v) = r
//  cor(x, z1 + z2 + z3) = p
  double b = 0.5;
  colvec p_vec = p * ones(3);
  mat Vz = eye(3, 3) / 3;
  
  mat Ve;
  Ve << 1 << r * sqrt(1 - pow(p,2)) << endr
     << r * sqrt(1 - pow(p,2)) << 1 - pow(p, 2) << endr;

//Default to nominal 90% CIs
  double nominal_coverage = 0.9;
  double alpha_1step = 1 - nominal_coverage;
//Same level for each interval in the 2-step procedures
  double alpha_2step = alpha_1step / 2; 
//Number of simulations for sim-based CIs
  int n_CI_sims = 1000;
//Grid for tau
  int n_tau_grid = 100;

  mat OLS(n_reps, 2, fill::zeros);
  mat TSLS(n_reps, 2, fill::zeros);
  mat FMSC_naive(n_reps, 2, fill::zeros);
  mat FMSC_1step(n_reps, 2, fill::zeros);
  mat FMSC_correct(n_reps, 2, fill::zeros);
  mat AVG_1step(n_reps, 2, fill::zeros);
  mat AVG_correct(n_reps, 2, fill::zeros);
  
  for(int i = 0; i < n_reps; i++){
    
    //Simulate data and calculate estimators
    dgp_OLS_IV data(b, p_vec, Ve, Vz, n);
    fmsc_OLS_IV results(data.x, data.y, data.z);
    
    //CIs that do not require draw_CI_sims()
    OLS.row(i) = results.CI_ols(alpha_1step);
    TSLS.row(i) = results.CI_tsls(alpha_1step);
    FMSC_naive.row(i) = results.CI_fmsc_naive(alpha_1step);
    
    //Initialization for CIs constructed using simulations
    results.draw_CI_sims(n_CI_sims);
    
    //One-step simulation-based CIs
    FMSC_1step.row(i) = results.CI_fmsc_1step(alpha_1step);
    AVG_1step.row(i) = results.CI_AVG_1step(alpha_1step);
    
    //Two-step simulation-based CIs
    FMSC_correct.row(i) = results.CI_fmsc_correct(alpha_2step, 
                                                  alpha_2step,
                                                  n_tau_grid);
    AVG_correct.row(i) = results.CI_AVG_correct(alpha_2step, 
                                                alpha_2step,
                                                n_tau_grid);
    
  }

  CharacterVector col_names = CharacterVector::create("OLS",
                                                      "TSLS",
                                                      "FMSC_naive",
                                                      "FMSC_1step",
                                                      "FMSC_correct", 
                                                      "AVG_1step",  
                                                      "AVG_correct"); 
  
  NumericVector cover = NumericVector::create(coverage_prob(OLS, b),
                                              coverage_prob(TSLS, b),
                                              coverage_prob(FMSC_naive, b),
                                              coverage_prob(FMSC_1step, b),
                                              coverage_prob(FMSC_correct, b),
                                              coverage_prob(AVG_1step, b),
                                              coverage_prob(AVG_correct, b));
  cover.names() = col_names;
  
  NumericVector width = NumericVector::create(median_width(OLS),
                                              median_width(TSLS),
                                              median_width(FMSC_naive),
                                              median_width(FMSC_1step),
                                              median_width(FMSC_correct),
                                              median_width(AVG_1step),
                                              median_width(AVG_correct));
  width.names() = col_names;
  
  return List::create(Named("coverage.prob") = cover,
                      Named("median.width") = width);
}