首页 文章

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

提问于
浏览
2

在R中运行我的GLM模型后,我使用 corr=TRUE 运行summary命令以获取模型中各种变量的相关系数 . 我想要做的是将它们输出到CSV文件,以便我可以在Excel中打开它们 .

我已经尝试过使用 coef 命令以及其他几种方法,我很幸运得到了相关系数,只有Estimate,Std . 错误,t值和p值 .

1 回答

  • 2

    你可以使用 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
    

    您可以将此输出并将其写入csv,该csv应保留表格式结构:

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

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

    相关性(仅当相关性为真时 . )估计系数的估计相关性 .

相关问题