打印glm的摘要

3

我遇到了保存/打印glm模型摘要输出的问题。这是我的代码:

logmodel=glm(amal~age + LC1 + LC2 + LC3 + LC4, data=random_new, family="binomial")
summary(logmodel)

输出结果为:

Call:
glm(formula = amal ~ age + LC1 + LC2 + LC3 + LC4, family = "binomial", 
data = random100_new)

Deviance Residuals: 
    Min        1Q    Median        3Q       Max  
-1.17907  -0.59278  -0.00008  -0.00008   2.37302  

Coefficients:
              Estimate Std. Error z value Pr(>|z|)    
(Intercept) -2.336e-03  1.155e-01  -0.020    0.984    
age          2.633e-05  9.838e-04   0.027    0.979    
LC11        -1.957e+01  4.065e+02  -0.048    0.962    
LC21        -2.752e+00  1.762e-01 -15.617   <2e-16 ***
LC31        -1.957e+01  4.065e+02  -0.048    0.962    
LC41        -1.648e+00  1.275e-01 -12.918   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 2888.7  on 3499  degrees of freedom
Residual deviance: 1907.0  on 3494  degrees of freedom
AIC: 1919

Number of Fisher Scoring iterations: 18

我尝试过;

result=summary.glm(logmodel)$coefficients write.csv(result, file="x.csv")

但是这只保存了模型的系数。有没有办法保存所有汇总输出?

谢谢。


无论是sink还是capture output,都有许多先前的答案说明它们的使用。 - IRTFM
1
我是一个新的R语言用户,用了大约两个月。我不知道有一个名为sink或capture output的函数。在提问之前,我在这个网站上进行了研究,但没有找到答案。无论如何,感谢你的帮助。 - user3525533
2个回答

7
您可以使用:

sink("outfile.txt")  ## switch standard output to a file
summary(logmodel)
sink()               ## don't forget to turn off redirection
                     ## lots of scope for confusion here!

capture.output 可能更安全/推荐,因为你运行的风险较小,不会忘记取消重定向...

writeLines(capture.output(summary(logmodel)),con="outfile.txt")

将输出发送到CSV文件中并没有真正意义...


注:此句话涉及上下文,请注意理解。

+1 for capture.output - gagolews
1
非常感谢Ben。writeLines函数对我来说完美运作。 - user3525533

3

将模型的整个输出保存为CSV格式是没有意义的,因为概要输出不是一个表格。如果您想保存完整的输出,实际上是在保存一个列表(而不是表格/ CSV)。您可以这样做:

x<-summary(yourmodel)
save(x, file="modelresults.Rdata")

真实而有用,但我认为这不是OP想要的 - 我认为他们实际上想要保存打印的表示形式... - Ben Bolker
是的,我确实尝试了save("Rda")函数。但它没有起作用,因为我想要保存并打印它。 - user3525533

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接