Table 海关星级和标准误差低于系数估计
Table with customs stars and standard error BELOW coefficient estimated
我正在手动生成模型摘要 table,其中包括参数估计值、标准误差和自定义星级。不幸的是,我想要标准误差 BELOW 参数估计而不是 NEXT TO 它。我的最终目标是将其导出到 LaTeX。现在我正在使用 xtable()
来做。
请仔细考虑以下示例以及相应的所需输出。
示例结构
#example betas and sd
b<- structure(c(1.5, 3.5, 1.4), .Dim = c(3L,1L), .Dimnames = list(c("beta_x1", "beta_x2","beta_x3"), "Parameters"))
sd<- structure(c(0.02, 15.0, 1.025), .Dim = c(3L,1L), .Dimnames = list(c("se.beta_x1", "se.beta_x2","se.beta_x3"), "Sd"))
# example p-values
p_val <- as.numeric(pt(q = abs(b/sd),d= 1000 , lower=FALSE))
star_fn <- function(x){
out <- ifelse(x <= 0.1, ifelse(x <= 0.05, ifelse(x <= 0.01, "***", "**"), '*'), "")
out
}
# Generating Stars
stars <- star_fn(p_val)
# Table (to LaTeX)
tab<- cbind(Coef = format(b,digits = 3),sd = paste("(", sprintf("%.3f", sd), ")", sep=""), Stars =stars)
tab
Parameters sd Stars
beta_x1 "1.5" "(0.020)" "***"
beta_x2 "3.5" "(15.000)" ""
beta_x3 "1.4" "(1.025)" "*"
library(xtable)
xtable(tab)
期望的输出
structure(list(Name = c("beta_x1", " ", "beta_x2", " ", "beta_x3",
" "), Coef = c("1.5***", "(0.020)", "3.5", "(15.000)", "1.4*",
"(1.025)")), row.names = c(NA, -6L), class = "data.frame")
Name Coef
1 beta_x1 1.5***
2 (0.020)
3 beta_x2 3.5
4 (15.000)
5 beta_x3 1.4*
6 (1.025)
xtable(tab)
这里有一个选项,我们将 matrix
转换为 data.frame
,将 row.names 作为列,转为长格式,replace
duplicated
'Name' 中的元素空白 (''
)
library(dplyr)
library(tidyr)
library(tibble)
tab %>%
as.data.frame %>%
rownames_to_column('Name') %>%
unite(Coef, Parameters, Stars, sep="") %>%
pivot_longer(cols = -Name, values_to = 'Coef') %>%
select(-name) %>%
mutate(Name = replace(Name, duplicated(Name), ""))
-输出
# A tibble: 6 x 2
# Name Coef
# <chr> <chr>
#1 "beta_x1" 1.5***
#2 "" (0.020)
#3 "beta_x2" 3.5
#4 "" (15.000)
#5 "beta_x3" 1.4*
#6 "" (1.025)
我正在手动生成模型摘要 table,其中包括参数估计值、标准误差和自定义星级。不幸的是,我想要标准误差 BELOW 参数估计而不是 NEXT TO 它。我的最终目标是将其导出到 LaTeX。现在我正在使用 xtable()
来做。
请仔细考虑以下示例以及相应的所需输出。
示例结构
#example betas and sd
b<- structure(c(1.5, 3.5, 1.4), .Dim = c(3L,1L), .Dimnames = list(c("beta_x1", "beta_x2","beta_x3"), "Parameters"))
sd<- structure(c(0.02, 15.0, 1.025), .Dim = c(3L,1L), .Dimnames = list(c("se.beta_x1", "se.beta_x2","se.beta_x3"), "Sd"))
# example p-values
p_val <- as.numeric(pt(q = abs(b/sd),d= 1000 , lower=FALSE))
star_fn <- function(x){
out <- ifelse(x <= 0.1, ifelse(x <= 0.05, ifelse(x <= 0.01, "***", "**"), '*'), "")
out
}
# Generating Stars
stars <- star_fn(p_val)
# Table (to LaTeX)
tab<- cbind(Coef = format(b,digits = 3),sd = paste("(", sprintf("%.3f", sd), ")", sep=""), Stars =stars)
tab
Parameters sd Stars
beta_x1 "1.5" "(0.020)" "***"
beta_x2 "3.5" "(15.000)" ""
beta_x3 "1.4" "(1.025)" "*"
library(xtable)
xtable(tab)
期望的输出
structure(list(Name = c("beta_x1", " ", "beta_x2", " ", "beta_x3",
" "), Coef = c("1.5***", "(0.020)", "3.5", "(15.000)", "1.4*",
"(1.025)")), row.names = c(NA, -6L), class = "data.frame")
Name Coef
1 beta_x1 1.5***
2 (0.020)
3 beta_x2 3.5
4 (15.000)
5 beta_x3 1.4*
6 (1.025)
xtable(tab)
这里有一个选项,我们将 matrix
转换为 data.frame
,将 row.names 作为列,转为长格式,replace
duplicated
'Name' 中的元素空白 (''
)
library(dplyr)
library(tidyr)
library(tibble)
tab %>%
as.data.frame %>%
rownames_to_column('Name') %>%
unite(Coef, Parameters, Stars, sep="") %>%
pivot_longer(cols = -Name, values_to = 'Coef') %>%
select(-name) %>%
mutate(Name = replace(Name, duplicated(Name), ""))
-输出
# A tibble: 6 x 2
# Name Coef
# <chr> <chr>
#1 "beta_x1" 1.5***
#2 "" (0.020)
#3 "beta_x2" 3.5
#4 "" (15.000)
#5 "beta_x3" 1.4*
#6 "" (1.025)