Skip to content

Commit

Permalink
name depth ranges in default output style
Browse files Browse the repository at this point in the history
handy, but makes for a larger output obj than 'classic' mode
  • Loading branch information
obrl-soil committed Feb 16, 2019
1 parent 00b1522 commit 0ef5142
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
8 changes: 7 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# mpspline2 0.1.1

* Internal: correct S4 access methods for SoilProfileCollection objects (h/t Dylan Beaudette)
* Outputs in 'default' mode now have `names()` attributes where they refer to a range e.g. "000_005cm"

# mpspline2 0.1.0

* Added a `NEWS.md` file to track changes to the package.
* Added a `NEWS.md` file to track changes to the package.
* And did everything else.
21 changes: 16 additions & 5 deletions R/mpspline.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,17 @@ mpspline_est1 <- function(s = NULL, var_name = NULL, lam = NULL) {
# solve Z for the input data values
s_bar <- solve(Z, s[[var_name]])

# name s_bar with input depth ranges
names(s_bar) <- mapply(function(u, l) {
paste0(sprintf('%03d', u), '_', sprintf('%03d', l), '_cm')
}, u = as.integer(s[[2]]), l = as.integer(s[[3]]))

# calculate the fitted value at the knots (middle of each input range)
b <- as.vector(6 * R_inv %*% Q %*% s_bar)
b0 <- c(0, b)
b1 <- c(b, 0)
gamma <- (b1 - b0) / (th * 2)
alfa <- s_bar - b0 * th / 2 - gamma * th^2/3
alfa <- s_bar - b0 * th / 2 - gamma * th^2/3 # nb s_bar names inherit

# just return the stuff needed for subsequent steps
list("s_bar" = s_bar, "b0" = b0, "b1" = b1,
Expand Down Expand Up @@ -225,6 +230,7 @@ mpspline_fit1 <- function(s = NULL, p = NULL, var_name = NULL,
b1 <- p[['b1']]
gamma <- p[['gamma']]
alfa <- p[['alfa']]
names(alfa) <- NULL

nj <- max(s[[3]])
if (nj > md) { nj <- md } # if profile > max d, ignore the deeper part
Expand Down Expand Up @@ -408,14 +414,19 @@ mpspline <- function(obj = NULL, var_name = NULL, lam = 0.1,
"tmse" = t)
})

dnms <- mapply(function(u, l) {
paste0(sprintf('%03d', u), '_', sprintf('%03d', l), '_cm')
}, u = d[1:(length(d) - 1)], l = d[2:length(d)])

splined <- lapply(splined, function(x) {
names(x[['est_dcm']]) <- dnms
x
})

if(out_style == 'default') { return(splined) }
if(out_style == 'classic') { # warning: causes slowdown
mh <- max(sapply(splined, function(i) length(i[[2]])), na.rm =TRUE)

dnms <- mapply(function(u,l) {
paste0(sprintf('%03d', u), '_', sprintf('%03d', l), '_cm')
}, u = d[1:(length(d) - 1)], l = d[2:length(d)])

list('idcol' = sapply(splined, function(i) i[[1]]),
'var.fitted' = t(sapply(splined, function(i) {
x <- rep(NA, mh)
Expand Down

0 comments on commit 0ef5142

Please sign in to comment.