Movatterモバイル変換


[0]ホーム

URL:


Colorizing equations and swapping variablenames

Particularly when teaching, it can be helpful to highlight specificpieces of the equation. With {equatiomatic}, we can do this as part ofthe equation extraction. For example, imagine we have a simple linearregression model, like this:

library(equatiomatic)data("penguins",package ="equatiomatic")slr<-lm(bill_length_mm~ body_mass_g,data = penguins)

We may want to start by highlighting the independent and dependentvariables. We can do this with colors. For example:

extract_eq(slr,var_colors =c(bill_length_mm ="cornflowerblue",body_mass_g    ="firebrick"  ))

\[{\color{cornflowerblue}{\operatorname{bill\_length\_mm}}} = \alpha +\beta_{1}({\color{firebrick}{\operatorname{body\_mass\_g}}}) + \epsilon\]

and then we can use those same colors later in text, as in “TheDependent Variable is the lengthof the penguins bill, which is predicted by theIndependent Variable, the body mass ofthe penguin.”

Note that we colorize the variables through thevar_colors= argument, which takes a named vector. The nameis equal to the variable you’d like to change the color of, and theelement is the actual color.

We could also take this further, to colorize the coefficients (theGreek notation). This argument structure is slightly different, takingeither a single color, or a vector of colors. The notation includesthree Greek characters, representing the model intercept, slope, andresidual variance. We can just colorize the intercept with code likethis:

extract_eq(slr,var_colors =c(bill_length_mm ="cornflowerblue",body_mass_g    ="firebrick"),greek_colors =c("#3bd100",rep("black",2)  ))

