Skip to content

latexMatrix vignette examples #57

Closed

Description

@friendly

The vignette is coming along nicely. It would be nice to add one or two more substantive examples.
What comes to mind is an example of a linear hypothesis, $\mathbf{C} \mathbf{B} = \mathbf{0}$, but
spelled out using latexMatrix().

Here's one I worked out by hand before we did the symbolic stuff: A MLM with three $y$ s and 3 $x$ s,
testing whether neither $x_2$ and $x_3$ contribute

image

My manual code:

\begin{eqnarray*}
\mathcal{H}_0 : \mathbf{C} \mathbf{B} & = 
\begin{bmatrix}
0 & 1 & 1 & 0
\end{bmatrix}
\begin{pmatrix}
	\beta_{0,y_1} & \beta_{0,y_2} & \beta_{0,y_3} \\ 
	\beta_{1,y_1} & \beta_{1,y_2} & \beta_{1,y_3} \\ 
	\beta_{2,y_1} & \beta_{2,y_2} & \beta_{2,y_3} \\ 
	\beta_{3,y_1} & \beta_{3,y_2} & \beta_{3,y_3} 
\end{pmatrix} \\ \\
& = 
\begin{bmatrix}
	\beta_{1,y_1} & \beta_{1,y_2} & \beta_{1,y_3} \\ 
	\beta_{2,y_1} & \beta_{2,y_2} & \beta_{2,y_3} \\ 
\end{bmatrix}
= 
\mathbf{0}_{(2 \times 3)}
\end{eqnarray*}

What's the best way to do something like this now? Don't really need the double subscripts.

Activity

  1. philchalmers commented on Aug 20, 2024

    @philchalmers
    Collaborator

    I had to push a prefix.col and prefix.row arg to latexMatrix(), but this will do it:

    Eqn("\\mathcal{H}_0 : \\mathbf{C} \\mathbf{B} & = ",
        latexMatrix(matrix(c(0,1,1,0), nrow=1)),
        latexMatrix('\\beta', ncol = 3, nrow=4, comma=TRUE, prefix.col = 'y_'),
        Eqn_newline(), Eqn_newline(), 
        latexMatrix('\\beta', ncol = 3, nrow=2, comma=TRUE, prefix.col = 'y_', lhs = '&' ),
        "= \\mathbf{0}_{(2 \\times 3)}", 
        align=TRUE)
    
  2. friendly commented on Aug 20, 2024

    @friendly
    OwnerAuthor

    Beautiful! Thanks so much.

    I wonder if this might be of enough general use for a linear_hypothesis() function, at least for the C B = part,
    also giving the result of C %*% B?

  3. john-d-fox commented on Aug 20, 2024

    @john-d-fox
    Collaborator
  4. friendly commented on Aug 20, 2024

    @friendly
    OwnerAuthor

    I fleshed out this example in the vignette. However there's a slight problem with the intercept in terms of with how the context is described, and what can be represented in this form using latexMatrix.
    Note in my manual example, I included the intercept as row 1 of B.

    I simplified Phil's example a bit for sake of exposition:

    C <- latexMatrix(matrix(c(0,1,1,0), nrow=1), matrix = "bmatrix")
    B <- latexMatrix('\\beta', ncol = 3, nrow=4,   comma=TRUE, prefix.col = 'y_')
    B0 <- latexMatrix('\\beta', ncol = 3, nrow=2, comma=TRUE, prefix.col = 'y_')
    
    Eqn("\\mathcal{H}_0 : \\mathbf{C} \\mathbf{B} & = ",
        C, B,
        Eqn_newline(), Eqn_newline(), 
        '&\n',
        B0,
        "= \\mathbf{0}_{(2 \\times 3)}", 
        align=TRUE)
    

    Which typesets as:

    image

    But, if I try to start the rows of \beta at zero I get an error:

    > B <- latexMatrix('\\beta', ncol = 3, nrow=4, 
    +                  zero.based = c(TRUE, FALSE),
    +                  comma=TRUE, prefix.col = 'y_')
    Error in latexMatrix("\\beta", ncol = 3, nrow = 4, zero.based = c(TRUE,  : 
      zero-based indexing not available for numeric 'nrow'
    

    I don't want to push this too far to require more mods or extra arguments for the sake of this example, but I don't see why numeric rows/cols can't be zero-based.

    A less-than-general alternative is to simplify the description of a linear hypothesis by ignoring the intercept.

  5. john-d-fox commented on Aug 20, 2024

    @john-d-fox
    Collaborator
  6. friendly commented on Aug 20, 2024

    @friendly
    OwnerAuthor

    I'll try, but we're going out for the aft. here.

  7. philchalmers commented on Aug 20, 2024

    @philchalmers
    Collaborator

    Hi, I think that prefix.row and prefix.col are nice additions. I'd put them below suffix because it seem sensible to me for the prefix and suffix args to be together. Also, maybe we should get rid of the lhs argument. Can't what it does be done directly in Eqn()? John

    I agree with removal of the lhs argument. Using it in longer, more structured presentation feels a little awkward.

  8. john-d-fox commented on Aug 20, 2024

    @john-d-fox
    Collaborator
  9. john-d-fox commented on Aug 20, 2024

    @john-d-fox
    Collaborator
  10. friendly commented on Aug 20, 2024

    @friendly
    OwnerAuthor

    I suppose that what was intended by removing the lhs arg is just to specify
    one as one of the ... args to Eqn(), as in

    Eqn("\\mathbf{X} =",
         latexMatrix())
    

    I note that Eqn_hspace() has a lhs arg, but I don't quite understand what is intended.

    The other functions documented with Eqn() should be at least mentioned in the Description

  11. philchalmers commented on Aug 21, 2024

    @philchalmers
    Collaborator

    Hi Michael,

    Eqn_hspace() can be used to add general spaces uses LaTeX convention or apply symmetric spacing around a middle point, most often symbols such as '='.

    # add white space
    > Eqn_hspace(3)
    [1] "\\;"
    
    # add symmetric space around =
    > Eqn_hspace(3, '=')
    [1] "\\;=\\;"
    
    # add asymmetric space
    > Eqn_hspace(3, '=', 5)
    [1] "\\;=\\quad"
    
  12. john-d-fox commented on Aug 21, 2024

    @john-d-fox
    Collaborator

    A thought: Rather than using functions to add things like a newline, might it be more natural to export a variable -- something like Eqn_newline with contents ' \\\\ \n' instead?

  13. philchalmers commented on Aug 21, 2024

    @philchalmers
    Collaborator
  14. friendly commented on Aug 21, 2024

    @friendly
    OwnerAuthor

    @john-d-fox The change to allow zero-based matrices with numeric dimensions works perfectly for my example, and is useful generally. Does it screw up anything with the dim slot?

  15. john-d-fox commented on Aug 21, 2024

    @john-d-fox
    Collaborator
  16. friendly commented on Aug 22, 2024

    @friendly
    OwnerAuthor

    That seems reasonable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions