## ----intro-knitr-opts, include = FALSE---------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 6, fig.height = 4, fig.align = "center" ) ## ----setup-------------------------------------------------------------------- library(MetaHunt) set.seed(1) ## ----simulate----------------------------------------------------------------- G <- 40; m <- 120; K_true <- 3 x <- seq(0, 1, length.out = G) basis <- rbind(sin(pi * x), cos(pi * x), x) # 3 true bases on the grid W <- data.frame(w1 = rnorm(m), w2 = rnorm(m)) # study-level covariates beta <- cbind(c(1, -0.8), c(-0.5, 1.2), c(0, 0)) pi_true <- exp(as.matrix(W) %*% beta) pi_true <- pi_true / rowSums(pi_true) F_hat <- pi_true %*% basis + matrix(rnorm(m * G, sd = 0.05), m, G) dim(F_hat) ## ----fit---------------------------------------------------------------------- fit <- metahunt(F_hat, W, K = 3) fit ## ----plot-bases--------------------------------------------------------------- plot(fit, x_axis = x, col = c("#0072B2", "#D55E00", "#009E73")) ## ----predict------------------------------------------------------------------ W_new <- data.frame(w1 = c(0, 1, -1), w2 = c(0, -0.5, 1), row.names = c("baseline", "high w1, low w2", "low w1, high w2")) f_pred <- predict(fit, newdata = W_new) dim(f_pred) oldpar <- par(mar = c(4, 4.5, 3, 1)) matplot(x, t(f_pred), type = "l", lty = 1, col = c("#0072B2", "#D55E00", "#009E73"), xlab = "x", ylab = expression(tilde(f)(x)), main = "Predicted target functions") legend("topright", legend = rownames(W_new), col = 1:3, lty = 1, bty = "n") par(oldpar) ## ----predict-wrapped---------------------------------------------------------- predict(fit, newdata = W_new, wrapper = mean)