TABLE OF CONTENTS


splineUtils/plotspline [ Functions ]

NAME

    plotspline --- plot a spline given a set of knots and parameters

FUNCTION

Create a plot of a spline curve, optionally plotting knots and component splines

SYNOPSIS

428 plotspline <- function(knots, theta, ord, npoints = 1000, plotknots = T, plotmean = F,
429         plotsplines = F, norm = F, xlim = NULL, ylim = NULL, col = "red", ...)

INPUTS

    knots          vector of length K+Q containing a set of knot positions, with
                   the Q border knots repeated at each end.
    theta          vector of length K with spline parameters
    ord            spline order Q
    npoints        number of points at which to evaluate the spline
    plotknots      whether to plot the knots as vertical lines
    plotmean       whether to plot the spline mean as a vertical line
    plotsplines    whether to plot the component spline basis functions
    norm           whether to normalize the spline (as for the frailty)
    ...            other parameters for plot

OUTPUTS

    none

SOURCE

432 {
433     knots <- knots[knots>-Inf]
434     theta <- theta[theta>-Inf]
435     if(is.null(xlim)) xlim = range(knots)
436     # Generate point set for evaluation
437     x = seq(from = xlim[1], to = xlim[2], length = npoints)
438     dx = diff(xlim) / npoints
439     #   Compute spline basis
440     spl <- csplinedesign(knots, x = x, ord = ord)
441     if(norm){
442         Bint <- cevalBinte(knots, ord)
443         for(i in 1:dim(spl)[1]) spl[i, ] <- spl[i, ] / Bint
444     }
445     #   Evaluate the spline at the set of points x
446     splsum <- spl%*%exp(theta) 
447     if(norm) splsum <- splsum / sum(exp(theta))
448     ymax <- max(splsum)
449     if(is.null(ylim)) ylim <- c(0, ymax)
450     #   Plot basis functions
451     if(plotsplines){
452         matplot(x, spl / max(spl) * ylim[2] / 2, type = "l", lty = 2, xlim = xlim,
453                 ylim = ylim, ...)
454         lines(x, splsum, col = col, lwd = 2, lty = 1)
455     }else{
456         plot(x, splsum, col = col, type = "l", lwd = 2, lty = 1, xlim = xlim,
457                 ylim = ylim, ...)
458     }
459     if(plotknots) abline(v = knots, col = "grey")
460     # Compute and plot the mean
461     if(plotmean){
462         Bint <- colSums(spl) / dx
463         spl.norm <- spl%*%mdiag(1 / Bint)
464         Eint <- rep(0, dim(spl)[2])
465         for(i in 1:dim(spl)[2]) Eint[i] <- sum(spl.norm[, i] * x) / dx
466         E <- Eint%*%exp(theta) / sum(exp(theta))
467         abline(v = E, col = "grey", lwd = 2)
468     }
469 }