Questions
Exercise 1
# 1.
tsibble <- tute1 %>%
mutate(Quarter = yearquarter(Quarter)) %>%
as_tsibble(index = Quarter)
tsibble
## # A tsibble: 100 x 4 [1Q]
## Quarter Sales AdBudget GDP
## <qtr> <dbl> <dbl> <dbl>
## 1 1981 Q1 1020. 659. 252.
## 2 1981 Q2 889. 589 291.
## 3 1981 Q3 795 512. 291.
## 4 1981 Q4 1004. 614. 292.
## 5 1982 Q1 1058. 647. 279.
## 6 1982 Q2 944. 602 254
## 7 1982 Q3 778. 531. 296.
## 8 1982 Q4 932. 608. 272.
## 9 1983 Q1 996. 638. 260.
## 10 1983 Q2 908. 582. 280.
## # … with 90 more rows
## # ℹ Use `print(n = ...)` to see more rows
# 2.
# plotting each variable using geom_line
ggplot(data = tsibble, mapping = aes(x = Quarter, y = Sales)) +
geom_line()

ggplot(data = tsibble, mapping = aes(x = Quarter, y = AdBudget)) +
geom_line()

ggplot(tsibble, aes(x = Quarter, y = GDP)) +
geom_line()

# 3.
# changing the rows and columns to use for facet_grid
tsibble2 <- tsibble %>%
pivot_longer(cols = Sales:GDP, names_to = "Variable", values_to = "count")
tsibble2
## # A tsibble: 300 x 3 [1Q]
## # Key: Variable [3]
## Quarter Variable count
## <qtr> <chr> <dbl>
## 1 1981 Q1 Sales 1020.
## 2 1981 Q1 AdBudget 659.
## 3 1981 Q1 GDP 252.
## 4 1981 Q2 Sales 889.
## 5 1981 Q2 AdBudget 589
## 6 1981 Q2 GDP 291.
## 7 1981 Q3 Sales 795
## 8 1981 Q3 AdBudget 512.
## 9 1981 Q3 GDP 291.
## 10 1981 Q4 Sales 1004.
## # … with 290 more rows
## # ℹ Use `print(n = ...)` to see more rows
# Now, plotting with facet_grid
ggplot(data = tsibble2) +
geom_line(mapping = aes(x = Quarter, y = count)) +
facet_grid(cols = vars(Variable))

Exercise 2
# 1.
# I used the function 'install.packages("USgas")' to install USgas and then viewed the data
View(us_total)
# 2.
# I constructed a tsibble with year as the index and state as the key.
tsibl <- us_total %>%
as_tsibble(index = year, key = state)
tsibl
## # A tsibble: 1,266 x 3 [1Y]
## # Key: state [53]
## year state y
## <int> <chr> <int>
## 1 1997 Alabama 324158
## 2 1998 Alabama 329134
## 3 1999 Alabama 337270
## 4 2000 Alabama 353614
## 5 2001 Alabama 332693
## 6 2002 Alabama 379343
## 7 2003 Alabama 350345
## 8 2004 Alabama 382367
## 9 2005 Alabama 353156
## 10 2006 Alabama 391093
## # … with 1,256 more rows
## # ℹ Use `print(n = ...)` to see more rows
# 3.
# I first manipulated the data below
tsibl2 <- tsibl %>%
filter(state == c("Maine", "Vermont", "New Hampshire", "Massachusetts", "Connecticut", "Rhode Island")) %>%
mutate(y = y / 1e3)
# and then plotted the results
ggplot(tsibl2, mapping = aes(x = year, y = y, color = state)) +
geom_line() +
labs(y = "Gas Consumption (thousands)", x = "Year", title = "Demand for Natural Gas")

Exercise 3
# 1.
# Make it a tsibble identical to tourism data set
# When you want to make a date into a year with quarter that looks better
tourism2 <- tourism %>%
mutate(Quarter = yearquarter(Quarter))%>%
as_tsibble(index = Quarter, key = c(Region, State, Purpose))
tourism2
## # A tsibble: 24,320 x 5 [1Q]
## # Key: Region, State, Purpose [304]
## Quarter Region State Purpose Trips
## <qtr> <chr> <chr> <chr> <dbl>
## 1 1998 Q1 Adelaide South Australia Business 135.
## 2 1998 Q2 Adelaide South Australia Business 110.
## 3 1998 Q3 Adelaide South Australia Business 166.
## 4 1998 Q4 Adelaide South Australia Business 127.
## 5 1999 Q1 Adelaide South Australia Business 137.
## 6 1999 Q2 Adelaide South Australia Business 200.
## 7 1999 Q3 Adelaide South Australia Business 169.
## 8 1999 Q4 Adelaide South Australia Business 134.
## 9 2000 Q1 Adelaide South Australia Business 154.
## 10 2000 Q2 Adelaide South Australia Business 169.
## # … with 24,310 more rows
## # ℹ Use `print(n = ...)` to see more rows
# 2.
# It says to find the combination of 'Region' and 'Purpose'
# had the max number of overnight trips on avg, so I grouped
# it by Region and Purpose, found the average for each combo.
# and then put the Avg column in descending order to show the
# max averages. I then made another column to prove that
# Avg is in the correct order (showing max values).
avg <- tourism2 %>%
group_by(Region, Purpose) %>%
mutate(Avg = mean(Trips)) %>%
arrange(desc(Avg)) %>%
ungroup() %>%
mutate(Max = max(Avg))
avg
## # A tsibble: 24,320 x 7 [1Q]
## # Key: Region, State, Purpose [304]
## Quarter Region State Purpose Trips Avg Max
## <qtr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
## 1 1998 Q1 Sydney New South Wales Visiting 818. 747. 747.
## 2 1998 Q2 Sydney New South Wales Visiting 639. 747. 747.
## 3 1998 Q3 Sydney New South Wales Visiting 674. 747. 747.
## 4 1998 Q4 Sydney New South Wales Visiting 881. 747. 747.
## 5 1999 Q1 Sydney New South Wales Visiting 784. 747. 747.
## 6 1999 Q2 Sydney New South Wales Visiting 767. 747. 747.
## 7 1999 Q3 Sydney New South Wales Visiting 681. 747. 747.
## 8 1999 Q4 Sydney New South Wales Visiting 728. 747. 747.
## 9 2000 Q1 Sydney New South Wales Visiting 645. 747. 747.
## 10 2000 Q2 Sydney New South Wales Visiting 590. 747. 747.
## # … with 24,310 more rows
## # ℹ Use `print(n = ...)` to see more rows
# 3.
# Show me the total amount of trips per state because
# When you combine Region and Purpose, you get state
tt <- tourism %>%
group_by(State) %>%
summarise(Trips = sum(Trips)) %>%
ungroup()
tt
## # A tsibble: 640 x 3 [1Q]
## # Key: State [8]
## State Quarter Trips
## <chr> <qtr> <dbl>
## 1 ACT 1998 Q1 551.
## 2 ACT 1998 Q2 416.
## 3 ACT 1998 Q3 436.
## 4 ACT 1998 Q4 450.
## 5 ACT 1999 Q1 379.
## 6 ACT 1999 Q2 558.
## 7 ACT 1999 Q3 449.
## 8 ACT 1999 Q4 595.
## 9 ACT 2000 Q1 600.
## 10 ACT 2000 Q2 557.
## # … with 630 more rows
## # ℹ Use `print(n = ...)` to see more rows
Exercise 4
# 1.
# Using autoplot
autoplot(aus_arrivals)
## Plot variable not specified, automatically selected `.vars = Arrivals`

