将 GLM 相关系数导出到 R 中的 csv

Export GLM Coefficients of Correlation to csv in R

在 R 中 运行 设置我的 GLM 模型后,我 运行 使用 corr=TRUE 的汇总命令来获取模型中各个变量的相关系数。我想做的是将它们输出到 CSV 文件,这样我就可以在 Excel.

中打开它们

我尝试使用 coef 命令以及我在本网站上找到的其他几种方法将模型输出写入 csv 文件。到目前为止,我还没有运气得到相关系数,只是估计,标准。误差,t值和p值。

您可以使用 capture.output()。假设 model 是您的 GLM:

capture.output(summary(model, corr = TRUE), file = "filename.csv")

这会获取摘要的确切文本并将其写入文件。

更新

要仅获取相关系数,您可以访问 summary 对象的 correlation 元素(仅在 corr = TRUE 时有效):

df = data.frame(a = runif(100), b = runif(100))
model <- glm(a ~ b, data = df)

s <- summary(model, corr = TRUE)
s$correlation

#>             (Intercept)          b
#> (Intercept)   1.0000000 -0.8334063
#> b            -0.8334063  1.0000000

您可以获取此输出并将其写入应保留 table 类结构的 csv:

write.csv(s$correlation, file = "filename.csv")

Here is the documentation for summary.glm 显示了可以提取的列表的不同成员。

correlation
(only if correlation is true.) The estimated correlations of the estimated coefficients.