打印glm的摘要

时间:2014-05-13 18:56:49

标签: r printing

我正在努力保存/打印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")

但它只是保存了模型的系数。有没有办法保存所有摘要输出?

感谢。

2 个答案:

答案 0 :(得分: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 :(得分:3)

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

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