/* Jackknife/Bootstrap for the slope of a regression */ #include #include #include /* Prototypes for functions in statlib.c : Compile by e.g. gcc -o ProgName Progname.c statlib.c with ProgName.c, statlib.c, and statlib.h in the default directory. */ #include "statlib.h" #define MAXN 50 #define NBOOT 10000 /* Data for single covariate */ int nn=10; double xx[MAXN]; double xbar,xsse; double tcrit; /* 95% critical value for t(nn-2) */ /* Constants for regressions with normal errors */ double nmu=3.0, nbeta=3.0, nsigma=20.0; /* Constants for regressions with exponential errors */ double exmu=3.0, exbeta=3.0, exsigma=20.0; /* Number of bootstrap replications and default starting RN seed */ /* Nboot is also the number of simulated CIs for coverage probabilities */ int nboot=NBOOT; unsigned long startseed=123456; /* Prototypes */ void show_ols_analysis(const char *name, const double *yy, const double *xx, int nn, double truemu, double truebeta, double truesigma); void find_normal_ols_ci_coverage(int nsims, double beta, double mu, double sigma, int nn); void find_exponential_ols_ci_coverage(int nsims, double beta, double mu, double sigma, int nn); void do_jackknife(const char *name, const double *yy, const double *xx, int nn); void find_normal_jackknife_ci_coverage(int nsims, double beta, double mu, double sigma, int nn); void find_exponential_jackknife_ci_coverage(int nsims, double beta, double mu, double sigma, int nn); void do_bootstrap_record(const char *name, const double *yy, const double *xx, int nn); void find_normal_bootstrap_record_ci_coverage(int nsims, double beta, double mu, double sigma, int nn); void find_exponential_bootstrap_record_ci_coverage(int nsims, double beta, double mu, double sigma, int nn); void do_bootstrap_residual(const char *name, const double *yy, const double *xx, int nn); void find_normal_bootstrap_residual_ci_coverage(int nsims, double beta, double mu, double sigma, int nn); void find_exponential_bootstrap_residual_ci_coverage(int nsims, double beta, double mu, double sigma, int nn); /* For a final table of confidence intervals */ /* and a final table of coverage probabilities */ void load_ci_values(const char *name, double med, double cilow, double cihigh); void show_ci_table(void); void load_ci_coverage_values(const char *name, int ncover); void show_ci_coverage_table(void); int main(int argc, char **argv) { int i; double xsum, ysum,ybar, yy[MAXN]; unsigned long wseed; /* Helpful for segmentation faults and other fatal errors */ /* This function is in statlib.c */ set_newsignals(); printf ( "\nCompare Jackknife/Bootstrap methods for the slope of a regression\n"); /* Initialize the randum-number generator: */ /* Allow a new seed (or 0) to be entered on the command line. */ /* Setrand(0) initializes the seed from the system clock. */ /* If nothing is entered on the command line, the default value */ /* of startseed is used. */ if (argc>=2) startseed=atol(argv[1]); wseed=setrand(startseed); printf ("Starting random-number seed: %lu\n",wseed); for (i=0; i0 && (i%15)==0) printf("\n"); printf (" %g",xx[i]); } printf("\n"); /* 95% critical value for t(nn-2): crit_tdist() is in statlib.c */ /* This value is needed for Student-t confidence intervals */ tcrit=crit_tdist(nn-2,0.05); printf ( "\nREGRESSION WITH NORMAL ERRORS (n=%d, mu=%g, beta=%g, sigma=%g)\n", nn, nmu, nbeta, nsigma); for (i=0; i0 && (i%4)==0) printf("\n"); printf (" (%g, %.2f)",xx[i],yy[i]); } printf("\n"); show_ols_analysis("Normal", yy,xx,nn, nmu,nbeta,nsigma); find_normal_ols_ci_coverage(nboot, nbeta,nmu,nsigma, nn); printf ("\nJACKKNIFE ESTIMATES OF BETA FOR NORMAL DATA:\n"); do_jackknife("Normal", yy,xx,nn); find_normal_jackknife_ci_coverage(nboot, nbeta,nmu,nsigma, nn); printf ("\nBOOKSTRAP RECORD ESTIMATES OF BETA FOR NORMAL DATA:\n"); do_bootstrap_record("Normal", yy,xx,nn); find_normal_bootstrap_record_ci_coverage(nboot, nbeta,nmu,nsigma, nn); printf ("\nBOOKSTRAP RESIDUAL ESTIMATES OF BETA FOR NORMAL DATA:\n"); do_bootstrap_residual("Normal", yy,xx,nn); find_normal_bootstrap_residual_ci_coverage(nboot, nbeta,nmu,nsigma, nn); printf ( "\nREGRESSION WITH EXPONENTIAL ERRORS (n=%d, mu=%g, beta=%g, sigma=%g)\n", nn, exmu, exbeta, exsigma); for (i=0; i0 && (i%4)==0) printf("\n"); printf (" (%g, %.2f)",xx[i],yy[i]); } show_ols_analysis("Exponential", yy,xx,nn, nmu,nbeta,nsigma); find_exponential_ols_ci_coverage(nboot, nbeta,nmu,nsigma, nn); printf ("\nJACKKNIFE ESTIMATES OF BETA FOR EXPONENTIAL DATA:\n"); do_jackknife("Exponential", yy,xx,nn); find_exponential_jackknife_ci_coverage(nboot, exbeta,exmu,exsigma, nn); printf ("\nBOOKSTRAP RECORD ESTIMATES OF BETA FOR EXPONENTIAL DATA:\n"); do_bootstrap_record("Exponential", yy,xx,nn); find_exponential_bootstrap_record_ci_coverage(nboot, exbeta,exmu,exsigma, nn); printf ("\nBOOKSTRAP RESIDUAL ESTIMATES OF BETA FOR EXPONENTIAL DATA:\n"); do_bootstrap_residual("Exponential", yy,xx,nn); find_exponential_bootstrap_residual_ci_coverage(nboot, exbeta,exmu,exsigma, nn); show_ci_table(); show_ci_coverage_table(); show_randnums_used(); printf ("Initial random-number seed: %lu\n",wseed); return 0; } double get_mean(const double *xx, int nn) { int i; double xsum; for (xsum=0.0, i=0; i0) klow=kk-1; /* k-th from the bottom */ khigh=nboot-1-klow; /* k-th from the top */ printf ( " %.4f (%.4f, %.4f) Observed beta=%.4f\n", bootmed, betastar[klow], betastar[khigh], obs_beta); sprintf(nbuff, "%s, Bootstrap %s", name, boottype); /* Make a non-local copy of the string */ rname=strdup(nbuff); load_ci_values(rname, bootmed, betastar[klow], betastar[khigh]); } void do_bootstrap_record(const char *name, const double *yy, const double *xx, int nn) { int i,ns; double obs_beta, betastar[NBOOT]; obs_beta=get_betahat(yy,xx,nn,NULL); printf("Filling the bootstrap ``beta(star)'' array:\n"); for (ns=0; ns0 && (i%5)==0) printf("\n"); printf (" %.3f",resid[i]); } printf("\n"); printf("Filling the bootstrap ``beta(star)'' array:\n"); for (ns=0; ns0) klow=kk-1; /* k-th from the bottom */ khigh=nboot-1-klow; /* k-th from the top */ if (betastar[klow]mxwid) mxwid=len; for (i=0; imxwid) mxwid=len; for (i=0; i