diff --git a/dev/Eqn_test_quarto.qmd b/dev/Eqn_test_quarto.qmd index 8068c97..0da3f97 100644 --- a/dev/Eqn_test_quarto.qmd +++ b/dev/Eqn_test_quarto.qmd @@ -1,5 +1,5 @@ --- -title: "Eqn() and ref() test with Quarto" +title: "Using Eqn() and ref() test with Quarto" engine: knitr --- @@ -8,13 +8,13 @@ engine: knitr library(matlib) ``` -Standard equation and reference +A standard equation and reference, as a displayed equation in Quarto: $$ f\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k} $$ {#eq-binom} -See @eq-binom, and [-@eq-binom] +This can be referenced as: See @eq-binom, and [-@eq-binom] Syntax generated with `Eqn()`. Raw output: @@ -23,7 +23,7 @@ Eqn("f\\left(k\\right) = \\binom{n}{k} p^k\\left(1-p\\right)^{n-k}", quarto=TRUE) ``` -Rendered. +Rendered: ```{r} #| results: 'asis' @@ -51,7 +51,7 @@ Will also work within an equation, not that this is particularly useful. "@eq-binom" ``` -# Auto use `quarto=TRUE` if .qmd file used +## Auto use `quarto=TRUE` if .qmd file used Internally, set global to TRUE if .qmd file detected. Error raised if files have the same name but different extension (`Eqn.qmd` and `Eqn.Rmd` will raise error if in same dir). @@ -63,3 +63,15 @@ Eqn("f\\left(k\\right) = \\binom{n}{k} p^k\\left(1-p\\right)^{n-k}", label = 'eq See `{r} ref("eq-auto", quarto=TRUE)` for auto-detected quarto label. Of course, the @ notation still works too; e.g., see @eq-auto for details + +## A more complex example + +Here, we illustrate the decomposition of sums of squares and product (SSP) matrices in a simple one-way MANOVA design. +```{r} +data(dogfood, package = "heplots") +str(dogfood) + +#fit the model +dogfood.mod <- lm(cbind(start, amount) ~ formula, data=dogfood) +``` + diff --git a/dev/SSP-decomp.png b/dev/SSP-decomp.png new file mode 100644 index 0000000..203ad0c Binary files /dev/null and b/dev/SSP-decomp.png differ diff --git a/dev/hat-matrix-underbrace.png b/dev/hat-matrix-underbrace.png new file mode 100644 index 0000000..b2f559d Binary files /dev/null and b/dev/hat-matrix-underbrace.png differ diff --git a/dev/under-over.R b/dev/under-over.R index a3159ad..9348071 100644 --- a/dev/under-over.R +++ b/dev/under-over.R @@ -24,51 +24,76 @@ underbrace <- function(x, label=NULL){ } if (FALSE) { - X <- latexMatrix("x", 2,2) - SSP_E <- latexMatrix("y", 2,2) - - - Eqn(X + SSP_E) - - # do overbrace directly - Eqn("\\overset{\\mathbf{X}}{", X, "}") - - # show X + SSP_E = (X+SSP_E) - - Eqn("\\overset{\\mathbf{X}}{", X, "}", - "+", - "\\overset{\\mathbf{SSP_E}}{", SSP_E, "}", - "=", - "\\overset{\\mathbf{X + SSP_E}}{", X+SSP_E, "}", - ) - - # but these don't work - Eqn(overset(X, "\\mathbf{X}")) - Eqn(overset(SSP_E, "\\mathbf{SSP_E}")) - data(dogfood, package = "heplots") dogfood.mod <- lm(cbind(start, amount) ~ formula, data=dogfood) dogfood.aov <- car::Anova(dogfood.mod) - SSP_H <- dogfood.aov$SSP - SSP_E <- dogfood.aov$SSPE - SSP_T <- SSP_H$formula + SSP_E - # make row/colnames disappear - rownames(SSP_T) <- rownames(SSP_H) <- rownames(SSP_E) <- NULL - colnames(SSP_T) <- colnames(SSP_H) <- colnames(SSP_E) <- NULL - + SSP_H <- dogfood.aov$SSP[["formula"]] |> round(digits = 2) + SSP_E <- dogfood.aov$SSPE |> round(digits = 2) + SSP_T <- SSP_H + SSP_E + + # make row/colnames disappear + options(print.latexMatrix = list(display.labels=FALSE)) + # do overbrace directly: WORKS Eqn("\\overset{\\mathbf{SSP}_T}{", SSP_T, "}") + # but these don't work: + Eqn(overset(SSP_H, "\\mathbf{SSP_H}")) + Eqn(overset(SSP_E, "\\mathbf{SSP_E}")) + + # use latexMatrix() + H <- latexMatrix(SSP_H) + Eqn(overset(H, "\\mathbf{SSP_H}")) + # show SSP_H + SSP_E = (SSP_H+SSP_E) + # What I want to get here: + eqn <- " +\begin{equation*} +\overset{\mathbf{SSP}_T} + {\begin{pmatrix} + 35.4 & -59.2 \\ + -59.2 & 975.9 \\ + \end{pmatrix}} += +\overset{\mathbf{SSP}_H} + {\begin{pmatrix} + 9.69 & -70.94 \\ + -70.94 & 585.69 \\ + \end{pmatrix}} ++ +\overset{\mathbf{SSP}_E} + {\begin{pmatrix} + 25.8 & 11.8 \\ + 11.8 & 390.3 \\ + \end{pmatrix}} +\end{equation*} + " - Eqn("\\overset{\\mathbf{SSP_H}}{", SSP_H, "}", + # this generates an error: + # argument is missing, with no default + Eqn("\\overset{\\mathbf{SSP_T}}{", SSP_T, "}", "+", - "\\overset{\\mathbf{SSP_E}}{", SSP_E, "}", + "\\overset{\\mathbf{SSP_H}}{", SSP_H, "}", "=", - "\\overset{\\mathbf{SSP_T}}{", SSP_T, "}", + "\\overset{\\mathbf{SSP_E}}{", SSP_E, "}", ) + ## -------Underbrace: + # I want to generate the equation \hat{y} = X (X'X)^{-1} X' y with a brace underneath showing the H matrix + # This manual LaTeX works: + eqn <- " + \mathbf{\hat{y}} + = \underbrace{\mathbf{X}(\mathbf{X}^{\top}\mathbf{X})^{-1}\mathbf{X}^{\top}}_\mathbf{H}\mathbf{y} + " + # generate this with underbrace() + H <- "\\mathbf{X}(\\mathbf{X}^{\\top}\\mathbf{X})^{-1}\\mathbf{X}^{\\top}" + underbrace(H, "\\mathbf{H}") + # Do this with Eqn() - WORKS, at least when all the elements are just symbols, not matrices + Eqn("\\mathbf{\\hat{y}} =", underbrace(H, "\\mathbf{H}"), "\\mathbf{y}") + + # We can even combine this with overbrace + Eqn(overbrace(underbrace(H, "\\mathbf{H}"), "\\mathbf{\\hat{y}}")) }