# NOT RUN {
library(lme4)
if(require(lmerTest)){
##### BASED ON lmerTest HELP PAGE #########
# define function to fit a model based on response
modFun <- function(y)
{
ham$y <- y
lmer(y ~ Gender + Information * Product + (1 | Consumer) +
(1 | Product), data=ham)
}
# define a function to select a model (based on a model)
selFun <- function(mod) step(mod)
# define a function which extracts the results of the selection procedure
extractSelFun <- function(this_mod){
this_mod <- attr(this_mod, "model")
if(class(this_mod)=="lm")
return(attr(this_mod$coefficients, "names")) else
return(c(names(fixef(this_mod)), names(getME(this_mod, "theta"))))
}
## backward elimination of non-significant effects:
(step_result <- selFun(modFun(ham$Informed.liking)))
attr(step_result, "model")
## Elimination tables for random- and fixed-effect terms:
(sel <- extractSelFun(step_result))
## Now we can finally define the function checking the congruency
## with the original selection
checkFun <- function(yb){
this_mod <- modFun(yb)
setequal( extractSelFun(selFun(this_mod)), sel )
}
# Now let's compute valid p-values conditional on the selection
# }
# NOT RUN {
res <- mocasin(attr(step_result, "model"), this_y = ham$Informed.liking,
checkFun = checkFun, which = 1, nrSamples = 8, trace = FALSE)
# print(res)
# }
# NOT RUN {
}
# gamm4 example similar to the one from gamm4 help page
if(require(gamm4)){
set.seed(0)
dat <- gamSim(1, n = 500, scale = 2) ## simulate 4 term additive truth
dat$y <- 3 + dat$x0^2 + rnorm(n = 500)
br <- gamm4(y~ s(x0) + s(x1), data = dat)
summary(br$gam) ## summary of gam
# do not use any selection
checkFun <- function(yb) TRUE
# }
# NOT RUN {
res <- mocasin(br, this_y = dat$y,
checkFun = checkFun,
nrlocs = c(0.7),
nrSamples = 100)
# print result
res
# }
# NOT RUN {
}
# }
Run the code above in your browser using DataLab