Skip to content

Commit

Permalink
flesh out dev/under-over.R tests
Browse files Browse the repository at this point in the history
  • Loading branch information
friendly committed Nov 20, 2024
1 parent 8ff1307 commit c1d2c90
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 37 deletions.
22 changes: 17 additions & 5 deletions dev/Eqn_test_quarto.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "Eqn() and ref() test with Quarto"
title: "Using Eqn() and ref() test with Quarto"
engine: knitr
---

Expand All @@ -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:
Expand All @@ -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'
Expand Down Expand Up @@ -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).
Expand All @@ -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)
```
Binary file added dev/SSP-decomp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dev/hat-matrix-underbrace.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
89 changes: 57 additions & 32 deletions dev/under-over.R
Original file line number Diff line number Diff line change
Expand Up @@ -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}}"))

}

0 comments on commit c1d2c90

Please sign in to comment.