######################################################################################## ######################################################################################## #I. play with some data ######################################################################################## ######################################################################################## # A. a number ######################################################################################## X<-3 X class(X) str(X) # B. a word ######################################################################################## X<-"butIDontWannaCode" X class(X) str(X) # C. a vector ######################################################################################## X<-c(1,2,3,4,5) X class(X) str(X) # D. a mixed bag? ######################################################################################## X<-c(1,2,3,"I","still","dont","wanna","code") X class(X) str(X) # E. see whats in your workspace ######################################################################################## ls() # F. remove X, then look again ######################################################################################## rm(X) ls() ######################################################################################## ######################################################################################## #0. logistics--btw, '#' at start of line means the line is a comment ######################################################################################## ######################################################################################## #A. where are you? # Note that, although it is a PC, the file-naming conventions are like unix/linux # e.g., "/" instead of "\" in full-path file names # also upper-vs-lower case matters! # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - getwd() #B. where would you like to be? # 1. what commands might be available to help us with directories? # "apropos" can search for commands containing a string we provide apropos("folder") #darn, no commands with "folder" in them apropos("dir") #yes! "dir" was a good hint for things we can do with irectories # 2. try for help on a command that looks good... precede the command with "?" ?dir.exists # 3. apply your chosen command and set the working directory print(dir.exists("D:/ibf")) #machine-specific to the PC used for demo # a. set the working directory if(dir.exists("D:/ibf")) { setwd("D:/ibf") #again, machine-specific to the PC used for demo getwd() } # b. how to see the files? apropos("files") #show commands with word "files" ?list.files #get help on a command that looks good list.files() #execute the command # i. regular expression--ends with "r" # nice summary at https://www.icewarp.com/support/online_help/203030104.htm #list.files(pattern="*.r$") # c. check the new location # note that getwd() and print(getwd()) appear to have the same effect # why? any command--like getwd()--renders its result to the environment from which it was called.... # in this case, the interactive terminal print(getwd()) ######################################################################################## ######################################################################################## #I. get some data ... ######################################################################################## ######################################################################################## #A. if youre NOT running online.... saras package installer/loader ######################################################################################## source("http://www.ceresanalytics.com/R_for_IBF_BootCamp/installLoadPkg.r") # 1. package needed to read excel installLoadPkg("psych") #A. From the web, load this file: ________________ # 1. for ease of access, assign the filename to a character variable # this can be a web reference (as shown) or a filename using unix-like syntax, # e.g. "C:/users/sara/myFile.csv" myFilename<-"http://ceresanalytics.com/R_for_IBF_BootCamp/EDA_timeSeries.txt" # 2. then read it in # the read.delim functions first argument is file... # you can either specify parameters by name, or place them in the order read tryCatch({ myData<-read.table(myFilename,sep="\t",header=TRUE) }, error=function(e) { print(paste("YOU HAVE NO FILE NAMED",myFilename)) }) # Note the use of "<-" in assignments. This is a good pracice! # Why: "=" is used for temporary assignments, such as passing parameters to functions # "==" is used to test equality of values # So youll always be able to search your code for assignments, passed parameters, and equivalence tests # *if* you restrict usage of "<-","=", and "==" to each # a. find out about the cars dataframe you just read in str(myData) # b. render it to your terminal # when you just "blurt out" an object, it will be rendered to whatever environment # from which its called myData # c. what class is it class(myData) ######################################################################################## ######################################################################################## #II. Make a var for test/train, then create training and test datasets ######################################################################################## ######################################################################################## # A. initialize as 1 (for "yes") ######################################################################################## myData$isTrain<-1 str(myData) # B. change to 1 for last 4 observations ######################################################################################## # 1. how many rows and columns? # a. rows nrow(myData) # b. columns ncol(myData) # c. dimensions dim(myData) # 2. use subsetting [] to isolate last 4 rows # a. see it myData$isTrain # b. change it myData$isTrain[(nrow(myData)-3):nrow(myData)]<-0 # c. see the data print(myData) # C. create training and test datasets ######################################################################################## trainData<-myData[myData$isTrain==1,] testData<-myData[myData$isTrain==0,] # 1. check all dimensions dim(trainData) dim(testData) dim(myData) ######################################################################################## ######################################################################################## #II. Exploratory Data Analysis # NOTE: all data included, though a purist might look only at training data ######################################################################################## ######################################################################################## # A. what does a univariate line plot look like, as raw data? ######################################################################################## plot( x=myData$GMER, type="l" ) # 1. for plotting, you can suppress the X axis and put in your own labels # the catch is, just like a simple univariate plot, your X axis doesnt scale # see https://www.statmethods.net/advgraphs/axes.html plot( x=myData$GMER, type="l", xaxt="n" ) # 2. do it again, suppressing X-axis labels, then label the axes the way you want plot( x=myData$GMER, type="l", xaxt="n" ) # a. add the labels axis(1, at=1:length(myData$GMER), #which positions do you label labels=myData$YearQuarterLabel, #what labels do you put there las=1 #use las=1 to put labels at right angles ) # 3. make a dep var into a time series gmer_TS<-ts(myData$GMER, frequency = 4, start = c(myData$Year[1], myData$Quarter[1])) # 1st obs year and quarter... # 4. compare the structure of original data series with time series # assumes equal periodicity--which you can fill in with a SQL query str(myData$GMER) str(gmer_TS) # 5. plot it ... dont tell it "l" this time plot(x=gmer_TS) # i. whats special about ts plot? synatax of plot command is plot.class, e.g. plot.ts class(gmer_TS) # 6. make 3 series in the data frame each into time series myData<-myData myData$FURN<-ts(myData$FURN, frequency = 4, start = c(myData$Year[1], myData$Quarter[1])) #as above myData$FURN_highgrowth<-ts(myData$FURN_highgrowth, frequency = 4, start = c(myData$Year[1], myData$Quarter[1])) #as above myData$FURN_goesToZero<-ts(myData$FURN_goesToZero, frequency = 4, start = c(myData$Year[1], myData$Quarter[1])) #as above # 7. now look at 3 at a time... # #i. min and max for uniform presentation myYMin<-min(c(myData$FURN,myData$FURN_highgrowth,myData$FURN_goesToZero)) myYMax<-max(c(myData$FURN,myData$FURN_highgrowth,myData$FURN_goesToZero)) # #ii. "par" opens your plot window, then "mfrow" is how many par(mfrow=c(1,3)) plot(myData$FURN,ylim=c(myYMin,myYMax),main="Furniture sales, base case") plot(myData$FURN_highgrowth, ylim=c(myYMin,myYMax), main="Furniture sales, high case") plot(myData$FURN_goesToZero, ylim=c(myYMin,myYMax), main="Furniture sales, low case") # B. what do potential predictors look like? ######################################################################################## # 1. in these data, potential predictors are WASA, EMPL, QTR_4 and hypoInterestRate predictorNames<-c("WASA","EMPL","QTR_4","hypoInterestRate") # a. does this vector work for subsetting? head(myData[,predictorNames]) # b. can you concatenate this vector with the predictor subsest? depVarName<-"FURN" head(myData[,c(depVarName,predictorNames)]) # 2. look at your predictors, each on their own describe(myData[,predictorNames]) # look at each predictors standard deviation relative to its mean # see how hypoInterestRate is virtually constant? # 3. look at your predictors vs. the dependent variable # youll again concatenate the dependent variable (FURN) with the predictor names (WASA, EMPL, QTR_4 and hypoInterestRate) pairs(myData[c(depVarName,predictorNames)],pch=19,lower.panel=NULL)