Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

A02 Lectura de Datos y Calculo de Betas

Due Jun 15 at 5:35pm Points 100 Questions 40 Time Limit None


Allowed Attempts 4

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.

Take the Quiz Again

Attempt History
Attempt Time Score
KEPT Attempt 2 21 minutes 100 out of 100

LATEST Attempt 2 21 minutes 100 out of 100

Attempt 1 52 minutes 90 out of 100

 Answers will be shown after your last attempt

Score for this attempt: 100 out of 100


Submitted Jun 8 at 10:24pm
This attempt took 21 minutes.

Question 1 2.5 / 2.5 pts

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", ]

sbux[sbux$Date == "2017-12-29" & sbux$Date == "2018-01-05", ]

sbux[sbux$Date >= "2017-12-29" & sbux$Date <= "2018-01-05", ]

sbux[sbux$Date == "2017-12-29" | sbux$Date == "2018-01-05", ]

Question 2 2.5 / 2.5 pts

Which of the following commands reads only the Date and Adj.Close columns of file
sbux.csv?

sbux.csv is located in the Working Directory and contains 314 observations of 7


variables in the following order: Date, Open, High, Low, Close, Adj.Close, Volume.

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)]

Question 3 2.5 / 2.5 pts

How can we calculate simple (discrete) returns if we have prices in an xts called "p"?

r <- lag(p / log(p))

r <- p / lag(p) - 1

r <- lag(p) / p + 1

r <- log(p / lag(p))


Question 4 2.5 / 2.5 pts

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 , ])

Question 5 2.5 / 2.5 pts

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)

Question 6 2.5 / 2.5 pts

What is the output of the following code in R?

for (i in c(10,23,5,8)){
print(i)
}

10

8
Error

Infinite loop

10

23

Question 7 2.5 / 2.5 pts

What is the pipe operator in R?

->

%/%

%>%

Question 8 2.5 / 2.5 pts

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

Question 9 2.5 / 2.5 pts

What is w doing in the following code in R?

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 is a function that renames the columns to more appropriate variable names

It keeps track of the counter i in the loop

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

Question 10 2.5 / 2.5 pts

What is the output of the following While loops?

n <- 1
while(n <= 1)
{
print(n)
n <- n + 1
}
n<-1
while(n <= 1)
{
n <- n + 1
print(n)
}

1 and 2 respectively

The loops never end

2 and 2 respectively

1 and 1 respectively

Question 11 2.5 / 2.5 pts

Which of the following commands displays the observations on 2017-12-29 and on


2018-12-31 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-12-31"]

sbux[sbux$Date == "2017-12-29" | sbux$Date == "2018-12-31",]

sbux[sbux$Date == "2017-12-29" & sbux$Date == "2018-12-31",]

sbux[,sbux$Date == "2017-12-29" | sbux$Date == "2018-12-31"]

Question 12 2.5 / 2.5 pts

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

Question 13 2.5 / 2.5 pts

Output of the following While loop:

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

Question 14 2.5 / 2.5 pts

What does the pipe operator do in R?


It returns the integer of a division

It will forward a value, or the result of an expression, into the next function call.

It is an advanced assignment operator

It returns the remainder of a division

Question 15 2.5 / 2.5 pts

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)

Question 16 2.5 / 2.5 pts

What is stored in z, after running the following code?

stocks <- c("fb.csv", "amzn.csv", "sbux.csv", "dis.csv", "wmt.csv")


z <- sub(".csv", "", stocks)

44444

".csv" ".csv" ".csv" ".csv" ".csv"


"" "" "" "" ""

"fb" "amzn" "sbux" "dis" "wmt"

Question 17 2.5 / 2.5 pts

What is the output of the following code in R?

i <- 1
while (i >= 5) {
print(i)
i <- i + 1
}

Infinite loop

Nothing happens

Question 18 2.5 / 2.5 pts

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)

Question 19 2.5 / 2.5 pts

What is the output of the following code in R?

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

Question 21 2.5 / 2.5 pts

The output of the following For loop is:

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

Question 22 2.5 / 2.5 pts

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.

