modelsummary unexpected latex output - latex

With the following code I produce the laTex table in the image below. As you might notice there are a few things wrong with the output.
The title is missing
P-values in the wrong place
The footnote is misaligned
Any help is greatly appreciated!
library(tidyverse)
library(modelsummary)
library(gt)
data <- as.data.frame(ChickWeight)
mod_control <- lm(weight ~ Time , data = data)
mod_treat <- lm(weight ~ Time + Diet, data = data)
mod_one_list <- list(mod_control, mod_treat)
# coefmap
cm <- c("(Intercept)"="Konstant",
"Time" = "Tid",
"Num.Obs." = "n")
# gof_map
gm <- list(list(raw = "nobs", clean = "N", fmt = 0))
# title
tit <- "En beskrivning här"
# produce table
modelsummary(mod_one_list,
output = "gt",
stars = T,
title = tit,
coef_map = cm,
gof_map = gm,
vcov = "HC1") %>%
tab_spanner(label = '(1)', columns = 2) %>%
tab_spanner(label = "(2)", columns = 3) %>%
tab_footnote("För standardfel använder vi HC1",
locations = cells_body(rows = 1, columns = 2)) %>%
as_latex() %>%
cat()

This is an issue with the gt package. When adding both footnotes and
source notes (which is what modelsummary uses to report significance
stars), gt puts both types of notes in different mini-pages. This
breaks alignment in LaTeX.
You can see this by inspecting the code of this minimal example:
library(gt)
dat <- mtcars[1:4, 1:4]
gt(dat) |>
tab_source_note(source_note = "source note") |>
tab_footnote("footnote", locations = cells_body(rows = 1, columns = 2)) |>
as_latex() |>
cat()
## \captionsetup[table]{labelformat=empty,skip=1pt}
## \begin{longtable}{rrrr}
## \toprule
## mpg & cyl & disp & hp \\
## \midrule
## 21.0 & 6\textsuperscript{1} & 160 & 110 \\
## 21.0 & 6 & 160 & 110 \\
## 22.8 & 4 & 108 & 93 \\
## 21.4 & 6 & 258 & 110 \\
## \bottomrule
## \end{longtable}
## \vspace{-5mm}
## \begin{minipage}{\linewidth}
## \textsuperscript{1}footnote \\
## \end{minipage}
## \begin{minipage}{\linewidth}
## source note\\
## \end{minipage}
I am not sure if the gt maintainers would consider this a “bug”, but
it might be worth it to report it on their repository anyway:
https://github.com/rstudio/gt/issues
For what it’s worth, I think that the default LaTeX output with
modelsummary(model, output="latex") generally works better, because it
uses kableExtra, which seems to prioritize LaTeX a bit more.

Related

Create dummy variable for all neighborhood region depending on another dummy variable (sf object)

I have a spatial dataframe (sf) of all European NUTS2 regions. Within this sf object certain regions have a dummy = 1, and others a dummy = 0.
How can I create a new dummy (lets say "dummy_neighbor") where all the neighboring (st_touches?) regions of the regions with dummy==1 get a dummy_neighbor==1, and all those not touching a dummy==1 region get a dummy_neighbor==0?
For the time being I have this work-around. But I guess there musst be an easier solution?
# load packages
library(sf)
library(here)
library(tidyverse)
library(spdep)
library(expp)
# import nuts 2 sf object
nuts2_sf <- st_read(here("Data", "nuts2_data", "final_nuts.shp"))
# take row numbers as extra column for later
nuts2_sf$rownumber = 1:nrow(nuts2_sf)
#neighbouring list
neighbour <- poly2nb(nuts2_sf, row.names="NUTS_ID", queen=TRUE)
# transform nb into data frame
nb_df <- plyr::ldply(neighbour, rbind)
nb_df$rownumber = 1:nrow(nb_df) # get rownbumer as column
# merge neighbour-list-df with sf by rownumber
df <- merge(nuts2_sf, nb_df, by = "rownumber")
# extract all neighbours of tp100_d=1
# (tp100_d is the name of the original dummy variable)
df_dummy <- df %>% filter(tp100_d == 1)
df_dummy$geometry <- NULL
all_neighbours <- as.vector(as.matrix(df_dummy[,c(66:76)])) %>% unique(.)
# create new neighbourhood-dummy for all neighbours (but not if its a neighbour that has a 1 in its original dummy)
nuts2_sf <- nuts2_sf %>% mutate(nb_dummy = ifelse(rownumber %in% all_neighbours & tp100_d == 0, 1, 0))

Hide p_value and put stars to significant OR gtsummary

I'm using gtsummary package.
I need to merge different univariate logistic regression and in order to have a good presentation, I want to hide the p_value and bold or put a star to the significant OR (p< 0.05).
Anyone can help me?
Maybe it's easier to use another presentation type like kable, huxtable, I don't know?
Thank you for your help.
Have a nice day
There is a function called add_significance_stars() that hides the p-value and adds stars to the estimate indicating various levels of statistical significance. I've also added code to bold the estimate if significant with modify_table_styling().
library(gtsummary)
#> #BlackLivesMatter
packageVersion("gtsummary")
#> [1] '1.4.0'
tbl <-
trial %>%
select(death, age, grade) %>%
tbl_uvregression(
y = death,
method = glm,
method.args = list(family = binomial),
exponentiate = TRUE
) %>%
# add significance stars to sig estimates
add_significance_stars() %>%
# additioanlly bolding significant estimates
modify_table_styling(
columns = estimate,
rows = p.value < 0.05,
text_format = "bold"
)
Created on 2021-04-14 by the reprex package (v2.0.0)
Here's a quick huxtable version:
l1 <- glm(I(cyl==8) ~ gear, data = mtcars, family = binomial)
l2 <- glm(I(cyl==8) ~ carb, data = mtcars, family = binomial)
huxtable::huxreg(l1, l2, statistics = "nobs", bold_signif = 0.05)
────────────────────────────────────────────────────
(1) (2)
───────────────────────────────────
(Intercept) 5.999 * -1.880 *
(2.465) (0.902)
gear -1.736 *
(0.693)
carb 0.579 *
(0.293)
───────────────────────────────────
nobs 32 32
────────────────────────────────────────────────────
*** p < 0.001; ** p < 0.01; * p < 0.05.
Column names: names, model1, model2
It doesn't show it here, but the significant coefficients are bold on screen (and in any other kind of output).

