1

I am writing a bookdown document where I want to hold the positions of figures and tables. This works properly most of the time, except when using modelsummary.

I tried to create a working example. My document looks like the following (except with ''' instead of the usual sign to initiate code - I don't know if there is a better way to illustrate this)

---
title: "Example:  \nExample 123"
subtitle: "Working Paper"
author: "Me"
output: 
  bookdown::pdf_document2:
    fig_caption: yes
    df_print: paged
    keep_md: true
bibliography: library.bib
floatsintext: yes
header-includes:  \usepackage{xcolor} \usepackage{float}
linestretch: 1.5
graphics: yes
---

'''{r packages, echo = FALSE, results = FALSE, message = FALSE, warning = FALSE}
library(tidyverse)
library(kableExtra)
library(sf)
library(expss)
library(modelsummary)
library(fixest)
'''

# Introduction

This is my introduction.


# Analysis

This is my analysis:

## First part

Here is my first part

## Second part

Here is my second part

''' {r regression1, fig.cap = "Regression 1", echo = F, fig.pos = "H", fig.align='center'}

regression1 <- feols(mpg ~ cyl | drat, mtcars) 


modelsummary(regression1, coef_omit = "Int", stars = c("***" = 0.01, "**" = 0.05, "*" = 0.1), gof_omit = "IC$|Lik.|RMSE|Std.Errors", gof_map = tribble(
  ~raw,        ~clean,          ~fmt,
  "nobs",      "N",             0,
  "r.squared", "R2",            2,
  "adj.r.squared", "R2 Adj.", 2,
  "r2.within",                 "R2 Within",          2,
  "r2.within.adjusted",        "R2 Within Adj.",     2
  ), title = "Regression 1") %>% footnote(general = "ABC", threeparttable = TRUE)
'''

## Third part

The analysis continues.


''' {r regression2, out.extra='', fig.cap = "Regression 2", echo = F, fig.pos = "H", fig.align='center'}


regression2 <- feols(mpg ~ cyl | csw(vs + am, vs^am), mtcars) 

names(regression2) <- c("Model 1", "Model 2")

modelsummary(regression2, coef_omit = "Int", stars = c("***" = 0.01, "**" = 0.05, "*" = 0.1), gof_omit = "IC$|Lik.|RMSE|Std.Errors", coef_rename = c("mpg" = "MPG", "cyl" = "CYL"), gof_map = tribble(
  ~raw,        ~clean,          ~fmt,
  "nobs",      "N",             0,
  "FE: country", "Country fixed effects", 0,
  "FE: year", "Year fixed effects", 0,
  "FE: country^year", "Country-year fixed effects", 0,
  "r.squared", "R2",            2,
  "adj.r.squared", "R2 Adj.", 2,
  "r2.within",                 "R2 Within",          2,
  "r2.within.adjusted",        "R2 Within Adj.",     2
  ), title = "Regression 2") %>% footnote(general = "ABC", threeparttable = TRUE)
'''

In the PDF, the table of the second regression is shown under section 2.2 - Second part and not under 2.3. - Third part, where it is placed in the markdown document. This does not happen with other figures and tables. How do I hold the modelsummary table positions?

Anton
  • 254
  • 1
  • 9

1 Answers1

1

I found a solution: set the modelsummary output to output = "kableExtra" (which might not even be necessary) and add %>% kable_styling(latex_options = c("HOLD_position")) at the end of the code.

Anton
  • 254
  • 1
  • 9
  • Thanks for doing the research and sharing your solution! The tables in `modelsummary` are actually drawn by one of the supported external packages: `kableExtra` or `gt` or `huxtable` or `flextable`. For questions about appearance and positioning, it is often useful to refer to those packages' documentation. You can eventually accept your own answer if you don't want people to read this question thinking there is no available solution. – Vincent Jul 06 '22 at 19:04
  • 1
    Thank you for your comment and the hints! I accepted my own answer. – Anton Jul 07 '22 at 09:21