# compare with splines
library(splines)
library(SimSurvNMarker)
xs <- seq(1, 5, length.out = 10L)
bks <- c(1, 5)
iks <- 2:4
# we get the same
if(require(Matrix)){
r1 <- unclass(ns(xs, knots = iks, Boundary.knots = bks, intercept = TRUE))
r2 <- get_ns_spline(knots = sort(c(iks, bks)), intercept = TRUE,
do_log = FALSE)(xs)
cat("Rank is correct: ", rankMatrix(cbind(r1, r2)) == NCOL(r1), "\n")
r1 <- unclass(ns(log(xs), knots = log(iks), Boundary.knots = log(bks),
intercept = TRUE))
r2 <- get_ns_spline(knots = log(sort(c(iks, bks))), intercept = TRUE,
do_log = TRUE)(xs)
cat("Rank is correct (log):", rankMatrix(cbind(r1, r2)) == NCOL(r1), "\n")
}
# the latter is faster
system.time(
replicate(100,
ns(xs, knots = iks, Boundary.knots = bks, intercept = TRUE)))
system.time(
replicate(100,
get_ns_spline(knots = sort(c(iks, bks)), intercept = TRUE,
do_log = FALSE)(xs)))
func <- get_ns_spline(knots = sort(c(iks, bks)), intercept = TRUE,
do_log = FALSE)
system.time(replicate(100, func(xs)))
Run the code above in your browser using DataLab