library(mgcViz)
set.seed(2) ## simulate some data...
dat <- gamSim(1,n=500,dist="normal",scale=2)
dat$logi <- as.logical( sample(c(TRUE, FALSE), nrow(dat), replace = TRUE) )
dat$fac <- as.factor( sample(c("A1", "A2", "A3"), nrow(dat), replace = TRUE) )
# Fit GAM and get gamViz object
fit <- mqgamV(y ~ fac + s(x0) + s(x1, x2) + x3 + logi, data = dat,
qu = c(0.2, 0.4, 0.6, 0.8))
print(plot(fit, select = 1:4, allTerms = T), pages = 1)
if (FALSE) {
# Example where we are fitting the same model to different datasets, but
# plotting the estimate effects together
dat <- list()
for(ii in 1:4){
# Simulate 4 datasets, we are adding 2 factor variables "fac" and "ref" just
# for illustrating the plotting method (the two factors have no effect on y)
n <- 1000
dat[[ii]] <- gamSim(1,n=n,dist="normal",scale=2)
dat[[ii]]$fac <- as.factor( sample(c("A1", "A2", "A3"), n, replace = TRUE) )
dat[[ii]]$ref <- as.factor( sample(letters[1:10], n, replace = TRUE) )
}
# Estimating model on each dataset
mods <- list()
for(ii in 1:4){
mods[[ii]] <- gamV(y~s(x0)+s(x1, x2)+x3+fac+s(ref, bs = "re"), data = dat[[ii]])
}
# Names will be used to identify the four models we have fitted
names(mods) <- c("M1", "M2", "M3", "M4")
# Plotting on the same plots
print(plot.mgamViz(mods, allTerms = TRUE), pages = 1)
}
Run the code above in your browser using DataLab