R:接收器工作不正常

时间:2015-01-05 22:53:18

标签: r sink

我在下面的代码的最后一行中遇到了第四个问题(抱歉有大量代码,我不确定需要多少代码)

library(mlogit)
library(foreign)


clogit <- read.table("~/R/clogit.dat", col.names=c("mode", "ttme", "invc", "invt", "gc", "chair",
    "hinc", "psize", "indj", "indi", "aasc", "tasc", "basc", "casc", 
    "hinca", "psizea", "z", "nij", "ni"), na.strings= "-999")

clogit$mode.ids <-factor(rep(1:4,210), labels=c("air", "train", "bus", "car"))

CLOGIT <- mlogit.data(clogit,shape="long", choice="mode", alt.var="mode.ids")

res1 <- mlogit(mode~ttme+gc, data=clogit,shape="long", alt.var="mode.ids")
summary(res1)

res2 <- mlogit(mode~ttme+gc, data=CLOGIT)
summary(res2)


res3 <- mlogit(mode~ttme + gc | hinc, data=CLOGIT)
summary(res3)

res4 <- mlogit(mode~ttme | hinc | gc, data=CLOGIT)

sink("~/R/res4.txt")
    cat("Here are my results:\n")
    summary(res4)
sink()

最后一行中的sink("~/R/res4.txt")函数将存储"Here are my results"行,而不是summary(res4)行中的.txt行。

键入summary(res4)会生成正确的数据集,我不明白为什么不包含summary(res4)的输出。有没有人有办法解决吗?感谢。

1 个答案:

答案 0 :(得分:5)

存在与回归过程一样多的汇总函数,其中许多汇总函数使用cat而不会返回返回值。我的建议是使用cat和capture.output,它们都有file目标参数和append选项:

cat("Here are my results:\n", file="~/R/res4.txt")
capture.output( summary(res4), file"~/R/res4.txt", append=TRUE)
相关问题