Let's start by getting data to play with! You will use the mathematical functions on these data.

# Loading the quantmod package
library(quantmod)

# Access financial information on Apple stock
getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31") 
##  "AAPL"
head(AAPL)
##            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
## 2020-01-02   74.0600   75.1500  73.7975    75.0875   135480400      73.78592
## 2020-01-03   74.2875   75.1450  74.1250    74.3575   146322800      73.06856
## 2020-01-06   73.4475   74.9900  73.1875    74.9500   118387200      73.65079
## 2020-01-07   74.9600   75.2250  74.3700    74.5975   108872000      73.30441
## 2020-01-08   74.2900   76.1100  74.2900    75.7975   132079200      74.48360
## 2020-01-09   76.8100   77.6075  76.5500    77.4075   170108400      76.06570

## Smallest element

What is the min of Apple's adjusted stock value? Complete the following code.

quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31")

# Insert the missing code below
quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31")

# Insert the missing code below
min(AAPL$AAPL.Adjusted) grade_code(correct = "Good answer, well done!") ## Largest element What is the max of Apple's adjusted stock value? quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31") # Insert the missing code below quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31") # Insert the missing code below max(AAPL$AAPL.Adjusted)
grade_code(correct = "Good answer, well done!")

## Median

What is the median of Apple's adjusted stock value?

quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31")

# Insert the missing code below
quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31")

# Insert the missing code below
median(AAPL$AAPL.Adjusted) grade_code(correct = "Good answer, well done!") ## Mean What is the mean of Apple's adjusted stock value? quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31") # Insert the missing code below quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31") # Insert the missing code below mean(AAPL$AAPL.Adjusted)
grade_code(correct = "Good answer, well done!")

## Trimmed mean

What is the trimmed mean at 5% of Apple's adjusted stock value?

quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31")

# Insert the missing code below
quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31")

# Insert the missing code below
mean(AAPL$AAPL.Adjusted, trim = 0.05) grade_code(correct = "Good answer, well done!") ## Sample quantile What is the third quartile (75%) of Apple's adjusted stock value? quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31") # Insert the missing code below quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31") # Insert the missing code below quantile(AAPL$AAPL.Adjusted, probs = 0.75)
grade_code(correct = "Good answer, well done!")

What is the first quartile (25%) and 80% of Apple's adjusted stock value?

quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31")

# Insert the missing code below
quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31")

# Insert the missing code below
quantile(AAPL$AAPL.Adjusted, probs = c(0.25, 0.80)) grade_code(correct = "Good answer, well done!") ## Sample variance and std deviation What is the sample variance and std deviation of Apple's adjusted stock value? quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31") # Insert the missing code below quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31") # Insert the missing code below var(AAPL$AAPL.Adjusted); sd(AAPL$AAPL.Adjusted) grade_code(correct = "Good answer, well done!") ## Interquartile Range What is the IQR value of Apple's adjusted stock? quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31") # Insert the missing code below quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31") # Insert the missing code below IQR(AAPL$AAPL.Adjusted)
grade_code(correct = "Good answer, well done!")

## Sample Skewness

$$\vert g_{1} \vert$$ = Apple adjusted stock. Is $$\vert g_{1} \vert > 2\sqrt{6/n}$$?

quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31")

# Insert the missing code below
quantmod::getSymbols("AAPL", src="yahoo", from="2020-01-01", to="2020-12-31")

# Insert the missing code below
library(e1071)
skewness(AAPL$AAPL.Adjusted) 2*sqrt(6/length(AAPL$AAPL.Adjusted))
grade_code(correct = "Good answer, well done!")

Quiz