smooth.construct.gmrf.smooth.spec: Construct a smoother using a GMRF smoothing prior

View source: R/gmrf_smooth_spec.R

smooth.construct.gmrf.smooth.specR Documentation

Construct a smoother using a GMRF smoothing prior

Description

This function is used internally in mgcv when fitting a smoother of type gmrf. More details to be added.

Usage

## S3 method for class 'gmrf.smooth.spec'
smooth.construct(object, data, knots)

Arguments

object

a smooth specification object, usually generated by a term ‘s(...,bs="gmrf", penalty = list(Q = ...))’. ‘x’ is a factor variable giving labels for the nodes in the GMRF graph, and the ‘xt’ argument is obligatory: see details.

data

a list containing just the data (including any ‘by’ variable) required by this term, with names corresponding to ‘object$term’ (and ‘object$by’). The ‘by’ variable is the last element.

knots

not used.

Value

An object of class ‘"gmrf.smooth"’.

Examples

require(gmrf)
n <- 100
idx <- 1:n
idy <- c(5:40, 60:n)
set.seed(64)
Q <- getQrw(n, order = 2)
x <- simQ(exp(1) * Q)
# simulate the first 3/4, say
y <- x + rnorm(n) * 3.5
y <- y[idy]
## set up variables for smoothing
rownames(Q) <- colnames(Q) <- idx
## fit an RW2 smoother with restricted df
g1 <- gam(y ~ s(idy, bs = "gmrf", xt = list(penalty = Q), k = length(y)-1), method="REML")
summary(g1)
plot(idy, y, xlim = range(idx), ylim = range(x,y))
lines(idx, x, col = "blue", lwd = 2)
pred <- predict(g1, newdata = list(idy = idx), se = TRUE)
lines(idx, pred$fit, col = "red", lwd = 2)
lines(idx, pred$fit + 2*pred$se.fit, col = "red", lty = 2)
lines(idx, pred$fit - 2*pred$se.fit, col = "red", lty = 2)

Faskally/gmrf documentation built on Sept. 21, 2023, 1:16 p.m.