### R code from vignette source "plot3logit.Rnw" ################################################### ### code chunk number 1: preliminaries ################################################### options(prompt = "R> ", continue = "+ ", width = 70, useFancyQuotes = FALSE) library("MASS") library("plot3logit") ################################################### ### 2. Ternary plots and trinomial logit regression ################################################### ################################################### ### code chunk number 2: plot3logit.Rnw:359-363 ### Fit the model ################################################### library("nnet") data("USvote2016", package = "plot3logit") modVote <- multinom(vote ~ educ + gender + race + birthyr, data = droplevels(USvote2016), trace = FALSE) ################################################### ### 4. Computation and representation of vector fields ################################################### ################################################### ### 4.1 Computation of vector fields ################################################### ################################################### ### code chunk number 3: plot3logit.Rnw:576-579 ### Read model from list ################################################### fittedModel <- list(B = matrix(c(2, 0.3, -0.2, 0.2, 1, 0.1, -0.4, -0.3), ncol = 2, dimnames = list(c("(Intercept)", "X1", "X2", "X3"))), levels = c("Class A", "Class B", "Class C")) ################################################### ### code chunk number 4: plot3logit.Rnw:585-587 ### Extract "model3logit" from "fittedModel" ################################################### library("plot3logit") extract3logit(fittedModel) ################################################### ### code chunk number 5: plot3logit.Rnw:627-630 ### Set the numeric vector of covariate changes ################################################### Delta <- rep(0, 17) Delta[7] <- 1 Delta ################################################### ### code chunk number 6: plot3logit.Rnw:634-635 ### Set argument "delta" explicitly ################################################### field3logit(model = modVote, delta = Delta) ################################################### ### code chunk number 7: plot3logit.Rnw:641-642 ### Set argument "delta" through a named vector ################################################### field3logit(model = modVote, delta = c(genderFemale = 1, raceBlack = 1)) ################################################### ### code chunk number 8: plot3logit.Rnw:652-653 ### Set argument "delta" through covariate names ################################################### field3logit(model = modVote, delta = "genderFemale") ################################################### ### code chunk number 9: plot3logit.Rnw:666-667 (eval = FALSE) ### Set argument "delta" through a named vector ################################################### field3logit(model = fittedModel, delta = c(X1 = 0.5, X2 = -2, X3 = 1)) ################################################### ### code chunk number 10: plot3logit.Rnw:669-670 ### Set argument "delta" through a mathematical expression ################################################### field3logit(model = fittedModel, delta = "0.5 * X1 + X3 - 2 * X2") ################################################### ### code chunk number 11: plot3logit.Rnw:684-685 ### Set argument "delta" when covariate names contains special characters ################################################### field3logit(modVote, delta = "genderFemale + `birthyr[1940,1950)`") ################################################### ### code chunk number 13: visualization ### Draw Figure 5 ################################################### ptsAB <- list(A = c(0.3, 0.4, 0.3), B = c(0.5, 0.1, 0.4)) par(mfrow = c(2, 2), cex = 0.5, mar = rep(0, 4)) # Top-left plot(field3logit(modVote, "genderFemale", edge = 0.1)) # Top-right plot(field3logit(modVote, "genderFemale", nstreams = 4)) # Bottom-left plot(field3logit(modVote, "genderFemale", p0 = ptsAB)) TernaryPoints(ptsAB) TernaryText(ptsAB, labels = names(ptsAB), pos = 1) # Bottom-right plot(field3logit(modVote, "genderFemale", p0 = ptsAB, narrows = 1)) TernaryPoints(ptsAB) TernaryText(ptsAB, labels = names(ptsAB), pos = 1) ################################################### ### 4.2 Representation of vector fields ################################################### ################################################### ### code chunk number 14: plot3logit.Rnw:897-899 ### Draw Figure 2a ################################################### fieldFemale <- field3logit(modVote, "genderFemale") gg3logit(fieldFemale) + stat_field3logit() ################################################### ### code chunk number 15: plot3logit.Rnw:911-913 ### Fortified "field3logit" object ################################################### set.seed(3109) fortfieldFemale <- fortify(field3logit(modVote, "genderFemale")) set.seed(NULL) fortfieldFemale ################################################### ### code chunk number 16: plot3logit.Rnw:919-922 ### Draw Figure 2a by means of a fortified "field3logit" object ################################################### gg3logit(fortfieldFemale, aes(x = Clinton, y = Trump, z = Other, xend = Clinton_end, yend = Trump_end, zend = Other_end, group = group, type = type)) + stat_field3logit() ################################################### ### 4.3. Handling multiple fields ################################################### ################################################### ### code chunk number 17: plot3logit.Rnw:981-991 ### Combine two "field3logit" objects into a "multifield3logit" object ################################################### refprofile <- list(c(1/3, 1/3, 1/3)) fieldBlack <- field3logit(model = modVote, delta = "raceBlack", label = "Black", p0 = refprofile, narrows = 1) fieldHispanic <- field3logit(model = modVote, delta = "raceHispanic", label = "Hispanic", p0 = refprofile, narrows = 1) mfieldrace <- fieldBlack + fieldHispanic mfieldrace ################################################### ### code chunk number 18: plot3logit.Rnw:1011-1016 ### Combine a "multifield3logit" object and a field3logit" object ################################################### fieldAsian <- field3logit(model = modVote, delta = "raceAsian", label = "Asian", p0 = refprofile, narrows = 1) mfieldrace <- mfieldrace + fieldAsian mfieldrace ################################################### ### code chunk number 19: plot3logit.Rnw:1029-1036 ### Define a list of parameters for a set of "field3logit" objects ################################################### race_effects <- list( list(delta = "raceBlack", label = "Black"), list(delta = "raceHispanic", label = "Hispanic"), list(delta = "raceAsian", label = "Asian"), list(delta = "raceMixed", label = "Mixed"), list(delta = "raceOther", label = "Other") ) ################################################### ### code chunk number 20: plot3logit.Rnw:1040-1044 ### Create the "multifield3logit" object through function "field3logit" ################################################### mfieldrace <- field3logit(model = modVote, delta = race_effects, p0 = refprofile, narrows = 1) mfieldrace ################################################### ### code chunk number 21: plot3logit.Rnw:1065-1067 ### Use the syntax based on delimiters "<<...>>" ################################################### field3logit(model = modVote, delta = "<>", p0 = refprofile, narrows = 1) ################################################### ### code chunk number 22: plot3logit.Rnw:1080-1082 ### Specify the labels of fields directly through argument "label" ################################################### field3logit(model = modVote, delta = c("raceBlack", "raceAsian"), label = c("BLACK", "ASIAN")) ################################################### ### code chunk number 23: plot3logit.Rnw:1088-1090 ### Use argument "label" to set the prefix of labels ################################################### mfdecade <- field3logit(modVote, "<>", label = "Born in ") mfdecade ################################################### ### code chunk number 24: plot3logit.Rnw:1094-1098 ### Overwrite the labels of a "multifield3logit" object ################################################### labels(mfdecade) labels(mfdecade) <- c("Fourties", "Fifties", "Sixties", "Seventies", "Eighties and Nineties") mfdecade ################################################### ### code chunk number 25: plot3logit.Rnw:1114-1116 ### Draw a "multifield3logit" object ################################################### gg3logit(mfieldrace, aes(colour = label)) + stat_field3logit() + labs(colour = "Race (ref.: White)") ################################################### ### 5. Confidence regions ################################################### ################################################### ### 5.1 Computation ################################################### ################################################### ### code chunk number 26: plot3logit.Rnw:1247-1248 ### Add confidence regions to a "multifield3logit" object ################################################### mfieldrace <- add_confregions(mfieldrace) ################################################### ### 5.2 Representation ################################################### ################################################### ### code chunk number 27: plot3logit.Rnw:1281-1284 ### Draw Figure 4 ################################################### gg3logit(mfieldrace) + stat_field3logit(aes(colour = label)) + stat_conf3logit(aes(fill = label)) + labs(colour = "Race (ref.: White)", fill = "Race (ref.: White)") ################################################### ### code chunk number 28: plot3logit.Rnw:1288-1306 ### Draw Figure 3 from scratch ################################################### library("tidyverse") tibble(race = levels(USvote2016$race), educ = "High school grad.", gender = "Male", birthyr = "[1970,1980)" ) %>% mutate(delta = "genderFemale", label = race) %>% group_by(delta, label) %>% nest() %>% mutate(p0 = map(data, ~ list(predict(modVote, .x, type = "probs")))) %>% select(-data) %>% transpose -> gender_by_race mfieldGbyR <- field3logit(modVote, gender_by_race, narrows = 1, conf = 0.95) gg3logit(mfieldGbyR) + stat_field3logit(aes(colour = label)) + stat_conf3logit(aes(fill = label)) + tern_limits(T = 0.8, R = 0.8) + labs(colour = "Profile", fill = "Profile") ################################################### ### 6. Wrappers ################################################### ################################################### ### code chunk number 29: plot3logit.Rnw:1318-1319 (eval = FALSE) ################################################### ## stat_field3logit() + stat_conf3logit() ################################################### ### code chunk number 30: plot3logit.Rnw:1328-1329 (eval = FALSE) ################################################### ## gg3logit() + stat_3logit() ################################################### ### code chunk number 31: plot3logit.Rnw:1332-1333 (eval = FALSE) ################################################### ## gg3logit() + stat_field3logit() + stat_conf3logit() ################################################### ### code chunk number 32: plot3logit.Rnw:1344-1346 # Full syntax ################################################### gg3logit(mfieldrace) + stat_field3logit(aes(colour = label)) + stat_conf3logit(aes(fill = label)) ################################################### ### code chunk number 33: plot3logit.Rnw:1349-1350 ### "stat_3logit" ################################################### gg3logit(mfieldrace) + stat_3logit(aes(colour = label), aes(fill = label)) ################################################### ### code chunk number 34: plot3logit.Rnw:1353-1355 ### "autoplot ################################################### autoplot(mfieldrace, mapping_field = aes(colour = label), mapping_conf = aes(fill = label))