cvgrid: cross validation grid search for l1-penalized additive mixed...

Description Usage Arguments Examples

View source: R/cvgrid.R

Description

This function runs cross-validation grid search to estimate the smoothing parameters

Usage

1
2
3
4
cvgrid(y, X, rand, id, data, K = 5, se1 = FALSE, smoothInit = NULL,
  pathLength = 20, useTestRanEff = TRUE, tauMax = NULL, revPath = FALSE,
  paramOrder = NULL, epsilonAbs = 1e-04, epsilonRel = 1e-04,
  iterMax = 1000, centerZ = FALSE, verbose = 2, fileName = NULL)

Arguments

y

character string denoting output variable

X

list of fixed effect design matrices setup by l1smooth

rand

list of random effect terms setup by randomEffects

id

character string denoting subject id variable

data

dataset

se1

TRUE/FALSE if true, sets smoothing parameter to value with maximum CV error within 1 sd of the value that minimizes CV error

smoothInit

vector of initial smoothing parameter values

pathLength

length of each smoothing parameter path (evenly spaced on the log scale)

useTestRanEff

TRUE/FALSE if true, estimates random effects with test sample

tauMax

scalar max value to use in tau path. If NULL, set to maximum value used for fixed effect smoothing parameters.

revPath

TRUE/FALSE if true, estimates smoothing paths from smallest to largest

paramOrder

order of smoothing parameters to be fit

epsilonAbs

absolute error in ADMM (tolerance for convergence)

epsilonRel

relative error in ADMM (tolerance for convergence)

iterMax

maximum number of ADMM iterations

centerZ

TRUE/FALSE If True, imposes centering constraints on random effects

verbose

integer (0, 1, 2) amount of information printed to terminal. 0 = no information, 1 = path iteration, 2 = path iteration + fold results

saveFile

file name for saving CV results as csv file, e.g. "CV_results". Do not include extension. If NULL, no file saved.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
library(psplinesl1)
data(simData)

# setup p-spline matrices
X <- list(ps(x = "x", data = simData, 
             norder = 2, k = 1, width = 0.05,
             center = TRUE))

# setup random effect matrices
rand <- re(x = "x", id = "id", data = simData,
           randomCurves = FALSE)

# run cross validation and view results
cvOut <- cvgrid(y = "y", id = "id", X = X, rand = rand,
                  K = 5,
                  pathLength = 20,
                  data = simData)
cvOut

# fit model with all data
a1 <- admm(y = "y", X, Z = rand$Z, S = rand$S,
            tau = cvOut$smoothOpt[1],
            lambda = cvOut$smoothOpt[2:(length(X)+1)],
            rho = min(5, max(cvOut$smoothOpt)),
            data = simData)

# get and plot fitted model with confidence bands
CI <- ci(model = a1, alpha = 0.05)
plot(CI)

# extract values from ci object for custom plotting
CIpoly <- data.frame(x = c(CI[[1]]$x, rev(CI[[1]]$x)), 
                     y = c(CI[[1]]$lower, rev(CI[[1]]$upper)))

ggplot(aes(x = x, y = y), data = newDat)+
  geom_polygon(data = CIpoly, fill = "grey")+
  geom_line(aes(x = CI[[1]]$x, y = CI[[1]]$smooth))

bdsegal/psplinesl1 documentation built on July 22, 2019, 11:38 a.m.