【问题标题】:Table with customs stars and standard error BELOW coefficient estimated带有海关星级和标准误差的表低于估计的系数
【发布时间】:2021-05-26 00:34:08
【问题描述】:

我正在手动生成一个模型汇总表,其中包括参数估计值、标准误差和自定义星号。不幸的是,我希望标准误差 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)

【问题讨论】:

    标签: r latex linear-regression statsmodels


    【解决方案1】:

    这是一个选项,我们将matrix 转换为data.frame,将row.names 作为列,转为长格式,replace 'Name' 中的duplicated 元素为空白(@987654325 @)

    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) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多