将 R 中的链梯输出导出到 CSV 文件
Posted
技术标签:
【中文标题】将 R 中的链梯输出导出到 CSV 文件【英文标题】:Export chainladder output in R to CSV file 【发布时间】:2016-03-22 12:40:09 【问题描述】:我在导出 chainladder
函数从包 ChainLadder
创建的输出时遇到问题。我想将输出导出为 CSV 文件或 XLSX 文件。
我尝试使用函数save
进行保存,但是当我打开 CSV 文件时,出现了一些奇怪的字符。
我的输出看起来像:
> chainladder(RAA)
$Models
$Models[[1]]
Call:
lm(formula = y ~ x + 0, data = data.frame(x = Triangle[, i],
y = Triangle[, i + 1]), weights = weights[, i]/Triangle[,
i]^delta[i])
Coefficients:
x
2.999
$Models[[2]]
Call:
lm(formula = y ~ x + 0, data = data.frame(x = Triangle[, i],
y = Triangle[, i + 1]), weights = weights[, i]/Triangle[,
i]^delta[i])
Coefficients:
x
1.624
$Models[[3]]
Call:
lm(formula = y ~ x + 0, data = data.frame(x = Triangle[, i],
y = Triangle[, i + 1]), weights = weights[, i]/Triangle[,
i]^delta[i])
Coefficients:
x
1.271
$Models[[4]]
Call:
lm(formula = y ~ x + 0, data = data.frame(x = Triangle[, i],
y = Triangle[, i + 1]), weights = weights[, i]/Triangle[,
i]^delta[i])
Coefficients:
x
1.172
$Models[[5]]
Call:
lm(formula = y ~ x + 0, data = data.frame(x = Triangle[, i],
y = Triangle[, i + 1]), weights = weights[, i]/Triangle[,
i]^delta[i])
Coefficients:
x
1.113
$Models[[6]]
Call:
lm(formula = y ~ x + 0, data = data.frame(x = Triangle[, i],
y = Triangle[, i + 1]), weights = weights[, i]/Triangle[,
i]^delta[i])
Coefficients:
x
1.042
$Models[[7]]
Call:
lm(formula = y ~ x + 0, data = data.frame(x = Triangle[, i],
y = Triangle[, i + 1]), weights = weights[, i]/Triangle[,
i]^delta[i])
Coefficients:
x
1.033
$Models[[8]]
Call:
lm(formula = y ~ x + 0, data = data.frame(x = Triangle[, i],
y = Triangle[, i + 1]), weights = weights[, i]/Triangle[,
i]^delta[i])
Coefficients:
x
1.017
$Models[[9]]
Call:
lm(formula = y ~ x + 0, data = data.frame(x = Triangle[, i],
y = Triangle[, i + 1]), weights = weights[, i]/Triangle[,
i]^delta[i])
Coefficients:
x
1.009
$Triangle
dev
origin 1 2 3 4 5 6 7 8 9 10
1981 5012 8269 10907 11805 13539 16181 18009 18608 18662 18834
1982 106 4285 5396 10666 13782 15599 15496 16169 16704 NA
1983 3410 8992 13873 16141 18735 22214 22863 23466 NA NA
1984 5655 11555 15766 21266 23425 26083 27067 NA NA NA
1985 1092 9565 15836 22169 25955 26180 NA NA NA NA
1986 1513 6445 11702 12935 15852 NA NA NA NA NA
1987 557 4020 10946 12314 NA NA NA NA NA NA
1988 1351 6947 13112 NA NA NA NA NA NA NA
1989 3133 5395 NA NA NA NA NA NA NA NA
1990 2063 NA NA NA NA NA NA NA NA NA
$delta
[1] 1 1 1 1 1 1 1 1 1
$weights
dev
origin 1 2 3 4 5 6 7 8 9 10
1981 1 1 1 1 1 1 1 1 1 1
1982 1 1 1 1 1 1 1 1 1 NA
1983 1 1 1 1 1 1 1 1 NA NA
1984 1 1 1 1 1 1 1 NA NA NA
1985 1 1 1 1 1 1 NA NA NA NA
1986 1 1 1 1 1 NA NA NA NA NA
1987 1 1 1 1 NA NA NA NA NA NA
1988 1 1 1 NA NA NA NA NA NA NA
1989 1 1 NA NA NA NA NA NA NA NA
1990 1 NA NA NA NA NA NA NA NA NA
attr(,"class")
[1] "ChainLadder" "TriangleModel" "list"
有人知道如何将上面的输出保存到 CSV 文件吗?
谢谢!
【问题讨论】:
【参考方案1】:您要导出输出列表的哪一部分?
这是一个将元素 Triangle 存储在 .csv 文件中的示例:
library(ChainLadder)
cd <- chainladder(RAA)
write.csv(cd$Triangle, file = "***/36154658/test.csv")
如果您想导出每个元素,您可以创建不同的 .csv 文件,也可以通过循环将结果附加到单个元素。见:
?write.csv
编辑: 也许您只是想将 print() 函数的输出保存在文本文件中?
cd <- chainladder(RAA)
sink("***/36154658/test4.txt")
cd
sink()
unlink("test4.txt")
【讨论】:
我想导出函数返回的所有内容。它在输出的三角形部分工作正常。但是当我尝试将write.csv
用于cd$Models[1]
时,它会返回错误Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class ""lm"" to a data.frame
。你可能知道我做错了什么吗?
这个错误相当的说。函数 lm() 的结果是类“lm”的对象。 write.csv() 调用的函数 data.frame() 无法从该对象创建类“data.frame”的对象。根据您想要的结果,您可以将“lm”对象转换为其他类。也许write.csv(as.character(cd$Models[[1]]), file = "***/36154658/test2.csv")
是你想要的。
也许你有计算出的系数就足够了? write.csv(cd$Models[[1]][1], file = "***/36154658/test3.csv")
我想导出所有内容。整个模型,不仅仅是系数。
也许您只是想将 print() 函数的输出保存在文本文件中?以上是关于将 R 中的链梯输出导出到 CSV 文件的主要内容,如果未能解决你的问题,请参考以下文章