## This is the replication script to accompany "Conditional Visualization for ## Statistical Models: An Introduction to the condvis Package in R" ## The figures in the article representing snapshots of interactive graphics are ## created from the following code, by pressing the 's' key during the ## interactive session. There are some slight differences, mainly in the size of ## plot symbols. These can be changed with `cex` in the `xcplotpar` list. The R ## console is busy during interactive sessions. Interactive sessions may be ## terminated with the 'q' key. Closing the graphics device can also terminate ## interactive sessions. set.seed(1234) library("condvis") ## Example from 1.1, and Figures 3, 4, and 5. data("fev", package = "covreg") ## Tidy up data. fev$gender <- factor(fev$male, 0:1, c("female", "male")) fev$smoke <- factor(fev$smoke, 0:1, c("no", "yes")) ## Visualise marginal relationship of fev with smoking. plot(fev ~ smoke, data = fev, xlab = "smokes", ylab = "FEV", col = "gray", boxwex = 0.35) ## Fit a model. library("e1071") m1 <- svm(fev ~ gender + smoke + age + height, data = fev) ## Create interactive graphic. ceplot(data = fev, model = m1, sectionvars = "smoke", type = "separate", xcplotpar = list(cex = 0.4)) ceplot(data = fev, model = m1, sectionvars = "smoke", type = "shiny", xcplotpar = list(cex = 0.4)) ## Workflow example. data("mtcars", package = "datasets") m2 <- lm(mpg ~ wt + hp, data = mtcars) ceplot(data = mtcars, model = m2, sectionvars = "hp", type = "default") ## Section 3.4: FEV data, and Figure 12 data("fev", package = "covreg") ## Tidy up data. fev$gender <- factor(fev$male, 0:1, c("female", "male")) fev$male <- NULL fev$smoke <- factor(fev$smoke, 0:1, c("no", "yes")) ## Fit models. library("randomForest") library("mgcv") m3 <- list( RF = randomForest(fev ~ ., data = fev), lm = lm(fev ~ ., data = fev), gam = mgcv::gam(fev ~ smoke + gender + s(age) + s(height), data = fev)) ## Create interactive graphic. ceplot(data = fev, model = m3, sectionvars = "smoke", type = "separate", xcplotpar = list(cex = 0.4)) ## Section 3.4: Powerplant data, and Figures 13 and 14. library("e1071") library("mgcv") data("powerplant", package = "condvis") m4 <- list( svm = svm(PE ~ ., data = powerplant), gam = gam(PE ~ s(AT) + s(V) + s(AP) + s(RH), data = powerplant)) ceplot(data = powerplant, model = m4["svm"], sectionvars = "AT", type = "separate") ceplot(data = powerplant, model = m4, sectionvars = "AT", type = "separate", threshold = 0.5) ## Rotate using arrow keys. ceplot(data = powerplant, model = m4["svm"], sectionvars = c("AT", "V"), type = "separate", view3d = TRUE, threshold = 0.2) ## Section 3.4: Wine data, and Figure 15. library("randomForest") data("wine", package = "condvis") wine$Class <- as.factor(wine$Class) m5 <- randomForest(Class ~ Alcohol + Malic + Ash + Magnesium + Phenols + Flavanoids, data = wine) ceplot(data = wine, model = m5, sectionvars = c("Alcohol", "Phenols"), type = "shiny") ceplot(data = wine, model = m5, sectionvars = c("Alcohol", "Phenols"), type = "separate", selectortype = "pcp", threshold = 1.5)