#Foundation and Application of Econometrics, Spring 2015, Pepared by Dr. I-Ming Chiu #Accompany 368_Handout01.pdf rm(list=ls()) getwd() #find where the default working directory is setwd() #you need to fill in the path of your working directory or do it from the pull-down manual #Codes used in part I rm(list=ls()) studentID = c(1, 2, 3, 4, 5) examscore = c(85, 77, 63, 90, 68) studytime = c(2, 1.5, 0.9, 2.2, 1.3) gender = c("F", "M", "M", "F","F") mode(studentID) mode(gender) Try the following studentIDnew = c("1", "2", "3", "4", "5") mode(studentIDnew) #What’s the outcome? Make a guess. academicrecord = data.frame(studentID, examscore, studytime, gender) #create a data frame x = seq(1,20) y = seq(1,20, 2) z = 1:10 x[x > 10] #using condition x[11:20] #using location (index) x[-(1:10)] #using the location of the complements #Codes used in part II #Example ONE rm(list=ls()) Data1 = read.table("Chicago.txt",header=T) # data file is in the working directory. Data1 = read.table("C:/CHIU_DOCUMENT/Academics_School/Rutgers All/2015/FAE 368/Data/Chicago.txt",header=T) # if the data file is stored in other folder, the folder path must be provided either manually or using the window manual under "File" option names(Data1) #show variable names dim(Data1) #dimension of Data1 str(Data1) #structure of Data1 head(Data1) #show the first six observations (the default) of Data1 head(Data1, 10) #show the first ten observations of Data1 Data1[1:2,1:4] #show the first two rows and the first four columns of Data1 Data1$income #the outcome is the whole column of “income” variable Data1[,7] = Data1$income/1000 #rescale the “income” variable head(Data1) # the first six observations of Data1 after rescaling boxplot(Data1$income) #boxplot (1st, 2nd and 3rd quartile, etc) hist(Data1$income) #histogram summary(Data1$income) #summarize important statistics for income variable plot(Data1$race,Data1$involact,main="FAIR insurance vs. Race") #draw a scatter diagram abline(lm(Data1$involact~Data1$race)) #add a regression line to the scatter diagram pairs(Data1) #pair-wise association plot for all the variables (seven of them) in Data1 #Example TWO install.packages(quantmod) #I will explain what package is in class library(quantmod) #use “library” to call out the package getSymbols("GOOG", src = "yahoo") #src = source of the web site head(GOOG) plot(GOOG[,4],main="Google/Closed Price") # GOOG[,4]: fourth column in data GOOG summary(GOOG[,4]) #summarize the data data.2014.08 = window(GOOG,start=as.Date("2014-08-01"),end=as.Date("2014-12-31")) #"window": subset the data dim(data.2014.08) summary(GOOG[,4]) #summarize the data data.2014.08 = window(GOOG,start=as.Date("2014-08-01"),end=as.Date("2014-12-31")) #"window": subset the data dim(data.2014.08) plot(data.2014.08[,4],main="Google/Closed Price (2014/08~2014/12)") summary(data.2014.08[,4]) #Codes used in part III #Population Growth Forecasting/A Simulated Model rm(list=ls()) N = rep(0,11) N[1] = 324.01 #http://www.worldometers.info/world-population/us-population/ Time = rep(0,11) Time[1] = 2014 a = 0.007 #average popultaiton growth rate in the us between 2009 and 2013 (The World Bank data) #start looping here for (i in 2:11){ N[i] = (1 + a)*N[i-1] + rnorm(1,10,4) Time[i] = 2013 + i } plot(Time[2:11], N[2:11], type="b", main = "Population Forescast (2015~2024)",col="red")