2015年8月21日星期五

[StackOverflow]quantmod-error-cannot-open-url


I started to experience an error today with the quantmod package. Anybody else have the same error when running this code (or requesting symbols in general)?
library(quantmod) getSymbols("CPIAUCNS",src="FRED")
Error:
Error in download.file(paste(FRED.URL, "/", Symbols[[i]], "/", "downloaddata/", : cannot open URL 'http://research.stlouisfed.org/fred2/series/CPIAUCNS/downloaddata/CPIAUCNS.csv'
The URL itself works fine.

Another (temporary) solution is to call one of the following before the actual getSymbols script:
options(download.file.method="libcurl")
or
options(download.file.method="wget")
or
options(download.file.method="wininet")
The first option works for me (on Mac).
Thanks Paul Gilbert from Rmetrics (bottom post)

2015年8月7日星期五

[转]Rmarkdown输出PDF中的中文支持解决方案

原文出处http://rokia.org/?p=438
用Rmarkdown制作PDF时(需要安装CTEX套件,完全安装),如果里面有中文字符,在生成PDF的时候经常会出错。因此常有一些几种方式处理:
(1)将.rmd文件生成md,然后用pandoc生成PDF
(2)用Rstudio生成html,在用chrome浏览器打开,通过打印方式,生成PDF
以上两种都很麻烦,很喜欢Rstudio里面直接knitr pdf的感觉。
感谢高光银童鞋提示以下的新方法,可以在某种程度上方面操作
具体内容如下:
1.需要安装Rstudio的最新版本
2.安装rticles包
library(devtools)
devtools::install_github("rstudio/rticles")
3.新建rmarkdown文件-From Template--Ctex Document
这里的ctex模版是文章模版,如果需要presentation格式,需要自己修改下latex模版。
详细说明文档见:https://github.com/rstudio/rticles/pull/16

2015年8月4日星期二

城投债(WIND口径)结构分析

城投债(WIND口径)结构分析

载入数据:

setwd("C:/Users/shihchosen/Desktop")
library(ggplot2)
library(scales)
bond<-read.csv("城投债20150804.csv")
names(bond)
##  [1] "证券代码"               "证券简称"              
##  [3] "发行人中文名称"         "债券期限.年...单位..年"
##  [5] "起息日期"               "到期日期"              
##  [7] "发行总额..单位..亿元"   "息票品种"              
##  [9] "利率类型"               "计息方式"              
## [11] "票面利率.发行时."       "税率..单位..."         
## [13] "上市地点"               "是否城投债"            
## [15] "城投行政级别"           "发行时债项评级"        
## [17] "发行时主体评级"
head(bond)
##    证券代码   证券简称         发行人中文名称 债券期限.年...单位..年
## 1   7110.IB   02苏交通   江苏交通控股有限公司                     15
## 2 120204.SH   02苏交通   江苏交通控股有限公司                     15
## 3 120301.SH   03沪轨道           上海久事公司                     15
## 4 038003.IB 03沪轨道债           上海久事公司                     15
## 5 058009.IB 05粤交通债 广东省交通集团有限公司                     15
## 6 111026.SZ   05粤交通 广东省交通集团有限公司                     15
##     起息日期   到期日期 发行总额..单位..亿元 息票品种 利率类型 计息方式
## 1 2002-12-12 2017-12-12                   15     附息 固定利率     单利
## 2 2002-12-12 2017-12-12                   15     附息 固定利率     单利
## 3 2003-02-19 2018-02-19                   40     附息 固定利率     单利
## 4 2003-02-19 2018-02-19                   40     附息 固定利率     单利
## 5 2005-06-29 2020-06-29                   15     附息 固定利率     单利
## 6 2005-06-29 2020-06-29                   15     附息 固定利率     单利
##   票面利率.发行时. 税率..单位... 上市地点 是否城投债 城投行政级别
## 1             4.51            20   银行间         否             
## 2             4.51            20     上海         否             
## 3             4.51            20     上海         否             
## 4             4.51            20   银行间         否             
## 5             5.30            20   银行间         否             
## 6             5.30            20     深圳         否             
##   发行时债项评级 发行时主体评级
## 1            AAA            AAA
## 2            AAA            AAA
## 3            AAA            AAA
## 4            AAA            AAA
## 5            AAA            AAA
## 6            AAA            AAA

定义函数

# function isobar: 输入待分析列,标题,作出柱形图
isobar <- function(bond,xlab,ylab="频数",title="柱形图"){
  df <- data.frame(x=factor(bond))
    dfTab <- as.data.frame(table(df))
    colnames(dfTab)[1] <- "x"
    ggplot(df) + 
    geom_bar(aes(x,fill=x), color="black") + 
    geom_text(data=dfTab,aes(x=x,y=Freq,label=Freq),vjust=0) +
    xlab(xlab)+ylab(ylab) + 
    ggtitle(title) + 
    theme(legend.position="none")
}
  
# function isobar: 输入待分析列,标题,作出累积柱形图#

cumbar<-function(bond,xlab,ylab="频数",title="柱形累积图"){
  df <- data.frame(x=factor(bond))
    dfTab <- as.data.frame(table(df))
    colnames(dfTab)[1] <- "x"
    ggplot(dfTab,aes(x="", y=Freq, fill=x)) + geom_bar(stat = "identity", color="black")+ 
    geom_text(data=dfTab,aes(label=Freq),hjust = 0.5, vjust = 3,position = "stack")+
    xlab(xlab)+ylab(ylab) + 
    ggtitle(title)
}

# function pieChart:画饼状图
pieChart <- function(bond,xlab,ylab="频数",title="饼状图"){
    df <- data.frame(x=factor(bond))
    dfTab <- as.data.frame(table(df))
    colnames(dfTab)[1] <- "x"
    ggplot(dfTab,aes(x="", y=Freq, fill=x)) + geom_bar(stat = "identity", color="black")+ 
    xlab(xlab)+ylab(ylab) + 
    ggtitle(title)+
     coord_polar("y", start=0)
}

债券期限

summary(bond[,4])
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##  0.2459  5.0000  7.0000  5.7970  7.0000 20.0000       2
p <- ggplot(bond, aes(factor(0), bond[,4]))
p + geom_boxplot()+ xlab("债券期限")+ ylab("单位(年)")+title("债券期限箱形图")
## Error in title("债券期限箱形图"): plot.new has not been called yet

发行总额

summary(bond[,7])
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   0.035   6.000  10.000  11.340  15.000 100.000       6
summary(bond[,7],na.rm = TRUE)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   0.035   6.000  10.000  11.340  15.000 100.000       6
p <- ggplot(bond, aes(factor(0), bond[,7]))
p + geom_boxplot()+ xlab("发行总额")+ ylab("单位(亿元)")+ title("发行总额箱形图")
## Error in title("发行总额箱形图"): plot.new has not been called yet

息票品种

summary(bond[,8])
##                  到期一次还本付息             附息 
##                2              453             5056
isobar(bond[,8],xlab="息票品种", ylab="频率", title="息票品种柱形图")
plot of chunk unnamed-chunk-5
pieChart(bond[,8],xlab="息票品种", ylab="频率", title="息票品种柱形图")

plot of chunk unnamed-chunk-5

利率类型

summary(bond[,9])
##          浮动利率 固定利率 累进利率 
##        2       33     4717      759
isobar(bond[,9],xlab="利率类型", ylab="频率", title="利率类型柱形图")
plot of chunk unnamed-chunk-6
pieChart(bond[,9],xlab="利率类型", ylab="频率", title="利率类型柱形图")
plot of chunk unnamed-chunk-6

计息方式

summary(bond[,10])
##      单利 
##    2 5509
isobar(bond[,10],xlab="计息方式", ylab="频率", title="计息方式柱形图")
plot of chunk unnamed-chunk-7
pieChart(bond[,10],xlab="计息方式", ylab="频率", title="计息方式柱形图")
plot of chunk unnamed-chunk-7

票面利率

summary(bond[,11])
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   2.980   5.800   6.610   6.579   7.310  12.000       7
p <- ggplot(bond, aes(factor(0), bond[,4]))
p + geom_boxplot()+ xlab("票面利率")+ ylab("单位(%)")+title("票面利箱形图")
## Error in title("票面利箱形图"): plot.new has not been called yet

税率

城投债参考公司债的税率20%

summary(bond[,12])
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##      20      20      20      20      20      20       2

上市地点

summary(bond[,13])
##                              其他             上海             深圳 
##                2                1             1445               41 
##   天津股权交易所     银行柜台债券           银行间 浙江股权交易中心 
##               10               18             3971               23
isobar(bond[,13],xlab="上市地点", ylab="频率", title="上市地点柱形图")
plot of chunk unnamed-chunk-10
pieChart(bond[,13],xlab="上市地点", ylab="频率", title="上市地点饼状图")

plot of chunk unnamed-chunk-10

是否城投债

summary(bond[,14])
##        否   是 
##    2  460 5049
isobar(bond[,14],xlab="是否城投债", ylab="频率", title="是否城投债柱形图")
plot of chunk unnamed-chunk-11
pieChart(bond[,14],xlab="是否城投债", ylab="频率", title="是否城投债饼状图")

plot of chunk unnamed-chunk-11

城投行政级别

summary(bond[,15])
##                            地级市 省及省会(单列市)       县及县级市 
##             1477             1524             1982              528
isobar(bond[,15],xlab="城投行政级别", ylab="频率", title="城投行政级别柱形图")
plot of chunk unnamed-chunk-12
pieChart(bond[,15],xlab="城投行政级别", ylab="频率", title="城投行政级别债饼状图")
plot of chunk unnamed-chunk-12

发行时债项评级

summary(bond[,16])
##         -  A-1   A+   AA  AA-  AA+  AAA 
##    2 1023  297    2 2300   58 1263  566
isobar(bond[,16],xlab="债项评级", ylab="频率", title="发行时债项评级柱形图")
plot of chunk unnamed-chunk-13
pieChart(bond[,16],xlab="债项评级", ylab="频率", title="发行时债项评级饼状图")
plot of chunk unnamed-chunk-13

发行时主体评级

summary(bond[,17])
##         A   A-   A+   AA  AA-  AA+  AAA  BBB BBB- 
##  123    4    5   12 2807  407 1265  885    1    2
isobar(bond[,17],xlab="主体评级", ylab="频率", title="发行时主体评级柱形图")
plot of chunk unnamed-chunk-14
pieChart(bond[,17],xlab="主体评级", ylab="频率", title="发行时主体评级饼状图")
plot of chunk unnamed-chunk-14