This question already has an answer here:
R : Format the text file output

(1 个回答)


7年前关闭。




我正在努力保存/打印 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("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 文件并没有任何意义......

关于r - 打印 glm 的摘要,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23639401/

10-12 22:39