R: Sink not working correctly

久未见 提交于 2019-12-01 10:10:03

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/27789311/r-sink-not-working-correctly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!