Tip2: We want the information on each combination of Date-Ticker to coincide.

prices<-merge(A, B)

prices<-merge(A, B, by.x="Ticker", by.y="Ticker")

prices<-merge(A, B, by.x=c("Fecha", "Ticker"), by.y=c("Date", "Ticker"))

prices<-merge(A, B, by.x="Fecha", by.y="Date")

Question 23 2.5 / 2.5 pts

How do we rename the columns of data.frame amzn in R?

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")

names(amzn) <-"Dates", amzn"

rownames(amzn)<-c("Dates", "amzn")

amzn<-colnames(c("Dates", "amzn"))

Question 24 2.5 / 2.5 pts

What can be said about the comparison of cbind() vs merge()?

merge() is preferred since it joins data.frames considering their common columns

cbind() is preferred since it joins data.frames considering their common columns

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

Question 25 2.5 / 2.5 pts

How are the residuals of a linear regression calculated?

betas <- solve(t(x) %*% x) %*% t(x) %*% y

yhat <- x %*% betas

sse <- sum((y- yhat)^2)

e <- y-yhat

Question 26 2.5 / 2.5 pts

The output of the following For loop is:

z <- c("AMZN", "NFLX", "FB", "MSFT", "TSLA")


for (i in 1:5) {
print(paste(i, z[i]))
}

"AMZN"

"NFLX"

"FB"

"MSFT"

"TSLA"

Error
"1 AMZN"

"2 NFLX"

"3 FB"

"4 MSFT"

"5 TSLA"

Question 27 2.5 / 2.5 pts

"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.

Would the following command work to read the file?

data<-read.csv(file="wmt.csv")

No, since the location of the file is missing.

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).

No, since it's necessary to specify R to skip 1 row.

Question 28 2.5 / 2.5 pts

Regarding the For and While loops below...

for (i in 10:1)
{
print(i)
}

n <- 10
while(n >= 1)
{
print(n)
n <- n - 1
}

Their outputs are not the same

The For loop does more iterations than the While loop

Their outputs are the same

The While loop shown might never end

Question 29 2.5 / 2.5 pts

What is the output of the following code in R?

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

Question 30 2.5 / 2.5 pts

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()

Question 31 2.5 / 2.5 pts

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)

Question 32 2.5 / 2.5 pts

What is the following command doing?

names(p) <- sub(".Adjusted", "", names(p))

Substituting the empty spaces in the names of p, with ".Adjusted"

Removing the ".Adjusted" text portion of all the row names of p

Replacing the ".Adjusted" text portion of all the names of p, with a space

Removing the ".Adjusted" text portion of all the column names of p

Question 33 2.5 / 2.5 pts

What does the following command do in R?

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

Question 34 2.5 / 2.5 pts

What type of variable is fb, after the following command?

fb<-getSymbols("FB",src="yahoo",from="2017-12-29",to="2019-04-02",auto.assign=F)

matrix

xts

data.frame

text

Question 35 2.5 / 2.5 pts

How are the coefficients of a linear regression estimated?

yhat <- x %*% betas

betas <- solve(t(x) %*% x) %*% t(x) %*% y

sst <- sum((y- ybar)^2)

e <- y-yhat
Question 36 2.5 / 2.5 pts

Regarding the For and While loops below...

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

Their outputs are the same

The While loop never ends

Question 37 2.5 / 2.5 pts

What do the commands head(x, n) and tail(x, n) do in R?

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

Question 38 2.5 / 2.5 pts


How are the fitted values of a regression estimated?

e <- y-yhat

ssr <- sum((yhat- ybar)^2)

betas <- solve(t(x) %*% x) %*% t(x) %*% y

yhat <- x %*% betas

Question 39 2.5 / 2.5 pts

How can we calculate log (continuous) returns if we have prices in an xts called "p"?

r <- lag(p) / p + 1

r <- log(p / lag(p))

r <- lag(p / log(p))

r <- p / lag(p) - 1

Question 40 2.5 / 2.5 pts

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

Quiz Score: 100 out of 100

You might also like