# NOT RUN {
(fm3 <- lmer(strength ~ 1 + (1|sample) + (1|batch), Pastes))
fm3_step <- stepcAIC(fm3, direction = "backward", trace = TRUE, data = Pastes)
fm3_min <- lm(strength ~ 1, data=Pastes)
fm3_min_step <- stepcAIC(fm3_min, groupCandidates = c("batch", "sample"),
direction="forward", data=Pastes, trace=TRUE)
fm3_min_step <- stepcAIC(fm3_min, groupCandidates = c("batch", "sample"),
direction="both", data=Pastes, trace=TRUE)
# try using a nested group effect which is actually not nested -> warning
fm3_min_step <- stepcAIC(fm3_min, groupCandidates = c("batch", "sample", "batch/sample"),
direction="both", data=Pastes, trace=TRUE)
Pastes$time <- 1:dim(Pastes)[1]
fm3_slope <- lmer(data=Pastes, strength ~ 1 + (1 + time | cask))
fm3_slope_step <- stepcAIC(fm3_slope,direction="backward", trace=TRUE, data=Pastes)
fm3_min <- lm(strength ~ 1, data=Pastes)
fm3_min_step <- stepcAIC(fm3_min,groupCandidates=c("batch","sample"),
direction="forward", data=Pastes,trace=TRUE)
fm3_inta <- lmer(strength ~ 1 + (1|sample:batch), data=Pastes)
fm3_inta_step <- stepcAIC(fm3_inta,groupCandidates=c("batch","sample"),
direction="forward", data=Pastes,trace=TRUE)
fm3_min_step2 <- stepcAIC(fm3_min,groupCandidates=c("cask","batch","sample"),
direction="forward", data=Pastes,trace=TRUE)
fm3_min_step3 <- stepcAIC(fm3_min,groupCandidates=c("cask","batch","sample"),
direction="both", data=Pastes,trace=TRUE)
# }
# NOT RUN {
fm3_inta_step2 <- stepcAIC(fm3_inta,direction="backward",
data=Pastes,trace=TRUE)
# }
# NOT RUN {
##### create own example
na <- 20
nb <- 25
n <- 400
a <- sample(1:na,400,replace=TRUE)
b <- factor(sample(1:nb,400,replace=TRUE))
x <- runif(n)
y <- 2 + 3 * x + a*.02 + rnorm(n) * .4
a <- factor(a)
c <- interaction(a,b)
y <- y + as.numeric(as.character(c))*5
df <- data.frame(y=y,x=x,a=a,b=b,c=c)
smallMod <- lm(y ~ x)
# }
# NOT RUN {
# throw error
stepcAIC(smallMod, groupCandidates=c("a","b","c"), data=df, trace=TRUE, returnResult=FALSE)
smallMod <- lm(y ~ x, data=df)
# throw error
stepcAIC(smallMod, groupCandidates=c("a","b","c"), data=df, trace=TRUE, returnResult=FALSE)
# get it all right
mod <- stepcAIC(smallMod, groupCandidates=c("a","b","c"),
data=df, trace=TRUE,
direction="forward", returnResult=TRUE)
# make some more steps...
stepcAIC(smallMod, groupCandidates=c("a","b","c"), data=df, trace=TRUE,
direction="both", returnResult=FALSE)
mod1 <- lmer(y ~ x + (1|a), data=df)
stepcAIC(mod1, groupCandidates=c("b","c"), data=df, trace=TRUE, direction="forward")
stepcAIC(mod1, groupCandidates=c("b","c"), data=df, trace=TRUE, direction="both")
mod2 <- lmer(y ~ x + (1|a) + (1|c), data=df)
stepcAIC(mod2, data=df, trace=TRUE, direction="backward")
mod3 <- lmer(y ~ x + (1|a) + (1|a:b), data=df)
stepcAIC(mod3, data=df, trace=TRUE, direction="backward")
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab