## ----setup, include = FALSE--------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", dpi = 300, dev = "png" ) ## ----first, fig.height=3.8, fig.width=6, out.width="75%"---------------------- library(gridmicrotex) library(grid) latex_options(device_math = TRUE) plot(1:10, (1:10)^2, main = r"(Slope $\hat{\beta}_1 = \sum_{i=1}^{n} x_i^2$)", xlab = "Cost $5-$10 per unit", ylab = r"($\frac{y}{2}$)") text(3, 80, r"($\int_0^\infty e^{-x^2}\,dx$)", col = "steelblue") ## ----beyond, fig.height=4.6, fig.width=6.5------------------------------------ set.seed(42) x <- seq(0, 3, length.out = 60) y <- 2 + 1.4 * x^2 + rnorm(60, sd = 1.1) fit <- lm(y ~ poly(x, 2, raw = TRUE)) cf <- signif(coef(summary(fit)), 3) title_eq <- r"($$\hat{y} = \beta_0 + \beta_1 x + \beta_2 x^2$$)" # Make room for the tall title h <- latex_dims(title_eq, gp = gpar(fontsize = par("ps")))$height top <- ceiling(convertHeight(h, "bigpts", TRUE) / (par("cin")[2] * 72 * par("mex"))) + 2 par(mar = c(4.5, 5.5, top, 2)) plot(x, y, pch = 19, col = "grey45", cex = 0.7, ylim = c(0, 22), main = title_eq, xlab = r"($x$)", ylab = r"($y = f(x) + \varepsilon$)") lines(x, predict(fit), col = "#B22222", lwd = 2) # Build the table as a single line of text tab <- sprintf(paste0( r"(\begin{array}{|l|r|r|}\hline)", r"(\rowcolor{#E8F0FE}\text{Term} & \hat{\beta} & \text{SE}\\\hline)", r"(\beta_0 & %s & %s\\)", r"(\beta_1 & %s & %s\\)", r"(\beta_2 & %s & %s\\\hline\end{array})"), cf[1, 1], cf[1, 2], cf[2, 1], cf[2, 2], cf[3, 1], cf[3, 2]) text(0.05, 18, tab, adj = c(0, 0.5), cex = 0.75) text(2.95, 1.2, r"($\varepsilon \sim \mathcal{N}(0, \sigma^2)$)", adj = c(1, 0), cex = 0.95, col = "#B22222") ## ----margin------------------------------------------------------------------- h <- latex_dims(r"($\frac{a}{b}$)", gp = gpar(fontsize = par("ps")))$height bp <- convertHeight(h, "bigpts", TRUE) # Lines of margin needed bp / (par("cin")[2] * 72 * par("mex")) ## ----off, include = FALSE----------------------------------------------------- latex_options(device_math = FALSE)