HEX
Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1j PHP/8.4.25
System: Windows NT DESKTOP-4TAV2RJ 10.0 build 19045 (Windows 10) AMD64
User: fred (0)
PHP: 8.4.25
Disabled: NONE
Upload Files
File: C:/Program Files/R/R-4.4.0/library/survival/tests/nsk.R
library(survival)
library(splines)

# the nsk function should give the same solution as ns, but with a different
#  parameterization
#
xx <- runif(500, 1, 100)
yy <- 10*log(xx) + rnorm(500, 0, 2)
tdata <- data.frame(xx=xx, yy=yy)
fit1 <- lm(yy ~ ns(xx, df=4), tdata, model=TRUE)
fit2 <- lm(yy ~ nsk(xx, df=4, b=0), tdata)
all.equal(predict(fit1), predict(fit2))  # same solution

xattr <- attributes(fit1$model[[2]])
allknots <- sort(c(xattr$knots, xattr$Boundary.knots)) # knots that were used
pred.knot <- predict(fit1, newdata=list(xx=allknots))
all.equal(pred.knot[-1] - pred.knot[1], coef(fit2)[-1],
          check.attributes = FALSE)