We use Library(ISLR) to use ISLR package
library(ISLR)
We will analyze the “Smarket” data set from the ISLR package in R. For this data fortunately the observations, i.e., row vectors, are ordered by time, “Year.” It is very hard to see the trends of these percentages via human eyes, but we can try to extract some information from this data using what we learned in this section. Since these observations are ordered, we can see how these percentages are up or down in terms of time. For example, we want to know the trend of the percentage returned for the previous day (the second column vector of the “Smarket” matrix), and we can plot them in terms of time. In order to do so, we will use the plot() function in R. First we will extract information on the number of row vectors (the number of observations) and the number of columns of the “Smarket” matrix.
dim(Smarket)
## [1] 1250 9
We know we have 1250 row vectors and 9 column vectors. In the plot() function, the first argument is the x-axis, and the second argument is the y-axis. In this example, since all observations are ordered by their record time from oldest to newest, we will assign the index for each observation by using a colon “:.”
x <- 1:1250
Then we use the plot() function. Here we assign the index of the observations as the x-axis and the percentage for the previous day (the second column of the matrix) as the y-axis.
plot(x, Smarket[,2],type="l")
so we will focus on the observations from the year 2001. The first 242 observations in the data set are from the year 2001, so we will take the first 242 observations using a colon “:” command. The command
Smarket[1:242,2]
## [1] 0.381 0.959 1.032 -0.623 0.614 0.213 1.392 -0.403 0.027 1.303
## [11] 0.287 -0.498 -0.189 0.680 0.701 -0.562 0.546 -1.747 0.359 -0.151
## [21] -0.841 -0.623 -1.334 1.183 -0.865 -0.218 0.812 -1.891 -1.736 -1.851
## [31] -0.195 -0.556 1.749 -0.766 -1.431 0.104 -0.568 0.586 0.998 0.645
## [41] 0.226 -2.476 -4.318 1.483 -2.584 0.587 -1.962 1.763 -2.408 -1.792
## [51] -0.406 1.991 1.128 2.557 -2.443 -0.463 1.078 -1.246 -3.439 -0.290
## [61] 4.368 -1.998 0.812 2.707 -0.213 1.510 -0.323 1.028 3.889 1.254
## [71] -0.854 -1.498 -1.216 1.594 0.470 1.501 -0.287 1.359 0.078 -1.487
## [81] 1.444 -0.245 -0.183 -0.449 -0.029 -0.758 0.261 0.042 2.845 0.272
## [91] 0.269 1.615 -0.263 -1.553 0.320 -1.182 -0.779 -1.566 0.620 0.386
## [101] 0.511 1.299 -1.055 0.546 -0.940 -0.836 0.116 -1.135 -1.750 -0.452
## [111] -0.488 0.343 0.871 1.136 -0.945 -0.551 -0.151 -0.468 1.249 -0.148
## [121] 1.008 -0.184 -1.232 -2.350 0.688 -1.440 -0.113 2.369 0.624 -1.088
## [131] 0.997 -0.554 0.605 -0.343 -1.637 -1.627 1.608 1.045 0.240 -0.108
## [141] 0.557 0.388 0.396 -0.524 -1.142 0.327 -1.733 -0.008 0.569 0.095
## [151] -0.383 -0.734 0.309 -1.666 0.812 -1.208 0.696 -0.276 1.965 -0.483
## [161] -1.501 -1.115 -1.700 0.403 -0.056 -0.106 -2.239 -1.864 0.623 -4.922
## [171] -0.580 -1.611 -3.106 -1.903 3.898 0.879 -0.517 1.149 2.192 -0.230
## [181] 1.231 1.993 -0.247 0.164 -0.834 -0.536 2.294 1.521 -0.527 -0.153
## [191] 0.694 -1.863 -0.787 0.456 1.530 -0.470 0.039 1.372 0.411 -2.382
## [201] -1.717 -0.001 2.295 0.286 1.439 1.453 -0.273 0.246 0.158 -0.177
## [211] 1.856 0.186 0.090 -0.314 1.090 -0.730 -0.493 1.171 0.615 -0.684
## [221] -1.825 1.035 -0.066 -0.838 1.319 2.232 -0.278 -0.753 -1.587 -0.278
## [231] 0.027 -1.556 0.331 1.003 0.755 0.581 -0.838 0.435 -0.021 0.412
## [241] 0.675 0.336
displays percentages for the previous day from the year 2001. Also, for the index, we will take the first 242 indices.
x <- seq_along(Smarket[1:242,2])
Now we will use the plot() function.
plot(x, Smarket[1:242,2],type="l")