如何解决有没有办法将 HTML tab_model 对象转换为 R 对象,如 data.frame 或 tibble?
有没有办法将 HTML tab_model 对象转换为 R 对象,如 data.frame 或 tibble?
如果/何时可以,请告诉我。谢谢。
这是我用来创建 tab_model 对象的代码:
# 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)
解决方法
这样的事情怎么样:
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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。