Skip to content

Commit 3815a6f

Browse files
committed
Migrate to Rcpp
1 parent ea519ac commit 3815a6f

File tree

17 files changed

+310
-122
lines changed

17 files changed

+310
-122
lines changed

DESCRIPTION

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ NeedsCompilation: no
1414
Imports:
1515
lattice,
1616
yaImpute,
17-
stats
17+
stats,
18+
Rcpp
1819
Suggests:
1920
testthat
21+
LinkingTo: Rcpp
2022
License: BSD_3_clause + file LICENSE
2123
URL: https://github.com/stlplus
2224
Note: This is experimental software distributed free of charge and

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export(remainder)
2626
export(seasonal)
2727
export(stlplus)
2828
export(trend)
29+
importFrom(Rcpp,sourceCpp)
2930
importFrom(lattice,panel.abline)
3031
importFrom(lattice,panel.loess)
3132
importFrom(lattice,panel.segments)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ Version 0.5.0
22
----------------------------------------------------------------------
33

44
- Major overhaul of code to match more modern R package development standards
5+
- Update old C interface to Rcpp

R/RcppExports.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file was generated by Rcpp::compileAttributes
2+
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
3+
4+
c_interp <- function(m, fits, slopes, at) {
5+
.Call('stlplus_c_interp', PACKAGE = 'stlplus', m, fits, slopes, at)
6+
}
7+
8+
c_loess <- function(xx, yy, degree, span, ww, m, l_idx, max_dist) {
9+
.Call('stlplus_c_loess', PACKAGE = 'stlplus', xx, yy, degree, span, ww, m, l_idx, max_dist)
10+
}
11+
12+
c_ma <- function(x, n_p) {
13+
.Call('stlplus_c_ma', PACKAGE = 'stlplus', x, n_p)
14+
}
15+

R/cpp_wrappers.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.interp <- function(m, fits, slopes, at) {
2+
if(any(is.nan(fits))) {
3+
ind <- !is.nan(fits)
4+
c_interp(m[ind], fits[ind], slopes[ind], at)
5+
} else {
6+
c_interp(m, fits, slopes, at)
7+
}
8+
}

R/loess_stl.R

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#' @importFrom yaImpute ann
2+
#' @importFrom Rcpp sourceCpp
3+
#' @useDynLib stlplus
24
.loess_stlplus <- function(x = NULL, y, span, degree, weights = NULL,
35
m = c(1:length(y)), y_idx = !is.na(y), noNA = all(y_idx), blend = 0,
46
jump = ceiling(span / 10), at = c(1:length(y))) {
@@ -53,16 +55,13 @@
5355
# max_dist <- max_dist * (span / n)
5456
max_dist <- max_dist + (span - n) / 2
5557

56-
out <- .C("loess_stlplus", as.double(x[y_idx]), as.double(y[y_idx]),
57-
as.integer(n), as.integer(degree), as.integer(span),
58-
as.double(weights[y_idx]), as.integer(m), as.integer(n_m),
59-
as.integer(l_idx - 1), as.integer(r_idx - 1), as.double(max_dist),
60-
result = double(n_m), slopes = double(n_m), PACKAGE = "stlplus")
58+
out <- c_loess(x[y_idx], y[y_idx], degree, span, weights[y_idx],
59+
m, l_idx - 1, as.double(max_dist))
6160

6261
res1 <- out$result
6362
# do interpolation
6463
if(jump > 1)
65-
res1 <- .stlplus_interp(m, out$result, out$slope, at)
64+
res1 <- .interp(m, out$result, out$slope, at)
6665
# res1 <- approx(x = m, y = out$result, xout = at)$y
6766

6867
if(blend > 0 && blend <= 1 && degree >= 1) {
@@ -98,17 +97,14 @@
9897
# right now, a lot of unnecessary calculation is done at the interior
9998
# where blending doesn't matter
10099

101-
tmp <- .C("loess_stlplus", as.double(x[y_idx]), as.double(y[y_idx]),
102-
as.integer(n), as.integer(0), as.integer(sp0), as.double(weights[y_idx]),
103-
as.integer(m2), as.integer(n_m2), as.integer(l_idx2-1),
104-
as.integer(r_idx2-1), as.double(max_dist2),
105-
result = double(n_m2), slopes = double(n_m2), PACKAGE = "stlplus")
100+
tmp <- c_loess(x[y_idx], y[y_idx], 0, sp0, weights[y_idx],
101+
m2, l_idx2-1, max_dist2)
106102

107103
if(jump > 1) {
108-
res2_left <- .stlplus_interp(left,
104+
res2_left <- .interp(left,
109105
head(tmp$result, length(left)),
110106
head(tmp$slope, length(left)), left_interp)
111-
res2_right <- .stlplus_interp(right,
107+
res2_right <- .interp(right,
112108
tail(tmp$result, length(right)),
113109
tail(tmp$slope, length(right)), right_interp)
114110
} else {

R/ma.R

Lines changed: 0 additions & 10 deletions
This file was deleted.

R/stlplus.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
#' @example man-roxygen/ex-stlplus.R
4444
#' @export
4545
#' @rdname stlplus
46-
#' @useDynLib stlplus
4746
stlplus <- function(x, t = NULL, n.p, s.window, s.degree = 1,
4847
t.window = NULL, t.degree = 1,
4948
fc.window = NULL, fc.degree = NULL, fc.name = NULL,
@@ -301,14 +300,14 @@ stlplus.default <- function(x, t = NULL, n.p, s.window, s.degree = 1,
301300
tmps <- .loess_stlplus(y = cycleSub, span = s.window, degree = s.degree,
302301
m = cs.ev, weights = w[cycleSubIndices == i], blend = s.blend,
303302
jump = s.jump, at = c(0:(cycleSub.length + 1)))
304-
C[c(cs1, cycleSubIndices, cs2)==i] <- tmps
303+
C[c(cs1, cycleSubIndices, cs2) == i] <- tmps
305304
# approx(x = cs.ev, y = tmps, xout = c(0:(cycleSub.length + 1)))$y
306305
}
307306
}
308307

309308
# Step 3: Low-pass filtering of collection of all the cycle-subseries
310309
# moving averages
311-
ma3 <- .ma(C, n.p)
310+
ma3 <- c_ma(C, n.p)
312311

313312
l.ev <- seq(1, n, by = l.jump)
314313
if(tail(l.ev, 1) != n) l.ev <- c(l.ev, n)

R/stlplus_interp.R

Lines changed: 0 additions & 7 deletions
This file was deleted.

R/zzz.R

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)