3. Inspecting time series. The dataframe, ts_simple_returns
, which you constructed in the previous problem
has been defined for you.
Use the summary()
function to compute descriptive statistics for the dataframe.
Use the plot()
function to visualize the time series for the simple returns to Microsoft's stock.
date <- c("Jan-02","Feb-02","Mar-02","Apr-02","May-02","Jun-02","Jul-02","Aug-02","Sep-02","Oct-02",
"Nov-02","Dec-02","Jan-03","Feb-03","Mar-03","Apr-03","May-03","Jun-03","Jul-03","Aug-03","Sep-03",
"Oct-03","Nov-03","Dec-03","Jan-04","Feb-04","Mar-04","Apr-04","May-04","Jun-04","Jul-04","Aug-04",
"Sep-04","Oct-04","Nov-04","Dec-04","Jan-05","Feb-05","Mar-05","Apr-05","May-05","Jun-05","Jul-05",
"Aug-05","Sep-05","Oct-05","Nov-05","Dec-05","Jan-06","Feb-06","Mar-06","Apr-06","May-06","Jun-06",
"Jul-06","Aug-06","Sep-06","Oct-06","Nov-06","Dec-06","Jan-07","Feb-07","Mar-07","Apr-07","May-07",
"Jun-07","Jul-07","Aug-07","Sep-07","Oct-07","Nov-07","Dec-07","Jan-08","Feb-08","Mar-08","Apr-08",
"May-08","Jun-08","Jul-08","Aug-08","Sep-08","Oct-08","Nov-08","Dec-08","Jan-09","Feb-09","Mar-09",
"Apr-09","May-09","Jun-09","Jul-09","Aug-09","Sep-09","Oct-09","Nov-09","Dec-09","Jan-10","Feb-10",
"Mar-10","Apr-10","May-10","Jun-10","Jul-10","Aug-10","Sep-10","Oct-10","Nov-10","Dec-10","Jan-11",
"Feb-11","Mar-11","Apr-11","May-11","Jun-11","Jul-11","Aug-11","Sep-11","Oct-11","Nov-11","Dec-11",
"Jan-12","Feb-12","Mar-12","Apr-12","May-12","Jun-12","Jul-12","Aug-12","Sep-12","Oct-12","Nov-12",
"Dec-12","Jan-13","Feb-13","Mar-13","Apr-13","May-13","Jun-13","Jul-13","Aug-13","Sep-13","Oct-13",
"Nov-13","Dec-13","Jan-14","Feb-14","Mar-14","Apr-14","May-14","Jun-14","Jul-14","Aug-14","Sep-14",
"Oct-14","Nov-14","Dec-14","Jan-15","Feb-15","Mar-15","Apr-15","May-15","Jun-15","Jul-15","Aug-15",
"Sep-15","Oct-15","Nov-15","Dec-15","Jan-16","Feb-16","Mar-16","Apr-16","May-16","Jun-16","Jul-16",
"Aug-16","Sep-16","Oct-16","Nov-16","Dec-16","Jan-17","Feb-17","Mar-17","Apr-17","May-17","Jun-17",
"Jul-17","Aug-17","Sep-17","Oct-17","Nov-17","Dec-17","Jan-18","Feb-18")
sp500 <- c(1130.199951,1106.72998,1147.390015,1076.920044,1067.140015,989.820007,
911.619995,916.070007,815.280029,885.76001,936.309998,879.820007,855.700012,
841.150024,848.179993,916.919983,963.590027,974.5,990.309998,1008.01001,995.969971,
1050.709961,1058.199951,1111.920044,1131.130005,1144.939941,1126.209961,1107.300049,
1120.680054,1140.839966,1101.719971,1104.23999,1114.579956,1130.199951,1173.780029,
1211.920044,1181.27002,1203.599976,1180.589966,1156.849976,1191.5,1191.329956,
1234.180054,1220.329956,1228.810059,1207.01001,1249.47998,1248.290039,1280.079956,
1280.660034,1302.880005,1310.609985,1270.050049,1270.060059,1278.530029,1303.800049,
1335.819946,1377.76001,1400.630005,1418.030029,1437.900024,1406.800049,1420.829956,
1482.369995,1530.619995,1504.660034,1455.180054,1473.959961,1527.290039,1545.790039,
1479.630005,1467.969971,1378.599976,1330.449951,1326.410034,1385.969971,1399.619995,
1276.689941,1269.420044,1287.829956,1164.170044,968.669983,888.609985,902.98999,
823.090027,729.570007,793.590027,872.73999,923.26001,920.820007,990.219971,1019.52002,
1054.910034,1036.180054,1098.890015,1116.560059,1073.890015,1105.359985,1171.22998,
1188.579956,1087.300049,1031.099976,1107.530029,1049.719971,1143.48999,1185.709961,
1186.599976,1257.619995,1289.140015,1328.640015,1329.47998,1365.209961,1345.199951,
1320.640015,1292.589966,1219.119995,1131.209961,1251,1246.910034,1258.859985,1312.449951,
1365.900024,1408.469971,1397.859985,1309.869995,1362.329956,1379.319946,1406.540039,
1440.900024,1412.199951,1416.339966,1426.189941,1498.109985,1514.680054,1569.180054,
1597.550049,1631.709961,1609.780029,1689.420044,1635.949951,1682.410034,1758.699951,
1806.550049,1845.859985,1782.680054,1857.680054,1873.959961,1884.390015,1923.869995,
1962.290039,1929.800049,2004.069946,1971.439941,2018.209961,2065.780029,2058.899902,
1996.670044,2105.22998,2067.629883,2087.379883,2108.639893,2067,2104.48999,1970.089966,
1919.650024,2080.76001,2082.929932,2038.199951,1936.939941,1937.089966,2056.620117,
2067.169922,2093.939941,2099.340088,2173.149902,2171.330078,2164.330078,2128.679932,
2200.169922,2251.570068,2285.590088,2380.129883,2362.340088,2388.5,2415.649902,
2431.389893,2477.100098,2474.419922,2521.199951,2583.209961,2645.100098,2683.72998,
2816.449951,2715.219971)
microsoft <- c(31.855,29.17,30.155001,26.129999,25.455,27.35,23.99,24.540001,21.870001,26.735001,
28.84,25.85,23.73,23.700001,24.209999,25.57,24.610001,25.639999,26.41,26.52,27.799999,
26.139999,25.709999,27.370001,27.65,26.530001,24.93,26.129999,26.23,28.559999,
28.49,27.299999,27.65,27.969999,26.809999,26.719999,26.280001,25.16,24.17,25.299999,
25.799999,24.84,25.610001,27.379999,25.73,25.700001,27.68,26.15,28.15,26.870001,
27.209999,24.15,22.65,23.299999,24.059999,25.700001,27.35,28.709999,29.360001,29.860001,
30.860001,28.17,27.870001,29.940001,30.690001,29.469999,28.99,28.73,29.459999,
36.810001,33.599998,35.599998,32.599998,27.200001,28.379999,28.52,28.32,27.51,25.719999,
27.290001,26.690001,22.33,20.219999,19.440001,17.1,16.15,18.370001,20.26,20.889999,
23.77,23.52,24.65,25.719999,27.73,29.41,30.48,28.18,28.67,29.290001,30.540001,
25.799999,23.01,25.809999,23.469999,24.49,26.67,25.26,27.91,27.73,26.58,25.389999,
25.92,25.01,26,27.4,26.6,24.889999,26.629999,25.58,25.959999,29.530001,31.74,32.259998,
32.02,29.190001,30.59,29.469999,30.82,29.76,28.540001,26.620001,26.709999,27.450001,
27.799999,28.610001,33.099998,34.900002,34.540001,31.84,33.400002,33.279999,35.41,38.130001,
37.41,37.84,38.310001,40.990002,40.400002,40.939999,41.700001,43.16,45.43,46.360001,
46.950001,47.810001,46.450001,40.400002,43.849998,40.66,48.639999,46.860001,44.150002,
46.700001,43.52,44.259998,52.639999,54.349998,55.48,55.09,50.880001,55.23,49.869999,
53,51.169998,56.68,57.459999,57.599998,59.919998,60.259998,62.139999,64.650002,63.98,
65.860001,68.459999,69.839996,68.93,72.699997,74.769997,74.489998,83.18,84.169998,
85.540001,95.010002,93.769997)
# Import zoo library.
library(zoo)
# Define data frame.
data = data.frame(date, sp500, microsoft)
# Compute number of rows.
N <- nrow(data)
# Define date vector for new dataframe.
date <- as.yearmon(date[2:N], format='%b-%y')
# Define new dataframe.
simple_returns = data.frame(date)
simple_returns$sp500 <- 100*(data$sp500[2:N]/data$sp500[1:N-1] - 1)
simple_returns$microsoft <- 100*(data$microsoft[2:N]/data$microsoft[1:N-1] - 1)
# Drop infs.
simple_returns <- simple_returns[is.finite(simple_returns$microsoft) & is.finite(simple_returns$sp500), ]
# Import the xts library.
library(xts)
# Convert columns of simple_returns (other than date) into a time series object.
ts_simple_returns <- xts(simple_returns[, -1], order.by=simple_returns$date)
# Plot microsoft time series.
summary(___)
# Plot microsoft time series.
___
# Plot microsoft time series.
summary(ts_simple_returns)
# Plot microsoft time series.
plot(ts_simple_returns$microsoft)
test_error()
success_msg("Well done!")
R Console
to see the summary statistics you generated.