visreg的目的
visreg的特色
visreg的使用
2018-05-28
visreg的目的
visreg的特色
visreg的使用
pacman::p_load(visreg, gridExtra, ggplot2, dplyr)
theme_set(theme_bw())
dta <- read.table("hs0.txt", header = TRUE)
## Warning: package 'bindrcpp' was built under R version 3.4.3
物件導向(object-oriented)
廣泛用於許多模型
只要一行
美術風格支援主流繪圖包(lattice, ggplot2)
\(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)
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
fit <- lm(Ozone ~ Solar.R + Wind + Temp, data = airquality) visreg(fit) visreg(fit, "Wind")
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 = "")
當在 \(Z\) 條件下, \(X\) 對 \(Y\) 的影響為何? \(E(Y\sim X | Z)\)
從 \(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")
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")
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")
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")
visreg2d(fit, "Wind", "Temp", plot.type = "rgl")
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)")