Call a single cell of LaTeX Table

I would like to call a single value from a table inside a textbody.
% latex table generated in R 3.5.1 by xtable 1.8-3 package
% Wed Dec 12 13:56:07 2018
\begin{table}[ht]
\centering
\begin{tabular}{rrrrr}
\hline
& Estimate & Std. Error & z value & Pr($>$$|$z$|$) \\
\hline
(Intercept) & -1.819 & 0.926 & -1.964 & 0.050 \\
hiveH2 & 2.418 & 0.951 & 2.542 & 0.011 \\
nectar+ & 0.827 & 0.947 & 0.873 & 0.383 \\
\hline
\end{tabular}
\label{tab:x}
\end{table}
I would like to be able to write something along the line:
The z value of Hive~2 is \call{tab:x{z value}{hiveH2}}.
That should compile as:
The z value of Hive 2 is 2.542.
the LaTeX table was generated with a R script (neither R markdown nor R sweave):
library(lme4)
library(xtable)
data <- matrix(c("H1","H1","H2","H2","H1","H1","H2","H2","H1","H1","H2","H2","H1","H1","H2","H2","H1","H1","H2","H2","H1","H1","H2","H2","H1","H1","H2","H2",
"+","+","-","-","+","+","-","-","+","+","-","-","+","+","-","-","-","-","+","+","-","-","+","+","-","-","+","+",
0.00000000,0.00000000,0.25000000,0.00000000,0.00000000,0.00000000,0.00000000,0.10000000,0.20000000,0.00000000,0.10000000,0.00000000,0.00000000,0.00000000,0.00000000,0.09090909,0.09090909,0.00000000,0.41666667,0.08333333,0.00000000,0.14285714,0.85714286,0.61538462,0.00000000,0.00000000,0.25000000,1.10000000),
nrow=28, ncol=3)
data <- as.data.frame(data)
colnames(data) <- c("hive", "nectar", "severity")
glm <- glm(severity ~ hive + nectar, data = data, family = binomial)
summary.glm <- summary(glm)
summary.glm
print(xtable(summary.glm, type = "latex", caption.placement = getOption("xtable.caption.placement", "bottom"),
digits = 3,
label = "tab:x",
table.placement = getOption("xtable.table.placement", "h")),
file = "test.tex")

LaTeX formula with columns

Based on info from this source - http://en.wikibooks.org/wiki/LaTeX/Advanced_Mathematics.
I tried to have 3 columns in equation:
<math>
\begin{align}
f(x) & = h(&x, y)\\
& = G(&x, y,\\
& &z)
\end{align}
</math>
to have x and z aligned, but it is not aligned...
I'm rying with wikipedia sandbox.
You should use an array to properly achieve multiple alignment points. Also, some spacing adjustments (\! is a negative math space) make the display look better:
<math>
\begin{array}{rll}
f(x) &\!\!\! = h(&\!\!\!\! x, y) \\
&\!\!\! = G(&\!\!\!\! x, y, \\
&\!\!\! &\!\!\!\! z)
\end{array}
</math>

Left align block of equations

I want to left align a block of equations. The equations in the block itself are aligned, but that's not related at all to my question! I want to left align the equations rather than have them centered all the time, because it looks dumb with narrow centered equations.
Example, I want to left align this
\begin{align*}
|\vec a| &= \sqrt{3^{2}+1^{2}} = \sqrt{10} \\
|\vec b| &= \sqrt{1^{2}+23^{2}} = \sqrt{530} \\
\cos v &= \frac{26}{\sqrt{10} \cdot \sqrt{530}} \\
v &= \cos^{-1} \left(\frac{26}{\sqrt{10} \cdot \sqrt{530}}\right) \\
v &= \uuline{69.08...\degree}
\end{align*}
but also this
\begin{align*}
f(x) = -1.25x^{2} + 1.5x
\end{align*}
How is this done? If it's even possible.
Try to use the fleqn document class option.
\documentclass[fleqn]{article}
(See also http://en.wikibooks.org/wiki/LaTeX/Basics for a list of other options.)
You can use \begin{flalign}, like the example bellow:
\begin{flalign}
&f(x) = -1.25x^{2} + 1.5x&
\end{flalign}
Try this:
\begin{flalign*}
&|\vec a| = \sqrt{3^{2}+1^{2}} = \sqrt{10} & \\
&|\vec b| = \sqrt{1^{2}+23^{2}} = \sqrt{530} &\\
&\cos v = \frac{26}{\sqrt{10} \cdot \sqrt{530}} &\\
&v = \cos^{-1} \left(\frac{26}{\sqrt{10} \cdot \sqrt{530}}\right) &\\
\end{flalign*}
The & sign separates two columns, so an & at the beginning of a line means that the line starts with a blank column.
The fleqn option in the document class will apply left aligning setting in all equations of the document. You can instead use \begin{flalign}. This will align only the desired equations.

Resources