ex2.nb.html - Code Show All Code Hide All Code Download Rmd...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Code Show All Code Hide All Code Download Rmd Data import, manipulation and plotting Data import and preparation Robert J. Shiller dataset We begin by importing the data that we have at our disposition. 2. load data into R studio and label time series appropriately. dataie <- read.table(file = "/Users/robertamorelli/Desktop/Econometrics /data/ie_data2016_11.csv",header = T, skip = 7, nrows = 1751, stringsAsFactors = F, sep = ";", dec = ".") dataie <- data.matrix(dataie[1:1751, 1:11]) # this is done to convert everything into numbers. Is a way to ensure that you won't have string characters due to improper reading of the .csv file. In case at least one of the column entries is a character string, all the column will be coerced to the string format. Il morale della favola: construct your datasets properly! head(dataie) # displays several first entries of the object (vector, matrix, data frame etc.) dimnames(dataie)[[2]] #displays the names of the columns if such are present Having loaded our dataset, we want to convert it to a format that will be useful for the analysis. There are two competing ways to store the data: using data frames or using arrays. Arrays permit to store multidimensional arrays of data of the same type , while data frames store the data in two dimensions (just like a matrix), allowing though for the storage of different types of data. We will store the useful data in a data frame, as this format provides several benefits regarding subsetting, plotting and function methods. Instead, the use of arrays will be done for the intermediate computations, as sometimes it is easier to logically allocate data in multidimensional classification. Another useful feature we will be making use of is the time-series data class. It will be useful for convenient plotting and, sometimes, subsetting. tsdata <- ts(dataie, start = c(1871, 1), frequency = 12, names = dimnames(dataie) [[2]]) # creates a time series object tsdata <- na.omit(tsdata) # omitting the rows with NA presence s1 <- window(tsdata[, "P"], start = c(1900, 1), end = c(1950, 12)) # creating a subset of a time series. With time series objects subsetting works in a slightly different way, so be careful plot(s1, ylab = "Price", main = "S&P500 price", col = "red", lwd = 2) # a hint of how it looks # now I want to initiate the data frame, in which we will store the series useful for the future data01 <- as.data.frame(tsdata) # be careful as the data do not possess the time series class any more. # to have a proper time index, we add another columnt to the data frame data01$date <- seq(from = tsp(tsdata)[1], to = tsp(tsdata)[2], length.out = nrow(tsdata))
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Data transformations All the essential data transformations, such as taking logarithms, performing linear transformations or taking the lagged values is performed in an easy way for any arrays 1 and time series in particular. t0 <- tsp(tsdata)[1] tT <- attributes(tsdata)$tsp[2] x0 <- tsdata[, "P"] x1 <- log(x0) x2 <- x0 * 12 + 4 x3 <- stats::lag(x0, 240) par(mfrow = c(2, 2)) plot(x0, main = "price") plot(x1, main = "log of price") plot(x2, main = "linear transformation of price") plot(x3, main = "lag of price", xlim = c(t0, tT)) plot(window(x3, start = c(1900, 1), end = c(1950, 12)), main = "lag of price") par(mfrow = c(1, 1)) We can also obtain summary statistics of all the data or, otherwise, compute separate statistics of interest.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern