-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlm.d
285 lines (244 loc) · 6.24 KB
/
lm.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
module betterr.lm;
import std.array, std.conv, std.exception, std.stdio;
import betterr.r;
import betterr.dataframe, betterr.list, betterr.matrix, betterr.rdata, betterr.vector;
/* This is a configuration. Additional options will be added in the
* future. The data is not stored in here.
* We need to handle the many ways data can be sent to lm.
*
* We use the Subset struct mainly so it can take a single integer or
* an array of integers as the argument. */
struct LMConfig {
string lhs;
string[] rhs;
bool intercept = true;
Subset subset;
struct Subset {
long[] obs;
alias obs this;
this(long ii) {
obs = [ii];
}
this(long[] ii) {
obs = ii;
}
/* Following standard D practice, this is exclusive of i2. */
this(long i1, long i2) {
foreach(ii; i1..i2) {
obs ~= ii;
}
}
void opAssign(long a) {
obs = [a];
}
void opOpAssign(string op)(long ii) {
if (op == "~") {
obs ~= ii;
}
}
void opOpAssign(string op)(long[] ii) {
if (op == "~") {
obs ~= ii;
}
}
void opOpAssign(string op)(long i1, long i2) {
if (op == "~") {
foreach(ii; i1..i2) {
obs ~= ii;
}
}
}
IntVector vec() {
return IntVector(obs);
}
}
string rhsFormula() {
if (rhs.length > 0) {
import std.string: join;
return rhs.join("+");
} else {
return "";
}
}
}
LMFit lm(T1, T2)(T1 y, T2 x) {
return lm(y.name, x.name, "", "", true);
}
LMFit lm(T1, T2)(T1 y, T2 x, LMConfig conf) {
if ( (conf.subset !is null) & (conf.subset.length > 0) ) {
auto s = conf.subset.vec;
LMFit result = lm(y.name, x.name, s.name, "", conf.intercept);
s.free();
return result;
} else {
return lm(y.name, x.name, "", "", conf.intercept);
}
}
LMFit lm(DataFrame df, LMConfig conf) {
if (conf.subset.length > 0) {
return lm(conf.lhs, conf.rhsFormula, conf.subset.vec.name, df.name, conf.intercept);
} else {
return lm(conf.lhs, conf.rhsFormula, "", df.name, conf.intercept);
}
}
/* These regressions should work down to this. Arguments are variable
* names as they appear inside R. */
LMFit lm(string y, string x, string subset, string data, bool intercept=true) {
string optional;
if (subset.length > 0) {
optional ~= ", subset=" ~ subset;
}
if (data.length > 0) {
optional ~= ", data=" ~ data;
}
string rhs = x;
if (!intercept) {
rhs ~= "-1";
}
string cmd = `lm(` ~ y ~ ` ~ ` ~ rhs ~ optional ~ `)`;
LMFit result;
result.fit = List(cmd);
result.summary = List(`summary(` ~ result.fit.name ~ `)`);
result.intercept = intercept;
return result;
}
/* Restructure a bit. Pass LMConfig struct as an optional argument if
* you want to set arguments. The current approach is clumsy. */
struct LMFit {
List fit;
List summary;
bool intercept;
double pred(double x) {
Vector b = beta();
if (intercept) {
enforce(b.length == 2, "Wrong number of predictors in call to pred");
return b[0] + b[1]*x;
} else {
enforce(b.length == 1, "Wrong number of predictors in call to pred");
return b[0]*x;
}
}
double pred(Vector x) {
Vector b = beta();
double result = 0.0;
if (intercept) {
result += b[0];
foreach(ii; 1..b.length) {
result += b[ii]*x[ii-1];
}
return result;
} else {
foreach(ii; 0..b.length) {
result += b[ii]*x[ii];
}
return result;
}
}
void print(string msg="") {
if (msg.length > 0) {
writeln(msg ~ ":");
}
printR(fit.x);
}
Vector beta() {
return Vector(fit["coefficients"]);
}
Matrix coefficients() {
return Matrix(summary.name ~ "[['coefficients']]");
}
Vector residuals() {
return Vector(fit["residuals"]);
}
Vector fittedValues() {
return Vector(fit["fitted.values"]);
}
int dfResidual() {
return fit["df.residual"].as!int;
}
List model() {
return List(fit.name ~ "['model']");
}
double sigma() {
return summary["sigma"].as!double;
}
double rsq() {
return summary["r.squared"].as!double;
}
double adjrsq() {
return summary["adj.r.squared"].as!double;
}
double fstat() {
return summary["fstatistic"].as!double;
}
List unscaledCov() {
return List(summary.name ~ "['cov.unscaled']");
}
Matrix nwCov() {
return Matrix("sandwich::NeweyWest(" ~ fit.name ~ ")");
}
Vector nwStdErrors() {
return Vector("sqrt(diag(sandwich::NeweyWest(" ~ fit.name ~ ")))");
}
Matrix nwCoefficients() {
evalRQ([
`tmp <- ` ~ summary.name ~ `[["coefficients"]]`,
`tmp[,2] <- sqrt(diag(sandwich::NeweyWest(` ~ fit.name ~ `)))`,
`tmp[,3] <- tmp[,1]/tmp[,2]`,
`tmp2 <- tmp[,-4]`]);
return Matrix("tmp2");
}
Matrix whiteCov() {
return Matrix("sandwich::vcovHC(" ~ fit.name ~ ")");
}
Matrix whiteCoefficients() {
evalRQ([
`tmp <- ` ~ summary.name ~ `[["coefficients"]]`,
`tmp[,2] <- sqrt(diag(sandwich::vcovHC(` ~ fit.name ~ `)))`,
`tmp[,3] <- tmp[,1]/tmp[,2]`,
`tmp2 <- tmp[,-4]`]);
return Matrix("tmp2");
}
}
extern(C) {
void dqrls_(double *x, int *n, int *p, double *y, int *ny,
double *tol, double *b, double *rsd, double *qty, int *k,
int *jpvt, double *qraux, double *work);
}
struct RegOutput {
double[] coef;
double[] residuals;
double[] effects;
int rank;
}
/* This is the FORTRAN function that R uses for OLS, and we can call
* it directly without creating R objects.
*
* Note that x is overwritten, so we make a copy. If you don't care
* about that (as might be the case in a simulation) and you want max
* performance, comment out the creation of xreg and send x.ptr
* to dqrls_ instead.
*
* In a simulation setting, you can avoid new allocations by
* allocating coef, residuals, effects, pivot, qraux, and work once
* and then sending their pointers as an argument to the function. */
RegOutput dqrls(Vector y, Matrix x, double tol=1e-07) {
RegOutput result;
int n = x.rows.to!int;
int p = x.cols.to!int;
int ny = 1;
result.coef = new double[p];
result.residuals = new double[n];
result.effects = new double[n];
double[] xreg;
xreg.reserve(n*p);
foreach(val; x.ptr[0..n*p]) {
xreg ~= val;
}
auto pivot = new int[p];
auto qraux = new double[p];
auto work = new double[2*p];
dqrls_(xreg.ptr, &n, &p, y.ptr, &ny, &tol, result.coef.ptr,
result.residuals.ptr, result.effects.ptr, &(result.rank),
pivot.ptr, qraux.ptr, work.ptr);
return result;
}