将R中的chainladder输出导出为CSV文件

时间:2016-03-22 12:40:09

标签: r csv save

我在导出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 个答案:

答案 0 :(得分:0)

您要导出哪个输出列表?

这是一个将元素Triangle存储在.csv文件中的示例:

library(ChainLadder)

cd <- chainladder(RAA)

write.csv(cd$Triangle, file = "stackoverflow/36154658/test.csv")

如果要导出每个元素,可以创建不同的.csv文件,也可以将结果附加到单个元素。参见:

?write.csv

修改
也许您只想将print() - 函数的输出保存在文本文件中?

cd <- chainladder(RAA)

sink("stackoverflow/36154658/test4.txt")
cd
sink()
unlink("test4.txt")