Initial code of matlab generation scripts and cdd-crossvalidation
[PsN.git] / bin / bootstrap
blobe8bd5e74f35afcc81e01a529b99e4091ee271eb5
1 #!/usr/local/bin/perl
3 use FindBin qw($Bin);
4 use lib "$Bin/../lib";
6 # Don't edit the line below, it must look exactly like this.
7 # Everything above this line will be replaced #
9 use PsN;
10 use model;
11 use tool::bootstrap;
12 use strict;
13 use debug;
14 use ui;
15 use Getopt::Long;
16 use common_options;
17 use Data::Dumper;
19 my $cmd_line = $0 . " " . join( " ", @ARGV );
21 ## Configure the command line parsing
22 Getopt::Long::config("auto_abbrev");
24 ## Declare the options
25 my %options;
27 my %required_options = ();
28 my %optional_options = ("bca"=>'',
29 "samples:i"=>'',
30 "sample_size:i"=>'',
31 "stratify_on:s"=>'',
32 "mplots"=>'',
33 "skip_minimization_terminated"=>'',
34 "skip_covariance_step_terminated"=>'',
35 "skip_with_covstep_warnings"=>'',
36 "skip_estimate_near_boundary"=>'' );
38 my $res = GetOptions( \%options,
39 @common_options::get_opt_strings,
40 keys(%required_options),
41 keys(%optional_options) );
43 exit unless $res;
45 common_options::set_globals( \%options );
47 my %help_text;
48 $help_text{Pre_help_message} = <<'EOF';
49 <h3 class="heading1">bootstrap</h3>
51 Perl script for nonparametric boostrap of NONMEM runs.
53 <h3 class="heading1">Usage:</h3>
54 EOF
56 $help_text{Description} = <<'EOF';
57 <h3 class="heading1">Description:</h3>
59 The Bootstrap can be used to calculate bias, standard errors and
60 confidence intervals. It does so by resampling with replacement
61 from the data, see Efron B, An Introduction to the Bootstrap,
62 Chap. & Hall, London UK, 1993. To compute standard errors for
63 all parameters of a model using the non-parametric bootstrap
64 implemented here, roughly 200 model fits are necessary. To assess
65 95% confidence intervals approximatly 2000 runs will suffice.
66 EOF
67 $help_text{Examples} = <<'EOF';
68 <h3 class="heading1">Example:</h3>
70 <p class="style2">bootstrap -samples=200 run89.mod</p>
72 This will run a non-parametric bootstrap of 200 samples and give
73 you good estimates of the standard errors of the parameter
74 estimates. You may get some estimates for the confidence
75 intervals too, but they will generally not be of high quality.
78 <p class="style2">bootstrap -samples=2000 -bca run89.mod</p>
80 This will run a non-parametric bootstrap using the BCa technique
81 (See An introduction to the bootstrap, Efron, 1993). The BCa is
82 intended for caclulation of second-order correct confidence
83 intervals.
86 <p class="style2">bootstrap -samples=2000 -bca -stratify_on=5 run89.mod </p>
88 This is the same BCa approach as above but with stratification
89 on the factors of column five.
90 EOF
92 $help_text{Options} = <<'EOF';
93 <h3 class="heading1">Options:</h3>
95 The options are given here in their long form. Any option may be
96 abbreviated to any nonconflicting prefix. The -threads option
97 may be abbreviated to <span class="style2">-t</span> (or even <span class="style2">-thr</span>) but -<span class="style2">debug</span> may not be
98 abbreviated to <span class="style2">-d</span> because it conflicts with <span class="style2">-debug_packages</span> and
99 <span class="style2">-debug_subroutines</span>.
101 The following options are valid:
104 $help_text{-samples} = <<'EOF';
105 <p class="style2">-samples</p>
107 The number of bootstrap samples.
110 $help_text{-sample_size} = <<'EOF';
111 <p class="style2">-sample_size</p>
113 The number of subjects in each bootstrap data set. The default
114 value is set to the number of individuals in the original data
115 set.
118 $help_text{-bca} = <<'EOF';
119 <p class="style2">-bca</p>
121 Using the <span class="style2">-bca </span>option, the bootstrap
122 utility will calculate the confidence intervals through the BCa
123 method. The default approach however, is not to use the BCa (see
124 Efron B, An introduction to the Bootstrap, 1993). The BCa is
125 intended for calculation of second-order correct confidence
126 intervals.
129 $help_text{-stratify_on} = <<'EOF';
130 <p class="style2">-stratify_on=integer|string</p>
132 It may be necessary to use stratification in the resampling
133 procedure. For example, if the original data consists of two
134 groups of patients - say 10 patients with full pharmacokinetic
135 profiles and 90 patients with sparse steady state concentration
136 measurements - it may be wise to restrict the resampling
137 procedure to resample within the two groups, producing bootstrap
138 data sets that all contain 10 rich + 90 sparse data patients but
139 with different compositions. The default is not to use
140 stratification. Set <span class="style2">-stratify_on</span> to
141 the column that defines the two groups. If a string is used with
142 stratify_on the header in the datafile is used to map the string
143 to a column number.
146 $help_text{-mplots} = <<'EOF';
147 <p class="style2">-mplots</p>
149 Generate matlab scripts for making various plots of the result.
151 $help_text{Post_help_message} = <<'EOF';
152 Also see 'execute -help' for a description of common options.
155 common_options::online_help('bootstrap',\%options, \%help_text, \%required_options, \%optional_options);
158 ## Check that we do have a model file
159 if ( scalar(@ARGV) < 1 ) {
160 print "A model file must be specified. Use 'bootstrap -h' for help.\n";
161 exit;
164 if( scalar(@ARGV) > 1 ){
165 print "Bootstrap can only handle one modelfile. Use 'boostrap -h' for help.\n";
166 exit;
169 'ui' -> category( 'bootstrap' );
170 'ui' -> silent(1) if $options{'silent'};
172 debug -> level( $options{'debug'} );
173 debug -> package( $options{'debug_package'} );
174 debug -> subroutine( $options{'debug_subroutine'} );
175 debug -> warn_with_trace( $options{'warn_with_trace'} );
177 my $eval_string = common_options::model_parameters(\%options);
179 my $model = model -> new ( eval( $eval_string ),
180 filename => @ARGV[0],
181 ignore_missing_output_files => 1 );
183 my $type = defined $options{'bca'} ? 'bca' : undef;
185 ## Create new Bootstrap object:
186 my $bs = tool::bootstrap ->
187 new ( eval( $common_options::parameters ),
188 models => [ $model ],
189 samples => $options{'samples'},
190 subjects => $options{'sample_size'},
191 type => $type,
192 stratify_on => $options{'stratify_on'},
193 skip_minimization_terminated => $options{'skip_minimization_terminated'},
194 skip_covariance_step_terminated => $options{'skip_covariance_step_terminated'},
195 skip_with_covstep_warnings => $options{'skip_with_covstep_warnings'},
196 skip_estimate_near_boundary => $options{'skip_estimate_near_boundary'} );
198 open(CMD, ">", $bs -> directory . "/command.txt");
199 print CMD $cmd_line, "\n";
200 close(CMD);
202 if ( $options{'summarize'} ) {
203 $bs -> prepare_results();
204 $bs -> print_results();
205 # $bs -> print_summary();
206 } else {
207 ui -> print(category => 'bootstrap',
208 message => "Starting execution of the bootstrap sets.");
209 $bs -> run;
210 $bs -> prepare_results();
211 $bs -> print_results();
212 # $bs -> print_summary();
215 if( $options{'mplots'} ){
216 $bs -> create_matlab_scripts();
219 ui -> print( category => 'bootstrap',
220 message => "Done");