Search code examples
rdataframetransformsjplot

Is there a way to convert the HTML tab_model object into an R object, like a data.frame or tibble?


Is there a way to convert the HTML tab_model object into an R object, like a data.frame or tibble?

Please let me know if/when you can. Thanks.



Here is the code I used to create the tab_model object:




# Loads packages
# ---- NOTE: making plots and diamonds dataset
if(!require(ggplot2)){install.packages("ggplot2")}
# ---- NOTE: run mixed effects models
if(!require(lme4)){install.packages("lme4")}
# ---- NOTE: for data wrangling
if(!require(dplyr)){install.packages("dplyr")}
# ---- NOTE: for sjPlot command
if(!require(sjPlot)){install.packages("sjPlot")}

# dataset creation

## for dataset with top 300 rows
# ---- NOTE: selects only the top 300 rows of the dataset
diamonds_top300 <- data.frame(dplyr::top_n(ggplot2::diamonds, 300, y))
# ---- NOTE: gives dataset info
head(diamonds_top300)
str(diamonds_top300)
colnames(diamonds_top300)
nrow(diamonds_top300)

# model creation

## DV price for diamonds_top300, using poisson family
(
  mlm_top300_poisson_price <- 
    lme4::glmer(
      price ~ cut + color + carat + (1 | clarity) + (1 | depth),
      data = diamonds_top300,
      family = poisson()
    )
)

# creates tab_model object

## tab_model of mlm_top300_poisson_price
tab_model_mlm_top300_poisson_price <- 
  sjPlot::tab_model(mlm_top300_poisson_price)




Solution

  • What about something like this:

     library(XML)
     # first you parse the html, then you put it as table, taking the first
    df <- data.frame(readHTMLTable(htmlParse(tab_model_mlm_top300_poisson_price))[1])
    # first row as colnames
    colnames(df) <- df[1,]
    # remove the fake first row
    df <- df[-1,]
    
     df
                         Predictors Incidence Rate Ratios                CI      p
    2                   (Intercept)               6754.63 5410.16 – 8433.23 <0.001
    3                  cut [linear]                  1.08       1.07 – 1.09 <0.001
    4               cut [quadratic]                  0.95       0.94 – 0.96 <0.001
    5                   cut [cubic]                  1.04       1.04 – 1.05 <0.001
    6              cut [4th degree]                  1.04       1.04 – 1.05 <0.001
    7                color [linear]                  0.94       0.93 – 0.95 <0.001
    8             color [quadratic]                  0.97       0.97 – 0.98 <0.001
    9                 color [cubic]                  0.92       0.91 – 0.92 <0.001
    10           color [4th degree]                  1.12       1.11 – 1.12 <0.001
    11           color [5th degree]                  0.88       0.87 – 0.88 <0.001
    12           color [6th degree]                  1.05       1.04 – 1.05 <0.001
    13                        carat                  1.43       1.42 – 1.44 <0.001
    14               Random Effects                  <NA>              <NA>   <NA>
    15                           s2                  0.00              <NA>   <NA>
    16                     t00depth                  0.01              <NA>   <NA>
    17                   t00clarity                  0.08              <NA>   <NA>
    18                          ICC                  1.00              <NA>   <NA>
    19                    N clarity                     6              <NA>   <NA>
    20                      N depth                    83              <NA>   <NA>
    21                 Observations                   305              <NA>   <NA>
    22 Marginal R2 / Conditional R2         0.183 / 0.999              <NA>   <NA>