gpt4 book ai didi

R Shiny : Rendering summary. ivreg 输出

转载 作者:行者123 更新时间:2023-12-03 22:45:09 25 4
gpt4 key购买 nike

我正在尝试在 R Shiny 中呈现工具变量回归摘要

代码如下:

iv=ivreg(lwage~educ+exper|nearc4+exper)
summary(iv)

当我使用 renderTable 时,出现以下错误:没有适用于“xtable”的适用方法应用于“summary.ivreg”类的对象

有什么解决这个问题的建议吗?

这是我的网站,如果你想看看我到底在做什么: https://ieconometrics.shinyapps.io/test/

最佳答案

renderTable 期望一个存在 xtable 方法的对象,您可以看到可用的方法:methods(xtable),它不适用于 summary.ivreg,您可以自己构建一个方法或使用以下代码获得类似这样的结果:

enter image description here

library(shiny)
library(AER)
library(ReporteRs)

# define server
server <- function(input, output) {

output$raw_summary <- renderPrint({
fm <- ivreg(log(packs) ~ log(rprice) + log(rincome) | log(rincome) + tdiff + I(tax/cpi),
data = CigarettesSW, subset = year == "1995")
print(summary(fm))
})

output$summary_table <- renderUI({
fm <- ivreg(log(packs) ~ log(rprice) + log(rincome) | log(rincome) + tdiff + I(tax/cpi),
data = CigarettesSW, subset = year == "1995")
data = summary(fm)$coefficients
data = as.data.frame(data)
# get signif codes
signif.codes = cut( data[,4]
, breaks = c( -Inf, 0.001, 0.01, 0.05, Inf)
, labels= c("***", "**", "*", "" ) )

# format the data values
data[, 1] = formatC( data[, 1], digits=3, format = "f")
data[, 2] = formatC( data[, 2], digits=3, format = "f")
data[, 3] = formatC( data[, 3], digits=3, format = "f")
data[, 4] = ifelse( data[, 4] < 0.001, "< 0.001", formatC( data[, 4], digits=5, format = "f"))
# add signif codes to data
data$Signif = signif.codes

# create an empty FlexTable
coef_ft = FlexTable( data = data, add.rownames=TRUE
, body.par.props = parRight(), header.text.props = textBold()
, header.columns = T
)
# center the first column and set text as bold italic
coef_ft[,1] = parCenter()
coef_ft[,1] = textBoldItalic()

# define borders
coef_ft = setFlexTableBorders( coef_ft
, inner.vertical = borderNone(), inner.horizontal = borderDotted()
, outer.vertical = borderNone(), outer.horizontal = borderSolid()
)
return(HTML(as.html(coef_ft)))
})
}

# define ui
ui <- shinyUI(fluidPage(
p("Raw summary"),
verbatimTextOutput(outputId = "raw_summary"),

p("Pretty model summary table :"),
uiOutput(outputId = "summary_table")
))

# Call the app
shinyApp(ui = ui, server = server)

关于R Shiny : Rendering summary. ivreg 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27245173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com