## R code used in Statistical Analysis of Financial Data ## James E. Gentle ## Code in the Appendix to Chapter 1 #############SF1020103##################### c <- "c" b <- 'abc' c b #############SF1020105##################### x <- 3.3 is.matrix(x) is.numeric(x) is.double(x) is.integer(x) #############SF1020107##################### y <- as.integer(x) y is.integer(y) as.character(y) as.character(x) a <- "4" is.integer(a) z <- as.integer(a) z is.integer(z) #############SF1020110##################### y <- 1:9 y x <- c(9,8,7,6,5,4,3,2,1) x 9:1 -1:2 -1:2+3 -1:(2+3) 3*-1:2 (3*-1):2 #############SF1020112##################### x <- c(9,8,7,0,10,4,3,2,1) x[1] x[c(3,1,3)] x[c(-1,-3)] length(x) length(x[c(3,1,3)]) length(x[c(-1,-3)]) which.min(x) x[which.max(x)] #############SF1020135##################### mylist <- list("one_to_three"=1:3, "a", list("four_to_six"=4:6, "b", list(7:9, "c"))) str(mylist) names(mylist) mylist$one_to_three mylist[1] mylist$one_to_three[2] mylist[[1]][2] mylist[[3]]$four_to_six mylist[[3]][1] mylist[[3]][3][1] mylist[[3]][[3]][1] #############SF1020140##################### x <- 9 sqrt(x) y <- c(1,4,9,16,25) sqrt(y) #############SF1020145##################### r1 <- c(1,2,3) r2 <- c(4,5,6) r3 <- c(7,8,9) c1 <- c(1,4,7) c2 <- c(2,5,8) c3 <- c(3,6,9) AA <- rbind(r1,r2,r3) BB <- cbind(c1,c2,c3) AA BB AA["r1",] AA["r2",] AA[r1,] AA["r1",2] AA[r1,2] apply(AA,2,mean) #############SF1020150##################### A <- matrix(c(1,2,3,4,5,6), nrow=3) dim(A) b <- A[,2] dim(b) # b is not two-dimensional length(b) C <- A[,2, drop=FALSE] dim(C) # C is two-dimensional #############SF1020160##################### Stks <- matrix(c(155.15, 200, 31.06, 400, 43.94, 400, 81.92, 300), byrow=TRUE, ncol=2) colnames(Stks) <- c("Price","Quantity") rownames(Stks) <- c("AAPL","BAC","INTC","MSFT") colnames(Stks) Stks Stks["BAC","Price"] Stks["BAC", ] Stks[, "Price"] #############SF1020162##################### Symbol <- c("AAPL", "BAC","INTC","MSFT") Price <- c(155.15, 31.06, 43.94, 81.92) Quantity <- c( 200, 400, 400, 300) Stocks <- data.frame(Symbol, Price, Quantity) Stocks Stocks$Price Stocks[1,2] Stocks[2,3] Stocks[1, ] names(Stocks) colnames(Stocks) rownames(Stocks) class(Stocks$Price) class(Stocks$Symbol) #############SF1020164##################### sex <- c("m", "m", "f", "m", "f", "f") sexf <- factor(sex) sexf levels(sexf) str(sexf) length(sexf) xf<- factor(c(5,4,3,2,1)) levels(xf) str(xf) sum(xf) #############SF1020166##################### Sector <- factor(c("Tech", "Fin", "Tech", "Tech")) Stocks <- data.frame(Sector, Symbol, Price, Quantity, stringsAsFactors=FALSE) Stocks class(Stocks$Symbol) class(Stocks$Sector) #############SF1020169##################### x <- -4 z <- sqrt(x) # x is numeric, and z must be numeric z <- sqrt(as.complex(x)) z is.complex(z) is.numeric(z) y <- complex(r=3,i=4) y y <- 3+4i y Mod(y) Conj(y) #############SF1020171##################### c <- "c" length(c) nchar(c) b <- 'abcde' length(b) nchar(b) d <-c(c,b) d length(d) nchar(d) e <- paste(c,b) e length(e) nchar(e) f <- paste(c,b,sep="") f length(f) nchar(f) substring(f,1,1) substring(f,c(1,2),c(1,6)) #############SF1020174##################### x <- as.Date("Oct 24, 1929",format="%b %d, %Y") y <- as.Date("29 October 1929",format="%d %B %Y") z <- as.Date("10/19/87",format="%m/%d/%y") class(x) mode(x) format(z, "%b %d, %Y") weekdays(c(x,y,z)) z-x x-y y x+5 class(x-y) seq(x, length=6, by="days") seq(x, length=6, by="weeks") #############SF1020177##################### model1<-formula("y~x1+x1^2+log(x2)") model1 class(model1) #############SF1020180##################### dpois(3,lambda=5) ppois(3,lambda=5) qpois(0.2650259,lambda=5) #############SF1020182##################### S1 <- c(1,2,3,2) S2 <- c(3,2,1) S3 <- c(4,3,2,1) setequal(S1,S2) union(S1,S3) union(S1,S1) intersect(S1,S3) intersect(S1,S1) 1 %in% S1 5 %in% S1 #############SF1020184##################### myFun <- function(x1,y1, x2,y2, x) { # Given the points (x1,y1) and (x2,y2) and a set of abscissas x, # determine the ordinates at x for the line that goes through all of them. slope <- (y2-y1)/(x2-x1) intercept <- y1 - slope*x1 y <- slope*x + intercept return(y) } #############SF1020188##################### t <- function(x) return(x+5) x <- matrix(c(1,2,3,4),nrow=2) t(x) base::t(x) #############SF1020209##################### set.seed(12345) lambda <- 5 n <- 1000 x <- rpois(n, lambda=lambda) mean(x) sd(x) #############SF1020215##################### set.seed(12345) m <- 100 s <- 10 n <- 1000 x <- rnorm(n, mean=m, sd = s) mean(x) sd(x) #############SF1020225##################### library(xts) dates17 <- lubridate::date( c("2017-02-01","2017-02-02","2017-02-03", "2017-01-27","2017-01-30","2017-01-31", "2017-01-03","2017-01-04","2017-01-05")) INTC <- c( 36.52, 36.68, 36.52, 37.98, 37.42, 36.82, 36.60, 36.41, 36.35) GSPC <- c(2279.55, 2280.85, 2297.42, 2294.69, 2280.90, 2278.87, 2257.83, 2270.75, 2269.00) datadf <- data.frame(INTC,GSPC) rownames(datadf) <- dates17 datadf ## a data frame dataxts <- xts(datadf,dates17) dataxts ## an xts object #############SF1020227##################### dataxts[8,2] dataxts["20170202", 2] dataxts["2017-02-02", "GSPC"] dataxts["20170202"] dataxts["201702"] dataxts["201702/201702"] # same dataxts["20170128/20170201", "INTC"] dataxts["20170131/"] #############SF1020228##################### dataxtsw <- to.period(dataxts, "weeks", OHLC=FALSE) dataxtsw dataxtsm1 <- to.period(dataxts, "months", OHLC=FALSE) dataxtsm1 dataxtsm2 <- to.period(dataxtsm1, "months", OHLC=FALSE) dataxtsm2 #############SF1020235##################### dataxts1<-xts(matrix(c(36.35,36.52, 62.58,63.68),ncol=2), order.by=date(c("2017-01-05","2017-02-03"))) names(dataxts1)=c("INTC","MSFT") dataxts2<-xts(c(NA,2294.69,2297.42), order.by=date(c("2017-01-05","2017-01-27","2017-02-03"))) names(dataxts2)="GSPC" dataxts1 dataxts2 merge(dataxts1, dataxts2) merge(dataxts1, dataxts2,join="inner") na.omit(merge(dataxts1, dataxts2)) #############SF1020250##################### library(Rcpp) cppFunction("NumericVector myFunC(double x1,double y1, double x2,double y2, NumericVector x) { //Given the points (x1,y1) and (x2,y2) and a set of abscissas x, //determine the ordinates at x for the line that goes through all of them. int n = x.size(); NumericVector y(n); double slope = (y2-y1)/(x2-x1); double intercept = y1 - slope*x1; for (int i = 0; i