# Using gg_season
aus_arrivals %>%
mutate(Arrivals = Arrivals/ 1e3) %>%
gg_season() +
labs(y = "Arrivals (thousands)")
## Plot variable not specified, automatically selected `y = Arrivals`

# Using gg_subseries
aus_arrivals %>%
mutate(Arrivals = Arrivals/ 1e3) %>%
gg_subseries() +
labs(y = "Arrivals (thousands)")
## Plot variable not specified, automatically selected `y = Arrivals`

# 2. Any unsual observations?
# I am curious to why there are so many fulctuations through out each year. Every country seems to
# follow their own individual trend, seasonality, or cyclic pattern. Towards the year 2010, I noticed
# that New Zealand continues its increasing trend; However, the other countries Arrival's seem
# to decrease. Japan seems to have the most bizzare pattern, increasing to then tremendously
# decreasing. I wonder if the 9/11 attack had any impact.
Exercise 5
set.seed(8)
myseries <- aus_retail %>%
filter(`Series ID` == sample(aus_retail$`Series ID`,1))
myseries
## # A tsibble: 441 x 5 [1M]
## # Key: State, Industry [1]
## State Industry `Series ID` Month Turnover
## <chr> <chr> <chr> <mth> <dbl>
## 1 New South Wales Household goods retailing A3349397X 1982 Apr 211.
## 2 New South Wales Household goods retailing A3349397X 1982 May 224.
## 3 New South Wales Household goods retailing A3349397X 1982 Jun 216.
## 4 New South Wales Household goods retailing A3349397X 1982 Jul 226.
## 5 New South Wales Household goods retailing A3349397X 1982 Aug 217.
## 6 New South Wales Household goods retailing A3349397X 1982 Sep 213.
## 7 New South Wales Household goods retailing A3349397X 1982 Oct 224.
## 8 New South Wales Household goods retailing A3349397X 1982 Nov 244.
## 9 New South Wales Household goods retailing A3349397X 1982 Dec 393.
## 10 New South Wales Household goods retailing A3349397X 1983 Jan 220.
## # … with 431 more rows
## # ℹ Use `print(n = ...)` to see more rows
autoplot(myseries)
## Plot variable not specified, automatically selected `.vars = Turnover`

gg_season(myseries)
## Plot variable not specified, automatically selected `y = Turnover`

gg_subseries(myseries)
## Plot variable not specified, automatically selected `y = Turnover`

gg_lag(myseries)
## Plot variable not specified, automatically selected `y = Turnover`

myseries %>%
ACF(Turnover) %>%
autoplot()

