03_R_intro

# 03_R_intro - Time Series Analysis  AMS 316 ...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Time Series Analysis  AMS 316  Programming language and software environment  for data manipulation, calculation and graphical  display.  Originally created by Ross Ihaka and Robert  Gentleman at University of Auckland, and now  developed by the R Development Core Team.   T IS FREE  I   re-compiled binary versions are provided for Microsoft  P Windows, Mac OS X, and several other Linux/Unix-like  operating systems    pen source code available freely available on GNU  O General Public License    or computationally-intensive tasks, C, C++ and Fortran  F code can be linked and called at run time    n effective data handling and storage facility  A    suite of operators for calculations on arrays, in  A particular matrices     large, coherent, integrated collection of intermediate  A tools for data analysis    raphical facilities for data analysis and display either  G directly at the computer or on hardcopy  >help.start() > help(solve) > ?solve // get more information on “solve” > help(“[[“) > help.start()   // help for special characters and “if”, “for”… // launch a Web browser 1. R is case sensitive. 2. Commands can be executed by calling an external file. > source(“commands.R”) 3. The following functions can be used to display the names of (most of) the objects which are currently stored within R. > objects() > ls() 4. Objects can be removed by the following function. > rm(x, y, z) 1.  Vectors and asignments > x<- c(10.4, 5.6, 4.1) > assign(“x”, c(10.4, 5.6, 4.1) ) > y <- c(x, 0, 1/x) 2. 3. Vector arithmetic > v<-2*x + y +1 > sum( (x-mean(x))^2/length(x)-1) > sort(x) // returns a vector of the same size as x // with the elements arranged in increasing order. > max(x) > min(x) Sequence generation > z <- seq(-5, 5, by=0.2) > z <- rep(x, times=5) File types that can be imported into R:          .data, .txt, .xls, .xlsx, .html, .xml, etc.  Example of importing text files into R:  data<-read.table(“C:/……/data.txt”, header=TRUE, sep=“\t”)  Other data import commands: scan() ……  For data import/export:   http://cran.r-project.org/doc/manuals/R-data.html  Writing your own functions attach(dummy)    //Make the columns in the data frame visible as variables//   lrf <- lowess(x, y)    //Make a nonparametric local regression function//  plot(x, y)      //Standard point plot//  lines(x, lrf\$y)    //Add in the local regression//  abline(0, 1, lty=3)    //The true regression line: (intercept 0, slope 1)//  abline(coef(fm))    //Unweighted regression line//  abline(coef(fm1), col = "red")  //Weighted regression line//  detach()      //Remove data frame from the search path//  plot(fitted(fm),  resid(fm),            xlab="Fitted values", ylab="Residuals",            main="Residuals vs Fitted")         /*A standard regression diagnostic plot to check for     qqnorm(resid(fm), main="Residuals Rankit Plot")        /*A normal scores plot to check for skewness, kurtosis and     rm(fm, fm1, lrf, x, dummy)  //Clean up again//  Plot Types: Line Charts, Bar Charts, Histograms, Pie Charts, Dot  Charts, etc.  Format:  >PLOT-TYPE(PLOT-DATA, DETAILS)  PLOT-TYPE: plot, plot.xy, barplot, pie, dotchart, etc.  PLOT-DATA: Data, Data\$XXX, as.matrix(Data), etc.  Details: axes, col, pch, lty, ylim, type, xlab, ylab, etc.  For graphics plot:  http://www.harding.edu/fmccown/R/  A comparison of GM monthly returns & SP500  monthly returns. GM and SP500 monthly return  data during the period of Jan. 2002 to Jun. 2007  are taken. Plotted in R, they will be analyzed and  compared.  Data from: http://www.stanford.edu/~xing/ statfinbook/data.html  GM<-read.table("C:/R Data/GM.txt", header=TRUE, sep=“")  SP<-read.table("C:/R Data/SP.txt", header=TRUE, sep="")  plot(GM)  lines(GM\$logret, lty=1)  lines(SP\$logret, type="o", lty=1, pch="+", col="red")  x<-1:66  GML<-lm(GM\$logret~x)  SPL<-lm(SP\$logret~x)  abline(coef(GML), type="h", lwd=3)  abline(coef(SPL), col="red", type="h", lwd=3)  Introduction to packages All R functions and datasets are stored in packages. Only when a  package is loaded are its contents available. This is  down both for  efficiency, and to aid package developers.  To see which packages are installed at your site, issue the  command  >library(boot)  Users connected to the Internet can use install.packages() and  update.packages() to install and update packages.  To see packages currently loaded, use search().  An easier way: use TS functions to plot time series. US unemployment rate from 1987 to 2007 are taken as a time series for analysis. Data from http://www.stanford.edu/~xing/statﬁnbook/data.html 2004~2006 1948~2007 An easier way: use TS functions to plot time series. US unemployment rate from 1987 to 2007 are taken as a time series for analysis. Data from http://www.stanford.edu/~xing/statﬁnbook/data.html ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online