\[{\color{cornflowerblue}{\operatorname{bill\_length\_mm}}} ={\color{#3bd100}{\alpha}} +{\color{black}{\beta}}_{1}({\color{firebrick}{\operatorname{body\_mass\_g}}})+ {\color{black}{\epsilon}}\]

Or all three with something like:

greek_col<-c("#1b9e77","#d95f02","#7570b3")extract_eq(slr,var_colors =c(bill_length_mm ="cornflowerblue",body_mass_g    ="firebrick"),greek_colors = greek_col)

\[{\color{cornflowerblue}{\operatorname{bill\_length\_mm}}} ={\color{#1b9e77}{\alpha}} +{\color{#d95f02}{\beta}}_{1}({\color{firebrick}{\operatorname{body\_mass\_g}}})+ {\color{#7570b3}{\epsilon}}\]

Note that all of this works with more complicated models as well. Forexample, consider a model with an interaction. By coloring the variablenames, we can follow both the main effects and the interaction.

m_interaction<-lm(bill_length_mm~ body_mass_g* flipper_length_mm,data = penguins)extract_eq(m_interaction,var_colors =c(body_mass_g       ="#ffa91f",flipper_length_mm ="#00d1ab"),greek_colors =c("black","#3A21B3","#58A1D9","#FF7582","black"),wrap =TRUE,terms_per_line =3)

\[\begin{aligned}\operatorname{bill\_length\_mm} &= {\color{black}{\alpha}} +{\color{#3A21B3}{\beta}}_{1}({\color{#ffa91f}{\operatorname{body\_mass\_g}}})+{\color{#58A1D9}{\beta}}_{2}({\color{#00d1ab}{\operatorname{flipper\_length\_mm}}})\+ \\&\quad{\color{#FF7582}{\beta}}_{3}({\color{#ffa91f}{\operatorname{body\_mass\_g}}}\times {\color{#00d1ab}{\operatorname{flipper\_length\_mm}}}) +{\color{black}{\epsilon}}\end{aligned}\]

Here, we’re using two different shades of blue to denote the maineffects, and a pink color to denote the interaction. At the same time,we see how the variables combine. We can also change the subscripts.Perhaps we want to have them match the coefficients. The interface forthe subscripts is exactly the same as the subscripts—either a singlecolor or a vector of colors. One potentially confusing part of this,however, is that the colors still need to correspond to their positionin the equation. If the term does not have a subscript, you can fill thepositions with NA values or any other color (it won’t matter becausethere are not subscripts for those terms).

extract_eq(m_interaction,var_colors =c(body_mass_g       ="#ffa91f",flipper_length_mm ="#00d1ab"),greek_colors =c("black","#3A21B3","#58A1D9","#FF7582","black"),subscript_colors =c(NA_character_,"#3A21B3","#58A1D9","#FF7582",NA_character_),wrap =TRUE,terms_per_line =3)

\[\begin{aligned}\operatorname{bill\_length\_mm} &= {\color{black}{\alpha}} +{\color{#3A21B3}{\beta}}_{{\color{#3A21B3}{1}}}({\color{#ffa91f}{\operatorname{body\_mass\_g}}})+{\color{#58A1D9}{\beta}}_{{\color{#58A1D9}{2}}}({\color{#00d1ab}{\operatorname{flipper\_length\_mm}}})\+ \\&\quad{\color{#FF7582}{\beta}}_{{\color{#FF7582}{3}}}({\color{#ffa91f}{\operatorname{body\_mass\_g}}}\times {\color{#00d1ab}{\operatorname{flipper\_length\_mm}}}) +{\color{black}{\epsilon}}\end{aligned}\]

Again, we may want to use these colors in our explanation. Forexample

In the above model, both thebodymass and theflipper lengthof penguins are used to predict their bill length. We estimate themain effect of body mass, themain effect of flipper length, and theirinteraction. Theinteraction implies that the relationbetween body mass and bill lengthdepends upon flipper length.Or, equivalently, that the relation between flipper length and billlength depends upon body mass.

Finally, there’s is one additional means by which we can controlcolors. By default, {equatiomatic} handles categorical variables byputting the corresponding levels in subscripts (relative to thereference group, which is omitted). We can also change the color ofthese variable subscripts, with thevar_subscript_colorsargument.

m_categorical<-lm(bill_length_mm~ species+ island,data = penguins)extract_eq(m_categorical,var_colors =c(species ="#FB2C4B",island  ="#643B77"),var_subscript_colors =c(species ="#0274B2",island  ="#FBA640"))

\[\operatorname{bill\_length\_mm} = \alpha +\beta_{1}({\color{#FB2C4B}{\operatorname{species}}}{\color{#0274B2}{_{\operatorname{Chinstrap}}}})+\beta_{2}({\color{#FB2C4B}{\operatorname{species}}}{\color{#0274B2}{_{\operatorname{Gentoo}}}})+\beta_{3}({\color{#643B77}{\operatorname{island}}}{\color{#FBA640}{_{\operatorname{Dream}}}})+\beta_{4}({\color{#643B77}{\operatorname{island}}}{\color{#FBA640}{_{\operatorname{Torgersen}}}})+ \epsilon\]

Note that the colorization is at the variable level, not thesubscript level.

Changing variable names

To make the previous equations more human-readable, we might want tochange the variable names. We can do this through a similar interfacewhile still keeping the colors intact. For example, our interactionmodel might look something like this:

extract_eq(m_interaction,swap_var_names =c("bill_length_mm"="Bill Length [mm]","body_mass_g"="Body Mass [g])","flipper_length_mm"="Flipper Length [mm]"),var_colors =c(flipper_length_mm   ="firebrick",body_mass_g         ="cornflowerblue"),wrap =TRUE,terms_per_line =3)

\[\begin{aligned}\operatorname{Bill\ Length\ [mm]} &= \alpha +\beta_{1}({\color{cornflowerblue}{\operatorname{Body\ Mass\ [g])}}}) +\beta_{2}({\color{firebrick}{\operatorname{Flipper\ Length\ [mm]}}})\ +\\&\quad \beta_{3}({\color{cornflowerblue}{\operatorname{Body\ Mass\[g])}}} \times {\color{firebrick}{\operatorname{Flipper\ Length\[mm]}}}) + \epsilon\end{aligned}\]

You can similarly change the variable subscript names. Forexample:

extract_eq(m_categorical,swap_var_names =c("bill_length_mm"="Bill Length [mm]","species"="Species","island"="Island"),swap_subscript_names =c(Chinstrap        ="little buddy",Gentoo           ="happy feet"),var_colors =c(species          ="#FB2C4B",island           ="#643B77"),var_subscript_colors =c(species          ="#0274B2",island           ="#FBA640"),wrap =TRUE,terms_per_line =3)

\[\begin{aligned}\operatorname{Bill\ Length\ [mm]} &= \alpha +\beta_{1}({\color{#FB2C4B}{\operatorname{Species}}}{\color{#0274B2}{_{\operatorname{little\buddy}}}}) +\beta_{2}({\color{#FB2C4B}{\operatorname{Species}}}{\color{#0274B2}{_{\operatorname{happy\feet}}}})\ + \\&\quad\beta_{3}({\color{#643B77}{\operatorname{Island}}}{\color{#FBA640}{_{\operatorname{Dream}}}})+\beta_{4}({\color{#643B77}{\operatorname{Island}}}{\color{#FBA640}{_{\operatorname{Torgersen}}}})+ \epsilon\end{aligned}\]

Current models and future plans

Everything shown above is fully implemented in all model typeshandled by {equatiomatic} with the exception of mixed effect models(lme4::lmer() andlme4::glmer()) andtime-series models. For mixed effects models, colorization has beenpartially implemented—you can use the interface shown above to changethe color or names variables, as well as variable subscripts. However,Greek characters cannot be colored automatically at present. Thesemodels will be fully implemented in a future release.

Finally, you might have noticed that the number and length ofarguments to equations can become rather long. Because of this, we arecurrently considering moving to a piped interface. The last example maythen turn into something like:

create_eq(m_categorical)|>swap_var_names("bill_length_mm"="Bill Length [mm]","species"="Species","island"="Island"  )|>swap_subscript_names(Chinstrap        ="little buddy",Gentoo           ="happy feet"  )|>colorize_variables(species          ="#FB2C4B",island           ="#643B77"  )|>colorize_variable_subscripts(species          ="#0274B2",island           ="#FBA640"  )|>wrap(terms_per_line =3)

The length is perhaps not a whole lot less, but we think thislayering approach might make building up equations easier andmore intuitive, not unlike how you build-up a plot with{ggplot2}.

If you have any feedback on this, or other features, please don’thesitate to get in touch.


[8]ページ先頭

©2009-2025 Movatter.jp