2018-05-28

summary

  • visreg的目的

  • visreg的特色

  • visreg的使用

setting

pacman::p_load(visreg, gridExtra, ggplot2, dplyr)
theme_set(theme_bw())
dta <- read.table("hs0.txt", header = TRUE)

complexity model

## Warning: package 'bindrcpp' was built under R version 3.4.3

feature

  • 物件導向(object-oriented)

  • 廣泛用於許多模型

  • 只要一行

  • 美術風格支援主流繪圖包(lattice, ggplot2)

partial residual plot

\(Residuals + \hat\beta_iX_i\) vs \(X_i\)

residual(model, type = "partial")[, "term_name"]  
termplot(model, partial.resid = TRUE, terms = "term_name")
mo <- lm(write ~ read + math + science + socst, dta)

partial residual plot

visreg example

head(airquality)[,c("Ozone", "Wind", "Solar.R", "Temp")]
##   Ozone Wind Solar.R Temp
## 1    41  7.4     190   67
## 2    36  8.0     118   72
## 3    12 12.6     149   74
## 4    18 11.5     313   62
## 5    NA 14.3      NA   56
## 6    28 14.9      NA   66

default

fit <- lm(Ozone ~ Solar.R + Wind + Temp, data = airquality)
visreg(fit)
visreg(fit, "Wind")

default

graphic option

visreg(fit, "Wind", 
       band = FALSE,
       partial = FALSE,
       rug = FALSE,
       line = list(col = "red"), 
       fill = list(col = "green"), 
       points = list(cex = 1.5, pch = 1),
       ylab = "", xlab = "", main = "")

Conditional and Contrast plot

  1. 當在 \(Z\) 條件下, \(X\) 對 \(Y\) 的影響為何? \(E(Y\sim X | Z)\)

  2. 從 \(X_0\) 變成 \(X_1\) 時,\(Y\) 的變化量為何? \(E(Y\sim X_1)-E(Y\sim X_0)\)

airquality$Heat <- cut(airquality$Temp, 3, labels = c("cool", "Mild", "Hot"))
fit.heat <- lm(Ozone ~ Solar.R + Wind + Heat, data = airquality)
visreg(fit.heat, "Heat", type = "conditional", main = "Conditional Plot")
visreg(fit.heat, "Heat", type = "contrast", main = "Contrast Plot")

Conditional and Contrast plot

Transformation

fit1 <- lm(Ozone ~ Solar.R + Wind + I(Wind^2) + Temp, data = airquality)
fit2 <- lm(log(Ozone) ~ Solar.R + Wind + Temp, data = airquality)
fit3 <- lm(log(Ozone) ~ Solar.R + Wind + I(Wind^2) + Temp, data = airquality)

visreg(fit1, "Wind", main = "trans x")
visreg(fit2, "Wind", trans = exp, partial = TRUE,
       ylab = "Ozone (log)", main = "trans y")
visreg(fit3, "Wind", trans = exp, partial = TRUE,
       ylab = "Ozone (log)", main = "trans x and y")

Transformation

Conditioning

visreg(fit, "Wind", cond = list(Temp = 50), main = "Temp = 50, SolarR = Median")
visreg(fit, "Wind", main = "all default: Median")
visreg(fit, "Wind", cond = list(Temp = 100), main = "Temp = 100, SolarR = Median")

Conditioning

Cross-sectional

fit <- lm(Ozone ~ Solar.R + Wind * Heat, data = airquality)
visreg(fit, "Wind", by = "Heat", main = "default: lattice")
visreg(fit, "Wind", by = "Heat", gg = TRUE) + labs(title = "ggplot2 style")
visreg(fit, "Wind", by = "Heat", overlay = TRUE, partial = FALSE)
visreg(fit, "Heat", by = "Wind")

Cross-sectional (lattice)

Cross-sectional (ggplot2)

Cross-sectional

Cross-sectional

Surface plots - Filled contour plots

Surface plots - Perspective plots

Surface plots - Dynamically Perspective plots (rgl)

visreg2d(fit, "Wind", "Temp", plot.type = "rgl")

GLM

data("birthwt", package = "MASS")
head(birthwt)
fit <- glm(low ~ age + race + smoke + lwt, data = birthwt, family = "binomial")
visreg(fit, "lwt", xlab = "Mother's weight", ylab = "Log odds (low birthweight)")
visreg(fit, "lwt", scale = "response", rug = 2,
       xlab = "Mother's weight", ylab = "Pr (low birthweight)")

GLM

Another Models

Another Models