library(quantmod)## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(dplyr)##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:xts':
##
## first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(PerformanceAnalytics)##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
library(IntroCompFinR)
library(readxl)for(s in c("asfdas", "MSFT")){
try(
getSymbols(Symbols=s,from="2020-02-02")
)
}## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## Warning: asfdas download failed; trying again.
## Error in getSymbols.yahoo(Symbols = "asfdas", env = <environment>, verbose = FALSE, :
## Unable to import "asfdas".
## el argumento "conn" está ausente, sin valor por omisión
a=1I can know the names of the R objects that I have in my environment:
robjects <- ls()
robjects## [1] "a" "MSFT" "s"
We need to read a list of tickers from an Excel file, and the ticker for the market that is also located in the same Excel file. Our program must read any list of tickers, get the corresponding stock price data from Yahoo, calculate returns and then run all market regression models of these stocks.
# Let´s start by loading the packages we would need
library(quantmod)
library(readxl)tickers.df<-read_excel("InputW7.xlsx",sheet = "tickers")mkticker.df<-read_excel("InputW7.xlsx",sheet = "mkt")# We put together the list of tickers and the market ticker in one vector
tickers.list<-c(mkticker.df$ticker,tickers.df$ticker)
class(tickers.list)## [1] "character"
tickers.list## [1] "^MXX" "AC.MX" "ACCELSAB.MX" "ACTINVRB.MX" "AEROMEX.MX"
## [6] "ALFAA.MX" "ALPEKA.MX" "ALSEA.MX" "AMXL.MX" "ARA.MX"
## [11] "QQQQ.MX" "ASURB.MX" "AUTLANB.MX" "AXTELCPO.MX" "CMOCTEZ.MX"
## [16] "CREAL.MX" "CULTIBAB.MX" "EDOARDOB.MX" "ELEKTRA.MX" "FEMSAUBD.MX"
## [21] "VVV.MX" "FIBRAPL14.MX" "FINDEP.MX" "FFF.MX" "FRAGUAB.MX"
## [26] "FUNO11.MX" "GAPB.MX" "GCARSOA1.MX" "GCC.MX" "HOMEX.MX"
## [31] "HOTEL.MX" "ICHB.MX" "LALAB.MX" "LAMOSA.MX" "MINSAB.MX"
## [36] "MONEXB.MX" "OMAB.MX" "PASAB.MX" "PINFRA.MX" "POCHTECB.MX"
## [41] "POSADASA.MX" "VITROA.MX" "VOLARA.MX" "WALMEX.MX"
#Bring the price data from 2016 from Yahoo, we sould use a Loop to get the price data of each ticker
for (t in tickers.list) {
try(getSymbols(t,
from = "2016-01-01",
periodicity = "monthly",
src = "yahoo") )
}## Warning: QQQQ.MX download failed; trying again.
## Error in getSymbols.yahoo(Symbols = "QQQQ.MX", env = <environment>, verbose = FALSE, :
## Unable to import "QQQQ.MX".
## el argumento "conn" está ausente, sin valor por omisión
## Warning: VVV.MX download failed; trying again.
## Error in getSymbols.yahoo(Symbols = "VVV.MX", env = <environment>, verbose = FALSE, :
## Unable to import "VVV.MX".
## el argumento "conn" está ausente, sin valor por omisión
## Warning: FFF.MX download failed; trying again.
## Error in getSymbols.yahoo(Symbols = "FFF.MX", env = <environment>, verbose = FALSE, :
## Unable to import "FFF.MX".
## el argumento "conn" está ausente, sin valor por omisión
# The function we used "try" is used to avoid the program to stop in case there is an error due lo the lack of information in yahoo finance. this function run everything in a safer way.Market tickers in Yahoo starts with the character ^, but when bringing the data with getSymbols, the character ^ is deleted to name the corresponding object. Then, I create a market ticker string without the ^character:
mkticker.df$ticker## [1] "^MXX"
nchar(mkticker.df$ticker)## [1] 4
mktick<-substr(mkticker.df$ticker,2,nchar(mkticker.df$ticker))
mktick## [1] "MXX"
#Assign to a vector that I would fill later
t.list<-c(mktick)#Now I check which tickers were brought from Yahoo with a loop of the existing objects. The ls() function brings the R objects that exist in the Globa Environment. In this list of objects I have to identify those objects that have a name equal to any of the tickers specified in the tickers.list vector. Then, I can try the following loop to construct a vector with only tickers that could be downloaded from Yahoo Finance:for(t in ls()) {
if (t %in% tickers.list){
t.list <- c(t.list , t)
}
}
t.list## [1] "MXX" "AC.MX" "ACCELSAB.MX" "ACTINVRB.MX" "AEROMEX.MX"
## [6] "ALFAA.MX" "ALPEKA.MX" "ALSEA.MX" "AMXL.MX" "ARA.MX"
## [11] "ASURB.MX" "AUTLANB.MX" "AXTELCPO.MX" "CMOCTEZ.MX" "CREAL.MX"
## [16] "CULTIBAB.MX" "EDOARDOB.MX" "ELEKTRA.MX" "FEMSAUBD.MX" "FIBRAPL14.MX"
## [21] "FINDEP.MX" "FRAGUAB.MX" "FUNO11.MX" "GAPB.MX" "GCARSOA1.MX"
## [26] "GCC.MX" "HOMEX.MX" "HOTEL.MX" "ICHB.MX" "LALAB.MX"
## [31] "LAMOSA.MX" "MINSAB.MX" "MONEXB.MX" "OMAB.MX" "PASAB.MX"
## [36] "PINFRA.MX" "POCHTECB.MX" "POSADASA.MX" "VITROA.MX" "VOLARA.MX"
## [41] "WALMEX.MX"
tickers.list<-t.list# I use the lapply function to apply the get function to each element of the tickers.list
objList <- lapply(tickers.list, get)
class(objList)## [1] "list"
#I merge the objects
prices.zoo <- do.call(merge, objList)
#The do.call function has as the first argument the function and as the second the list of objects#I can drop the objects from my environment using the same do.call function: Before dropping the objects, I first create a list from the vector, since the function do.call needs a list object:
tickerlist=as.list(tickers.list)
do.call(rm,tickerlist)Calculate the continously compounded returns for the adjusted price columns
returns.df <-as.data.frame(diff(log(Ad(prices.zoo))))
#The Ad function selects only the Adjusted prices# I create a list of tickers without .MX:
tickers.list<-sub(".MX","",tickerlist,fixed=TRUE)
tickers.list## [1] "MXX" "AC" "ACCELSAB" "ACTINVRB" "AEROMEX" "ALFAA"
## [7] "ALPEKA" "ALSEA" "AMXL" "ARA" "ASURB" "AUTLANB"
## [13] "AXTELCPO" "CMOCTEZ" "CREAL" "CULTIBAB" "EDOARDOB" "ELEKTRA"
## [19] "FEMSAUBD" "FIBRAPL14" "FINDEP" "FRAGUAB" "FUNO11" "GAPB"
## [25] "GCARSOA1" "GCC" "HOMEX" "HOTEL" "ICHB" "LALAB"
## [31] "LAMOSA" "MINSAB" "MONEXB" "OMAB" "PASAB" "PINFRA"
## [37] "POCHTECB" "POSADASA" "VITROA" "VOLARA" "WALMEX"
# The sub function identifies a substring and replace it with other substring
# Now I assign the column names to returns.df:
colnames(returns.df)<-tickers.list
# I can see the first rows of the first 3 tickers
head(returns.df[,1:3])Now I run the market regression models for all tickers
The market model can be estimated by running a pulled time-series regression model using the market returns as the independent variable, and the individual asset returns as the dependent variable:
Ri(t)=α+βRM(t)+εt
#According to the efficient market hypothesis, the expected value of the constant alpha (or beta0) in this regression model should be zero. Why? because if we find an individual asset that has a significant positive alpha coefficient, then this individual asset is expected to over perform the market. # In a regression model we try to explain a dependent variable with one or more independent variables. In the market model, we have the market returns as the independent or explanatory variable, and the stock return as the dependent variable. alfamod <- lm(ALFAA ~ MXX, data=returns.df)
# The dependent variable is ALFAA an the independent variable is MXX (the market returns)class(alfamod)## [1] "lm"
attributes(alfamod)## $names
## [1] "coefficients" "residuals" "effects" "rank"
## [5] "fitted.values" "assign" "qr" "df.residual"
## [9] "na.action" "xlevels" "call" "terms"
## [13] "model"
##
## $class
## [1] "lm"
summary(alfamod)##
## Call:
## lm(formula = ALFAA ~ MXX, data = returns.df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.35203 -0.04901 -0.01404 0.04003 0.35719
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.01601 0.01183 -1.353 0.181
## MXX 1.71579 0.26656 6.437 1.52e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.09816 on 67 degrees of freedom
## (4 observations deleted due to missingness)
## Multiple R-squared: 0.3821, Adjusted R-squared: 0.3729
## F-statistic: 41.43 on 1 and 67 DF, p-value: 1.521e-08
s <-summary(alfamod)
class(s)## [1] "summary.lm"
s$coefficients## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.01601245 0.0118321 -1.353305 1.805067e-01
## MXX 1.71578602 0.2665558 6.436873 1.520842e-08
s$coefficients[,1]## (Intercept) MXX
## -0.01601245 1.71578602
s$coefficients[,2]## (Intercept) MXX
## 0.0118321 0.2665558
# I can also use the function coefficients to directly estimate the beta coefficients in a vector:
betasalfa <- coefficients(lm(ALFAA ~ MXX, data=returns.df))How can we run several regression models using a loop?
#We transform the data frame to a matrix
rets.mat <- as.matrix(returns.df)
class(rets.mat)## [1] "matrix" "array"
head(rets.mat)## MXX AC ACCELSAB ACTINVRB AEROMEX
## X2016.01.01 NA NA NA NA NA
## X2016.02.01 0.001927060 0.019909097 0.010929071 -0.0817375976 -0.030795377
## X2016.03.01 0.048363107 0.076900795 0.000000000 0.0262678312 0.079862312
## X2016.04.01 -0.002101298 -0.005625702 0.000000000 0.0645385565 -0.057441505
## X2016.05.01 -0.007130790 0.048172377 -0.001087548 -0.0238728824 -0.029744905
## X2016.06.01 0.011091913 0.066081115 0.000000000 -0.0007150447 -0.008701492
## ALFAA ALPEKA ALSEA AMXL ARA
## X2016.01.01 NA NA NA NA NA
## X2016.02.01 -0.0267484546 0.149233763 0.060441425 -0.035817543 0.03683642
## X2016.03.01 0.0551088076 0.007366432 -0.050521864 0.084645076 0.16007482
## X2016.04.01 -0.0691327331 0.054805197 0.016063964 -0.099339329 0.03927998
## X2016.05.01 0.0006183686 -0.036870591 0.061936135 -0.074234381 -0.06108779
## X2016.06.01 -0.0275706746 0.080459373 0.002651773 -0.006219484 0.03101029
## ASURB AUTLANB AXTELCPO CMOCTEZ CREAL
## X2016.01.01 NA NA NA NA NA
## X2016.02.01 0.023398233 -0.06581298 -0.04370876 -0.0001829036 0.01822456
## X2016.03.01 0.024978897 0.22586094 0.10824728 0.0765345516 -0.04116895
## X2016.04.01 0.015139894 -0.08639476 -0.10562260 -0.0169106900 0.02860005
## X2016.05.01 0.096716698 0.01759566 -0.16800771 0.0002802475 0.01308914
## X2016.06.01 -0.002029307 0.03147607 -0.02830378 0.0244380708 -0.10175484
## CULTIBAB EDOARDOB ELEKTRA FEMSAUBD FIBRAPL14
## X2016.01.01 NA NA NA NA NA
## X2016.02.01 -0.11718423 0 -0.019094852 -0.01112042 0.00872727
## X2016.03.01 0.08513939 0 0.009134143 -0.01847325 0.05494250
## X2016.04.01 -0.02673505 0 -0.035231237 -0.04112184 -0.01246800
## X2016.05.01 -0.11566846 0 -0.117311730 0.04076221 0.02514235
## X2016.06.01 0.04469758 0 -0.118030627 0.02236657 0.08023531
## FINDEP FRAGUAB FUNO11 GAPB GCARSOA1
## X2016.01.01 NA NA NA NA NA
## X2016.02.01 -0.003025721 -8.701135e-02 0.03963338 -0.06089568 0.004515942
## X2016.03.01 0.087011377 0.000000e+00 0.07389787 0.07264196 0.095384512
## X2016.04.01 -0.005571045 -9.106013e-05 0.01972420 0.05035204 0.020880163
## X2016.05.01 0.128280199 -1.493029e-02 -0.02471699 0.14070035 -0.052016464
## X2016.06.01 -0.111659318 1.680444e-02 -0.01545081 0.02604295 0.004921394
## GCC HOMEX HOTEL ICHB LALAB
## X2016.01.01 NA NA NA NA NA
## X2016.02.01 0.019056780 -0.01245346 -0.02850549 0.01198740 -0.01147163
## X2016.03.01 0.086074588 -0.11403069 -0.02934196 0.17668908 0.10625622
## X2016.04.01 0.040409571 -0.04893062 0.09801349 0.11220243 -0.02298946
## X2016.05.01 -0.020202805 -0.22388129 -0.08079290 0.04912216 -0.03629514
## X2016.06.01 -0.006819703 -0.45462434 0.05803879 0.00000000 -0.09769443
## LAMOSA MINSAB MONEXB OMAB PASAB
## X2016.01.01 NA NA NA NA NA
## X2016.02.01 -0.0137932343 0.02829367 0.00000000 0.019828271 -0.038839784
## X2016.03.01 0.0408219503 0.00000000 -0.09331197 0.136199669 0.000000000
## X2016.04.01 -0.0297720971 0.05363011 -0.03762542 0.009283721 0.000000000
## X2016.05.01 -0.0280474319 -0.03493800 0.17614228 0.096664977 0.004938183
## X2016.06.01 -0.0002856871 0.00000000 -0.05335851 0.023334868 -0.004938183
## PINFRA POCHTECB POSADASA VITROA VOLARA
## X2016.01.01 NA NA NA NA NA
## X2016.02.01 0.01989910 -0.18537189 0.002328268 0.07975250 0.146026252
## X2016.03.01 0.08775580 0.05545976 0.000000000 0.00208839 0.030473355
## X2016.04.01 -0.05149506 -0.03729578 0.058707532 0.03283469 -0.019840882
## X2016.05.01 0.01062226 -0.10536052 0.000000000 0.03290447 -0.004183551
## X2016.06.01 0.02671249 0.00554018 0.000000000 -0.04942581 -0.041078195
## WALMEX
## X2016.01.01 NA
## X2016.02.01 -0.06527026
## X2016.03.01 -0.03731945
## X2016.04.01 0.03710767
## X2016.05.01 0.02259890
## X2016.06.01 0.02466993
ncol(rets.mat)## [1] 41
results.m = matrix(nrow=ncol(rets.mat)-1, ncol=2)
# I do a loop from 2 (column 2) to the last column of rets.mat:
for(i in 2:ncol(rets.mat)) {
results.m[i-1,] = coefficients(lm(rets.mat[,i] ~ rets.mat[,1]))
}
results.m## [,1] [,2]
## [1,] 2.689651e-03 0.68504536
## [2,] 1.127821e-02 0.21602260
## [3,] -3.278550e-03 0.34157229
## [4,] -3.049997e-02 1.01699871
## [5,] -1.601245e-02 1.71578602
## [6,] -2.246524e-03 1.15532085
## [7,] -1.150624e-02 2.40358700
## [8,] 5.275993e-03 0.84458238
## [9,] -2.656807e-03 0.79522421
## [10,] 4.499426e-03 1.39424541
## [11,] 1.119921e-02 0.56092558
## [12,] -9.233344e-03 0.72284971
## [13,] 8.815558e-03 0.28348490
## [14,] -1.797758e-02 0.56781980
## [15,] -1.101266e-02 0.09659386
## [16,] 0.000000e+00 0.00000000
## [17,] 2.192632e-02 0.30315094
## [18,] 3.781479e-05 0.87248305
## [19,] 1.140233e-02 0.59989085
## [20,] 2.092378e-03 0.16569717
## [21,] 5.507753e-03 0.19469081
## [22,] -4.451259e-03 1.53145720
## [23,] 7.389952e-03 1.63308815
## [24,] -1.932974e-03 1.14083319
## [25,] 1.646737e-02 1.00687198
## [26,] -6.171700e-02 0.31058756
## [27,] -9.955006e-03 0.58418997
## [28,] 1.734752e-02 0.10492757
## [29,] -1.320888e-02 1.12191342
## [30,] 1.070159e-02 0.44384068
## [31,] -2.415745e-03 -0.09131877
## [32,] 3.965128e-03 -0.01609301
## [33,] 5.127758e-03 1.70408919
## [34,] -7.538400e-03 0.15951450
## [35,] -6.309491e-03 0.94491451
## [36,] -5.326558e-03 -0.01139842
## [37,] -9.374762e-03 0.66239238
## [38,] -1.257278e-02 0.97551498
## [39,] 7.834151e-04 2.11305479
## [40,] 7.455687e-03 0.36664735
# I first define an empty matrix results.m with 2 columns and 2 rows since ncol(rets.mat) will be 3. I will fill out the content of this matrix in the loop.
# In this case the function coefficients is extracting only the beta coefficients of the regression lm(rets.mat[,i] ~ rets.mat[,1])). As you see, in this case the loop executes 2 iterations since the number of columns of the matrix rets.mat is only 3 columns. I start with column 2 since column one has the market returns, and I have stock returns in column 2 and 3. If I expand the list of tickers that I get from getSymbols this code will still run since the loop will stop until the number of columns of the matrix rets.mat.
# For each interation I am running the market regression for the corresponding column i. Let’s analyze the code lm(rets.mat[,i] ~ rets.mat[,1])). In this case, rets.mat[,i] means that I am considering as the dependent variable all returns of the column i of the matrix. In the case of rets.mat[,1] I am indicating that the independent variable will be the column 1 of the rets.mat matrix since the market return is located in column 1.
# The result will be saved in results.m.rm(list=ls())You have to write a function that estimates the market model for a stock, but now it has to return:
°beta coefficients (beta0 and beta1), °standard errors of beta coefficients, °p-values of beta coefficients, and °the # of valid (non-missing) observations used to run the regression.
Here are the specifications of the function:
Your function has to receive 2 parameters: 1) the continuously compounded (cc) return of a stock, and 2) the cc returns of the market.
Using the lm function, run a simple regression model according to the market model. Remember that the dependent variable must the stock return, and the independent variable must be the market returns.
Your function must return a vector with 7 values. The structure of this vector is illustrated in the following example:
# p- p-
#b0 se(b0) value(b0) b1 se(b1) value(b1)
#0.01 0.02 0.30 1.3 0.10 0.0001
# Non-missing
#35The non-missing observations can be calculated using the attriute df.residual of the regression model. You just have to add 2 to this value since the degrees of freedom of the regression residual is N-2, where N is the number of non-missing observations.
Test your function with at least 2 stocks (select any stocks you want), and use the correct market index. Display the vectors and INTERPRET the result of the beta coefficients of only ONE stock.
marketM<- function(stock_r, market_r){
model<- lm(stock_r~market_r)
msm<- summary(model)
final.vector<-c(msm$coefficients[1,c(1,2,4)],msm$coefficients[2,c(1,2,4)])
final.vector<-c(final.vector,model$df.residual+2)
names(final.vector) <- c("b0","se(b0)", "value(b0)", "b1", "se(b1)", "value(b1)" ,"#Non-,missing")
return(final.vector)
}getSymbols(c("AMZN","FB"),from= "2016-01-01", periodicity= "monthly", src = "yahoo")## [1] "AMZN" "FB"
adjprices <- merge(Ad(AMZN),Ad(FB))Returns<-diff(log(as.zoo(adjprices)))
head(Returns)## AMZN.Adjusted FB.Adjusted
## 2016-02-01 -0.06053515 -0.04829123
## 2016-03-01 0.07178343 0.06499437
## 2016-04-01 0.10534537 0.03004373
## 2016-05-01 0.09150022 0.01040659
## 2016-06-01 -0.00996940 -0.03887399
## 2016-07-01 0.05860212 0.08114603
# Now I test the function:
model1<- marketM(Returns$AMZN.Adjusted,Returns$FB.Adjusted)
# Now modelo1 will be the vector with the results:
model1## b0 se(b0) value(b0) b1 se(b1)
## 1.495256e-02 7.371292e-03 4.642957e-02 6.028990e-01 9.513850e-02
## value(b1) #Non-,missing
## 2.170362e-08 7.000000e+01
Bring data from Jan 2014 to Dec 2016 from the InputW7.xslx file.
Write the corresponding code to estimate the market models of all stocks. The specifications of the program are the following:
a)Write a loop to run the a market regression model for each stock.
b)You must use the function you defined in the previous part to run the market regression models.
c)Make sure your program runs with any number of tickers specified in the Excel file. I recommend you to start testing your program with few tickers (2 or 3), so create another excel file with only 2 or 3 stocks.
d)Your program must create a Matrix with the results of the models for all stocks. The structure of this matrix is the following:
# MATRIX RESULTS:
# p-
#STOCK b0 se(b0) value(b0) b1 se(b1)
#ALFA 0.01 0.02 0.30 1.3 0.10
#p-value(b1) # Non-missing
#0.0001 35For the stock names, use the rownames of the matrix.
Then, each row will be the regression results for each stock, and each column will have specific values of the betas, std errors, p-values, and the # of non-missing values used in the regression. Note that it is possible that some stocks might not have prices/returns for some periods.
tickers.df<-read_excel("InputW7.xlsx",sheet = "tickers")mkticker.df<-read_excel("InputW7.xlsx",sheet = "mkt")tickers.list<-c(mkticker.df$ticker,tickers.df$ticker)
class(tickers.list)## [1] "character"
tickers.list## [1] "^MXX" "AC.MX" "ACCELSAB.MX" "ACTINVRB.MX" "AEROMEX.MX"
## [6] "ALFAA.MX" "ALPEKA.MX" "ALSEA.MX" "AMXL.MX" "ARA.MX"
## [11] "QQQQ.MX" "ASURB.MX" "AUTLANB.MX" "AXTELCPO.MX" "CMOCTEZ.MX"
## [16] "CREAL.MX" "CULTIBAB.MX" "EDOARDOB.MX" "ELEKTRA.MX" "FEMSAUBD.MX"
## [21] "VVV.MX" "FIBRAPL14.MX" "FINDEP.MX" "FFF.MX" "FRAGUAB.MX"
## [26] "FUNO11.MX" "GAPB.MX" "GCARSOA1.MX" "GCC.MX" "HOMEX.MX"
## [31] "HOTEL.MX" "ICHB.MX" "LALAB.MX" "LAMOSA.MX" "MINSAB.MX"
## [36] "MONEXB.MX" "OMAB.MX" "PASAB.MX" "PINFRA.MX" "POCHTECB.MX"
## [41] "POSADASA.MX" "VITROA.MX" "VOLARA.MX" "WALMEX.MX"
for (t in tickers.list) {
try(getSymbols(t,
from = "2014-01-01",
to = "2016-12-31",
periodicity = "monthly",
src = "yahoo") )
}## Warning: QQQQ.MX download failed; trying again.
## Error in getSymbols.yahoo(Symbols = "QQQQ.MX", env = <environment>, verbose = FALSE, :
## Unable to import "QQQQ.MX".
## el argumento "conn" está ausente, sin valor por omisión
## Warning: VVV.MX download failed; trying again.
## Error in getSymbols.yahoo(Symbols = "VVV.MX", env = <environment>, verbose = FALSE, :
## Unable to import "VVV.MX".
## el argumento "conn" está ausente, sin valor por omisión
## Warning: FFF.MX download failed; trying again.
## Error in getSymbols.yahoo(Symbols = "FFF.MX", env = <environment>, verbose = FALSE, :
## Unable to import "FFF.MX".
## el argumento "conn" está ausente, sin valor por omisión
mkticker.df$ticker## [1] "^MXX"
nchar(mkticker.df$ticker)## [1] 4
mktick<-substr(mkticker.df$ticker,2,nchar(mkticker.df$ticker))
mktick## [1] "MXX"
t.list<-c(mktick)for(t in ls()) {
if (t %in% tickers.list){
t.list <- c(t.list , t)
}
}
t.list## [1] "MXX" "AC.MX" "ACCELSAB.MX" "ACTINVRB.MX" "AEROMEX.MX"
## [6] "ALFAA.MX" "ALPEKA.MX" "ALSEA.MX" "AMXL.MX" "ARA.MX"
## [11] "ASURB.MX" "AUTLANB.MX" "AXTELCPO.MX" "CMOCTEZ.MX" "CREAL.MX"
## [16] "CULTIBAB.MX" "EDOARDOB.MX" "ELEKTRA.MX" "FEMSAUBD.MX" "FIBRAPL14.MX"
## [21] "FINDEP.MX" "FRAGUAB.MX" "FUNO11.MX" "GAPB.MX" "GCARSOA1.MX"
## [26] "GCC.MX" "HOMEX.MX" "HOTEL.MX" "ICHB.MX" "LALAB.MX"
## [31] "LAMOSA.MX" "MINSAB.MX" "MONEXB.MX" "OMAB.MX" "PASAB.MX"
## [36] "PINFRA.MX" "POCHTECB.MX" "POSADASA.MX" "VITROA.MX" "VOLARA.MX"
## [41] "WALMEX.MX"
tickers.list<-t.listobjList <- lapply(tickers.list, get)
class(objList)## [1] "list"
prices.zoo <- do.call(merge, objList)tickerlist=as.list(tickers.list)
do.call(rm,tickerlist)returns.df <-as.data.frame(diff(log(Ad(prices.zoo))))colnames(returns.df)<-tickers.list#loop
results.m <- c()
for(i in 2:ncol(returns.df)) {
mrm<- marketM(returns.df[,i], returns.df[,1])
results.m<- rbind(results.m,mrm)
}colnames(results.m)<-c("b0","se(b0)", "value(b0)", "b1", "se(b1)", "value(b1)" ,"#Non-,missing")
head(results.m)## b0 se(b0) value(b0) b1 se(b1) value(b1)
## mrm 0.0086734868 0.006952404 0.22097593 1.0603253 0.2402732 1.027823e-04
## mrm -0.0008852498 0.001717389 0.60966603 -0.1022455 0.0593525 9.430668e-02
## mrm -0.0042596196 0.004862713 0.38737508 0.2472883 0.1680540 1.506339e-01
## mrm 0.0190174784 0.011423310 0.10542145 0.2870428 0.3947865 4.723016e-01
## mrm -0.0163890428 0.009516927 0.09441462 1.7454344 0.3289024 7.459658e-06
## mrm -0.0062097206 0.013684271 0.65295374 1.3496103 0.4729247 7.407732e-03
## #Non-,missing
## mrm 35
## mrm 35
## mrm 35
## mrm 35
## mrm 35
## mrm 35
ncol(results.m)## [1] 7
nrow(results.m)## [1] 40
rownames(results.m)<-tickers.list[2:length(tickers.list)]head(results.m)## b0 se(b0) value(b0) b1 se(b1)
## AC.MX 0.0086734868 0.006952404 0.22097593 1.0603253 0.2402732
## ACCELSAB.MX -0.0008852498 0.001717389 0.60966603 -0.1022455 0.0593525
## ACTINVRB.MX -0.0042596196 0.004862713 0.38737508 0.2472883 0.1680540
## AEROMEX.MX 0.0190174784 0.011423310 0.10542145 0.2870428 0.3947865
## ALFAA.MX -0.0163890428 0.009516927 0.09441462 1.7454344 0.3289024
## ALPEKA.MX -0.0062097206 0.013684271 0.65295374 1.3496103 0.4729247
## value(b1) #Non-,missing
## AC.MX 1.027823e-04 35
## ACCELSAB.MX 9.430668e-02 35
## ACTINVRB.MX 1.506339e-01 35
## AEROMEX.MX 4.723016e-01 35
## ALFAA.MX 7.459658e-06 35
## ALPEKA.MX 7.407732e-03 35