> ### Chapter 1 ###
> library(quantmod) # Load the package
> getSymbols("AAPL") # Download daily prices of Apple stock from Yahoo
[1] "AAPL"
> dim(AAPL) # (dimension): See the size of the downloaded data.
[1] 3042 6
> head(AAPL) # See the first 6 rows of the data
AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
2007-01-03 12.32714 12.36857 11.70000 11.97143 309579900 8.016820
2007-01-04 12.00714 12.27857 11.97429 12.23714 211815100 8.194759
2007-01-05 12.25286 12.31428 12.05714 12.15000 208685400 8.136404
2007-01-08 12.28000 12.36143 12.18286 12.21000 199276700 8.176582
2007-01-09 12.35000 13.28286 12.16429 13.22429 837324600 8.855811
2007-01-10 13.53571 13.97143 13.35000 13.85714 738220000 9.279611
> tail(AAPL) # See the last 6 rows of the data
AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
2019-01-25 155.48 158.13 154.32 157.76 33535500 157.76
2019-01-28 155.79 156.33 153.66 156.30 26192100 156.30
2019-01-29 156.25 158.13 154.11 154.68 41587200 154.68
2019-01-30 163.25 166.15 160.23 165.25 61109800 165.25
2019-01-31 166.11 169.00 164.56 166.44 40739600 166.44
2019-02-01 166.96 168.98 165.93 166.52 32647100 166.52
> chartSeries(AAPL,theme="white") # Plot the daily price and volume
> chartSeries(AAPL)#Not shown giving the same plot with black background.
> getSymbols("AAPL",from="2005-01-02", to="2010-12-31")
[1] "AAPL"
> head(AAPL)
AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
2005-01-03 4.627143 4.650714 4.471428 4.520714 172998000 3.027354
2005-01-04 4.556428 4.676429 4.497857 4.567143 274202600 3.058446
2005-01-05 4.604286 4.660714 4.575000 4.607143 170108400 3.085232
2005-01-06 4.619286 4.636428 4.523571 4.610714 176388800 3.087624
2005-01-07 4.642857 4.973571 4.625000 4.946429 556862600 3.312439
2005-01-10 4.987857 5.050000 4.848571 4.925714 431327400 3.298568
> getSymbols("UNRATE",src="FRED")#Download unemployment rates from FRED.
[1] "UNRATE"
> head(UNRATE)
UNRATE
1948-01-01 3.4
1948-02-01 3.8
1948-03-01 4.0
1948-04-01 3.9
1948-05-01 3.5
1948-06-01 3.6
> chartSeries(UNRATE,theme="white") # Plot monthly unemployment rates
> getSymbols("INTC",src="google") # Download data from Google.
错误: ‘getSymbols.google’ is defunct.
Google Finance stopped providing data in March, 2018.
You could try setting src = "yahoo" instead.
See help("Defunct") and help("quantmod-defunct")
> head(INTC)
Error in head(INTC) : 找不到对象'INTC'
> getSymbols("^TNX") # Download CBOE 10-year Treasures Notes
[1] "TNX"
Warning message:
^TNX contains missing values. Some functions will not work if objects contain missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to remove or replace them.
> head(TNX)
TNX.Open TNX.High TNX.Low TNX.Close TNX.Volume TNX.Adjusted
2007-01-03 4.658 4.692 4.636 4.664 0 4.664
2007-01-04 4.656 4.662 4.602 4.618 0 4.618
2007-01-05 4.587 4.700 4.583 4.646 0 4.646
2007-01-08 4.668 4.678 4.654 4.660 0 4.660
2007-01-09 4.660 4.670 4.644 4.656 0 4.656
2007-01-10 4.666 4.700 4.660 4.682 0 4.682
> chartSeries(TNX,theme="white",TA=NULL) # Obtain plot without volume.
> #
> setwd("C:/Users/rst/book/introTS/data") # Set my working directory
Error in setwd("C:/Users/rst/book/introTS/data") : 无法改变工作目录
> library(fBasics) # Load package
> da=read.table('d-ibm-0110.txt',header=T) # Load text data with names.
Error in file(file, "rt") : 无法打开链结
此外: Warning message:
In file(file, "rt") :
无法打开文件'd-ibm-0110.txt': No such file or directory
> head(da) # See the first 6 rows
Error in head(da) : 找不到对象'da'
> dim(da) # Dimension of the data object "da".
错误: 找不到对象'da'
> da <- read.csv("d-vix0411.csv",header=T) # Load csv data with names.
Error in file(file, "rt") : 无法打开链结
此外: Warning message:
In file(file, "rt") :
无法打开文件'd-vix0411.csv': No such file or directory
> #
> library(quantmod)
> getSymbols("AAPL",from="2007-01-03",to="2011-12-02") #Specify period
[1] "AAPL"
> AAPL.rtn=diff(log(AAPL$AAPL.Adjusted)) # Compute log returns
> chartSeries(AAPL.rtn,theme="white")
> getSymbols("^TNX",from="2007-01-03",to="2011-12-02")
[1] "TNX"
Warning message:
^TNX contains missing values. Some functions will not work if objects contain missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to remove or replace them.
> TNX.rtn=diff(TNX$TNX.Adjusted) # Compute changes
> chartSeries(TNX.rtn,theme="white")
> getSymbols("DEXUSEU",src="FRED") #Obtain exchange rates from FRED
[1] "DEXUSEU"
> head(DEXUSEU)
DEXUSEU
1999-01-04 1.1812
1999-01-05 1.1760
1999-01-06 1.1636
1999-01-07 1.1672
1999-01-08 1.1554
1999-01-11 1.1534
> tail(DEXUSEU)
DEXUSEU
2019-01-18 1.1362
2019-01-21 NA
2019-01-22 1.1359
2019-01-23 1.1390
2019-01-24 1.1322
2019-01-25 1.1407
> USEU.rtn=diff(log(DEXUSEU$DEXUSEU))
> chartSeries(DEXUSEU,theme="white")
> chartSeries(USEU.rtn,theme="white")
> #
> library(fBasics) # Load package
> da=read.table("d-mmm-0111.txt",header=T) # Load data
Error in file(file, "rt") : 无法打开链结
此外: Warning message:
In file(file, "rt") :
无法打开文件'd-mmm-0111.txt': No such file or directory
> # header=T means the first row of data file contains names.
> # deafult is no names.
> head(da) #Show the first 6 rows of data
Error in head(da) : 找不到对象'da'
> mmm=da[,2] # Obtain 3m simple returns
错误: 找不到对象'da'
> basicStats(mmm) #Compute summary statistics
Error in as.matrix(x) : 找不到对象'mmm'
> mean(mmm)
Error in mean(mmm) : 找不到对象'mmm'
> var(mmm)
Error in is.data.frame(x) : 找不到对象'mmm'
> stdev(mmm) # standard deviation
Error in stdev(mmm) : 找不到对象'mmm'
> t.test(mmm) # Testing mean return = 0
Error in t.test(mmm) : 找不到对象'mmm'
> s3=skewness(mmm)
Error in skewness(mmm) : 找不到对象'mmm'
> T=length(mmm) # Sample size
错误: 找不到对象'mmm'
> t3=s3/sqrt(6/T) # Skewness test
错误: 找不到对象's3'
> pp=2*(1-pnorm(t3)) # Compute p-value
Error in pnorm(t3) : 找不到对象't3'
> s4=kurtosis(mmm)
Error in kurtosis(mmm) : 找不到对象'mmm'
> t4=s4/sqrt(24/T) # Kurtosis test
错误: 找不到对象's4'
> normalTest(mmm,method='jb') # JB-test
Error in normalTest(mmm, method = "jb") : 找不到对象'mmm'
> #
> library(fBasics)
> da=read.table("d-mmm-0111.txt",header=T) # Load data
Error in file(file, "rt") : 无法打开链结
此外: Warning message:
In file(file, "rt") :
无法打开文件'd-mmm-0111.txt': No such file or directory
> mmm=da[,2] # Locate 3M simple returns
错误: 找不到对象'da'
> hist(mmm,nclass=30) # Histogram
Error in hist(mmm, nclass = 30) : 找不到对象'mmm'
> d1=density(mmm) # Obtain density estimate
Error in density(mmm) : 找不到对象'mmm'
> range(mmm) # Range of 3M returns
错误: 找不到对象'mmm'
> x=seq(-.1,.1,.001) # Create a sequence of x with increment 0.001.
> y1=dnorm(x,mean(mmm),stdev(mmm))
Error in mean(mmm) : 找不到对象'mmm'
> plot(d1$x,d1$y,xlab='rtn',ylab='density',type='l')
Error in plot(d1$x, d1$y, xlab = "rtn", ylab = "density", type = "l") :
找不到对象'd1'
> lines(x,y1,lty=2)
Error in xy.coords(x, y) : 找不到对象'y1'
> library(quantmod)
> getSymbols("AAPL",from="2011-01-03",to="2011-06-30")
[1] "AAPL"
> X=AAPL[,1:4] # Locate open, high, low, and close prices
> xx=cbind(as.numeric(X[,1]),as.numeric(X[,2]),as.numeric(X[,3]),as.numeric(X[,4]))
> source("ohlc.R") # Compile the R script
Error in file(filename, "r", encoding = encoding) : 无法打开链结
此外: Warning message:
In file(filename, "r", encoding = encoding) :
无法打开文件'ohlc.R': No such file or directory
> ohlc(xx,xl="days",yl="price",title="Apple Stock")
Error in ohlc(xx, xl = "days", yl = "price", title = "Apple Stock") :
没有"ohlc"这个函数
> source("ma.R") # Compile R script
Error in file(filename, "r", encoding = encoding) : 无法打开链结
此外: Warning message:
In file(filename, "r", encoding = encoding) :
无法打开文件'ma.R': No such file or directory
> getSymbols("AAPL",from="2010-01-02",to="2011-12-08")
[1] "AAPL"
> x1=as.numeric(AAPL$AAPL.Close) # Locate close price
> ma(x1,21)
Error in ma(x1, 21) : 没有"ma"这个函数
> da=read.table("m-ibmsp-2611.txt",header=T)
Error in file(file, "rt") : 无法打开链结
此外: Warning message:
In file(file, "rt") :
无法打开文件'm-ibmsp-2611.txt': No such file or directory
> head(da)
Error in head(da) : 找不到对象'da'
> ibm=log(da$ibm+1) # Transform to log returns
错误: 找不到对象'da'
> sp=log(da$sp+1)
错误: 找不到对象'da'
> tdx=c(1:nrow(da))/12+1926 # Create time index
Error in nrow(da) : 找不到对象'da'
> par(mfcol=c(2,1))
> plot(tdx,ibm,xlab='year',ylab='lrtn',type='l')
Error in plot(tdx, ibm, xlab = "year", ylab = "lrtn", type = "l") :
找不到对象'tdx'
> title(main='(a) IBM returns')
> plot(tdx,sp,xlab='year',ylab='lrtn',type='l') # X-axis first.
Error in plot(tdx, sp, xlab = "year", ylab = "lrtn", type = "l") :
找不到对象'tdx'
> title(main='(b) SP index')
> cor(ibm,sp) # Obtain sample correlation
Error in is.data.frame(y) : 找不到对象'sp'
> m1=lm(ibm~sp) # Fit the Market Model (linear model)
Error in eval(predvars, data, env) : 找不到对象'ibm'
> summary(m1)
Error in summary(m1) : 找不到对象'm1'
> plot(sp,ibm,cex=0.8) # Obtain scatter plot
Error in plot(sp, ibm, cex = 0.8) : 找不到对象'sp'
> abline(0.008,.807) # Add the linear regression line
> #
> da=read.table("m-ibmsp-2611.txt",header=T) #Load data
Error in file(file, "rt") : 无法打开链结
此外: Warning message:
In file(file, "rt") :
无法打开文件'm-ibmsp-2611.txt': No such file or directory
> dim(da)
错误: 找不到对象'da'
> ibm=log(da$ibm+1) # Compute log returns
错误: 找不到对象'da'
> sp=log(da$sp+1)
错误: 找不到对象'da'
> rt=cbind(ibm,sp) # Obtain bivariate returns
Error in cbind(ibm, sp) : 找不到对象'ibm'
> m1=apply(rt,2,mean) # Obtain sample means
Error in apply(rt, 2, mean) : dim(X)的值必需是正数
> v1=cov(rt) # Obtain sample covariance matrix
Error in cov(rt) : 要么同时设定'x'和'y',要么只设定矩阵式的'x'
> m1
错误: 找不到对象'm1'
> v1
错误: 找不到对象'v1'
> library(mnormt) # Load package
> x=rmnorm(1029,mean=m1,varcov=v1) # Simulation
Error in chol(varcov) : 找不到对象'v1'
> dim(x)
NULL
> plot(x[,2],x[,1],xlab='sim-sp',ylab='sim-ibm',cex=0.8)
Error in x[, 2] : 量度数目不对
>
|