从分位数回归 rqpd 中提取系数和 t 值

Extract coefficients and t-values from quantile regression, rqpd

我有一个分位数回归模型如下:

rqpdfit <- rqpd(cr1 ~ x1 + x2 + x3 + x4 + x5 + x6 +x7|id, 
panel(lambda = 1,taus=c(0.1, 0.25, 0.5, 0.75, 0.9),tauw=rep(1/5, 5), method="pfe"),
 data =pdata)

为了产生输出 table,我按分位数划分了结果。

cf_10<- summary(rqpdfit)$coefficients[1:8,]

这是第一个分位数输出:

                             Value   Std. Error     t value     Pr(>|t|)                
      (Intercept)[0.1] 14.4864410152 11.670505897  1.24128647 2.145773e-01
    > x1[0.1]          -1.1081682804  1.230039754 -0.90092070 3.676881e-01
    > x2[0.1]           0.5036482698  0.097472484  5.16708152 2.501200e-07
    > x3[0.1]          -0.8077127317  0.282774725 -2.85638234 4.308473e-03
    > x4[0.1]           0.0006560821  0.008695294  0.07545255 9.398587e-01
    > x5[0.1]          -0.0102064486  0.043276674 -0.23584180 8.135683e-01
    > x6[0.1]          -0.1081589250  0.061636404 -1.75478966 7.937665e-02
    > x7[0.1]          -0.7891778648  0.251492587 -3.13797665 1.714334e-03

但是,我不知道如何提取矩阵中的系数和 t 值(如果可能,使用重要的星号)。

谢谢

您可以检查您正在使用的(未命名的)包是否适用于 stargazer,否则,获得星星将不会自动进行。您通常可以使用 summary().

提取此信息
# coefficients and t-values in matrix
cf_10 <- summary(x)$coefficients[1:8,c(1,3)]
# p-values
cf_10.pVals <- summary(x)$coefficients[1:8,4]

如果你真的想要,你可以实现一个算法来将星星粘贴到系数上。

可能有更漂亮的方法,但这应该相当容易阅读:

# function to print stars
starPrinter <- function(pVal) {
  if(pVal < 0.01) return("***")
  if(pVal < 0.05) return("**")
  if(pVal < 0.1) return("*")

  return("")
}

# a matrix, with digits rounded to 3rd decimal
myTable <- round(cf_10, 3)
# get stars and put tvalues in parentheses
for(i in 1:nrow(myTable)) {
  myTable[i, 1] <- paste0(myTable[i, 1], starPrinter(cf_10.pVals[i]))
  myTable[i, 2] <- paste0("(", myTable[i, 2], ")")
}

如果你想把它变成一个向量,你有 coef1, tval1, coef2, tval2...

myEstVector.cf_10 <- as.character(t(myTable))

然后您可以将多个结果组合成一个新的 table:

myNewTable <- cbind(myEstVector.cf_10, myEstVector.cf_25, myEstVector.cf_99)

只是想添加到上面的线程。我按照上面的代码将结果导出为 csv 文件格式。

# y is the dependent variable 
# x6 is a vector of 6 independent variables. 
#setting regression
r6.form <- y ~ x6 |as.factor(cty)
r6090 <- rqpd(r6.form, panel= panel(taus=c(0.1, 0.25, 0.5, 0.75, 0.9), tauw = rep(1/5,5)), 
              na.action = 'na.omit', data=mydata)

#Storing results 
c6090 <- summary(r6090, se = "boot", R=1000, covariance = TRUE)

#making a data frame for results
df6 = as.data.frame(c6090$coefficients)

#coefficients and t-values in a matrix
cf_6 <- df6[1:71, c(1,3)]

#p-values
cf_6.pval <- df6[1:71,4]

#function to print stars
starPrinter <- function(pVal) {
  if(pVal < 0.01) return("***")
  if(pVal < 0.05) return("**")
  if(pVal < 0.1) return("*")

  return("")
}

# a matrix, with digits rounded to 3rd decimal
myTable6 <- round(cf_6, 3)

# get stars and put tvalues in parentheses
for(i in 1:nrow(myTable6)) {
  myTable6[i, 1] <- paste0(myTable6[i, 1], starPrinter(cf_6.pval[i]))
  myTable6[i, 2] <- paste0("(", myTable6[i, 2], ")")
}

#creating a vector
table.cf_6 <- (t(myTable6))

#exporting it to CSV file
write.table(table.cf_6, "c6090.csv", sep =',', row.names = TRUE)

这会将结果提取到具有变量名称的不同列中。