1 #Maximum size of stored data to predict next PM10
4 #Default lambda value (when too few data)
7 #Maximum error to keep a line in (incremental) data
10 #Turn a "vector" into 1D matrix if needed (because R auto cast 1D matrices)
11 matricize = function(x)
15 return (t(as.matrix(x)))
18 #Moore-Penrose pseudo inverse
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))
28 #Heuristic for k in knn algorithms
31 return ( max(1, min(50, ceiling(n^(2./3.)))) )
34 #Minimize lambda*||u||^2 + ||Xu - Y||^2
35 ridgeSolve = function(X, Y, lambda)
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)
45 #Return the indices (of rows, by default) without any NA
46 getNoNAindices = function(M, margin=1)
48 return (apply(M, margin, function(z)(!any(is.na(z)))))