R: Sink not working correctly

2019-03-03 02:51发布

I have an issue with the fourth from last line in the code below (sorry for large amounts of code, I am unsure of how much is necessary)

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()

The sink("~/R/res4.txt") function in the last line will store the "Here are my results" line, but not the summary(res4) line in the .txt file.

Typing summary(res4) produces the correct data set, and I don't understand why the output of summary(res4) isn't included. Does anyone have a solution? Thanks.

标签: r sink
1条回答
放我归山
2楼-- · 2019-03-03 03:31

There are as many summary functions as there are regression procedures and many of them use cat with would not get into a value returned. My suggestion is to use cat and capture.output both of which have a file destination parameter and an append option:

cat("Here are my results:\n", file="~/R/res4.txt")
capture.output( summary(res4), file"~/R/res4.txt", append=TRUE)
查看更多
登录 后发表回答