# There is a seasonal pattern and there is an increasing trend
# If turnover represents the retail trade turnover, then after
# analyzing the data, it seems as if Australia's purchasing power
# is increasing overtime. In other words, Australia is becoming wealthier
Exercise 6
# 1.
# I added on two columns of the mean and std of the Closing Stocks of FB
mean <- gafa_stock %>%
filter(Symbol == "FB") %>%
mutate(Mean = mean(Close), Std = sd(Close)) %>%
select(Close, Mean, Std)
mean
## # A tsibble: 1,258 x 4 [!]
## Close Mean Std Date
## <dbl> <dbl> <dbl> <date>
## 1 54.7 120. 41.3 2014-01-02
## 2 54.6 120. 41.3 2014-01-03
## 3 57.2 120. 41.3 2014-01-06
## 4 57.9 120. 41.3 2014-01-07
## 5 58.2 120. 41.3 2014-01-08
## 6 57.2 120. 41.3 2014-01-09
## 7 57.9 120. 41.3 2014-01-10
## 8 55.9 120. 41.3 2014-01-13
## 9 57.7 120. 41.3 2014-01-14
## 10 57.6 120. 41.3 2014-01-15
## # … with 1,248 more rows
## # ℹ Use `print(n = ...)` to see more rows
# To check my values, I calculated the values separately
# by using $.
# To check and see if the value is correct, I first made
# a data set with filter on FB to find nrow.
mean2 <- gafa_stock %>%
filter(Symbol == "FB")
mean(mean2$Close)
## [1] 120.4625
sd(mean2$Close)
## [1] 41.32364
# 2.
# using the diff function to find the difference of facebook stock prices
df <- diff(mean2$Close)
df
## [1] -0.149998 2.640000 0.719997 0.310002 -1.009999 0.719998
## [7] -2.029999 1.830002 -0.140004 -0.409999 -0.890000 2.209999
## [13] -1.000000 -0.879997 -2.180000 -0.900002 1.590000 -1.610000
## [19] 7.550003 1.489998 -1.090000 1.270000 -0.560001 -0.029999
## [25] 2.160000 -0.770001 1.299999 -0.400001 2.880005 -0.240006
## [31] 0.210007 0.759995 1.569999 -1.040001 2.190003 -0.930001
## [37] -0.589996 -0.320000 -0.480003 -1.049995 1.389999 2.769997
## [43] -0.730004 -1.039993 2.229996 -1.930001 0.779999 -2.049995
## [49] -1.110001 1.019997 0.450004 -0.950004 -1.269997 0.269997
## [55] -3.140000 0.790001 -4.500000 0.580002 -0.960003 0.230004
## [61] 2.379997 0.100002 -3.229999 -2.740002 0.200001 1.239998
## [67] 4.220001 -3.250000 -0.630001 0.360000 0.200001 0.630001
## [73] -0.780002 2.300003 1.789997 -1.669998 -0.490002 -3.160000
## [79] -1.570000 2.010003 1.629997 1.370003 -0.690003 0.760002
## [85] -2.690002 -1.140000 -0.630001 0.480004 2.590000 0.000000
## [91] -0.600002 -1.310002 0.100002 1.189999 -0.649998 1.930001
## [97] 0.029998 0.829998 2.130002 0.029998 0.320004 -0.530003
## [103] -0.219997 -0.210003 0.470001 -0.150001 -0.689999 0.380001
## [109] 2.889996 0.010002 -1.489998 0.209999 -0.309998 0.210000
## [115] 1.199996 -1.260002 0.160004 0.870003 0.349998 1.720001
## [121] -0.310005 0.470001 -0.309997 0.769997 -1.610001 -0.159996
## [127] -1.000000 -2.530003 2.210003 -0.099998 1.469993 1.560006
## [133] -0.730004 0.490006 -1.250000 2.009994 0.980004 -0.130005
## [139] 2.020004 3.690002 0.209999 -0.270004 -1.209999 0.970001
## [145] -2.029998 -0.290001 1.150001 -0.820000 -0.220001 0.699997
## [151] -0.110000 0.380004 -0.610000 0.939995 0.530006 -0.670006
## [157] 0.959999 0.700005 -0.480003 -0.239998 0.000000 0.449997
## [163] 0.940002 -1.330002 -0.769996 0.959999 1.860000 -0.849998
## [169] 0.119995 1.310005 0.629997 -1.220001 0.760002 0.489998
## [175] -0.439995 -2.900001 1.500000 0.349998 0.570000 0.910004
## [181] -1.110001 1.489998 0.250000 -1.320000 1.570000 0.209999
## [187] 0.040001 -2.489998 0.529999 0.360000 0.119996 -1.269997
## [193] 1.229996 -1.609993 -3.000000 0.079994 0.599998 -0.379997
## [199] -0.580002 3.320000 1.000000 1.740005 -0.319999 1.669998
## [205] 0.629997 -0.389999 0.489998 -4.909996 -1.750000 0.879997
## [211] -1.110001 1.880005 -0.930000 0.430000 0.339996 -0.599998
## [217] -0.389999 0.110000 -0.470001 0.629997 -0.639999 0.099998
## [223] -1.009994 0.269996 0.150002 0.260002 1.619995 1.990006
## [229] 0.079994 -2.599999 0.360001 -0.580002 0.360001 1.120003
## [235] 0.159996 0.319999 -0.659996 1.550003 0.099999 -0.840004
## [241] -2.299996 1.419999 2.290001 1.479995 1.570000 -0.839996
## [247] 0.159996 0.010002 -0.760002 -0.799996 -1.200004 0.430000
## [253] -1.259995 -1.040000 0.000000 2.029998 -0.440002 -1.019997
## [259] -0.270004 -0.169998 -2.229996 1.129997 1.059998 0.500000
## [265] 0.910004 0.180000 -0.330002 -1.720001 0.459999 1.760002
## [271] -2.089996 -0.920006 0.410004 0.229995 -0.019996 -1.140000
## [277] -0.029999 0.750000 1.320000 -0.279999 -0.490005 -0.140000
## [283] 1.110001 2.709999 0.480004 -1.060006 -0.389999 1.110001
## [289] 0.850006 -1.440003 0.779999 -0.150002 1.300004 0.309997
## [295] -1.199997 -0.570000 -1.889999 0.019997 1.360000 -0.879997
## [301] 0.019997 1.290001 1.550003 1.839996 1.050003 0.629997
## [307] 0.879998 -2.390000 0.090004 0.290001 -0.100006 -0.979996
## [313] -0.550003 -0.110000 0.880004 -0.120002 -0.040001 -0.110001
## [319] -0.129997 0.970001 0.509995 -0.809998 -0.400001 -1.529999
## [325] 2.309997 0.530007 1.009994 -2.219993 -0.880005 0.380005
## [331] -1.230004 -0.209999 -1.700004 0.220001 -0.180000 -1.250000
## [337] 0.540000 0.330002 0.080002 -0.500000 -0.550003 0.980003
## [343] 2.930001 -0.950005 0.459999 -0.250000 -0.079994 -0.070000
## [349] 0.059998 -1.209999 1.220001 -0.400001 -0.960000 1.099999
## [355] 0.150001 2.000000 -0.389999 0.089996 -1.470001 0.000000
## [361] 1.490006 -0.330002 -0.300003 -0.820000 0.349999 0.730003
## [367] 1.120003 -0.400002 2.229996 3.139999 0.980004 -0.879998
## [373] 0.029999 -2.209999 -0.030006 1.140007 0.379997 0.260002
## [379] -0.330002 -1.569999 0.229995 2.070000 2.150001 -0.419998
## [385] 0.080002 1.089996 4.120003 2.940003 0.479995 -1.349998
## [391] -1.599999 1.509995 -2.779999 1.120003 1.699997 -1.779999
## [397] -1.199997 0.129997 -0.080001 2.380004 -1.319999 -0.820000
## [403] -0.150001 -0.529999 0.569999 -0.760002 0.989998 -0.489998
## [409] 1.239998 0.140000 -4.750000 -4.500000 -3.970002 0.910004
## [415] 4.190002 2.540001 1.279999 -1.580002 -2.199997 2.659996
## [421] -1.739997 0.110000 1.269997 0.910003 1.540001 0.070000
## [427] 0.259995 0.590004 0.549995 0.889999 0.060006 1.150001
## [433] -2.590004 1.010002 0.440003 -1.640007 -3.559998 -2.540001
## [439] 3.230004 1.049995 1.120003 1.940002 -1.209999 -0.400001
## [445] 0.069999 0.769997 1.020004 -0.139999 -0.050003 1.889999
## [451] 1.580002 0.930000 -1.470001 0.110001 2.559997 2.520004
## [457] 1.579995 -0.070000 0.500000 0.680000 -2.909996 1.339997
## [463] -0.729996 1.360000 4.820000 -1.660004 -0.610000 1.420006
## [469] 1.099998 -0.990005 -4.070000 0.090004 1.089996 2.640000
## [475] -1.509995 1.059998 -0.370003 -1.209999 -0.329994 0.039993
## [481] -1.209999 2.880005 -1.050003 -1.690003 1.800003 -0.569999
## [487] 0.879997 -1.890000 0.820000 -3.299995 2.540001 -0.110001
## [493] 2.239998 -0.570000 -2.180000 0.729996 0.740005 -0.880005
## [499] 0.390000 0.910003 1.330002 -1.040001 -1.559997 -2.440003
## [505] 0.510002 0.239998 -5.050003 -0.589996 0.180000 1.860001
## [511] -3.930001 2.930001 -3.400002 0.290001 -0.910004 -0.189994
## [517] 3.779998 -0.930000 0.329994 -2.889999 14.660004 3.099998
## [523] 2.879997 -0.479995 -1.919999 -2.200004 -6.419998 -4.320000
## [529] -0.209999 1.459999 0.910004 0.099998 -0.400001 3.589996
## [535] -1.729996 1.099999 2.590004 -1.700005 1.419998 1.190003
## [541] -0.150002 -1.000000 2.900002 0.129997 -0.369995 -1.190003
## [547] -2.659996 0.199997 1.580002 -0.190002 2.090004 0.479995
## [553] 0.779999 1.510002 -1.160003 0.430000 0.400001 0.400002
## [559] 0.290001 0.510002 0.639999 2.449997 -1.440002 -0.599999
## [565] 1.960000 -3.509995 -0.330002 1.489998 -0.070000 -3.010002
## [571] -1.639999 1.620003 -0.099999 0.329994 -1.199997 0.809998
## [577] 1.840004 0.129997 1.020004 -2.880004 -0.460000 -1.339996
## [583] 0.129997 7.840004 0.849999 0.989998 -1.140000 0.629998
## [589] -0.250000 1.680000 -0.250000 1.260002 -0.980003 0.760002
## [595] -0.470001 -1.140000 -1.320000 0.300004 -0.840004 0.540000
## [601] -1.379997 1.729996 0.190002 1.580002 -0.090004 -0.569999
## [607] -0.029999 0.150001 -0.459999 0.320000 -1.029999 0.629997
## [613] 0.169999 -1.939995 -2.670006 0.990005 -0.340004 -0.209999
## [619] -1.370002 0.350006 1.009994 -0.469993 1.169998 -3.000000
## [625] -3.110001 3.729996 1.460007 0.119995 -0.089997 0.009995
## [631] 2.500000 -0.849999 1.390000 0.630005 0.059997 -1.150001
## [637] 0.510002 -0.430000 2.510002 1.239998 1.309997 -1.309997
## [643] 0.389999 0.629997 -0.409996 2.119995 1.660004 -1.059998
## [649] 0.369996 -1.220002 -0.579994 1.849999 0.790001 0.110000
## [655] -0.200004 -0.180001 0.020005 -0.020005 -0.979995 -0.599999
## [661] 1.070000 -0.459999 -0.350006 0.590004 0.220001 -0.890000
## [667] 0.409996 1.070000 1.580002 -0.700005 0.280007 0.049995
## [673] 0.340004 3.219994 1.320007 -0.779999 -3.170006 1.590004
## [679] -1.480003 0.559998 0.580009 0.720001 -0.420013 -0.009995
## [685] 1.300003 0.140000 -2.120003 -0.650001 1.380004 0.539994
## [691] -1.140000 0.180008 0.500000 -0.580002 0.279999 0.270004
## [697] 0.250000 1.250000 -1.360000 0.169998 -1.230003 0.059997
## [703] -0.339996 1.030006 1.539994 -0.110001 2.070007 1.209992
## [709] -0.990006 -1.250000 -1.349991 1.599991 -0.299988 -1.490005
## [715] -2.330002 -7.169998 0.750000 1.400002 2.069999 -1.040001
## [721] -2.379997 -1.780006 -3.939995 2.119995 -0.860001 1.450005
## [727] -0.770004 4.750000 -0.299996 -0.630005 -0.459999 0.030007
## [733] 0.459999 -2.450005 -3.320000 0.300004 2.029998 -0.120002
## [739] 0.639999 0.960007 0.769996 -1.910003 2.540001 -0.099999
## [745] 0.360001 -0.699997 -0.630005 -0.150002 -0.049995 -1.639999
## [751] -0.130005 0.740005 -1.090004 -0.570000 -1.299995 1.809998
## [757] 1.830001 1.979996 2.740006 1.489998 -0.550004 1.739998
## [763] 0.530007 1.719993 -0.469993 0.049995 -0.369995 -0.510002
## [769] 1.889992 0.440002 2.110001 1.300003 -0.600006 -1.199997
## [775] -0.659989 2.909989 -2.390000 0.140000 1.080002 -0.220002
## [781] 2.360001 -0.059998 0.050003 -0.139999 -0.199997 -0.410004
## [787] 0.399994 -0.309997 0.190002 2.399994 -0.759994 0.080001
## [793] 0.970002 -0.870011 1.880005 -0.660003 0.410003 0.250000
## [799] -0.119995 0.419998 0.520004 0.549988 0.810013 -0.279999
## [805] 0.399994 0.270004 -0.150009 0.100006 -1.430007 1.080001
## [811] -0.059997 0.809997 -0.019989 1.439988 0.889999 -0.239990
## [817] -0.360001 0.229996 -0.550003 0.120010 -0.680008 -0.389999
## [823] 0.259994 -1.119995 -0.339996 -0.190003 2.029999 -0.459991
## [829] 1.309997 1.529999 -0.120010 1.790008 1.020004 0.069993
## [835] 1.139999 2.550003 2.210007 0.319992 -0.979996 -0.949997
## [841] -0.610001 0.819993 -0.580002 -0.190003 -0.250000 0.290009
## [847] -0.140000 -0.410003 -4.929993 2.809998 0.399994 0.180007
## [853] -0.169998 1.969986 1.920014 0.169998 0.250000 -0.919998
## [859] 0.069992 2.080002 0.020004 -0.820007 0.309997 1.590012
## [865] -5.110001 -1.160004 2.239991 -0.429993 -0.449997 0.839996
## [871] 2.229996 -0.619995 1.660004 -0.510010 1.670013 -1.480011
## [877] -3.009994 2.660003 -2.200012 -0.059997 -2.550003 1.910003
## [883] -1.519989 2.619995 2.059998 1.770004 3.629990 0.360001
## [889] 0.710006 -0.240005 3.130005 1.279998 0.390000 -0.100006
## [895] 1.570007 -0.720001 0.330002 4.830001 2.009995 -3.199997
## [901] 0.610001 -0.559998 -0.710007 1.029999 2.360001 -0.750000
## [907] -0.050003 -3.779999 0.680008 2.669998 0.250000 -1.000000
## [913] -3.089996 0.500000 0.369995 1.860000 -0.929992 -0.970002
## [919] -1.419998 0.919998 0.809998 1.869995 2.050003 0.050003
## [925] -1.300003 1.369995 1.120011 -2.260010 2.559998 -0.549988
## [931] 0.089996 -2.089996 0.679992 -1.630004 2.510009 -0.350006
## [937] -1.059997 -0.570008 -7.669998 1.340012 3.469986 1.050003
## [943] 2.139999 -1.399994 0.490006 -1.540009 2.820007 0.989991
## [949] 0.270004 -0.910004 1.150009 -0.190002 1.190002 0.779999
## [955] 1.589997 -0.080002 -1.470001 0.419998 -3.709992 0.529999
## [961] -1.199997 0.029999 7.250000 1.989990 0.190003 2.600006
## [967] -3.740006 0.000000 1.250000 0.080002 -0.690002 -0.259995
## [973] -0.839996 0.309997 -0.699997 -0.120010 1.639999 -0.589996
## [979] -0.259995 3.119996 -0.990006 1.910004 0.250000 -0.610001
## [985] -7.289993 2.049988 -2.079987 -3.630005 1.360001 3.229996
## [991] 4.080001 -1.139999 0.039993 -2.079986 1.339996 0.089996
## [997] 1.789994 0.640014 -1.310012 -1.619996 -0.440002 -0.250000
## [1003] -1.209992 1.629990 0.300003 -1.459991 4.959991 3.250000
## [1009] -0.339996 2.520004 1.429993 -0.410004 -0.029999 -0.069992
## [1015] -8.400009 -0.979996 -0.789993 2.199997 1.489990 4.080002
## [1021] 3.980011 -2.800003 0.929993 2.520004 -4.020004 1.139999
## [1027] -0.229996 6.199997 -2.809997 -9.020004 4.050003 -5.130005
## [1033] -8.599991 4.529999 0.300003 -3.260010 6.370010 0.440003
## [1039] -2.600006 -1.350006 1.900009 1.080001 4.299988 1.640000
## [1045] -3.469986 -3.140000 -2.380005 0.679993 3.779999 -0.619995
## [1051] 3.930008 -1.370011 2.890000 -0.470001 -2.879990 2.309997
## [1057] -0.330001 1.229995 -12.529998 -4.410004 1.240005 -4.500000
## [1063] -5.500000 0.669999 -7.839997 0.809998 6.759994 -4.399994
## [1069] 0.720002 -1.009995 4.239990 -2.139999 0.729996 7.110000
## [1075] 1.280014 -2.450012 0.650009 0.309998 3.830002 -2.300003
## [1081] 1.740005 -1.820007 -0.440003 -6.149994 0.000000 14.470002
## [1087] -0.570008 -1.589996 1.860001 2.210006 -2.050003 2.589997
## [1093] 1.360000 0.949997 3.740006 2.869995 1.460006 -0.350006
## [1099] -2.319992 -1.120010 0.559998 -1.080002 1.810012 -0.690002
## [1105] 3.099991 -0.970001 -1.009995 0.820007 1.929993 4.110001
## [1111] 2.210006 -0.710006 -0.339997 -1.600006 -3.160003 0.920013
## [1117] 2.439987 0.860001 0.010010 4.399994 -0.959992 2.459992
## [1123] -0.819993 4.509995 -0.500000 0.240005 -5.389999 2.649994
## [1129] -3.160004 0.390000 -1.909989 3.039994 -4.630005 5.720001
## [1135] 4.779999 1.510009 -1.200012 -1.000000 4.380005 0.400009
## [1141] -0.090011 2.760009 -0.630004 -1.270005 1.850006 0.970002
## [1147] 3.759994 2.830002 -41.240005 -1.369996 -3.830001 1.520004
## [1153] -0.930008 4.720001 1.410004 7.910003 -1.880004 1.369995
## [1159] -2.089997 -2.830001 -0.209992 1.059998 -1.580002 -4.830002
## [1165] -0.899994 -1.300003 0.119995 1.020004 -0.740005 1.750000
## [1171] 2.810013 -1.200012 -0.360001 1.740005 -1.910003 -4.569992
## [1177] -3.980011 -4.649994 0.509994 1.140000 1.760009 -3.940002
## [1183] -0.639999 0.960006 -1.740005 -0.279999 2.759995 2.960006
## [1189] -3.090011 2.480011 -0.500000 2.039993 1.889999 -4.379989
## [1195] -2.020005 -3.110000 3.099991 -3.579987 -1.520004 -0.080002
## [1201] 0.649994 -6.519989 1.970001 0.389999 -0.220001 5.259995
## [1207] 0.639999 -4.500000 -0.869995 0.729996 -0.390000 -8.350006
## [1213] 4.910004 -5.580002 -3.279999 4.130005 5.569992 -0.039993
## [1219] -1.399994 -1.670013 1.260009 1.589997 -3.660004 -2.909988
## [1225] -3.410004 0.610001 2.059997 -0.369995 -4.320007 -7.979996
## [1231] 0.879990 2.390014 -3.090011 4.650009 -1.380005 1.759995
## [1237] 1.919998 1.930008 0.479995 -3.160003 1.700012 -2.210007
## [1243] 4.430008 0.229996 2.419998 0.509995 -0.949997 -3.869996
## [1249] 3.470002 -10.419999 0.159989 -8.449997 -0.889999 10.119995
## [1255] 0.340011 -1.320007 -2.110001
# Now I am finding the mean, standard deviation, kurtosis
# and Skewness of df
mean(df)
## [1] 0.06076372
sd(df)
## [1] 2.414555
kurtosis(df)
## [1] 71.02921
skewness(df)
## [1] -3.973192
# 3.
# I am first plotting the data to get an idea of the distribution
ggplot(data = mean2, mapping = aes(x = Close)) +
geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# Now to calculate them by hand.
# I found the sum of the FB close by
mean3 <- gafa_stock %>%
filter(Symbol == "FB") %>%
pull(Close)
mean3
## [1] 54.71 54.56 57.20 57.92 58.23 57.22 57.94 55.91 57.74 57.60
## [11] 57.19 56.30 58.51 57.51 56.63 54.45 53.55 55.14 53.53 61.08
## [21] 62.57 61.48 62.75 62.19 62.16 64.32 63.55 64.85 64.45 67.33
## [31] 67.09 67.30 68.06 69.63 68.59 70.78 69.85 69.26 68.94 68.46
## [41] 67.41 68.80 71.57 70.84 69.80 72.03 70.10 70.88 68.83 67.72
## [51] 68.74 69.19 68.24 66.97 67.24 64.10 64.89 60.39 60.97 60.01
## [61] 60.24 62.62 62.72 59.49 56.75 56.95 58.19 62.41 59.16 58.53
## [71] 58.89 59.09 59.72 58.94 61.24 63.03 61.36 60.87 57.71 56.14
## [81] 58.15 59.78 61.15 60.46 61.22 58.53 57.39 56.76 57.24 59.83
## [91] 59.83 59.23 57.92 58.02 59.21 58.56 60.49 60.52 61.35 63.48
## [101] 63.51 63.83 63.30 63.08 62.87 63.34 63.19 62.50 62.88 65.77
## [111] 65.78 64.29 64.50 64.19 64.40 65.60 64.34 64.50 65.37 65.72
## [121] 67.44 67.13 67.60 67.29 68.06 66.45 66.29 65.29 62.76 64.97
## [131] 64.87 66.34 67.90 67.17 67.66 66.41 68.42 69.40 69.27 71.29
## [141] 74.98 75.19 74.92 73.71 74.68 72.65 72.36 73.51 72.69 72.47
## [151] 73.17 73.06 73.44 72.83 73.77 74.30 73.63 74.59 75.29 74.81
## [161] 74.57 74.57 75.02 75.96 74.63 73.86 74.82 76.68 75.83 75.95
## [171] 77.26 77.89 76.67 77.43 77.92 77.48 74.58 76.08 76.43 77.00
## [181] 77.91 76.80 78.29 78.54 77.22 78.79 79.00 79.04 76.55 77.08
## [191] 77.44 77.56 76.29 77.52 75.91 72.91 72.99 73.59 73.21 72.63
## [201] 75.95 76.95 78.69 78.37 80.04 80.67 80.28 80.77 75.86 74.11
## [211] 74.99 73.88 75.76 74.83 75.26 75.60 75.00 74.61 74.72 74.25
## [221] 74.88 74.24 74.34 73.33 73.60 73.75 74.01 75.63 77.62 77.70
## [231] 75.10 75.46 74.88 75.24 76.36 76.52 76.84 76.18 77.73 77.83
## [241] 76.99 74.69 76.11 78.40 79.88 81.45 80.61 80.77 80.78 80.02
## [251] 79.22 78.02 78.45 77.19 76.15 76.15 78.18 77.74 76.72 76.45
## [261] 76.28 74.05 75.18 76.24 76.74 77.65 77.83 77.50 75.78 76.24
## [271] 78.00 75.91 74.99 75.40 75.63 75.61 74.47 74.44 75.19 76.51
## [281] 76.23 75.74 75.60 76.71 79.42 79.90 78.84 78.45 79.56 80.41
## [291] 78.97 79.75 79.60 80.90 81.21 80.01 79.44 77.55 77.57 78.93
## [301] 78.05 78.07 79.36 80.91 82.75 83.80 84.43 85.31 82.92 83.01
## [311] 83.30 83.20 82.22 81.67 81.56 82.44 82.32 82.28 82.17 82.04
## [321] 83.01 83.52 82.71 82.31 80.78 83.09 83.62 84.63 82.41 81.53
## [331] 81.91 80.68 80.47 78.77 78.99 78.81 77.56 78.10 78.43 78.51
## [341] 78.01 77.46 78.44 81.37 80.42 80.88 80.63 80.55 80.48 80.54
## [351] 79.33 80.55 80.15 79.19 80.29 80.44 82.44 82.05 82.14 80.67
## [361] 80.67 82.16 81.83 81.53 80.71 81.06 81.79 82.91 82.51 84.74
## [371] 87.88 88.86 87.98 88.01 85.80 85.77 86.91 87.29 87.55 87.22
## [381] 85.65 85.88 87.95 90.10 89.68 89.76 90.85 94.97 97.91 98.39
## [391] 97.04 95.44 96.95 94.17 95.29 96.99 95.21 94.01 94.14 94.06
## [401] 96.44 95.12 94.30 94.15 93.62 94.19 93.43 94.42 93.93 95.17
## [411] 95.31 90.56 86.06 82.09 83.00 87.19 89.73 91.01 89.43 87.23
## [421] 89.89 88.15 88.26 89.53 90.44 91.98 92.05 92.31 92.90 93.45
## [431] 94.34 94.40 95.55 92.96 93.97 94.41 92.77 89.21 86.67 89.90
## [441] 90.95 92.07 94.01 92.80 92.40 92.47 93.24 94.26 94.12 94.07
## [451] 95.96 97.54 98.47 97.00 97.11 99.67 102.19 103.77 103.70 104.20
## [461] 104.88 101.97 103.31 102.58 103.94 108.76 107.10 106.49 107.91 109.01
## [471] 108.02 103.95 104.04 105.13 107.77 106.26 107.32 106.95 105.74 105.41
## [481] 105.45 104.24 107.12 106.07 104.38 106.18 105.61 106.49 104.60 105.42
## [491] 102.12 104.66 104.55 106.79 106.22 104.04 104.77 105.51 104.63 105.02
## [501] 105.93 107.26 106.22 104.66 102.22 102.73 102.97 97.92 97.33 97.51
## [511] 99.37 95.44 98.37 94.97 95.26 94.35 94.16 97.94 97.01 97.34
## [521] 94.45 109.11 112.21 115.09 114.61 112.69 110.49 104.07 99.75 99.54
## [531] 101.00 101.91 102.01 101.61 105.20 103.47 104.57 107.16 105.46 106.88
## [541] 108.07 107.92 106.92 109.82 109.95 109.58 108.39 105.73 105.93 107.51
## [551] 107.32 109.41 109.89 110.67 112.18 111.02 111.45 111.85 112.25 112.54
## [561] 113.05 113.69 116.14 114.70 114.10 116.06 112.55 112.22 113.71 113.64
## [571] 110.63 108.99 110.61 110.51 110.84 109.64 110.45 112.29 112.42 113.44
## [581] 110.56 110.10 108.76 108.89 116.73 117.58 118.57 117.43 118.06 117.81
## [591] 119.49 119.24 120.50 119.52 120.28 119.81 118.67 117.35 117.65 116.81
## [601] 117.35 115.97 117.70 117.89 119.47 119.38 118.81 118.78 118.93 118.47
## [611] 118.79 117.76 118.39 118.56 116.62 113.95 114.94 114.60 114.39 113.02
## [621] 113.37 114.38 113.91 115.08 112.08 108.97 112.70 114.16 114.28 114.19
## [631] 114.20 116.70 115.85 117.24 117.87 117.93 116.78 117.29 116.86 119.37
## [641] 120.61 121.92 120.61 121.00 121.63 121.22 123.34 125.00 123.94 124.31
## [651] 123.09 122.51 124.36 125.15 125.26 125.06 124.88 124.90 124.88 123.90
## [661] 123.30 124.37 123.91 123.56 124.15 124.37 123.48 123.89 124.96 126.54
## [671] 125.84 126.12 126.17 126.51 129.73 131.05 130.27 127.10 128.69 127.21
## [681] 127.77 128.35 129.07 128.65 128.64 129.94 130.08 127.96 127.31 128.69
## [691] 129.23 128.09 128.27 128.77 128.19 128.47 128.74 128.99 130.24 128.88
## [701] 129.05 127.82 127.88 127.54 128.57 130.11 130.00 132.07 133.28 132.29
## [711] 131.04 129.69 131.29 130.99 129.50 127.17 120.00 120.75 122.15 124.22
## [721] 123.18 120.80 119.02 115.08 117.20 116.34 117.79 117.02 121.77 121.47
## [731] 120.84 120.38 120.41 120.87 118.42 115.10 115.40 117.43 117.31 117.95
## [741] 118.91 119.68 117.77 120.31 120.21 120.57 119.87 119.24 119.09 119.04
## [751] 117.40 117.27 118.01 116.92 116.35 115.05 116.86 118.69 120.67 123.41
## [761] 124.90 124.35 126.09 126.62 128.34 127.87 127.92 127.55 127.04 128.93
## [771] 129.37 131.48 132.78 132.18 130.98 130.32 133.23 130.84 130.98 132.06
## [781] 131.84 134.20 134.14 134.19 134.05 133.85 133.44 133.84 133.53 133.72
## [791] 136.12 135.36 135.44 136.41 135.54 137.42 136.76 137.17 137.42 137.30
## [801] 137.72 138.24 138.79 139.60 139.32 139.72 139.99 139.84 139.94 138.51
## [811] 139.59 139.53 140.34 140.32 141.76 142.65 142.41 142.05 142.28 141.73
## [821] 141.85 141.17 140.78 141.04 139.92 139.58 139.39 141.42 140.96 142.27
## [831] 143.80 143.68 145.47 146.49 146.56 147.70 150.25 152.46 152.78 151.80
## [841] 150.85 150.24 151.06 150.48 150.29 150.04 150.33 150.19 149.78 144.85
## [851] 147.66 148.06 148.24 148.07 150.04 151.96 152.13 152.38 151.46 151.53
## [861] 153.61 153.63 152.81 153.12 154.71 149.60 148.44 150.68 150.25 149.80
## [871] 150.64 152.87 152.25 153.91 153.40 155.07 153.59 150.58 153.24 151.04
## [881] 150.98 148.43 150.34 148.82 151.44 153.50 155.27 158.90 159.26 159.97
## [891] 159.73 162.86 164.14 164.53 164.43 166.00 165.28 165.61 170.44 172.45
## [901] 169.25 169.86 169.30 168.59 169.62 171.98 171.23 171.18 167.40 168.08
## [911] 170.75 171.00 170.00 166.91 167.41 167.78 169.64 168.71 167.74 166.32
## [921] 167.24 168.05 169.92 171.97 172.02 170.72 172.09 173.21 170.95 173.51
## [931] 172.96 173.05 170.96 171.64 170.01 172.52 172.17 171.11 170.54 162.87
## [941] 164.21 167.68 168.73 170.87 169.47 169.96 168.42 171.24 172.23 172.50
## [951] 171.59 172.74 172.55 173.74 174.52 176.11 176.03 174.56 174.98 171.27
## [961] 171.80 170.60 170.63 177.88 179.87 180.06 182.66 178.92 178.92 180.17
## [971] 180.25 179.56 179.30 178.46 178.77 178.07 177.95 179.59 179.00 178.74
## [981] 181.86 180.87 182.78 183.03 182.42 175.13 177.18 175.10 171.47 172.83
## [991] 176.06 180.14 179.00 179.04 176.96 178.30 178.39 180.18 180.82 179.51
## [1001] 177.89 177.45 177.20 175.99 177.62 177.92 176.46 181.42 184.67 184.33
## [1011] 186.85 188.28 187.87 187.84 187.77 179.37 178.39 177.60 179.80 181.29
## [1021] 185.37 189.35 186.55 187.48 190.00 185.98 187.12 186.89 193.09 190.28
## [1031] 181.26 185.31 180.18 171.58 176.11 176.41 173.15 179.52 179.96 177.36
## [1041] 176.01 177.91 178.99 183.29 184.93 181.46 178.32 175.94 176.62 180.40
## [1051] 179.78 183.71 182.34 185.23 184.76 181.88 184.19 183.86 185.09 172.56
## [1061] 168.15 169.39 164.89 159.39 160.06 152.22 153.03 159.79 155.39 156.11
## [1071] 155.10 159.34 157.20 157.93 165.04 166.32 163.87 164.52 164.83 168.66
## [1081] 166.36 168.10 166.28 165.84 159.69 159.69 174.16 173.59 172.00 173.86
## [1091] 176.07 174.02 176.61 177.97 178.92 182.66 185.53 186.99 186.64 184.32
## [1101] 183.20 183.76 182.68 184.49 183.80 186.90 185.93 184.92 185.74 187.67
## [1111] 191.78 193.99 193.28 192.94 191.34 188.18 189.10 191.54 192.40 192.41
## [1121] 196.81 195.85 198.31 197.49 202.00 201.50 201.74 196.35 199.00 195.84
## [1131] 196.23 194.32 197.36 192.73 198.45 203.23 204.74 203.54 202.54 206.92
## [1141] 207.32 207.23 209.99 209.36 208.09 209.94 210.91 214.67 217.50 176.26
## [1151] 174.89 171.06 172.58 171.65 176.37 177.78 185.69 183.81 185.18 183.09
## [1161] 180.26 180.05 181.11 179.53 174.70 173.80 172.50 172.62 173.64 172.90
## [1171] 174.65 177.46 176.26 175.90 177.64 175.73 171.16 167.18 162.53 163.04
## [1181] 164.18 165.94 162.00 161.36 162.32 160.58 160.30 163.06 166.02 162.93
## [1191] 165.41 164.91 166.95 168.84 164.46 162.44 159.33 162.43 158.85 157.33
## [1201] 157.25 157.90 151.38 153.35 153.74 153.52 158.78 159.42 154.92 154.05
## [1211] 154.78 154.39 146.04 150.95 145.37 142.09 146.22 151.79 151.75 150.35
## [1221] 148.68 149.94 151.53 147.87 144.96 141.55 142.16 144.22 143.85 139.53
## [1231] 131.55 132.43 134.82 131.73 136.38 135.00 136.76 138.68 140.61 141.09
## [1241] 137.93 139.63 137.42 141.85 142.08 144.50 145.01 144.06 140.19 143.66
## [1251] 133.24 133.40 124.95 124.06 134.18 134.52 133.20 131.09
Sum3 <- sum(mean3)
# And then used mean2 from before to find the number of rows
Nrow <- nrow(mean2)
# I then plugged it into the mean formula which is
meann <- Sum3/Nrow
meann
## [1] 120.4625
# For Standard deviation:
Std <- sqrt((sum((mean3 - meann)^2))/(Nrow - 1))
Std
## [1] 41.32364
# For Kurtosis:
K <- ((mean(mean3 - meann))^4)/(Std)^4
K
## [1] 7.809593e-67
# For Skewness:
S <- (mean(mean3 - meann))^3/(Std)^3
S
## [1] -2.627068e-50
Exercise 7
TSLA_2_ <- read_excel("C:/Users/14047/Downloads/TSLA (2).xls",
col_types = c("date", "skip", "skip",
"skip", "skip", "numeric", "skip"))
# 1.
# Loaded into R with the Import Dataset tab (had to convert
# the data into excel first).
#2.
# I did this through the Import Dataset tab also (I just skipped
# over the columns). The code for this is at the top of this sheet.
View(TSLA_2_)
# 3.
# The index is Daily (which is already transformed into my data)
stocks <- TSLA_2_ %>%
mutate(Date = as_date(Date)) %>%
as_tsibble(index = Date)
stocks
## # A tsibble: 169 x 2 [1D]
## Date Close
## <date> <dbl>
## 1 2022-01-03 400.
## 2 2022-01-04 383.
## 3 2022-01-05 363.
## 4 2022-01-06 355.
## 5 2022-01-07 342.
## 6 2022-01-10 353.
## 7 2022-01-11 355.
## 8 2022-01-12 369.
## 9 2022-01-13 344.
## 10 2022-01-14 350.
## # … with 159 more rows
## # ℹ Use `print(n = ...)` to see more rows
# 4.
# I first added another column to represent the Months through
# out the year and then filtered it.
stocks1 <- stocks %>%
mutate(Month = month(Date)) %>%
filter(Month == "6" )
stocks1
## # A tsibble: 21 x 3 [1D]
## Date Close Month
## <date> <dbl> <dbl>
## 1 2022-06-01 247. 6
## 2 2022-06-02 258. 6
## 3 2022-06-03 235. 6
## 4 2022-06-06 238. 6
## 5 2022-06-07 239. 6
## 6 2022-06-08 242. 6
## 7 2022-06-09 240. 6
## 8 2022-06-10 232. 6
## 9 2022-06-13 216. 6
## 10 2022-06-14 221. 6
## # … with 11 more rows
## # ℹ Use `print(n = ...)` to see more rows
# To plot it, I had to change the column name of "Adj. Close" to
# Close.
ggplot(data = stocks1, mapping = aes(x = Date, y = Close)) +
geom_line() +
labs(y = "TSLA Close", x = "June 2022", title = "TSLA Close for June 2022")

# 5.
# I calculated the mean and standard deviation by each month
# by grouping the months and then calculating them.
stocks2 <- stocks %>%
mutate(Month = month(Date)) %>%
group_by(Month) %>%
mutate(Mean = mean(Close), Std = sd(Close)) %>%
ungroup()
stocks2
## # A tsibble: 169 x 5 [1D]
## Date Close Month Mean Std
## <date> <dbl> <dbl> <dbl> <dbl>
## 1 2022-01-03 400. 1 337. 31.7
## 2 2022-01-04 383. 1 337. 31.7
## 3 2022-01-05 363. 1 337. 31.7
## 4 2022-01-06 355. 1 337. 31.7
## 5 2022-01-07 342. 1 337. 31.7
## 6 2022-01-10 353. 1 337. 31.7
## 7 2022-01-11 355. 1 337. 31.7
## 8 2022-01-12 369. 1 337. 31.7
## 9 2022-01-13 344. 1 337. 31.7
## 10 2022-01-14 350. 1 337. 31.7
## # … with 159 more rows
## # ℹ Use `print(n = ...)` to see more rows