楼主: tulipsliu
2027 2

[程序分享] 关于Ruey S. Tsay 第一章代码的回复 [推广有奖]

经济学论述自由撰稿人!

已卖:2752份资源

学科带头人

45%

还不是VIP/贵宾

-

威望
0
论坛币
386045 个
通用积分
527.0498
学术水平
127 点
热心指数
140 点
信用等级
103 点
经验
46986 点
帖子
1773
精华
0
在线时间
2509 小时
注册时间
2007-11-5
最后登录
2026-2-7

初级热心勋章

楼主
tulipsliu 在职认证  发表于 2019-9-20 18:48:22 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
论坛看到以前的好友发帖问这个R程序怎么运行。

这里上传修改过的代码到论坛,同时上传压缩包。问这个问题的帖子应该还在R论坛吧。自己去搜索。

修改好的主程序代码,名字叫 mainCalculate.R 。
记得修改 setwd("E:/Mystudio/zhaotao011") # Set my working directory 这句代码,我有E盘这个文件夹,如果你没有也照样会出错,已经能运行,但是有一个警告,有缺失值。
不过不影响程序运行到最后,所以没有修改。

  1. #----------------------------------------------------------------------------
  2. # A demostration of R using
  3. #----------------------------------------------------------------------------
  4. setwd("E:/Mystudio/zhaotao011") # Set my working directory
  5. #----------------------------------------------------------------------------
  6. #  Compile R script
  7. source("ma.R")
  8. source("ohlc.R")
  9. #----------------------------------------------------------------------------
  10. ### Chapter 1 ###
  11. library(quantmod)   # Load the package
  12. getSymbols("AAPL")  # Download daily prices of Apple stock from Yahoo
  13. dim(AAPL)  # (dimension): See the size of the downloaded data.
  14. head(AAPL)  # See the first 6 rows of the data
  15. tail(AAPL)   # See the last 6 rows of the data
  16. chartSeries(AAPL,theme="white")  # Plot the daily price and volume
  17. chartSeries(AAPL)#Not shown giving the same plot with black background.
  18. getSymbols("AAPL",from="2005-01-02", to="2010-12-31")
  19. head(AAPL)
  20. getSymbols("UNRATE",src="FRED")#Download unemployment rates from FRED.
  21. head(UNRATE)
  22. chartSeries(UNRATE,theme="white")  # Plot monthly unemployment rates
  23. getSymbols("INTC",src="yahoo")  # Download data from Google.
  24. head(INTC)
  25. getSymbols("^TNX") # Download CBOE 10-year Treasures Notes
  26. head(TNX)
  27. TNX<-na.fill(TNX,"extend")
  28. chartSeries(TNX,theme="white",TA=NULL) # Obtain plot without volume.

  29. library(fBasics) # Load package
  30. da=read.table('d-ibm-0110.txt',header=T) # Load text data with names.
  31. head(da) # See the first 6 rows
  32. dim(da)  # Dimension of the data object "da".
  33. da <- read.table("d-vix0411.txt",header=T) # Load csv data with names.
  34. #
  35. library(quantmod)
  36. getSymbols("AAPL",from="2007-01-03",to="2011-12-02") #Specify period
  37. AAPL.rtn=diff(log(AAPL$AAPL.Adjusted)) # Compute log returns
  38. chartSeries(AAPL.rtn,theme="white")
  39. getSymbols("^TNX",from="2007-01-03",to="2011-12-02")
  40. TNX.rtn=diff(TNX$TNX.Adjusted) # Compute changes
  41. chartSeries(TNX.rtn,theme="white")
  42. getSymbols("DEXUSEU",src="FRED") #Obtain exchange rates from FRED
  43. head(DEXUSEU)
  44. tail(DEXUSEU)
  45. USEU.rtn=diff(log(DEXUSEU$DEXUSEU))
  46. chartSeries(DEXUSEU,theme="white")
  47. chartSeries(USEU.rtn,theme="white")
  48. #
  49. library(fBasics) # Load package
  50. da=read.table("d-mmm-0111.txt",header=T) # Load data
  51. # header=T means the first row of data file contains names.
  52. # deafult is no names.
  53. head(da) #Show the first 6 rows of data
  54. mmm=da[,2]  # Obtain 3m simple returns
  55. basicStats(mmm) #Compute summary statistics
  56. mean(mmm)
  57. var(mmm)
  58. stdev(mmm) # standard deviation
  59. t.test(mmm)  # Testing mean return = 0
  60. s3=skewness(mmm)
  61. T=length(mmm) # Sample size
  62. t3=s3/sqrt(6/T) # Skewness test
  63. pp=2*(1-pnorm(t3)) # Compute p-value
  64. s4=kurtosis(mmm)
  65. t4=s4/sqrt(24/T) # Kurtosis test
  66. normalTest(mmm,method='jb') # JB-test
  67. #
  68. library(fBasics)
  69. da=read.table("d-mmm-0111.txt",header=T) # Load data
  70. mmm=da[,2] # Locate 3M simple returns
  71. hist(mmm,nclass=30) # Histogram
  72. d1=density(mmm)  # Obtain density estimate
  73. range(mmm)  # Range of 3M returns
  74. x=seq(-.1,.1,.001) # Create a sequence of x with increment 0.001.
  75. y1=dnorm(x,mean(mmm),stdev(mmm))
  76. plot(d1$x,d1$y,xlab='rtn',ylab='density',type='l')
  77. lines(x,y1,lty=2)
  78. library(quantmod)
  79. getSymbols("AAPL",from="2011-01-03",to="2011-06-30")
  80. X=AAPL[,1:4] # Locate open, high, low, and close prices
  81. xx=cbind(as.numeric(X[,1]),as.numeric(X[,2]),as.numeric(X[,3]),as.numeric(X[,4]))
  82. #source("ohlc.R") # Compile the R script
  83. ohlc(xx,xl="days",yl="price",title="Apple Stock")
  84. #source("ma.R")  # Compile R script
  85. getSymbols("AAPL",from="2010-01-02",to="2011-12-08")
  86. x1=as.numeric(AAPL$AAPL.Close) # Locate close price
  87. ma(x1,21)
  88. da=read.table("m-ibmsp-2611.txt",header=T)
  89. head(da)
  90. ibm=log(da$ibm+1) # Transform to log returns
  91. sp=log(da$sp+1)
  92. tdx=c(1:nrow(da))/12+1926 # Create time index
  93. par(mfcol=c(2,1))
  94. plot(tdx,ibm,xlab='year',ylab='lrtn',type='l')
  95. title(main='(a) IBM returns')
  96. plot(tdx,sp,xlab='year',ylab='lrtn',type='l') # X-axis first.
  97. title(main='(b) SP index')
  98. cor(ibm,sp)  # Obtain sample correlation
  99. m1=lm(ibm~sp)  # Fit the Market Model (linear model)
  100. summary(m1)
  101. plot(sp,ibm,cex=0.8)  # Obtain scatter plot
  102. abline(0.008,.807) # Add the linear regression line
  103. #
  104. da=read.table("m-ibmsp-2611.txt",header=T) #Load  data
  105. dim(da)
  106. ibm=log(da$ibm+1) # Compute log returns
  107. sp=log(da$sp+1)
  108. rt=cbind(ibm,sp) # Obtain bivariate returns
  109. m1=apply(rt,2,mean) # Obtain sample means
  110. v1=cov(rt) # Obtain sample covariance matrix
  111. m1
  112. v1
  113. library(mnormt) # Load package
  114. x=rmnorm(1029,mean=m1,varcov=v1) # Simulation
  115. dim(x)
  116. plot(x[,2],x[,1],xlab='sim-sp',ylab='sim-ibm',cex=0.8)
