## generate sample data
set.seed(4321)
s <- seq(0, 1, length.out = 100)
t <- seq(0, 1, length.out = 100)
p1 <- 5
p2 <- 6
r <- 10
n <- 50
beta_basis1 <- splines::bs(s, df = p1, intercept = TRUE) # first basis for beta
beta_basis2 <- splines::bs(s, df = p2, intercept = TRUE) # second basis for beta
data_basis <- splines::bs(s, df = r, intercept = TRUE) # basis for X
x_0 <- apply(matrix(rnorm(p1 * p2, sd = 1), p1, p2), 1,
fdaSP::softhresh, 1.5) # regression coefficients
x_fun <- beta_basis2 %*% x_0 %*% t(beta_basis1)
fun_data <- matrix(rnorm(n*r), n, r) %*% t(data_basis)
b <- fun_data %*% x_fun + rnorm(n * 100, sd = sd(fun_data %*% x_fun )/3)
## set the hyper-parameters
maxit <- 1000
rho_adaptation <- FALSE
rho <- 1
reltol <- 1e-5
abstol <- 1e-5
## fit functional regression model
mod <- f2fSP(mY = b, mX = fun_data, L = p1, M = p2,
group_weights = NULL, var_weights = NULL, standardize.data = FALSE, splOrd = 4,
lambda = NULL, nlambda = 30, lambda.min.ratio = NULL,
control = list("abstol" = abstol,
"reltol" = reltol,
"maxit" = maxit,
"adaptation" = rho_adaptation,
rho = rho,
"print.out" = FALSE))
mycol <- function (n) {
palette <- colorRampPalette(RColorBrewer::brewer.pal(11, "Spectral"))
palette(n)
}
cols <- mycol(1000)
oldpar <- par(mfrow = c(1, 2))
image(x_0, col = cols)
image(mod$sp.coefficients, col = cols)
par(oldpar)
oldpar <- par(mfrow = c(1, 2))
image(x_fun, col = cols)
contour(x_fun, add = TRUE)
image(beta_basis2 %*% mod$sp.coefficients %*% t(beta_basis1), col = cols)
contour(beta_basis2 %*% mod$sp.coefficients %*% t(beta_basis1), add = TRUE)
par(oldpar)
Run the code above in your browser using DataLab