Commit | Line | Data |
---|---|---|
a961f8a1 BA |
1 | #Maximum size of stored data to predict next PM10 |
2 | MAX_HISTORY = 10000 | |
3 | ||
4 | #Default lambda value (when too few data) | |
5 | LAMBDA = 2. | |
6 | ||
7 | #Maximum error to keep a line in (incremental) data | |
8 | MAX_ERROR = 20. | |
9 | ||
10 | #Turn a "vector" into 1D matrix if needed (because R auto cast 1D matrices) | |
11 | matricize = function(x) | |
12 | { | |
13 | if (!is.null(dim(x))) | |
14 | return (as.matrix(x)) | |
15 | return (t(as.matrix(x))) | |
16 | } | |
17 | ||
18 | #Moore-Penrose pseudo inverse | |
19 | mpPsInv = function(M) | |
20 | { | |
21 | epsilon = 1e-10 | |
22 | s = svd(M) | |
23 | sd = s$d ; sd[sd < epsilon] = Inf | |
24 | sd = diag(1.0 / sd, min(nrow(M),ncol(M))) | |
25 | return (s$v %*% sd %*% t(s$u)) | |
26 | } | |
27 | ||
28 | #Heuristic for k in knn algorithms | |
29 | getKnn = function(n) | |
30 | { | |
31 | return ( max(1, min(50, ceiling(n^(2./3.)))) ) | |
32 | } | |
33 | ||
34 | #Minimize lambda*||u||^2 + ||Xu - Y||^2 | |
35 | ridgeSolve = function(X, Y, lambda) | |
36 | { | |
37 | s = svd(X) | |
38 | deltaDiag = s$d / (s$d^2 + lambda) | |
39 | deltaDiag[!is.finite(deltaDiag)] = 0.0 | |
40 | if (length(deltaDiag) > 1) | |
41 | deltaDiag = diag(deltaDiag) | |
42 | return (s$v %*% deltaDiag %*% t(s$u) %*% Y) | |
43 | } | |
44 | ||
45 | #Return the indices (of rows, by default) without any NA | |
46 | getNoNAindices = function(M, margin=1) | |
47 | { | |
48 | return (apply(M, margin, function(z)(!any(is.na(z))))) | |
49 | } |