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);
50 int main(
int argc,
char **argv)
52 return ols_main(argc,argv);
55 static int ols_main(
int argc,
char **argv)
69 "Summary: Linear Regression by ordinary least squares (defaults in {})\n"+
70 "-desc <ifile> Field description file\n"+
71 "-data <ifile> Datafile, one vector per line\n"+
72 "-test <ifile> Datafile, for testing\n"+
73 "-robust Robust, may take longer\n"+
74 "-stepwise Order the features by contribution,\n"+
76 "-swlimit <float> {0.0}\n"+
77 " Percentage necessary improvement for stepwise\n"+
78 "-quiet No summary\n"+
80 "-output <ofile> Output file for coefficients\n"+
81 "-ignore <string> Filename or bracket list of fields to ignore\n",
86 ofile = al.
val(
"-output");
96 ignores = read_from_string(ig);
98 ignores = vload(ig,1);
104 cerr <<
"ols: no description file specified\n";
109 dataset.load_description(al.
val(
"-desc"),ignores);
110 dataset.ignore_non_numbers();
114 cerr <<
"ols: no data file specified\n";
118 wgn_load_dataset(dataset,al.
val(
"-data"));
121 test_dataset.load_description(al.
val(
"-desc"),ignores);
122 test_dataset.ignore_non_numbers();
123 wgn_load_dataset(test_dataset,al.
val(
"-test"));
124 load_ols_data(Xtest,Ytest,test_dataset);
128 load_ols_data(Xtest,Ytest,dataset);
130 load_ols_data(X,Y,dataset);
135 float swlimit = al.
fval(
"-swlimit");
139 names.
append(
"Intercept");
140 for (i=1; i < dataset.width(); i++)
141 names.
append(dataset.feat_name(i));
145 for (i=1; i<included.
length(); i++)
147 if (dataset.ignore(i) == TRUE)
153 if (!stepwise_ols(X,Y,names,swlimit,coeffs,Xtest,Ytest,included))
155 cerr <<
"OLS: failed stepwise ols" << endl;
159 else if (al.
present(
"-robust"))
166 for (i=1; i<included.
length(); i++)
168 if (dataset.ignore(i) == TRUE)
174 if (!robust_ols(X,Y,included,coeffs))
176 cerr <<
"OLS: failed robust ols" << endl;
180 else if (!ols(X,Y,coeffs))
182 cerr <<
"OLS: failed no pseudo_inverse" << endl;
186 if (coeffs.
save(ofile) != write_ok)
188 cerr <<
"OLS: failed to save coefficients in \"" << ofile <<
"\""
198 ols_apply(Xtest,coeffs,pred);
199 ols_test(Ytest,pred,cor,rmse);
201 printf(
";; RMSE %f Correlation is %f\n",rmse,cor);
212 X.
resize(d.length(),d.width());
215 for (n=0,p=d.head(); p != 0; p=p->next(),n++)
219 for (m=1; m < d.width(); m++)
INLINE const T & a_no_check(int row, int col) const
const access with no bounds check, care recommend
int num_columns() const
return number of columns
float fval(const EST_String &rkey, int m=1) const
EST_write_status save(const EST_String &filename, const EST_String &type=EST_FMatrix::default_file_type)
Save in file (ascii or binary)
INLINE int length() const
number of items in vector.
const int present(const K &rkey) const
Returns true if key is present.
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
INLINE const T & a_no_check(int n) const
read-only const access operator: without bounds checking
void append(const T &item)
add item onto end of list
void resize(int rows, int cols, int set=1)
resize matrix
void resize(int n, int set=1)
resize vector