R:接收器工作不正常

Mak*_*212 1 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()
Run Code Online (Sandbox Code Playgroud)

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

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

42-*_*42- 6

存在与回归过程一样多的汇总函数,并且其中许多用于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)
Run Code Online (Sandbox Code Playgroud)