# load 'gammi' package
library(gammi)
# mean function
eta <- function(x, z, additive = TRUE){
mx1 <- cos(2 * pi * (x - pi))
mx2 <- 30 * (z - 0.6)^5
mx12 <- 0
if(!additive) mx12 <- sin(pi * (x - z))
mx1 + mx2 + mx12
}
# generate mean function
set.seed(1)
n <- 1000
nsub <- 50
x <- runif(n)
z <- runif(n)
fx <- eta(x, z)
# generate random intercepts
subid <- factor(rep(paste0("sub", 1:nsub), n / nsub),
levels = paste0("sub", 1:nsub))
u <- rnorm(nsub, sd = sqrt(1/2))
# generate responses
y <- fx + u[subid] + rnorm(n, sd = sqrt(1/2))
# fit model via formula method
mod <- gammi(y ~ x + z, random = ~ (1 | subid))
mod
# get fitted values via predict
fit <- predict(mod, newdata = data.frame(x = x, z = z))
max(abs(fit - mod$fitted.values))
# get fitted values with SE and CI
fit <- predict(mod, newdata = data.frame(x = x, z = z), conf.int = TRUE)
head(fit)
# get fitted values with SE and CI for each term
fit <- predict(mod, newdata = data.frame(x = x, z = z),
type = "terms", conf.int = TRUE)
str(fit) # list with 4 components
head(sapply(fit, function(x) x[,1])) # for x effect
head(sapply(fit, function(x) x[,2])) # for z effect
Run the code above in your browser using DataLab