43 #include "EST_Wagon.h"
44 #include "EST_multistats.h"
45 #include "EST_cmd_line.h"
48 static int ols_main(
int argc,
char **argv);
79 int main(
int argc,
char **argv)
81 return ols_main(argc,argv);
84 static int ols_main(
int argc,
char **argv)
98 "Summary: Linear Regression by ordinary least squares (defaults in {})\n"+
99 "-desc <ifile> Field description file\n"+
100 "-data <ifile> Datafile, one vector per line\n"+
101 "-test <ifile> Datafile, for testing\n"+
102 "-robust Robust, may take longer\n"+
103 "-stepwise Order the features by contribution,\n"+
104 " implies robust.\n"+
105 "-swlimit <float> {0.0}\n"+
106 " Percentage necessary improvement for stepwise\n"+
107 "-quiet No summary\n"+
109 "-output <ofile> Output file for coefficients\n"+
110 "-ignore <string> Filename or bracket list of fields to ignore\n",
115 ofile = al.
val(
"-output");
117 ofile = al.
val(
"-o");
125 ignores = read_from_string(ig);
127 ignores = vload(ig,1);
133 cerr <<
"ols: no description file specified\n";
138 dataset.load_description(al.
val(
"-desc"),ignores);
139 dataset.ignore_non_numbers();
143 cerr <<
"ols: no data file specified\n";
147 wgn_load_dataset(dataset,al.
val(
"-data"));
150 test_dataset.load_description(al.
val(
"-desc"),ignores);
151 test_dataset.ignore_non_numbers();
152 wgn_load_dataset(test_dataset,al.
val(
"-test"));
153 load_ols_data(Xtest,Ytest,test_dataset);
157 load_ols_data(Xtest,Ytest,dataset);
159 load_ols_data(X,Y,dataset);
164 float swlimit = al.
fval(
"-swlimit");
168 names.
append(
"Intercept");
169 for (i=1; i < dataset.width(); i++)
170 names.
append(dataset.feat_name(i));
174 for (i=1; i<included.
length(); i++)
176 if (dataset.ignore(i) == TRUE)
182 if (!stepwise_ols(X,Y,names,swlimit,coeffs,Xtest,Ytest,included))
184 cerr <<
"OLS: failed stepwise ols" << endl;
188 else if (al.
present(
"-robust"))
195 for (i=1; i<included.
length(); i++)
197 if (dataset.ignore(i) == TRUE)
203 if (!robust_ols(X,Y,included,coeffs))
205 cerr <<
"OLS: failed robust ols" << endl;
209 else if (!ols(X,Y,coeffs))
211 cerr <<
"OLS: failed no pseudo_inverse" << endl;
215 if (coeffs.
save(ofile) != write_ok)
217 cerr <<
"OLS: failed to save coefficients in \"" << ofile <<
"\""
227 ols_apply(Xtest,coeffs,pred);
228 ols_test(Ytest,pred,cor,rmse);
230 printf(
";; RMSE %f Correlation is %f\n",rmse,cor);
241 X.
resize(d.length(),d.width());
244 for (n=0,p=d.head(); p != 0; p=p->next(),n++)
248 for (m=1; m < d.width(); m++)