| 3821 |
mrmanese |
1 |
#include "sqlite_dataframe.h"
|
|
|
2 |
#include <math.h>
|
|
|
3 |
|
|
|
4 |
#define TOL 1e-12
|
|
|
5 |
#define ZERO 0.000000000000000000
|
|
|
6 |
#define EQUAL_ZERO(x) (abs((x)) < TOL)
|
|
|
7 |
|
|
|
8 |
void F77_NAME(includ)(int *, int *, double *, double *, double *,
|
|
|
9 |
double *, double *, double *, double *, int *);
|
|
|
10 |
|
|
|
11 |
/* EQUAL_ZERO gives error */
|
|
|
12 |
static void __include(int np, int nrbar, double *D, double *rbar, double *thetab,
|
|
|
13 |
double *sserr, double *xrow, double y, double weight) {
|
|
|
14 |
int nextr = 0, i, k;
|
|
|
15 |
long double xi, xk, di, wxi, dpi, cbar, sbar;
|
|
|
16 |
for (i = 0; i < np; i++) {
|
|
|
17 |
if (weight == ZERO) return;
|
|
|
18 |
xi = xrow[i];
|
|
|
19 |
if (xi == ZERO) { nextr += np - i; continue; }
|
|
|
20 |
di = D[i];
|
|
|
21 |
wxi = weight * xi;
|
|
|
22 |
dpi = di + wxi * wxi;
|
|
|
23 |
cbar = di / dpi;
|
|
|
24 |
sbar = wxi / dpi;
|
|
|
25 |
weight *= cbar;
|
|
|
26 |
D[i] = dpi;
|
|
|
27 |
|
|
|
28 |
if (i < np - 1) {
|
|
|
29 |
for (k = i+1; k < np; k++) {
|
|
|
30 |
xk = xrow[k];
|
|
|
31 |
xrow[k] = xk - xi * rbar[nextr];
|
|
|
32 |
rbar[nextr] = cbar * rbar[nextr] + sbar * xk;
|
|
|
33 |
nextr++;
|
|
|
34 |
}
|
|
|
35 |
xk = y;
|
|
|
36 |
y = xk - xi * thetab[i];
|
|
|
37 |
thetab[i] = cbar * thetab[i] + sbar * xk;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
*sserr += weight * y * y;
|
|
|
41 |
}
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
SEXP sdf_do_biglm(SEXP sdfx, SEXP svecy, SEXP sdfx_dim, SEXP intercept) {
|
|
|
46 |
SEXP sdflm, qr, D, rbar, thetab, tmp;
|
|
|
47 |
int np, nrbar, nprotect = 0, intrcpt, i, ier;
|
|
|
48 |
double *xrow, y, *ptrD, *ptrrbar, *ptrthetab, sserr, weight;
|
|
|
49 |
sqlite3_stmt *stmt1, *stmt2;
|
| 4798 |
mrmanese |
50 |
const char *inamex, *inamey, *tblnamey, *varnamey;
|
| 3821 |
mrmanese |
51 |
|
|
|
52 |
inamex = SDF_INAME(sdfx);
|
|
|
53 |
|
|
|
54 |
inamey = SDF_INAME(svecy);
|
|
|
55 |
tblnamey = SVEC_TBLNAME(svecy);
|
|
|
56 |
varnamey = SVEC_VARNAME(svecy);
|
|
|
57 |
|
|
|
58 |
if (!USE_SDF1(inamex, TRUE, TRUE)) return R_NilValue;
|
|
|
59 |
if (!USE_SDF1(inamey, TRUE, TRUE)) return R_NilValue;
|
|
|
60 |
|
|
|
61 |
/* get number of columns for sdfx */
|
|
|
62 |
np = INTEGER(sdfx_dim)[1];
|
|
|
63 |
intrcpt = (LOGICAL(intercept)[0]) ? 1 : 0;
|
|
|
64 |
np += intrcpt;
|
|
|
65 |
nrbar = (np*(np-1))/2;
|
|
|
66 |
|
|
|
67 |
/* Rprintf("np: %d, nrbar %d\n", np, nrbar); */
|
|
|
68 |
|
|
|
69 |
PROTECT(D = NEW_NUMERIC(np)); nprotect++;
|
|
|
70 |
PROTECT(rbar = NEW_NUMERIC(nrbar)); nprotect++;
|
|
|
71 |
PROTECT(thetab = NEW_NUMERIC(np)); nprotect++;
|
|
|
72 |
xrow = (double *)R_alloc(np, sizeof(double));
|
|
|
73 |
|
|
|
74 |
/* initialize bigqr values */
|
|
|
75 |
if (intrcpt) xrow[0] = 1.0;
|
|
|
76 |
sserr = 0.0;
|
|
|
77 |
ptrD = REAL(D); ptrrbar = REAL(rbar); ptrthetab = REAL(thetab);
|
|
|
78 |
for (i = 0; i < np; i++) ptrD[i] = ptrrbar[i] = ptrthetab[i] = 0.0;
|
|
|
79 |
for ( ; i < nrbar; i++) ptrrbar[i] = 0.0;
|
|
|
80 |
|
|
|
81 |
/* setup sqlite_stmt's */
|
|
|
82 |
sprintf(g_sql_buf[0], "select * from [%s].sdf_data", inamex);
|
|
|
83 |
_sqlite_error(sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt1, NULL));
|
|
|
84 |
sprintf(g_sql_buf[0], "select [%s] from [%s].[%s]", varnamey, inamey, tblnamey);
|
|
|
85 |
_sqlite_error(sqlite3_prepare(g_workspace, g_sql_buf[0], -1, &stmt2, NULL));
|
|
|
86 |
|
|
|
87 |
if (intrcpt) {
|
|
|
88 |
while (sqlite3_step(stmt1) == SQLITE_ROW) {
|
|
|
89 |
sqlite3_step(stmt2);
|
|
|
90 |
for (i = 1; i < np; i++)
|
|
|
91 |
xrow[i] = sqlite3_column_double(stmt1, i);
|
|
|
92 |
y = sqlite3_column_double(stmt2, 0);
|
|
|
93 |
weight = 1.0;
|
|
|
94 |
/*__include(np, nrbar, ptrD, ptrrbar, ptrthetab, &sserr, xrow, y, 1.0); */
|
|
|
95 |
F77_CALL(includ)(&np, &nrbar, &weight, xrow, &y, ptrD, ptrrbar, ptrthetab, &sserr, &ier);
|
|
|
96 |
}
|
|
|
97 |
} else {
|
|
|
98 |
while (sqlite3_step(stmt1) == SQLITE_ROW) {
|
|
|
99 |
sqlite3_step(stmt2);
|
|
|
100 |
for (i = 0; i < np; i++)
|
|
|
101 |
xrow[i] = sqlite3_column_double(stmt1, i+1);
|
|
|
102 |
y = sqlite3_column_double(stmt2, 0);
|
|
|
103 |
/*__include(np, nrbar, ptrD, ptrrbar, ptrthetab, &sserr, xrow, y, 1.0); */
|
|
|
104 |
weight = 1;
|
|
|
105 |
F77_CALL(includ)(&np, &nrbar, &weight, xrow, &y, ptrD, ptrrbar, ptrthetab, &sserr, &ier);
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
sqlite3_finalize(stmt1);
|
|
|
110 |
sqlite3_finalize(stmt2);
|
|
|
111 |
|
|
|
112 |
/* setup bigqr SEXP */
|
|
|
113 |
PROTECT(qr = NEW_LIST(6)); nprotect++;
|
|
|
114 |
|
|
|
115 |
/* set names */
|
|
|
116 |
PROTECT(tmp = NEW_CHARACTER(6)); nprotect++;
|
|
|
117 |
SET_STRING_ELT(tmp, 0, mkChar("D"));
|
|
|
118 |
SET_STRING_ELT(tmp, 1, mkChar("rbar"));
|
|
|
119 |
SET_STRING_ELT(tmp, 2, mkChar("thetab"));
|
|
|
120 |
SET_STRING_ELT(tmp, 3, mkChar("ss"));
|
|
|
121 |
SET_STRING_ELT(tmp, 4, mkChar("checked"));
|
|
|
122 |
SET_STRING_ELT(tmp, 5, mkChar("tol"));
|
|
|
123 |
SET_NAMES(qr, tmp);
|
|
|
124 |
|
|
|
125 |
/* set values */
|
|
|
126 |
SET_VECTOR_ELT(qr, 0, D);
|
|
|
127 |
SET_VECTOR_ELT(qr, 1, rbar);
|
|
|
128 |
SET_VECTOR_ELT(qr, 2, thetab);
|
|
|
129 |
SET_VECTOR_ELT(qr, 3, ScalarReal(sserr));
|
|
|
130 |
SET_VECTOR_ELT(qr, 4, ScalarLogical(FALSE));
|
|
|
131 |
PROTECT(tmp = NEW_NUMERIC(np)); nprotect++;
|
|
|
132 |
for (i = 0; i < np; i++) REAL(tmp)[i] = 0.0;
|
|
|
133 |
SET_VECTOR_ELT(qr, 5, tmp);
|
|
|
134 |
|
|
|
135 |
/* set class */
|
|
|
136 |
SET_CLASS(qr, mkString("bigqr"));
|
|
|
137 |
|
|
|
138 |
/* setup sdflm SEXP */
|
|
|
139 |
PROTECT(sdflm = NEW_LIST(3)); nprotect++;
|
|
|
140 |
|
|
|
141 |
PROTECT(tmp = NEW_CHARACTER(3)); nprotect++;
|
|
|
142 |
SET_STRING_ELT(tmp, 0, mkChar("qr"));
|
|
|
143 |
SET_STRING_ELT(tmp, 1, mkChar("X"));
|
|
|
144 |
SET_STRING_ELT(tmp, 2, mkChar("intercept"));
|
|
|
145 |
SET_NAMES(sdflm, tmp);
|
|
|
146 |
|
|
|
147 |
SET_VECTOR_ELT(sdflm, 0, qr);
|
|
|
148 |
SET_VECTOR_ELT(sdflm, 1, duplicate(sdfx));
|
|
|
149 |
SET_VECTOR_ELT(sdflm, 2, ScalarLogical(intrcpt));
|
|
|
150 |
|
|
|
151 |
PROTECT(tmp = NEW_CHARACTER(2)); nprotect++;
|
|
|
152 |
SET_STRING_ELT(tmp, 0, mkChar("sdflm"));
|
|
|
153 |
SET_STRING_ELT(tmp, 1, mkChar("biglm"));
|
|
|
154 |
SET_CLASS(sdflm, tmp);
|
|
|
155 |
|
|
|
156 |
UNPROTECT(nprotect);
|
|
|
157 |
UNUSE_SDF2(inamex);
|
|
|
158 |
UNUSE_SDF2(inamey);
|
|
|
159 |
return sdflm;
|
|
|
160 |
}
|