复制代码


附件在这里,其实这个是那个作者论坛这本书里的代码,程序在 .tex 文本文件里。保存为 R 文件就可以。

不好意思,我下载这个文件夹的人的帖子时没仔细看,他的文件有里 .csv 文件,非常大。压缩后200多M,你们可以不下载。
这个程序已经可以运行,但是我最近很忙,忙很重要的代码。不方便论坛继续发帖修改程序读取他的数据演示一下。他的压缩文件太大,我无法上传。这里给论坛的这本书也有代码,复制我发的代码,保存他第一章的文件夹里,设定新的工作目录就可以运行。

An_Introduction_to_Analysis_of_Financial_Data_with_R 作者: Tsay


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝


已有 2 人评分论坛币 热心指数 收起 理由
贝里相位 + 1 精彩帖子
cheetahfly + 30 精彩帖子

总评分: 论坛币 + 30  热心指数 + 1   查看全部评分

劳动经济学

沙发
jiangbeilu 学生认证  发表于 2019-9-22 07:56:46
楼主分享的代码很好哈

藤椅
tulipsliu 在职认证  发表于 2019-9-23 12:31:56
jiangbeilu 发表于 2019-9-22 07:56
楼主分享的代码很好哈
已经下载作者的书籍和代码,运行其他章发现很多错误。
修改错误是需要时间的,最近在忙其他程序,没时间修复 Tsay 的程序错误。

他的代码以前没错的,因为R的升级变迁,有的估计是可以用新的工具包估计,新的函数和参数。

后续我有时间会继续在R板块提供修复的Tsay 的代码。  谢谢鼓励

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-2-8 06:26