Professional Documents
Culture Documents
Quiz 2
Quiz 2
Instructions
Este quiz consta de preguntas de opción múltiple, principalmente para reforzar los conceptos teóricos vistos en clase y
repasar algunas partes de programación importantes para el caso de uso estudiado.
Si alguna pregunta contiene código, es perfectamente válido utilizar R Studio para verificar la respuesta. La mayoría del
contenido ya se vio en clase, pero si hay algo nuevo tenderá a ser algo de código que pueden probar por su cuenta.
Tienes cuatro intentos para resolver este quiz y se debe terminar ANTES de la próxima sesión.
Attempt History
Attempt Time Score
KEPT Attempt 2 21 minutes 100 out of 100
Which of the following commands displays the observations between 2017-12-29 and
2018-01-05 of a data.frame sbux that stored the contents of sbux.csv?
sbux.csv contains 314 observations and 7 variables in the following order: Date, Open,
High, Low, Close, Adj Close, Volume
sbux[sbux$Date >= "2017-12-29" | sbux$Date <= "2018-01-05", ]
Which of the following commands reads only the Date and Adj.Close columns of file
sbux.csv?
data<-read.csv(file="sbux.csv")[,"Date":"Adj.Close"]
data<-read.csv(file="sbux.csv")[,1:6]
data<-read.csv(file="sbux.csv")[c(1,6),]
data<-read.csv(file="sbux.csv")[,c(1,6)]
How can we calculate simple (discrete) returns if we have prices in an xts called "p"?
r <- p / lag(p) - 1
r <- lag(p) / p + 1
How can we calculate the average per column, of all the columns in data frame
"returns"?
apply(returns, 2, mean)
apply(returns, 1, mean)
mean(returns)
mean(returns[2 , ])
How do you calculate the average of the column "price" in the data.frame "amzn"?
mean(price)
mean(amzn$price)
mean(price$amzn)
mean(amzn)
for (i in c(10,23,5,8)){
print(i)
}
10
8
Error
Infinite loop
10
23
->
%/%
%>%
The following R functions are very similar, except that library() stops the code if it
encounters an error, while require() continues executing the code. What do they do?
library(...)
require(...)
Download and install packages with additional functions, they need to be installed every
session they're going to be used
Load add-on packages, they need to be loaded every session they're going to be used
Download and install packages with additional functions, they need to be installed only
once per device
Load add-on packages, they need to be loaded only once per device
prices<-read.csv(stocks.files[1])[,c("Date","Adj.Close")]
names(prices)<-c("Date",tickers[1])
for(i in 2:n){
w<-read.csv(stocks.files[i])[,c("Date","Adj.Close")]
names(w)<-c("Date",tickers[i])
prices<-merge(prices,w)
}
It serves to store the contents of file i before merging them with the prices data.frame
It accumulates the Adj.Close of all the stock files, before being renamed to prices at the
last step
n <- 1
while(n <= 1)
{
print(n)
n <- n + 1
}
n<-1
while(n <= 1)
{
n <- n + 1
print(n)
}
1 and 2 respectively
2 and 2 respectively
1 and 1 respectively
sbux.csv contains 314 observations and 7 variables in the following order: Date, Open,
High, Low, Close, Adj Close, Volume.
We made a regression of the returns of Exxon Mobile (XOM) vs returns of the S&P 500
ETF (SPY) and returns of WTI oil (WTI), as in the example below.
How can we display in the console the regression's coefficients and their p-values
(significance)?
reg.xom<-lm(formula=XOM~SPY+WTI,data=returns)
reg.xom$residuals
reg.xom$coefficients
summary(reg.xom)
reg.xom$fitted
n<-1
while(n < 10)
{
n<-n+1
if(n==5)
{
n <- 10
}
print(n)
}
Outputs: 1, 2, 3, 4, 10
Outputs: 1, 2, 3, 4, 5
Outputs: 2, 3, 4, 10
Outputs: 2, 3, 4, 5, 10
It will forward a value, or the result of an expression, into the next function call.
Since dates tend to be stored as factors when reading data into data.frames, which of
the following commands transforms the Date column in the Prices data.frame to dates
instead of factors?
Tip: If the date format stored as a factor doesn't match the system date format, then
"as.character()" might be needed first, to apply a proper date format as text, and then
convert that to Date.
prices$Date<-as.Date(prices$Date)
prices$Date<-as.Date(prices)
prices<-as.Date(prices$Date)
prices$as.Date<-Date(prices$Date)
44444
i <- 1
while (i >= 5) {
print(i)
i <- i + 1
}
Infinite loop
Nothing happens
We made a regression of the returns of Exxon Mobile (XOM) vs returns of the S&P 500
ETF (SPY) and returns of WTI oil (WTI), as in the example below.
How can we store all the Betas in a new variable, to use later on?
reg.xom<-lm(formula=XOM~SPY+WTI,data=returns)
Betas<-reg.xom$coefficients
Betas<-reg.xom$residuals
Betas<-reg.xom$fitted
Betas<-summary(reg.xom)
for (i in 5:0){
print(i)
}
Nothing happens
Infinite loop
Question 20 2.5 / 2.5 pts
Which package are we using to read financial data directly from Yahoo Finance?
quadprog
quantmod
ggplot2
tidyverse
for (i in seq(10,0,-2))
{
print(i)
}
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
10, 8 , 6, 4, 2, 0
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
10, 8 , 6, 4, 2
Data.frame A contains the following columns: "Fecha", "Ticker", "Price" and data.frame
B contains the following columns: "Date", "Ticker", "Earnings", "Leverage".
Which of the following commands merges the data in order to have each stock's Price,
Earnings & Leverage for each date observed?
Tip1: "Date" and "Fecha" contain the date that corresponds to the rest of the financial
information in the row.
prices<-merge(A, B)
It only has two columns and we want the first column to be called "Date" and the
second column to be called "amzn".
names(amzn)<-c("Dates", "amzn")
rownames(amzn)<-c("Dates", "amzn")
amzn<-colnames(c("Dates", "amzn"))
cbind() is preferred, since it just adds the new columns without any other condition
merge() is preferred, since it just adds the new columns without any other condition
e <- y-yhat
"AMZN"
"NFLX"
"FB"
"MSFT"
"TSLA"
Error
"1 AMZN"
"2 NFLX"
"3 FB"
"4 MSFT"
"5 TSLA"
"wmt.csv" is located in the Working Directory, the first row of the file contains the names
of the variables in each column and the data begins immediately in row 2.
data<-read.csv(file="wmt.csv")
No, since it's necessary to specify that the first row contains headers or column-names.
Yes, since the default arguments of read.csv work for this case (file path, column names
and rows to skip).
for (i in 10:1)
{
print(i)
}
n <- 10
while(n >= 1)
{
print(n)
n <- n - 1
}
The For loop does more iterations than the While loop
for (i in seq(1,10,by=2)){
print(i)
}
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
5.0
Error
1
10
Which R function below returns a string vector with the names of the files at a specified
file path?
path.stocks()
list.files()
setwd()
file.path()
We made a regression of the returns of Exxon Mobile (XOM) vs returns of the S&P 500
ETF (SPY) and returns of WTI oil (WTI), as in the example below.
How can we display in the console the confidence intervals of the regression's
coefficients?
reg.xom<-lm(formula=XOM~SPY+WTI,data=returns)
confint(reg.xom)
COI(reg.xom)
reg.xom
summary(reg.xom)
Replacing the ".Adjusted" text portion of all the names of p, with a space
install.packages(...)
Load add-on packages, they need to be loaded every session they're going to be used
Download and install packages with additional functions, they need to be installed every
session they're going to be used
Download and install packages with additional functions, they need to be installed only
once per device
Load add-on packages, they need to be loaded only once per device
fb<-getSymbols("FB",src="yahoo",from="2017-12-29",to="2019-04-02",auto.assign=F)
matrix
xts
data.frame
text
e <- y-yhat
Question 36 2.5 / 2.5 pts
for (i in 1:10)
{
print(i)
}
n <- 1
while(n < 10)
{
print(n)
n <- n + 1
}
The For loop outputs more numbers than the While loop
The While loop outputs more numbers than the For loop
They display the first (head) or last (tail) n rows (default 6) of data set x
They display the first (head) or last (tail) x rows (default 6) of data set n
They display the first (tail) or last (head) x rows (default 6) of data set n
They display the first (tail) or last (head) n rows (default 6) of data set x
e <- y-yhat
How can we calculate log (continuous) returns if we have prices in an xts called "p"?
r <- lag(p) / p + 1
r <- p / lag(p) - 1
Data.frame "prices" has columns "bid" and "ask", how do we add a new column called
"midpoint"?
midpoint<-(prices$bid+prices$ask)/2
midpoint<-(bid+ask)/2
prices$midpoint<-(prices$bid+prices$ask)/2
prices$midpoint<-(bid+ask)/2