Hi all,
I attach my code, the think is I want to make a bar plot the last variable called "bwchist" so the X axis are "Accion" and the y axis are "reval" values. I have prove class(bwchist) and says dataframe but its still a list because it says me I have prove to unlist, but it doesnt work hist(bwchist) Error in hist.default(bwchist) : 'x' must be numeric Or barplot(bwchist) Error in barplot.default(bwchist) : 'height' must be a vector or a matrix library(PerformanceAnalytics) library(dplyr) library(tibble) library(lubridate) library(PerformanceAnalytics) library(quantmod) library(ggplot2) library(png) library(grid) library(RCurl) library(tidyquant) library(timetk) library(data.table) Acciona<- tq_get("ANA.MC",from = '2019-12-31',get = "stock.prices") ACS<- tq_get("ACS.MC",from = '2019-12-31',get = "stock.prices") Aena<- tq_get("AENA.MC",from = '2019-12-31',get = "stock.prices") Amadeus<- tq_get("AMS.MC",from = '2019-12-31',get = "stock.prices") ArcelorMittal<- tq_get("MTS.MC",from = '2019-12-31',get = "stock.prices") BBVA<- tq_get("BBVA.MC",from = '2019-12-31',get = "stock.prices") Sabadell<- tq_get("SAB.MC",from = '2019-12-31',get = "stock.prices") Santander<- tq_get("SAN.MC",from = '2019-12-31',get = "stock.prices") Bankinter<- tq_get("BKT.MC",from = '2019-12-31',get = "stock.prices") CaixaBank<- tq_get("CABK.MC",from = '2019-12-31',get = "stock.prices") Cellnex<- tq_get("CLNX.MC",from = '2019-12-31',get = "stock.prices") Enagas<- tq_get("ENG.MC",from = '2019-12-31',get = "stock.prices") ENCE<- tq_get("ENC.MC",from = '2019-12-31',get = "stock.prices") Endesa<- tq_get("ELE.MC",from = '2019-12-31',get = "stock.prices") Ferrovial<- tq_get("FER.MC",from = '2019-12-31',get = "stock.prices") Grifols<- tq_get("GRF.MC",from = '2019-12-31',get = "stock.prices") Iberdrola<- tq_get("IBE.MC",from = '2019-12-31',get = "stock.prices") Inditex<- tq_get("ITX.MC",from = '2019-12-31',get = "stock.prices") Colonial<- tq_get("COL.MC",from = '2019-12-31',get = "stock.prices") IAG<- tq_get("IAG.MC",from = '2019-12-31',get = "stock.prices") Mapfre<- tq_get("MAP.MC",from = '2019-12-31',get = "stock.prices") Melia<- tq_get("MEL.MC",from = '2019-12-31',get = "stock.prices") Merlin<- tq_get("MRL.MC",from = '2019-12-31',get = "stock.prices") Naturgy<- tq_get("NTGY.MC",from = '2019-12-31',get = "stock.prices") REE<- tq_get("REE.MC",from = '2019-12-31',get = "stock.prices") Repsol<- tq_get("REP.MC",from = '2019-12-31',get = "stock.prices") SGamesa<- tq_get("SGRE.MC",from = '2019-12-31',get = "stock.prices") Telefonica<- tq_get("TEF.MC",from = '2019-12-31',get = "stock.prices") Viscofan<- tq_get("VIS.MC",from = '2019-12-31',get = "stock.prices") Acerinox<- tq_get("ACX.MC",from = '2019-12-31',get = "stock.prices") Bankia<- tq_get("BKIA.MC",from = '2019-12-31',get = "stock.prices") CIE<- tq_get("CIE.MC",from = '2019-12-31',get = "stock.prices") MasMovil<- tq_get("MAS.MC",from = '2019-12-31',get = "stock.prices") Almirall<- tq_get("ALM.MC",from = '2019-12-31',get = "stock.prices") Indra<- tq_get("IDR.MC",from ='2019-12-31',get = "stock.prices") Indra_daily_returns <- Indra %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Indra_cum_returns <- Indra_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Almirall_daily_returns <- Almirall %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Almirall_cum_returns <- Almirall_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Acciona_daily_returns <- Acciona %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Acciona_cum_returns <- Acciona_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) ACS_daily_returns <- ACS %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column ACS_cum_returns <- ACS_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Aena_daily_returns <- Aena %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Aena_cum_returns <- Aena_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Amadeus_daily_returns <- Amadeus %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Amadeus_cum_returns <- Amadeus_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) ArcelorMittal_daily_returns <- ArcelorMittal %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column ArcelorMittal_cum_returns <- ArcelorMittal_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) BBVA_daily_returns <- BBVA %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column BBVA_cum_returns <- BBVA_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Sabadell_daily_returns <- Sabadell %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Sabadell_cum_returns <- Sabadell_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Santander_daily_returns <- Santander %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Santander_cum_returns <- Santander_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Bankinter_daily_returns <- Bankinter %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Bankinter_cum_returns <- Bankinter_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) CaixaBank_daily_returns <- CaixaBank %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column CaixaBank_cum_returns <- CaixaBank_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Cellnex_daily_returns <- Cellnex %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Cellnex_cum_returns <- Cellnex_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) CIE_daily_returns <- CIE %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column CIE_cum_returns <- CIE_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) CIE_daily_returns <- CIE %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column CIE_cum_returns <- CIE_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Enagas_daily_returns <- Enagas %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Enagas_cum_returns <- Enagas_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) ENCE_daily_returns <- ENCE %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column ENCE_cum_returns <- ENCE_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Endesa_daily_returns <- Endesa %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Endesa_cum_returns <- Endesa_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Ferrovial_daily_returns <- Ferrovial %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Ferrovial_cum_returns <- Ferrovial_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Grifols_daily_returns <- Grifols %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Grifols_cum_returns <- Grifols_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Iberdrola_daily_returns <- Iberdrola %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Iberdrola_cum_returns <- Iberdrola_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Inditex_daily_returns <- Inditex %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Inditex_cum_returns <- Inditex_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Colonial_daily_returns <- Colonial %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Colonial_cum_returns <- Colonial_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) IAG_daily_returns <- IAG %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column IAG_cum_returns <- IAG_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Mapfre_daily_returns <- Mapfre %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Mapfre_cum_returns <- Mapfre_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Melia_daily_returns <- Melia %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Melia_cum_returns <- Melia_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Merlin_daily_returns <- Merlin %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Merlin_cum_returns <- Merlin_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Naturgy_daily_returns <- Naturgy %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Naturgy_cum_returns <- Naturgy_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) REE_daily_returns <- REE %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column REE_cum_returns <- REE_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Repsol_daily_returns <- Repsol %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Repsol_cum_returns <- Repsol_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) SGamesa_daily_returns <- SGamesa %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column SGamesa_cum_returns <- SGamesa_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Telefonica_daily_returns <- Telefonica %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Telefonica_cum_returns <- Telefonica_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Viscofan_daily_returns <- Viscofan %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Viscofan_cum_returns <- Viscofan_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Acerinox_daily_returns <- Acerinox %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Acerinox_cum_returns <- Acerinox_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Bankia_daily_returns <- Bankia %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Bankia_cum_returns <- Bankia_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) MasMovil_daily_returns <- MasMovil %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column MasMovil_cum_returns <- MasMovil_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) Indra_daily_returns <- Indra %>% tq_transmute(select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column Indra_cum_returns <- Indra_daily_returns %>% mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod function mutate(cumulative_returns = cr - 1) bestworst<-c(Acciona_cum_returns[nrow(Acciona_cum_returns),4], ACS_cum_returns[nrow(ACS_cum_returns),4], Aena_cum_returns[nrow(Aena_cum_returns),4], Amadeus_cum_returns[nrow(Amadeus_cum_returns),4], ArcelorMittal_cum_returns[nrow(ArcelorMittal_cum_returns),4], BBVA_cum_returns[nrow(BBVA_cum_returns),4], Sabadell_cum_returns[nrow(Sabadell_cum_returns),4], Santander_cum_returns[nrow(Santander_cum_returns),4], Bankinter_cum_returns[nrow(Bankinter_cum_returns),4], CaixaBank_cum_returns[nrow(CaixaBank_cum_returns),4], Cellnex_cum_returns[nrow(Cellnex_cum_returns),4], Enagas_cum_returns[nrow(Enagas_cum_returns),4], ENCE_cum_returns[nrow(ENCE_cum_returns),4], Endesa_cum_returns[nrow(Endesa_cum_returns),4], Ferrovial_cum_returns[nrow(Ferrovial_cum_returns),4], Grifols_cum_returns[nrow(Grifols_cum_returns),4], Iberdrola_cum_returns[nrow(Iberdrola_cum_returns),4], Inditex_cum_returns[nrow(Inditex_cum_returns),4], Colonial_cum_returns[nrow(Colonial_cum_returns),4], IAG_cum_returns[nrow(IAG_cum_returns),4], Mapfre_cum_returns[nrow(Mapfre_cum_returns),4], Melia_cum_returns[nrow(Melia_cum_returns),4], Merlin_cum_returns[nrow(Merlin_cum_returns),4], Naturgy_cum_returns[nrow(Naturgy_cum_returns),4], REE_cum_returns[nrow(REE_cum_returns),4], Repsol_cum_returns[nrow(Repsol_cum_returns),4], SGamesa_cum_returns[nrow(SGamesa_cum_returns),4], Telefonica_cum_returns[nrow(Telefonica_cum_returns),4], Viscofan_cum_returns[nrow(Viscofan_cum_returns),4], Acerinox_cum_returns[nrow(Acerinox_cum_returns),4], Bankia_cum_returns[nrow(Bankia_cum_returns),4], CIE_cum_returns[nrow(CIE_cum_returns),4], MasMovil_cum_returns[nrow(MasMovil_cum_returns),4], Almirall_cum_returns[nrow(Almirall_cum_returns),4], Indra_cum_returns[nrow(Indra_cum_returns),4]) namebw<-c("Acciona", "ACS", "Aena", "Amadeus", "ArcelorMittal", "BBVA", "Sabadell", "Santander", "Bankinter", "CaixaBank", "Cellnex", "Enagas", "ENCE", "Endesa", "Ferrovial", "Grifols", "Iberdrola", "Inditex", "Colonial", "IAG", "Mapfre", "Melia", "Merlin", "Naturgy", "REE", "Repsol", "SGamesa", "Telefonica", "Viscofan", "Acerinox", "Bankia", "CIE", "MasMovil", "Almirall", "Indra") bwfinal <- matrix(bestworst, nrow =35 , ncol = 1) bwfinal2 <- matrix(namebw, nrow =35 , ncol = 1) bwc<-cbind(bwfinal2,bwfinal) colnames(bwc)=c("Accion","reval") bwc <- as.data.frame(bwc) colnames(bwchist)=c("Accion","reval") bwchist <-as.data.frame(bwc[order(bwc$reval), ]) [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
Dear Pedro
Some comments in-line On 30/07/2020 21:16, Pedro páramo wrote: > Hi all, > > I attach my code, the think is I want to make a bar plot the last variable > called "bwchist" > > so the X axis are "Accion" and the y axis are "reval" values. > > I have prove class(bwchist) and says dataframe but its still a list because > it says me > > I have prove to unlist, but it doesnt work > > hist(bwchist) > Error in hist.default(bwchist) : 'x' must be numeric So bwchist is not a numeric variable as hist needs. Aboce you said it is a data frame but data frames are not numeric. For future reference your example is way too long for anyone to go through and try to help you. Try next time to reduce it to the absolute minimum by removing sections while you still get the error. It is also easier to get help if you can remove unnecessary packages. It is also unreadable because you are posting in HTML and that makes the post unreadable as this is a plain text list. Michael > > Or > > barplot(bwchist) > Error in barplot.default(bwchist) : 'height' must be a vector or a matrix > > library(PerformanceAnalytics) > library(dplyr) > library(tibble) > library(lubridate) > library(PerformanceAnalytics) > library(quantmod) > library(ggplot2) > library(png) > library(grid) > library(RCurl) > library(tidyquant) > library(timetk) > library(data.table) > > > > Acciona<- tq_get("ANA.MC",from = '2019-12-31',get = "stock.prices") > ACS<- tq_get("ACS.MC",from = '2019-12-31',get = "stock.prices") > Aena<- tq_get("AENA.MC",from = '2019-12-31',get = "stock.prices") > Amadeus<- tq_get("AMS.MC",from = '2019-12-31',get = "stock.prices") > ArcelorMittal<- tq_get("MTS.MC",from = '2019-12-31',get = "stock.prices") > BBVA<- tq_get("BBVA.MC",from = '2019-12-31',get = "stock.prices") > Sabadell<- tq_get("SAB.MC",from = '2019-12-31',get = "stock.prices") > Santander<- tq_get("SAN.MC",from = '2019-12-31',get = "stock.prices") > Bankinter<- tq_get("BKT.MC",from = '2019-12-31',get = "stock.prices") > CaixaBank<- tq_get("CABK.MC",from = '2019-12-31',get = "stock.prices") > Cellnex<- tq_get("CLNX.MC",from = '2019-12-31',get = "stock.prices") > Enagas<- tq_get("ENG.MC",from = '2019-12-31',get = "stock.prices") > ENCE<- tq_get("ENC.MC",from = '2019-12-31',get = "stock.prices") > Endesa<- tq_get("ELE.MC",from = '2019-12-31',get = "stock.prices") > Ferrovial<- tq_get("FER.MC",from = '2019-12-31',get = "stock.prices") > Grifols<- tq_get("GRF.MC",from = '2019-12-31',get = "stock.prices") > Iberdrola<- tq_get("IBE.MC",from = '2019-12-31',get = "stock.prices") > Inditex<- tq_get("ITX.MC",from = '2019-12-31',get = "stock.prices") > Colonial<- tq_get("COL.MC",from = '2019-12-31',get = "stock.prices") > IAG<- tq_get("IAG.MC",from = '2019-12-31',get = "stock.prices") > Mapfre<- tq_get("MAP.MC",from = '2019-12-31',get = "stock.prices") > Melia<- tq_get("MEL.MC",from = '2019-12-31',get = "stock.prices") > Merlin<- tq_get("MRL.MC",from = '2019-12-31',get = "stock.prices") > Naturgy<- tq_get("NTGY.MC",from = '2019-12-31',get = "stock.prices") > REE<- tq_get("REE.MC",from = '2019-12-31',get = "stock.prices") > Repsol<- tq_get("REP.MC",from = '2019-12-31',get = "stock.prices") > SGamesa<- tq_get("SGRE.MC",from = '2019-12-31',get = "stock.prices") > Telefonica<- tq_get("TEF.MC",from = '2019-12-31',get = "stock.prices") > Viscofan<- tq_get("VIS.MC",from = '2019-12-31',get = "stock.prices") > Acerinox<- tq_get("ACX.MC",from = '2019-12-31',get = "stock.prices") > Bankia<- tq_get("BKIA.MC",from = '2019-12-31',get = "stock.prices") > CIE<- tq_get("CIE.MC",from = '2019-12-31',get = "stock.prices") > MasMovil<- tq_get("MAS.MC",from = '2019-12-31',get = "stock.prices") > Almirall<- tq_get("ALM.MC",from = '2019-12-31',get = "stock.prices") > Indra<- tq_get("IDR.MC",from ='2019-12-31',get = "stock.prices") > > Indra_daily_returns <- Indra %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Indra_cum_returns <- Indra_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Almirall_daily_returns <- Almirall %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Almirall_cum_returns <- Almirall_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Acciona_daily_returns <- Acciona %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Acciona_cum_returns <- Acciona_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > ACS_daily_returns <- ACS %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > ACS_cum_returns <- ACS_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Aena_daily_returns <- Aena %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Aena_cum_returns <- Aena_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Amadeus_daily_returns <- Amadeus %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Amadeus_cum_returns <- Amadeus_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > ArcelorMittal_daily_returns <- ArcelorMittal %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > ArcelorMittal_cum_returns <- ArcelorMittal_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > BBVA_daily_returns <- BBVA %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > BBVA_cum_returns <- BBVA_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > > Sabadell_daily_returns <- Sabadell %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Sabadell_cum_returns <- Sabadell_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Santander_daily_returns <- Santander %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Santander_cum_returns <- Santander_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > > Bankinter_daily_returns <- Bankinter %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Bankinter_cum_returns <- Bankinter_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > CaixaBank_daily_returns <- CaixaBank %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > CaixaBank_cum_returns <- CaixaBank_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Cellnex_daily_returns <- Cellnex %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Cellnex_cum_returns <- Cellnex_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > CIE_daily_returns <- CIE %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > CIE_cum_returns <- CIE_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > CIE_daily_returns <- CIE %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > CIE_cum_returns <- CIE_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Enagas_daily_returns <- Enagas %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Enagas_cum_returns <- Enagas_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > > ENCE_daily_returns <- ENCE %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > ENCE_cum_returns <- ENCE_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > Endesa_daily_returns <- Endesa %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Endesa_cum_returns <- Endesa_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > Ferrovial_daily_returns <- Ferrovial %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Ferrovial_cum_returns <- Ferrovial_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Grifols_daily_returns <- Grifols %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Grifols_cum_returns <- Grifols_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Iberdrola_daily_returns <- Iberdrola %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Iberdrola_cum_returns <- Iberdrola_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Inditex_daily_returns <- Inditex %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Inditex_cum_returns <- Inditex_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Colonial_daily_returns <- Colonial %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Colonial_cum_returns <- Colonial_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > IAG_daily_returns <- IAG %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > IAG_cum_returns <- IAG_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Mapfre_daily_returns <- Mapfre %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Mapfre_cum_returns <- Mapfre_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Melia_daily_returns <- Melia %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Melia_cum_returns <- Melia_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Merlin_daily_returns <- Merlin %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Merlin_cum_returns <- Merlin_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Naturgy_daily_returns <- Naturgy %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Naturgy_cum_returns <- Naturgy_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > REE_daily_returns <- REE %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > REE_cum_returns <- REE_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Repsol_daily_returns <- Repsol %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Repsol_cum_returns <- Repsol_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > SGamesa_daily_returns <- SGamesa %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > SGamesa_cum_returns <- SGamesa_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Telefonica_daily_returns <- Telefonica %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Telefonica_cum_returns <- Telefonica_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Viscofan_daily_returns <- Viscofan %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Viscofan_cum_returns <- Viscofan_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Acerinox_daily_returns <- Acerinox %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Acerinox_cum_returns <- Acerinox_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > > Bankia_daily_returns <- Bankia %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Bankia_cum_returns <- Bankia_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > MasMovil_daily_returns <- MasMovil %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > MasMovil_cum_returns <- MasMovil_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > Indra_daily_returns <- Indra %>% > tq_transmute(select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates Daily > returns > col_rename = "idr_returns") # renames the column > Indra_cum_returns <- Indra_daily_returns %>% > mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > function > mutate(cumulative_returns = cr - 1) > > bestworst<-c(Acciona_cum_returns[nrow(Acciona_cum_returns),4], > ACS_cum_returns[nrow(ACS_cum_returns),4], > Aena_cum_returns[nrow(Aena_cum_returns),4], > Amadeus_cum_returns[nrow(Amadeus_cum_returns),4], > ArcelorMittal_cum_returns[nrow(ArcelorMittal_cum_returns),4], > BBVA_cum_returns[nrow(BBVA_cum_returns),4], > Sabadell_cum_returns[nrow(Sabadell_cum_returns),4], > Santander_cum_returns[nrow(Santander_cum_returns),4], > Bankinter_cum_returns[nrow(Bankinter_cum_returns),4], > CaixaBank_cum_returns[nrow(CaixaBank_cum_returns),4], > Cellnex_cum_returns[nrow(Cellnex_cum_returns),4], > Enagas_cum_returns[nrow(Enagas_cum_returns),4], > ENCE_cum_returns[nrow(ENCE_cum_returns),4], > Endesa_cum_returns[nrow(Endesa_cum_returns),4], > Ferrovial_cum_returns[nrow(Ferrovial_cum_returns),4], > Grifols_cum_returns[nrow(Grifols_cum_returns),4], > Iberdrola_cum_returns[nrow(Iberdrola_cum_returns),4], > Inditex_cum_returns[nrow(Inditex_cum_returns),4], > Colonial_cum_returns[nrow(Colonial_cum_returns),4], > IAG_cum_returns[nrow(IAG_cum_returns),4], > Mapfre_cum_returns[nrow(Mapfre_cum_returns),4], > Melia_cum_returns[nrow(Melia_cum_returns),4], > Merlin_cum_returns[nrow(Merlin_cum_returns),4], > Naturgy_cum_returns[nrow(Naturgy_cum_returns),4], > REE_cum_returns[nrow(REE_cum_returns),4], > Repsol_cum_returns[nrow(Repsol_cum_returns),4], > SGamesa_cum_returns[nrow(SGamesa_cum_returns),4], > Telefonica_cum_returns[nrow(Telefonica_cum_returns),4], > Viscofan_cum_returns[nrow(Viscofan_cum_returns),4], > Acerinox_cum_returns[nrow(Acerinox_cum_returns),4], > Bankia_cum_returns[nrow(Bankia_cum_returns),4], > CIE_cum_returns[nrow(CIE_cum_returns),4], > MasMovil_cum_returns[nrow(MasMovil_cum_returns),4], > Almirall_cum_returns[nrow(Almirall_cum_returns),4], > Indra_cum_returns[nrow(Indra_cum_returns),4]) > > namebw<-c("Acciona", > "ACS", > "Aena", > "Amadeus", > "ArcelorMittal", > "BBVA", > "Sabadell", > "Santander", > "Bankinter", > "CaixaBank", > "Cellnex", > "Enagas", > "ENCE", > "Endesa", > "Ferrovial", > "Grifols", > "Iberdrola", > "Inditex", > "Colonial", > "IAG", > "Mapfre", > "Melia", > "Merlin", > "Naturgy", > "REE", > "Repsol", > "SGamesa", > "Telefonica", > "Viscofan", > "Acerinox", > "Bankia", > "CIE", > "MasMovil", > "Almirall", > "Indra") > > > bwfinal <- matrix(bestworst, nrow =35 , ncol = 1) > bwfinal2 <- matrix(namebw, nrow =35 , ncol = 1) > > bwc<-cbind(bwfinal2,bwfinal) > colnames(bwc)=c("Accion","reval") > bwc <- as.data.frame(bwc) > colnames(bwchist)=c("Accion","reval") > bwchist <-as.data.frame(bwc[order(bwc$reval), ]) > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > -- Michael http://www.dewey.myzen.co.uk/home.html ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
Hello,
I second Michael's opinion. When the post's code is very long, there is a tendency to have less answers. Please post the output of dput(head(bwchist, 30)) It's much shorter code and it recreates the data so we will be able to see what's wrong and try to find a solution. Hope this helps, Rui Barradas Às 15:44 de 31/07/2020, Michael Dewey escreveu: > Dear Pedro > > Some comments in-line > > On 30/07/2020 21:16, Pedro páramo wrote: >> Hi all, >> >> I attach my code, the think is I want to make a bar plot the last >> variable >> called "bwchist" >> >> so the X axis are "Accion" and the y axis are "reval" values. >> >> I have prove class(bwchist) and says dataframe but its still a list >> because >> it says me >> >> I have prove to unlist, but it doesnt work >> >> hist(bwchist) >> Error in hist.default(bwchist) : 'x' must be numeric > > So bwchist is not a numeric variable as hist needs. Aboce you said it > is a data frame but data frames are not numeric. > > For future reference your example is way too long for anyone to go > through and try to help you. Try next time to reduce it to the > absolute minimum by removing sections while you still get the error. > It is also easier to get help if you can remove unnecessary packages. > > It is also unreadable because you are posting in HTML and that makes > the post unreadable as this is a plain text list. > > Michael > > >> >> Or >> >> barplot(bwchist) >> Error in barplot.default(bwchist) : 'height' must be a vector or a >> matrix >> >> library(PerformanceAnalytics) >> library(dplyr) >> library(tibble) >> library(lubridate) >> library(PerformanceAnalytics) >> library(quantmod) >> library(ggplot2) >> library(png) >> library(grid) >> library(RCurl) >> library(tidyquant) >> library(timetk) >> library(data.table) >> >> >> >> Acciona<- tq_get("ANA.MC",from = '2019-12-31',get = "stock.prices") >> ACS<- tq_get("ACS.MC",from = '2019-12-31',get = "stock.prices") >> Aena<- tq_get("AENA.MC",from = '2019-12-31',get = "stock.prices") >> Amadeus<- tq_get("AMS.MC",from = '2019-12-31',get = "stock.prices") >> ArcelorMittal<- tq_get("MTS.MC",from = '2019-12-31',get = >> "stock.prices") >> BBVA<- tq_get("BBVA.MC",from = '2019-12-31',get = "stock.prices") >> Sabadell<- tq_get("SAB.MC",from = '2019-12-31',get = "stock.prices") >> Santander<- tq_get("SAN.MC",from = '2019-12-31',get = "stock.prices") >> Bankinter<- tq_get("BKT.MC",from = '2019-12-31',get = "stock.prices") >> CaixaBank<- tq_get("CABK.MC",from = '2019-12-31',get = "stock.prices") >> Cellnex<- tq_get("CLNX.MC",from = '2019-12-31',get = "stock.prices") >> Enagas<- tq_get("ENG.MC",from = '2019-12-31',get = "stock.prices") >> ENCE<- tq_get("ENC.MC",from = '2019-12-31',get = "stock.prices") >> Endesa<- tq_get("ELE.MC",from = '2019-12-31',get = "stock.prices") >> Ferrovial<- tq_get("FER.MC",from = '2019-12-31',get = "stock.prices") >> Grifols<- tq_get("GRF.MC",from = '2019-12-31',get = "stock.prices") >> Iberdrola<- tq_get("IBE.MC",from = '2019-12-31',get = "stock.prices") >> Inditex<- tq_get("ITX.MC",from = '2019-12-31',get = "stock.prices") >> Colonial<- tq_get("COL.MC",from = '2019-12-31',get = "stock.prices") >> IAG<- tq_get("IAG.MC",from = '2019-12-31',get = "stock.prices") >> Mapfre<- tq_get("MAP.MC",from = '2019-12-31',get = "stock.prices") >> Melia<- tq_get("MEL.MC",from = '2019-12-31',get = "stock.prices") >> Merlin<- tq_get("MRL.MC",from = '2019-12-31',get = "stock.prices") >> Naturgy<- tq_get("NTGY.MC",from = '2019-12-31',get = "stock.prices") >> REE<- tq_get("REE.MC",from = '2019-12-31',get = "stock.prices") >> Repsol<- tq_get("REP.MC",from = '2019-12-31',get = "stock.prices") >> SGamesa<- tq_get("SGRE.MC",from = '2019-12-31',get = "stock.prices") >> Telefonica<- tq_get("TEF.MC",from = '2019-12-31',get = "stock.prices") >> Viscofan<- tq_get("VIS.MC",from = '2019-12-31',get = "stock.prices") >> Acerinox<- tq_get("ACX.MC",from = '2019-12-31',get = "stock.prices") >> Bankia<- tq_get("BKIA.MC",from = '2019-12-31',get = "stock.prices") >> CIE<- tq_get("CIE.MC",from = '2019-12-31',get = "stock.prices") >> MasMovil<- tq_get("MAS.MC",from = '2019-12-31',get = "stock.prices") >> Almirall<- tq_get("ALM.MC",from = '2019-12-31',get = "stock.prices") >> Indra<- tq_get("IDR.MC",from ='2019-12-31',get = "stock.prices") >> >> Indra_daily_returns <- Indra %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Indra_cum_returns <- Indra_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Almirall_daily_returns <- Almirall %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Almirall_cum_returns <- Almirall_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Acciona_daily_returns <- Acciona %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Acciona_cum_returns <- Acciona_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> ACS_daily_returns <- ACS %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> ACS_cum_returns <- ACS_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Aena_daily_returns <- Aena %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Aena_cum_returns <- Aena_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Amadeus_daily_returns <- Amadeus %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Amadeus_cum_returns <- Amadeus_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> ArcelorMittal_daily_returns <- ArcelorMittal %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> ArcelorMittal_cum_returns <- ArcelorMittal_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> BBVA_daily_returns <- BBVA %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> BBVA_cum_returns <- BBVA_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> >> Sabadell_daily_returns <- Sabadell %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Sabadell_cum_returns <- Sabadell_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Santander_daily_returns <- Santander %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Santander_cum_returns <- Santander_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> >> Bankinter_daily_returns <- Bankinter %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Bankinter_cum_returns <- Bankinter_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> CaixaBank_daily_returns <- CaixaBank %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> CaixaBank_cum_returns <- CaixaBank_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Cellnex_daily_returns <- Cellnex %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Cellnex_cum_returns <- Cellnex_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> CIE_daily_returns <- CIE %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> CIE_cum_returns <- CIE_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> CIE_daily_returns <- CIE %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> CIE_cum_returns <- CIE_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Enagas_daily_returns <- Enagas %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Enagas_cum_returns <- Enagas_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> >> ENCE_daily_returns <- ENCE %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> ENCE_cum_returns <- ENCE_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> Endesa_daily_returns <- Endesa %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Endesa_cum_returns <- Endesa_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> Ferrovial_daily_returns <- Ferrovial %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Ferrovial_cum_returns <- Ferrovial_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Grifols_daily_returns <- Grifols %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Grifols_cum_returns <- Grifols_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Iberdrola_daily_returns <- Iberdrola %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Iberdrola_cum_returns <- Iberdrola_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Inditex_daily_returns <- Inditex %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Inditex_cum_returns <- Inditex_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Colonial_daily_returns <- Colonial %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Colonial_cum_returns <- Colonial_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> IAG_daily_returns <- IAG %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> IAG_cum_returns <- IAG_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Mapfre_daily_returns <- Mapfre %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Mapfre_cum_returns <- Mapfre_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Melia_daily_returns <- Melia %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Melia_cum_returns <- Melia_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Merlin_daily_returns <- Merlin %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Merlin_cum_returns <- Merlin_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Naturgy_daily_returns <- Naturgy %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Naturgy_cum_returns <- Naturgy_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> REE_daily_returns <- REE %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> REE_cum_returns <- REE_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Repsol_daily_returns <- Repsol %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Repsol_cum_returns <- Repsol_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> SGamesa_daily_returns <- SGamesa %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> SGamesa_cum_returns <- SGamesa_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Telefonica_daily_returns <- Telefonica %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Telefonica_cum_returns <- Telefonica_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Viscofan_daily_returns <- Viscofan %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Viscofan_cum_returns <- Viscofan_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Acerinox_daily_returns <- Acerinox %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Acerinox_cum_returns <- Acerinox_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> >> Bankia_daily_returns <- Bankia %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Bankia_cum_returns <- Bankia_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> MasMovil_daily_returns <- MasMovil %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> MasMovil_cum_returns <- MasMovil_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> Indra_daily_returns <- Indra %>% >> tq_transmute(select = adjusted, # this specifies which >> column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates Daily >> returns >> col_rename = "idr_returns") # renames the column >> Indra_cum_returns <- Indra_daily_returns %>% >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod >> function >> mutate(cumulative_returns = cr - 1) >> >> bestworst<-c(Acciona_cum_returns[nrow(Acciona_cum_returns),4], >> ACS_cum_returns[nrow(ACS_cum_returns),4], >> Aena_cum_returns[nrow(Aena_cum_returns),4], >> Amadeus_cum_returns[nrow(Amadeus_cum_returns),4], >> ArcelorMittal_cum_returns[nrow(ArcelorMittal_cum_returns),4], >> BBVA_cum_returns[nrow(BBVA_cum_returns),4], >> Sabadell_cum_returns[nrow(Sabadell_cum_returns),4], >> Santander_cum_returns[nrow(Santander_cum_returns),4], >> Bankinter_cum_returns[nrow(Bankinter_cum_returns),4], >> CaixaBank_cum_returns[nrow(CaixaBank_cum_returns),4], >> Cellnex_cum_returns[nrow(Cellnex_cum_returns),4], >> Enagas_cum_returns[nrow(Enagas_cum_returns),4], >> ENCE_cum_returns[nrow(ENCE_cum_returns),4], >> Endesa_cum_returns[nrow(Endesa_cum_returns),4], >> Ferrovial_cum_returns[nrow(Ferrovial_cum_returns),4], >> Grifols_cum_returns[nrow(Grifols_cum_returns),4], >> Iberdrola_cum_returns[nrow(Iberdrola_cum_returns),4], >> Inditex_cum_returns[nrow(Inditex_cum_returns),4], >> Colonial_cum_returns[nrow(Colonial_cum_returns),4], >> IAG_cum_returns[nrow(IAG_cum_returns),4], >> Mapfre_cum_returns[nrow(Mapfre_cum_returns),4], >> Melia_cum_returns[nrow(Melia_cum_returns),4], >> Merlin_cum_returns[nrow(Merlin_cum_returns),4], >> Naturgy_cum_returns[nrow(Naturgy_cum_returns),4], >> REE_cum_returns[nrow(REE_cum_returns),4], >> Repsol_cum_returns[nrow(Repsol_cum_returns),4], >> SGamesa_cum_returns[nrow(SGamesa_cum_returns),4], >> Telefonica_cum_returns[nrow(Telefonica_cum_returns),4], >> Viscofan_cum_returns[nrow(Viscofan_cum_returns),4], >> Acerinox_cum_returns[nrow(Acerinox_cum_returns),4], >> Bankia_cum_returns[nrow(Bankia_cum_returns),4], >> CIE_cum_returns[nrow(CIE_cum_returns),4], >> MasMovil_cum_returns[nrow(MasMovil_cum_returns),4], >> Almirall_cum_returns[nrow(Almirall_cum_returns),4], >> Indra_cum_returns[nrow(Indra_cum_returns),4]) >> >> namebw<-c("Acciona", >> "ACS", >> "Aena", >> "Amadeus", >> "ArcelorMittal", >> "BBVA", >> "Sabadell", >> "Santander", >> "Bankinter", >> "CaixaBank", >> "Cellnex", >> "Enagas", >> "ENCE", >> "Endesa", >> "Ferrovial", >> "Grifols", >> "Iberdrola", >> "Inditex", >> "Colonial", >> "IAG", >> "Mapfre", >> "Melia", >> "Merlin", >> "Naturgy", >> "REE", >> "Repsol", >> "SGamesa", >> "Telefonica", >> "Viscofan", >> "Acerinox", >> "Bankia", >> "CIE", >> "MasMovil", >> "Almirall", >> "Indra") >> >> >> bwfinal <- matrix(bestworst, nrow =35 , ncol = 1) >> bwfinal2 <- matrix(namebw, nrow =35 , ncol = 1) >> >> bwc<-cbind(bwfinal2,bwfinal) >> colnames(bwc)=c("Accion","reval") >> bwc <- as.data.frame(bwc) >> colnames(bwchist)=c("Accion","reval") >> bwchist <-as.data.frame(bwc[order(bwc$reval), ]) >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> [hidden email] mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > -- Este e-mail foi verificado em termos de vírus pelo software antivírus Avast. https://www.avast.com/antivirus ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
Hi Pedro,
Another 'best practice' and polite thing to do is link to other places you may have cross-posted. That will give people the opportunity to see if your questions has been answered in another forum. I saw your post on R-SIG-Finance (https://stat.ethz.ch/pipermail/r-sig-finance/2020q3/014979.html), and started to work on a solution. I don't know how to do this in tidyquant, but here's how you can do it with quantmod: # all tickers tk <- c("ANA.MC", "ACS.MC", "AENA.MC", "AMS.MC", "MTS.MC", "BBVA.MC", "SAB.MC", "SAN.MC", "BKT.MC", "CABK.MC", "CLNX.MC", "ENG.MC", "ENC.MC", "ELE.MC", "FER.MC", "GRF.MC", "IBE.MC", "ITX.MC", "COL.MC", "IAG.MC", "MAP.MC", "MEL.MC", "MRL.MC", "NTGY.MC", "REE.MC", "REP.MC", "SGRE.MC", "TEF.MC", "VIS.MC", "ACX.MC", "BKIA.MC", "CIE.MC", "MAS.MC", "ALM.MC", "IDR.MC") # download them into an environment ('e') require(quantmod) getSymbols(tk, from = "2019-12-31", env = (e <- new.env())) # extract adjusted close column adj <- lapply(e, Ad) # calculate daily returns from adjusted data, # merge into a xts matrix, and fill NA with 0 ret <- do.call(merge, c(lapply(adj, dailyReturn), fill = 0)) # cumulative returns cumret <- cumprod(1 + ret) - 1 # set names colnames(cumret) <- names(adj) last(cumret) # calculate histogram for period-to-date returns hist(drop(last(cumret))) I'm not sure that's the histogram you're looking for, but I hope it gives you a start toward a solution. Best, Josh On Fri, Jul 31, 2020 at 9:55 AM Rui Barradas <[hidden email]> wrote: > > Hello, > > I second Michael's opinion. When the post's code is very long, there is > a tendency to have less answers. > > Please post the output of > > > dput(head(bwchist, 30)) > > > It's much shorter code and it recreates the data so we will be able to > see what's wrong and try to find a solution. > > Hope this helps, > > Rui Barradas > > > Às 15:44 de 31/07/2020, Michael Dewey escreveu: > > Dear Pedro > > > > Some comments in-line > > > > On 30/07/2020 21:16, Pedro páramo wrote: > >> Hi all, > >> > >> I attach my code, the think is I want to make a bar plot the last > >> variable > >> called "bwchist" > >> > >> so the X axis are "Accion" and the y axis are "reval" values. > >> > >> I have prove class(bwchist) and says dataframe but its still a list > >> because > >> it says me > >> > >> I have prove to unlist, but it doesnt work > >> > >> hist(bwchist) > >> Error in hist.default(bwchist) : 'x' must be numeric > > > > So bwchist is not a numeric variable as hist needs. Aboce you said it > > is a data frame but data frames are not numeric. > > > > For future reference your example is way too long for anyone to go > > through and try to help you. Try next time to reduce it to the > > absolute minimum by removing sections while you still get the error. > > It is also easier to get help if you can remove unnecessary packages. > > > > It is also unreadable because you are posting in HTML and that makes > > the post unreadable as this is a plain text list. > > > > Michael > > > > > >> > >> Or > >> > >> barplot(bwchist) > >> Error in barplot.default(bwchist) : 'height' must be a vector or a > >> matrix > >> > >> library(PerformanceAnalytics) > >> library(dplyr) > >> library(tibble) > >> library(lubridate) > >> library(PerformanceAnalytics) > >> library(quantmod) > >> library(ggplot2) > >> library(png) > >> library(grid) > >> library(RCurl) > >> library(tidyquant) > >> library(timetk) > >> library(data.table) > >> > >> > >> > >> Acciona<- tq_get("ANA.MC",from = '2019-12-31',get = "stock.prices") > >> ACS<- tq_get("ACS.MC",from = '2019-12-31',get = "stock.prices") > >> Aena<- tq_get("AENA.MC",from = '2019-12-31',get = "stock.prices") > >> Amadeus<- tq_get("AMS.MC",from = '2019-12-31',get = "stock.prices") > >> ArcelorMittal<- tq_get("MTS.MC",from = '2019-12-31',get = > >> "stock.prices") > >> BBVA<- tq_get("BBVA.MC",from = '2019-12-31',get = "stock.prices") > >> Sabadell<- tq_get("SAB.MC",from = '2019-12-31',get = "stock.prices") > >> Santander<- tq_get("SAN.MC",from = '2019-12-31',get = "stock.prices") > >> Bankinter<- tq_get("BKT.MC",from = '2019-12-31',get = "stock.prices") > >> CaixaBank<- tq_get("CABK.MC",from = '2019-12-31',get = "stock.prices") > >> Cellnex<- tq_get("CLNX.MC",from = '2019-12-31',get = "stock.prices") > >> Enagas<- tq_get("ENG.MC",from = '2019-12-31',get = "stock.prices") > >> ENCE<- tq_get("ENC.MC",from = '2019-12-31',get = "stock.prices") > >> Endesa<- tq_get("ELE.MC",from = '2019-12-31',get = "stock.prices") > >> Ferrovial<- tq_get("FER.MC",from = '2019-12-31',get = "stock.prices") > >> Grifols<- tq_get("GRF.MC",from = '2019-12-31',get = "stock.prices") > >> Iberdrola<- tq_get("IBE.MC",from = '2019-12-31',get = "stock.prices") > >> Inditex<- tq_get("ITX.MC",from = '2019-12-31',get = "stock.prices") > >> Colonial<- tq_get("COL.MC",from = '2019-12-31',get = "stock.prices") > >> IAG<- tq_get("IAG.MC",from = '2019-12-31',get = "stock.prices") > >> Mapfre<- tq_get("MAP.MC",from = '2019-12-31',get = "stock.prices") > >> Melia<- tq_get("MEL.MC",from = '2019-12-31',get = "stock.prices") > >> Merlin<- tq_get("MRL.MC",from = '2019-12-31',get = "stock.prices") > >> Naturgy<- tq_get("NTGY.MC",from = '2019-12-31',get = "stock.prices") > >> REE<- tq_get("REE.MC",from = '2019-12-31',get = "stock.prices") > >> Repsol<- tq_get("REP.MC",from = '2019-12-31',get = "stock.prices") > >> SGamesa<- tq_get("SGRE.MC",from = '2019-12-31',get = "stock.prices") > >> Telefonica<- tq_get("TEF.MC",from = '2019-12-31',get = "stock.prices") > >> Viscofan<- tq_get("VIS.MC",from = '2019-12-31',get = "stock.prices") > >> Acerinox<- tq_get("ACX.MC",from = '2019-12-31',get = "stock.prices") > >> Bankia<- tq_get("BKIA.MC",from = '2019-12-31',get = "stock.prices") > >> CIE<- tq_get("CIE.MC",from = '2019-12-31',get = "stock.prices") > >> MasMovil<- tq_get("MAS.MC",from = '2019-12-31',get = "stock.prices") > >> Almirall<- tq_get("ALM.MC",from = '2019-12-31',get = "stock.prices") > >> Indra<- tq_get("IDR.MC",from ='2019-12-31',get = "stock.prices") > >> > >> Indra_daily_returns <- Indra %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Indra_cum_returns <- Indra_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Almirall_daily_returns <- Almirall %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Almirall_cum_returns <- Almirall_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Acciona_daily_returns <- Acciona %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Acciona_cum_returns <- Acciona_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> ACS_daily_returns <- ACS %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> ACS_cum_returns <- ACS_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Aena_daily_returns <- Aena %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Aena_cum_returns <- Aena_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Amadeus_daily_returns <- Amadeus %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Amadeus_cum_returns <- Amadeus_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> ArcelorMittal_daily_returns <- ArcelorMittal %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> ArcelorMittal_cum_returns <- ArcelorMittal_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> BBVA_daily_returns <- BBVA %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> BBVA_cum_returns <- BBVA_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> > >> Sabadell_daily_returns <- Sabadell %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Sabadell_cum_returns <- Sabadell_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Santander_daily_returns <- Santander %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Santander_cum_returns <- Santander_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> > >> Bankinter_daily_returns <- Bankinter %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Bankinter_cum_returns <- Bankinter_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> CaixaBank_daily_returns <- CaixaBank %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> CaixaBank_cum_returns <- CaixaBank_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Cellnex_daily_returns <- Cellnex %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Cellnex_cum_returns <- Cellnex_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> CIE_daily_returns <- CIE %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> CIE_cum_returns <- CIE_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> CIE_daily_returns <- CIE %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> CIE_cum_returns <- CIE_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Enagas_daily_returns <- Enagas %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Enagas_cum_returns <- Enagas_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> > >> ENCE_daily_returns <- ENCE %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> ENCE_cum_returns <- ENCE_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> Endesa_daily_returns <- Endesa %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Endesa_cum_returns <- Endesa_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> Ferrovial_daily_returns <- Ferrovial %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Ferrovial_cum_returns <- Ferrovial_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Grifols_daily_returns <- Grifols %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Grifols_cum_returns <- Grifols_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Iberdrola_daily_returns <- Iberdrola %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Iberdrola_cum_returns <- Iberdrola_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Inditex_daily_returns <- Inditex %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Inditex_cum_returns <- Inditex_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Colonial_daily_returns <- Colonial %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Colonial_cum_returns <- Colonial_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> IAG_daily_returns <- IAG %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> IAG_cum_returns <- IAG_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Mapfre_daily_returns <- Mapfre %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Mapfre_cum_returns <- Mapfre_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Melia_daily_returns <- Melia %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Melia_cum_returns <- Melia_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Merlin_daily_returns <- Merlin %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Merlin_cum_returns <- Merlin_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Naturgy_daily_returns <- Naturgy %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Naturgy_cum_returns <- Naturgy_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> REE_daily_returns <- REE %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> REE_cum_returns <- REE_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Repsol_daily_returns <- Repsol %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Repsol_cum_returns <- Repsol_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> SGamesa_daily_returns <- SGamesa %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> SGamesa_cum_returns <- SGamesa_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Telefonica_daily_returns <- Telefonica %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Telefonica_cum_returns <- Telefonica_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Viscofan_daily_returns <- Viscofan %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Viscofan_cum_returns <- Viscofan_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Acerinox_daily_returns <- Acerinox %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Acerinox_cum_returns <- Acerinox_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> > >> Bankia_daily_returns <- Bankia %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Bankia_cum_returns <- Bankia_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> MasMovil_daily_returns <- MasMovil %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> MasMovil_cum_returns <- MasMovil_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> Indra_daily_returns <- Indra %>% > >> tq_transmute(select = adjusted, # this specifies which > >> column > >> to select > >> mutate_fun = periodReturn, # This specifies what to do > >> with that column > >> period = "daily", # This argument calculates Daily > >> returns > >> col_rename = "idr_returns") # renames the column > >> Indra_cum_returns <- Indra_daily_returns %>% > >> mutate(cr = cumprod(1 + idr_returns)) %>% # using the cumprod > >> function > >> mutate(cumulative_returns = cr - 1) > >> > >> bestworst<-c(Acciona_cum_returns[nrow(Acciona_cum_returns),4], > >> ACS_cum_returns[nrow(ACS_cum_returns),4], > >> Aena_cum_returns[nrow(Aena_cum_returns),4], > >> Amadeus_cum_returns[nrow(Amadeus_cum_returns),4], > >> ArcelorMittal_cum_returns[nrow(ArcelorMittal_cum_returns),4], > >> BBVA_cum_returns[nrow(BBVA_cum_returns),4], > >> Sabadell_cum_returns[nrow(Sabadell_cum_returns),4], > >> Santander_cum_returns[nrow(Santander_cum_returns),4], > >> Bankinter_cum_returns[nrow(Bankinter_cum_returns),4], > >> CaixaBank_cum_returns[nrow(CaixaBank_cum_returns),4], > >> Cellnex_cum_returns[nrow(Cellnex_cum_returns),4], > >> Enagas_cum_returns[nrow(Enagas_cum_returns),4], > >> ENCE_cum_returns[nrow(ENCE_cum_returns),4], > >> Endesa_cum_returns[nrow(Endesa_cum_returns),4], > >> Ferrovial_cum_returns[nrow(Ferrovial_cum_returns),4], > >> Grifols_cum_returns[nrow(Grifols_cum_returns),4], > >> Iberdrola_cum_returns[nrow(Iberdrola_cum_returns),4], > >> Inditex_cum_returns[nrow(Inditex_cum_returns),4], > >> Colonial_cum_returns[nrow(Colonial_cum_returns),4], > >> IAG_cum_returns[nrow(IAG_cum_returns),4], > >> Mapfre_cum_returns[nrow(Mapfre_cum_returns),4], > >> Melia_cum_returns[nrow(Melia_cum_returns),4], > >> Merlin_cum_returns[nrow(Merlin_cum_returns),4], > >> Naturgy_cum_returns[nrow(Naturgy_cum_returns),4], > >> REE_cum_returns[nrow(REE_cum_returns),4], > >> Repsol_cum_returns[nrow(Repsol_cum_returns),4], > >> SGamesa_cum_returns[nrow(SGamesa_cum_returns),4], > >> Telefonica_cum_returns[nrow(Telefonica_cum_returns),4], > >> Viscofan_cum_returns[nrow(Viscofan_cum_returns),4], > >> Acerinox_cum_returns[nrow(Acerinox_cum_returns),4], > >> Bankia_cum_returns[nrow(Bankia_cum_returns),4], > >> CIE_cum_returns[nrow(CIE_cum_returns),4], > >> MasMovil_cum_returns[nrow(MasMovil_cum_returns),4], > >> Almirall_cum_returns[nrow(Almirall_cum_returns),4], > >> Indra_cum_returns[nrow(Indra_cum_returns),4]) > >> > >> namebw<-c("Acciona", > >> "ACS", > >> "Aena", > >> "Amadeus", > >> "ArcelorMittal", > >> "BBVA", > >> "Sabadell", > >> "Santander", > >> "Bankinter", > >> "CaixaBank", > >> "Cellnex", > >> "Enagas", > >> "ENCE", > >> "Endesa", > >> "Ferrovial", > >> "Grifols", > >> "Iberdrola", > >> "Inditex", > >> "Colonial", > >> "IAG", > >> "Mapfre", > >> "Melia", > >> "Merlin", > >> "Naturgy", > >> "REE", > >> "Repsol", > >> "SGamesa", > >> "Telefonica", > >> "Viscofan", > >> "Acerinox", > >> "Bankia", > >> "CIE", > >> "MasMovil", > >> "Almirall", > >> "Indra") > >> > >> > >> bwfinal <- matrix(bestworst, nrow =35 , ncol = 1) > >> bwfinal2 <- matrix(namebw, nrow =35 , ncol = 1) > >> > >> bwc<-cbind(bwfinal2,bwfinal) > >> colnames(bwc)=c("Accion","reval") > >> bwc <- as.data.frame(bwc) > >> colnames(bwchist)=c("Accion","reval") > >> bwchist <-as.data.frame(bwc[order(bwc$reval), ]) > >> > >> [[alternative HTML version deleted]] > >> > >> ______________________________________________ > >> [hidden email] mailing list -- To UNSUBSCRIBE and more, see > >> https://stat.ethz.ch/mailman/listinfo/r-help > >> PLEASE do read the posting guide > >> http://www.R-project.org/posting-guide.html > >> and provide commented, minimal, self-contained, reproducible code. > >> > > > > > -- > Este e-mail foi verificado em termos de vírus pelo software antivírus Avast. > https://www.avast.com/antivirus > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
On 2020-07-31 10:07 -0500, Joshua Ulrich wrote:
| On Fri, Jul 31, 2020 at 9:55 AM Rui Barradas wrote: | | Às 15:44 de 31/07/2020, Michael Dewey escreveu: | | | Dear Pedro | | | | | | Some comments in-line | | | | | | On 30/07/2020 21:16, Pedro páramo wrote: | | | | Hi all, | | | | | | | | I attach my code, the think is I | | | | want to make a bar plot the last | | | | variable called "bwchist" so the | | | | X axis are "Accion" and the y | | | | axis are "reval" values. | | | | | | | | I have prove class(bwchist) and | | | | says dataframe but its still a | | | | list because it says me I have | | | | prove to unlist, but it doesnt | | | | work | | | | | | | | hist(bwchist) | | | | Error in hist.default(bwchist) : 'x' must be numeric | | | | | | So bwchist is not a numeric | | | variable as hist needs. Aboce you | | | said it is a data frame but data | | | frames are not numeric. | | | | | | For future reference your example | | | is way too long for anyone to go | | | through and try to help you. Try | | | next time to reduce it to the | | | absolute minimum by removing | | | sections while you still get the | | | error. It is also easier to get | | | help if you can remove unnecessary | | | packages. | | | | | | It is also unreadable because you | | | are posting in HTML and that makes | | | the post unreadable as this is a | | | plain text list. | | | | Hello, | | | | I second Michael's opinion. When the | | post's code is very long, there is a | | tendency to have less answers. | | | | Please post the output of | | | | dput(head(bwchist, 30)) | | | | It's much shorter code and it | | recreates the data so we will be | | able to see what's wrong and try to | | find a solution. | | Hi Pedro, | | Another 'best practice' and polite | thing to do is link to other places | you may have cross-posted. That will | give people the opportunity to see if | your questions has been answered in | another forum. | | I saw your post on R-SIG-Finance | (https://stat.ethz.ch/pipermail/r-sig-finance/2020q3/014979.html), | and started to work on a solution. | | I don't know how to do this in | tidyquant, but here's how you can do | it with quantmod: | | # all tickers | tk <- c("ANA.MC", "ACS.MC", "AENA.MC", "AMS.MC", "MTS.MC", "BBVA.MC", "SAB.MC", | "SAN.MC", "BKT.MC", "CABK.MC", "CLNX.MC", "ENG.MC", "ENC.MC", "ELE.MC", | "FER.MC", "GRF.MC", "IBE.MC", "ITX.MC", "COL.MC", "IAG.MC", "MAP.MC", | "MEL.MC", "MRL.MC", "NTGY.MC", "REE.MC", "REP.MC", "SGRE.MC", "TEF.MC", | "VIS.MC", "ACX.MC", "BKIA.MC", "CIE.MC", "MAS.MC", "ALM.MC", "IDR.MC") | | # download them into an environment ('e') | require(quantmod) | getSymbols(tk, from = "2019-12-31", env = (e <- new.env())) | | # extract adjusted close column | adj <- lapply(e, Ad) | # calculate daily returns from adjusted data, | # merge into a xts matrix, and fill NA with 0 | ret <- do.call(merge, c(lapply(adj, dailyReturn), fill = 0)) | # cumulative returns | cumret <- cumprod(1 + ret) - 1 | # set names | colnames(cumret) <- names(adj) | last(cumret) | # calculate histogram for period-to-date returns | hist(drop(last(cumret))) | | I'm not sure that's the histogram | you're looking for, but I hope it | gives you a start toward a solution. | | Best, | Josh Wow Josh! That's very elegant. Myself now, I just plowed through the original code to make it simpler, but am at a loss as to how this histogram looks ... x <- c("ANA.MC", "ACS.MC", "AENA.MC", "AMS.MC", "MTS.MC", "BBVA.MC", "SAB.MC", "SAN.MC", "BKT.MC", "CABK.MC", "CLNX.MC", "ENG.MC", "ENC.MC", "ELE.MC", "FER.MC", "GRF.MC", "IBE.MC", "ITX.MC", "COL.MC", "IAG.MC", "MAP.MC", "MEL.MC", "MRL.MC", "NTGY.MC", "REE.MC", "REP.MC", "SGRE.MC", "TEF.MC", "VIS.MC", "ACX.MC", "BKIA.MC", "CIE.MC", "MAS.MC", "ALM.MC", "IDR.MC") stock.prices <- lapply(x, function(stock) { tidyquant::tq_get(x=stock,from = '2019-12-31',get = "stock.prices") }) names(stock.prices) <- x library(tidyquant) returns <- lapply(stock.prices, function(data) { tab <- tq_transmute( data = data, select = adjusted, # this specifies which column to select mutate_fun = periodReturn, # This specifies what to do with that column period = "daily", # This argument calculates Daily returns col_rename = "idr_returns") # renames the column tab[,"cr"] <- cumprod(1 + tab[,"idr_returns"]) tab[,"cumulative_returns"] <- tab[,"cr"] - 1 dplyr::pull( tab[nrow(tab[,"cumulative_returns"]), "cumulative_returns"] ) }) bestworst <- simplify2array(returns) namebw <- c("Acciona", "ACS", "Aena", "Amadeus", "ArcelorMittal", "BBVA", "Sabadell", "Santander", "Bankinter", "CaixaBank", "Cellnex", "Enagas", "ENCE", "Endesa", "Ferrovial", "Grifols", "Iberdrola", "Inditex", "Colonial", "IAG", "Mapfre", "Melia", "Merlin", "Naturgy", "REE", "Repsol", "SGamesa", "Telefonica", "Viscofan", "Acerinox", "Bankia", "CIE", "MasMovil", "Almirall", "Indra") bwc <- data.frame( symbol=names(bestworst), Accion=namebw, reval=bestworst) | | | | bwc<-cbind(bwfinal2,bwfinal) | | | | colnames(bwc)=c("Accion","reval") | | | | bwc <- as.data.frame(bwc) ... aaaand you know something's happening between here (where bwchist is created), but you don't know what it is, do you, Mr páramo? | | | | colnames(bwchist)=c("Accion","reval") | | | | bwchist <-as.data.frame(bwc[order(bwc$reval), ]) Best, Rasmus ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
Hi Rasmus, Josh and Rui,
First of all many thanks in advance about your help. The first thig is sometimes you say " you are posting in HTML and that makes the post unreadable as this is a plain text list" how can I put the code in the correct way, not html (attaching in txt?) The second about the code: I have used this: bwc <- cbind(bwfinal2,bwfinal) colnames(bwc)=c("Accion","reval") df <- matrix(unlist(bwc), nrow=nrow(bwc), byrow=F) colnames(bwchist)=c("Accion","reval") bwchist <-as.data.frame(bwc[order(df[,2]), ]) bwchist is the ordered cum stock returns in the year but because is a list it is not possible to plot and histogram with x (names of stocks) and the x axist the value of cum stocks (reval) when I put dput(bwchist) the console says: dput(bwchist) structure(list(Accion = list("REE", "Enagas", "Grifols", "Ferrovial", "Acerinox", "Naturgy", "Inditex", "Bankia", "ENCE", "Aena", "Bankinter", "Mapfre", "CaixaBank", "CIE", "Colonial", "Almirall", "Indra", "ArcelorMittal", "ACS", "Telefonica", "Amadeus", "BBVA", "Merlin", "Santander", "Repsol", "Melia", "Sabadell", "IAG", "Acciona", "Endesa", "MasMovil", "Iberdrola", "SGamesa", "Viscofan", "Cellnex"), reval = list(-0.0200827282700085, -0.0590294115600855, -0.214126598790964, -0.220773677809979, -0.229653300324357, -0.257944379583984, -0.283942789063822, -0.285159347392533, -0.303814713896458, -0.30734460425763, -0.309408155539818, -0.319912221435868, -0.322790949659181, -0.344047579452905, -0.347919538415482, -0.356898907103825, -0.374263261296661, -0.40147247119078, -0.405150043834815, -0.406022775042175, -0.413786100987797, -0.440679109311707, -0.442603156492871, -0.491634140733524, -0.499254932434042, -0.6, -0.709737357505148, -0.724461258850966, 0.0220528711420083, 0.0462767672643172, 0.115044247787611, 0.238734548714937, 0.274578114644054, 0.343422896082666, 0.387826126094928)), class = "data.frame", row.names = c(NA, -35L)) I try to make an hist or barplot but because it is a list no way to obtain the plot. Many thanks again for your help. I have printed two manuals to improve my level, but if you can help me, I would be very very gratefull. El vie., 31 jul. 2020 a las 18:28, Rasmus Liland (<[hidden email]>) escribió: > On 2020-07-31 10:07 -0500, Joshua Ulrich wrote: > | On Fri, Jul 31, 2020 at 9:55 AM Rui Barradas wrote: > | | Às 15:44 de 31/07/2020, Michael Dewey escreveu: > | | | Dear Pedro > | | | > | | | Some comments in-line > | | | > | | | On 30/07/2020 21:16, Pedro páramo wrote: > | | | | Hi all, > | | | | > | | | | I attach my code, the think is I > | | | | want to make a bar plot the last > | | | | variable called "bwchist" so the > | | | | X axis are "Accion" and the y > | | | | axis are "reval" values. > | | | | > | | | | I have prove class(bwchist) and > | | | | says dataframe but its still a > | | | | list because it says me I have > | | | | prove to unlist, but it doesnt > | | | | work > | | | | > | | | | hist(bwchist) > | | | | Error in hist.default(bwchist) : 'x' must be numeric > | | | > | | | So bwchist is not a numeric > | | | variable as hist needs. Aboce you > | | | said it is a data frame but data > | | | frames are not numeric. > | | | > | | | For future reference your example > | | | is way too long for anyone to go > | | | through and try to help you. Try > | | | next time to reduce it to the > | | | absolute minimum by removing > | | | sections while you still get the > | | | error. It is also easier to get > | | | help if you can remove unnecessary > | | | packages. > | | | > | | | It is also unreadable because you > | | | are posting in HTML and that makes > | | | the post unreadable as this is a > | | | plain text list. > | | > | | Hello, > | | > | | I second Michael's opinion. When the > | | post's code is very long, there is a > | | tendency to have less answers. > | | > | | Please post the output of > | | > | | dput(head(bwchist, 30)) > | | > | | It's much shorter code and it > | | recreates the data so we will be > | | able to see what's wrong and try to > | | find a solution. > | > | Hi Pedro, > | > | Another 'best practice' and polite > | thing to do is link to other places > | you may have cross-posted. That will > | give people the opportunity to see if > | your questions has been answered in > | another forum. > | > | I saw your post on R-SIG-Finance > | (https://stat.ethz.ch/pipermail/r-sig-finance/2020q3/014979.html), > | and started to work on a solution. > | > | I don't know how to do this in > | tidyquant, but here's how you can do > | it with quantmod: > | > | # all tickers > | tk <- c("ANA.MC", "ACS.MC", "AENA.MC", "AMS.MC", "MTS.MC", "BBVA.MC", " > SAB.MC", > | "SAN.MC", "BKT.MC", "CABK.MC", "CLNX.MC", "ENG.MC", "ENC.MC", "ELE.MC > ", > | "FER.MC", "GRF.MC", "IBE.MC", "ITX.MC", "COL.MC", "IAG.MC", "MAP.MC", > | "MEL.MC", "MRL.MC", "NTGY.MC", "REE.MC", "REP.MC", "SGRE.MC", "TEF.MC > ", > | "VIS.MC", "ACX.MC", "BKIA.MC", "CIE.MC", "MAS.MC", "ALM.MC", "IDR.MC") > | > | # download them into an environment ('e') > | require(quantmod) > | getSymbols(tk, from = "2019-12-31", env = (e <- new.env())) > | > | # extract adjusted close column > | adj <- lapply(e, Ad) > | # calculate daily returns from adjusted data, > | # merge into a xts matrix, and fill NA with 0 > | ret <- do.call(merge, c(lapply(adj, dailyReturn), fill = 0)) > | # cumulative returns > | cumret <- cumprod(1 + ret) - 1 > | # set names > | colnames(cumret) <- names(adj) > | last(cumret) > | # calculate histogram for period-to-date returns > | hist(drop(last(cumret))) > | > | I'm not sure that's the histogram > | you're looking for, but I hope it > | gives you a start toward a solution. > | > | Best, > | Josh > > Wow Josh! That's very elegant. > > Myself now, I just plowed through the > original code to make it simpler, but am > at a loss as to how this histogram looks > ... > > x <- c("ANA.MC", "ACS.MC", "AENA.MC", "AMS.MC", "MTS.MC", "BBVA.MC > ", > "SAB.MC", "SAN.MC", "BKT.MC", "CABK.MC", "CLNX.MC", "ENG.MC", > "ENC.MC", "ELE.MC", "FER.MC", "GRF.MC", "IBE.MC", "ITX.MC", > "COL.MC", "IAG.MC", "MAP.MC", "MEL.MC", "MRL.MC", "NTGY.MC", > "REE.MC", "REP.MC", "SGRE.MC", "TEF.MC", "VIS.MC", "ACX.MC", > "BKIA.MC", "CIE.MC", "MAS.MC", "ALM.MC", "IDR.MC") > stock.prices <- > lapply(x, function(stock) { > tidyquant::tq_get(x=stock,from = '2019-12-31',get = > "stock.prices") > }) > names(stock.prices) <- x > > library(tidyquant) > > returns <- lapply(stock.prices, function(data) { > tab <- > tq_transmute( > data = data, > select = adjusted, # this specifies which column > to select > mutate_fun = periodReturn, # This specifies what to do > with that column > period = "daily", # This argument calculates > Daily returns > col_rename = "idr_returns") # renames the column > tab[,"cr"] <- cumprod(1 + tab[,"idr_returns"]) > tab[,"cumulative_returns"] <- tab[,"cr"] - 1 > > dplyr::pull( > tab[nrow(tab[,"cumulative_returns"]), > "cumulative_returns"] > ) > }) > > bestworst <- simplify2array(returns) > > namebw <- > c("Acciona", "ACS", "Aena", "Amadeus", > "ArcelorMittal", "BBVA", "Sabadell", > "Santander", "Bankinter", > "CaixaBank", "Cellnex", "Enagas", > "ENCE", "Endesa", "Ferrovial", > "Grifols", "Iberdrola", "Inditex", > "Colonial", "IAG", "Mapfre", > "Melia", "Merlin", "Naturgy", "REE", > "Repsol", "SGamesa", "Telefonica", > "Viscofan", "Acerinox", "Bankia", > "CIE", "MasMovil", "Almirall", > "Indra") > > bwc <- data.frame( > symbol=names(bestworst), > Accion=namebw, > reval=bestworst) > > | | | | bwc<-cbind(bwfinal2,bwfinal) > | | | | colnames(bwc)=c("Accion","reval") > | | | | bwc <- as.data.frame(bwc) > > ... aaaand you know something's > happening between here (where bwchist is > created), but you don't know what it is, > do you, Mr páramo? > > | | | | colnames(bwchist)=c("Accion","reval") > | | | | bwchist <-as.data.frame(bwc[order(bwc$reval), ]) > > Best, > Rasmus > [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
Hello,
Thanks for the data in dput format. If you run str(bwchist) you will see that what you have is a data.frame, yes, but, with columns of class "list", not vectors. So the first step is to make them vectors, to unlist the lists. I will do it applying function unlist() to each of the columns. Since lapply returns a list, I remake a data.frame. The original is kept unchanged, the new object is bwch. bwch <- lapply(bwchist, unlist, recursive = FALSE) bwch <- do.call(cbind.data.frame, bwch) str(bwch) Now that everything is as it should, here are two ways of plotting bar graphs. #--- base R x11(width = 11.5, height = 6) old_par <- par(mar = par("mar") + c(1, 0, 0, 0)) bp <- barplot(bwch$reval, yaxt = "n", ylim = c(-1, 0.4)) axis(1, at = bp, labels = bwch$Accion, las = 2) axis(2, at = pretty(bwch$reval)) par(old_par) #--- package ggplot2 library(ggplot2) x11(width = 11.5, height = 6) ggplot(bwch, aes(factor(Accion, levels = Accion), reval)) + geom_col() + theme(axis.text.x = element_text(angle = 60, hjust = 1)) Hope this helps, Rui Barradas Às 19:48 de 03/08/2020, Pedro páramo escreveu: > Hi Rasmus, Josh and Rui, > > First of all many thanks in advance about your help. > > The first thig is sometimes you say " you are posting in HTML and that > makes the > post unreadable as this is a plain text list" how can I put the code > in the correct way, not html (attaching in txt?) > > The second about the code: > > I have used this: > > bwc <- cbind(bwfinal2,bwfinal) > colnames(bwc)=c("Accion","reval") > df <- matrix(unlist(bwc), nrow=nrow(bwc), byrow=F) > colnames(bwchist)=c("Accion","reval") > bwchist <-as.data.frame(bwc[order(df[,2]), ]) > > bwchist is the ordered cum stock returns in the year but because is a > list it is not possible to plot and histogram with x (names of stocks) > and the x axist the value of cum stocks (reval) > > when I put dput(bwchist) the console says: > > dput(bwchist) > structure(list(Accion = list("REE", "Enagas", "Grifols", "Ferrovial", > "Acerinox", "Naturgy", "Inditex", "Bankia", "ENCE", "Aena", > "Bankinter", "Mapfre", "CaixaBank", "CIE", "Colonial", "Almirall", > "Indra", "ArcelorMittal", "ACS", "Telefonica", "Amadeus", > "BBVA", "Merlin", "Santander", "Repsol", "Melia", "Sabadell", > "IAG", "Acciona", "Endesa", "MasMovil", "Iberdrola", "SGamesa", > "Viscofan", "Cellnex"), reval = list(-0.0200827282700085, > -0.0590294115600855, -0.214126598790964, -0.220773677809979, > -0.229653300324357, -0.257944379583984, -0.283942789063822, > -0.285159347392533, -0.303814713896458, -0.30734460425763, > -0.309408155539818, -0.319912221435868, -0.322790949659181, > -0.344047579452905, -0.347919538415482, -0.356898907103825, > -0.374263261296661, -0.40147247119078, -0.405150043834815, > -0.406022775042175, -0.413786100987797, -0.440679109311707, > -0.442603156492871, -0.491634140733524, -0.499254932434042, > -0.6, -0.709737357505148, -0.724461258850966, 0.0220528711420083, > 0.0462767672643172, 0.115044247787611, 0.238734548714937, > 0.274578114644054, 0.343422896082666, 0.387826126094928)), class = > "data.frame", row.names = c(NA, > -35L)) > > I try to make an hist or barplot but because it is a list no way to > obtain the plot. > > Many thanks again for your help. > > I have printed two manuals to improve my level, but if you can help > me, I would be very very gratefull. > > > > El vie., 31 jul. 2020 a las 18:28, Rasmus Liland (<[hidden email] > <mailto:[hidden email]>>) escribió: > > On 2020-07-31 10:07 -0500, Joshua Ulrich wrote: > | On Fri, Jul 31, 2020 at 9:55 AM Rui Barradas wrote: > | | Às 15:44 de 31/07/2020, Michael Dewey escreveu: > | | | Dear Pedro > | | | > | | | Some comments in-line > | | | > | | | On 30/07/2020 21:16, Pedro páramo wrote: > | | | | Hi all, > | | | | > | | | | I attach my code, the think is I > | | | | want to make a bar plot the last > | | | | variable called "bwchist" so the > | | | | X axis are "Accion" and the y > | | | | axis are "reval" values. > | | | | > | | | | I have prove class(bwchist) and > | | | | says dataframe but its still a > | | | | list because it says me I have > | | | | prove to unlist, but it doesnt > | | | | work > | | | | > | | | | hist(bwchist) > | | | | Error in hist.default(bwchist) : 'x' must be numeric > | | | > | | | So bwchist is not a numeric > | | | variable as hist needs. Aboce you > | | | said it is a data frame but data > | | | frames are not numeric. > | | | > | | | For future reference your example > | | | is way too long for anyone to go > | | | through and try to help you. Try > | | | next time to reduce it to the > | | | absolute minimum by removing > | | | sections while you still get the > | | | error. It is also easier to get > | | | help if you can remove unnecessary > | | | packages. > | | | > | | | It is also unreadable because you > | | | are posting in HTML and that makes > | | | the post unreadable as this is a > | | | plain text list. > | | > | | Hello, > | | > | | I second Michael's opinion. When the > | | post's code is very long, there is a > | | tendency to have less answers. > | | > | | Please post the output of > | | > | | dput(head(bwchist, 30)) > | | > | | It's much shorter code and it > | | recreates the data so we will be > | | able to see what's wrong and try to > | | find a solution. > | > | Hi Pedro, > | > | Another 'best practice' and polite > | thing to do is link to other places > | you may have cross-posted. That will > | give people the opportunity to see if > | your questions has been answered in > | another forum. > | > | I saw your post on R-SIG-Finance > | (https://stat.ethz.ch/pipermail/r-sig-finance/2020q3/014979.html), > | and started to work on a solution. > | > | I don't know how to do this in > | tidyquant, but here's how you can do > | it with quantmod: > | > | # all tickers > | tk <- c("ANA.MC <http://ANA.MC>", "ACS.MC <http://ACS.MC>", > "AENA.MC <http://AENA.MC>", "AMS.MC <http://AMS.MC>", "MTS.MC > <http://MTS.MC>", "BBVA.MC <http://BBVA.MC>", "SAB.MC > <http://SAB.MC>", > | "SAN.MC <http://SAN.MC>", "BKT.MC <http://BKT.MC>", "CABK.MC > <http://CABK.MC>", "CLNX.MC <http://CLNX.MC>", "ENG.MC > <http://ENG.MC>", "ENC.MC <http://ENC.MC>", "ELE.MC <http://ELE.MC>", > | "FER.MC <http://FER.MC>", "GRF.MC <http://GRF.MC>", "IBE.MC > <http://IBE.MC>", "ITX.MC <http://ITX.MC>", "COL.MC > <http://COL.MC>", "IAG.MC <http://IAG.MC>", "MAP.MC <http://MAP.MC>", > | "MEL.MC <http://MEL.MC>", "MRL.MC <http://MRL.MC>", "NTGY.MC > <http://NTGY.MC>", "REE.MC <http://REE.MC>", "REP.MC > <http://REP.MC>", "SGRE.MC <http://SGRE.MC>", "TEF.MC > <http://TEF.MC>", > | "VIS.MC <http://VIS.MC>", "ACX.MC <http://ACX.MC>", "BKIA.MC > <http://BKIA.MC>", "CIE.MC <http://CIE.MC>", "MAS.MC > <http://MAS.MC>", "ALM.MC <http://ALM.MC>", "IDR.MC <http://IDR.MC>") > | > | # download them into an environment ('e') > | require(quantmod) > | getSymbols(tk, from = "2019-12-31", env = (e <- new.env())) > | > | # extract adjusted close column > | adj <- lapply(e, Ad) > | # calculate daily returns from adjusted data, > | # merge into a xts matrix, and fill NA with 0 > | ret <- do.call(merge, c(lapply(adj, dailyReturn), fill = 0)) > | # cumulative returns > | cumret <- cumprod(1 + ret) - 1 > | # set names > | colnames(cumret) <- names(adj) > | last(cumret) > | # calculate histogram for period-to-date returns > | hist(drop(last(cumret))) > | > | I'm not sure that's the histogram > | you're looking for, but I hope it > | gives you a start toward a solution. > | > | Best, > | Josh > > Wow Josh! That's very elegant. > > Myself now, I just plowed through the > original code to make it simpler, but am > at a loss as to how this histogram looks > ... > > x <- c("ANA.MC <http://ANA.MC>", "ACS.MC <http://ACS.MC>", > "AENA.MC <http://AENA.MC>", "AMS.MC <http://AMS.MC>", "MTS.MC > <http://MTS.MC>", "BBVA.MC <http://BBVA.MC>", > "SAB.MC <http://SAB.MC>", "SAN.MC <http://SAN.MC>", > "BKT.MC <http://BKT.MC>", "CABK.MC <http://CABK.MC>", "CLNX.MC > <http://CLNX.MC>", "ENG.MC <http://ENG.MC>", > "ENC.MC <http://ENC.MC>", "ELE.MC <http://ELE.MC>", > "FER.MC <http://FER.MC>", "GRF.MC <http://GRF.MC>", "IBE.MC > <http://IBE.MC>", "ITX.MC <http://ITX.MC>", > "COL.MC <http://COL.MC>", "IAG.MC <http://IAG.MC>", > "MAP.MC <http://MAP.MC>", "MEL.MC <http://MEL.MC>", "MRL.MC > <http://MRL.MC>", "NTGY.MC <http://NTGY.MC>", > "REE.MC <http://REE.MC>", "REP.MC <http://REP.MC>", > "SGRE.MC <http://SGRE.MC>", "TEF.MC <http://TEF.MC>", "VIS.MC > <http://VIS.MC>", "ACX.MC <http://ACX.MC>", > "BKIA.MC <http://BKIA.MC>", "CIE.MC <http://CIE.MC>", > "MAS.MC <http://MAS.MC>", "ALM.MC <http://ALM.MC>", "IDR.MC > <http://IDR.MC>") > stock.prices <- > lapply(x, function(stock) { > tidyquant::tq_get(x=stock,from = '2019-12-31',get = > "stock.prices") > }) > names(stock.prices) <- x > > library(tidyquant) > > returns <- lapply(stock.prices, function(data) { > tab <- > tq_transmute( > data = data, > select = adjusted, # this specifies which > column to select > mutate_fun = periodReturn, # This specifies what > to do with that column > period = "daily", # This argument > calculates Daily returns > col_rename = "idr_returns") # renames the column > tab[,"cr"] <- cumprod(1 + tab[,"idr_returns"]) > tab[,"cumulative_returns"] <- tab[,"cr"] - 1 > > dplyr::pull( > tab[nrow(tab[,"cumulative_returns"]), > "cumulative_returns"] > ) > }) > > bestworst <- simplify2array(returns) > > namebw <- > c("Acciona", "ACS", "Aena", "Amadeus", > "ArcelorMittal", "BBVA", "Sabadell", > "Santander", "Bankinter", > "CaixaBank", "Cellnex", "Enagas", > "ENCE", "Endesa", "Ferrovial", > "Grifols", "Iberdrola", "Inditex", > "Colonial", "IAG", "Mapfre", > "Melia", "Merlin", "Naturgy", "REE", > "Repsol", "SGamesa", "Telefonica", > "Viscofan", "Acerinox", "Bankia", > "CIE", "MasMovil", "Almirall", > "Indra") > > bwc <- data.frame( > symbol=names(bestworst), > Accion=namebw, > reval=bestworst) > > | | | | bwc<-cbind(bwfinal2,bwfinal) > | | | | colnames(bwc)=c("Accion","reval") > | | | | bwc <- as.data.frame(bwc) > > ... aaaand you know something's > happening between here (where bwchist is > created), but you don't know what it is, > do you, Mr páramo? > > | | | | colnames(bwchist)=c("Accion","reval") > | | | | bwchist <-as.data.frame(bwc[order(bwc$reval), ]) > > Best, > Rasmus > -- Este e-mail foi verificado em termos de vírus pelo software antivírus Avast. https://www.avast.com/antivirus [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
In reply to this post by Pedro páramo
On 8/3/20 11:48 AM, Pedro páramo wrote: > Hi Rasmus, Josh and Rui, > > First of all many thanks in advance about your help. > > The first thig is sometimes you say " you are posting in HTML and that > makes the > post unreadable as this is a plain text list" how can I put the code in the > correct way, not html (attaching in txt?) Set your gmail client to deliver in plain text. Attached is a png file that is a screen shot of a Chrome mediated effort at composing a message and clicking the three-stacked-dots icon at the lower right corner. (Learn to use your chosen software tools by reading manuals or searching.) -- David. > > The second about the code: > > I have used this: > > bwc <- cbind(bwfinal2,bwfinal) > colnames(bwc)=c("Accion","reval") > df <- matrix(unlist(bwc), nrow=nrow(bwc), byrow=F) > colnames(bwchist)=c("Accion","reval") > bwchist <-as.data.frame(bwc[order(df[,2]), ]) > > bwchist is the ordered cum stock returns in the year but because is a list > it is not possible to plot and histogram with x (names of stocks) and the x > axist the value of cum stocks (reval) > > when I put dput(bwchist) the console says: > > dput(bwchist) > structure(list(Accion = list("REE", "Enagas", "Grifols", "Ferrovial", > "Acerinox", "Naturgy", "Inditex", "Bankia", "ENCE", "Aena", > "Bankinter", "Mapfre", "CaixaBank", "CIE", "Colonial", "Almirall", > "Indra", "ArcelorMittal", "ACS", "Telefonica", "Amadeus", > "BBVA", "Merlin", "Santander", "Repsol", "Melia", "Sabadell", > "IAG", "Acciona", "Endesa", "MasMovil", "Iberdrola", "SGamesa", > "Viscofan", "Cellnex"), reval = list(-0.0200827282700085, > -0.0590294115600855, -0.214126598790964, -0.220773677809979, > -0.229653300324357, -0.257944379583984, -0.283942789063822, > -0.285159347392533, -0.303814713896458, -0.30734460425763, > -0.309408155539818, -0.319912221435868, -0.322790949659181, > -0.344047579452905, -0.347919538415482, -0.356898907103825, > -0.374263261296661, -0.40147247119078, -0.405150043834815, > -0.406022775042175, -0.413786100987797, -0.440679109311707, > -0.442603156492871, -0.491634140733524, -0.499254932434042, > -0.6, -0.709737357505148, -0.724461258850966, 0.0220528711420083, > 0.0462767672643172, 0.115044247787611, 0.238734548714937, > 0.274578114644054, 0.343422896082666, 0.387826126094928)), class = > "data.frame", row.names = c(NA, > -35L)) > > I try to make an hist or barplot but because it is a list no way to obtain > the plot. > > Many thanks again for your help. > > I have printed two manuals to improve my level, but if you can help me, I > would be very very gratefull. > > > > El vie., 31 jul. 2020 a las 18:28, Rasmus Liland (<[hidden email]>) > escribió: > >> On 2020-07-31 10:07 -0500, Joshua Ulrich wrote: >> | On Fri, Jul 31, 2020 at 9:55 AM Rui Barradas wrote: >> | | Às 15:44 de 31/07/2020, Michael Dewey escreveu: >> | | | Dear Pedro >> | | | >> | | | Some comments in-line >> | | | >> | | | On 30/07/2020 21:16, Pedro páramo wrote: >> | | | | Hi all, >> | | | | >> | | | | I attach my code, the think is I >> | | | | want to make a bar plot the last >> | | | | variable called "bwchist" so the >> | | | | X axis are "Accion" and the y >> | | | | axis are "reval" values. >> | | | | >> | | | | I have prove class(bwchist) and >> | | | | says dataframe but its still a >> | | | | list because it says me I have >> | | | | prove to unlist, but it doesnt >> | | | | work >> | | | | >> | | | | hist(bwchist) >> | | | | Error in hist.default(bwchist) : 'x' must be numeric >> | | | >> | | | So bwchist is not a numeric >> | | | variable as hist needs. Aboce you >> | | | said it is a data frame but data >> | | | frames are not numeric. >> | | | >> | | | For future reference your example >> | | | is way too long for anyone to go >> | | | through and try to help you. Try >> | | | next time to reduce it to the >> | | | absolute minimum by removing >> | | | sections while you still get the >> | | | error. It is also easier to get >> | | | help if you can remove unnecessary >> | | | packages. >> | | | >> | | | It is also unreadable because you >> | | | are posting in HTML and that makes >> | | | the post unreadable as this is a >> | | | plain text list. >> | | >> | | Hello, >> | | >> | | I second Michael's opinion. When the >> | | post's code is very long, there is a >> | | tendency to have less answers. >> | | >> | | Please post the output of >> | | >> | | dput(head(bwchist, 30)) >> | | >> | | It's much shorter code and it >> | | recreates the data so we will be >> | | able to see what's wrong and try to >> | | find a solution. >> | >> | Hi Pedro, >> | >> | Another 'best practice' and polite >> | thing to do is link to other places >> | you may have cross-posted. That will >> | give people the opportunity to see if >> | your questions has been answered in >> | another forum. >> | >> | I saw your post on R-SIG-Finance >> | (https://stat.ethz.ch/pipermail/r-sig-finance/2020q3/014979.html), >> | and started to work on a solution. >> | >> | I don't know how to do this in >> | tidyquant, but here's how you can do >> | it with quantmod: >> | >> | # all tickers >> | tk <- c("ANA.MC", "ACS.MC", "AENA.MC", "AMS.MC", "MTS.MC", "BBVA.MC", " >> SAB.MC", >> | "SAN.MC", "BKT.MC", "CABK.MC", "CLNX.MC", "ENG.MC", "ENC.MC", "ELE.MC >> ", >> | "FER.MC", "GRF.MC", "IBE.MC", "ITX.MC", "COL.MC", "IAG.MC", "MAP.MC", >> | "MEL.MC", "MRL.MC", "NTGY.MC", "REE.MC", "REP.MC", "SGRE.MC", "TEF.MC >> ", >> | "VIS.MC", "ACX.MC", "BKIA.MC", "CIE.MC", "MAS.MC", "ALM.MC", "IDR.MC") >> | >> | # download them into an environment ('e') >> | require(quantmod) >> | getSymbols(tk, from = "2019-12-31", env = (e <- new.env())) >> | >> | # extract adjusted close column >> | adj <- lapply(e, Ad) >> | # calculate daily returns from adjusted data, >> | # merge into a xts matrix, and fill NA with 0 >> | ret <- do.call(merge, c(lapply(adj, dailyReturn), fill = 0)) >> | # cumulative returns >> | cumret <- cumprod(1 + ret) - 1 >> | # set names >> | colnames(cumret) <- names(adj) >> | last(cumret) >> | # calculate histogram for period-to-date returns >> | hist(drop(last(cumret))) >> | >> | I'm not sure that's the histogram >> | you're looking for, but I hope it >> | gives you a start toward a solution. >> | >> | Best, >> | Josh >> >> Wow Josh! That's very elegant. >> >> Myself now, I just plowed through the >> original code to make it simpler, but am >> at a loss as to how this histogram looks >> ... >> >> x <- c("ANA.MC", "ACS.MC", "AENA.MC", "AMS.MC", "MTS.MC", "BBVA.MC >> ", >> "SAB.MC", "SAN.MC", "BKT.MC", "CABK.MC", "CLNX.MC", "ENG.MC", >> "ENC.MC", "ELE.MC", "FER.MC", "GRF.MC", "IBE.MC", "ITX.MC", >> "COL.MC", "IAG.MC", "MAP.MC", "MEL.MC", "MRL.MC", "NTGY.MC", >> "REE.MC", "REP.MC", "SGRE.MC", "TEF.MC", "VIS.MC", "ACX.MC", >> "BKIA.MC", "CIE.MC", "MAS.MC", "ALM.MC", "IDR.MC") >> stock.prices <- >> lapply(x, function(stock) { >> tidyquant::tq_get(x=stock,from = '2019-12-31',get = >> "stock.prices") >> }) >> names(stock.prices) <- x >> >> library(tidyquant) >> >> returns <- lapply(stock.prices, function(data) { >> tab <- >> tq_transmute( >> data = data, >> select = adjusted, # this specifies which column >> to select >> mutate_fun = periodReturn, # This specifies what to do >> with that column >> period = "daily", # This argument calculates >> Daily returns >> col_rename = "idr_returns") # renames the column >> tab[,"cr"] <- cumprod(1 + tab[,"idr_returns"]) >> tab[,"cumulative_returns"] <- tab[,"cr"] - 1 >> >> dplyr::pull( >> tab[nrow(tab[,"cumulative_returns"]), >> "cumulative_returns"] >> ) >> }) >> >> bestworst <- simplify2array(returns) >> >> namebw <- >> c("Acciona", "ACS", "Aena", "Amadeus", >> "ArcelorMittal", "BBVA", "Sabadell", >> "Santander", "Bankinter", >> "CaixaBank", "Cellnex", "Enagas", >> "ENCE", "Endesa", "Ferrovial", >> "Grifols", "Iberdrola", "Inditex", >> "Colonial", "IAG", "Mapfre", >> "Melia", "Merlin", "Naturgy", "REE", >> "Repsol", "SGamesa", "Telefonica", >> "Viscofan", "Acerinox", "Bankia", >> "CIE", "MasMovil", "Almirall", >> "Indra") >> >> bwc <- data.frame( >> symbol=names(bestworst), >> Accion=namebw, >> reval=bestworst) >> >> | | | | bwc<-cbind(bwfinal2,bwfinal) >> | | | | colnames(bwc)=c("Accion","reval") >> | | | | bwc <- as.data.frame(bwc) >> >> ... aaaand you know something's >> happening between here (where bwchist is >> created), but you don't know what it is, >> do you, Mr páramo? >> >> | | | | colnames(bwchist)=c("Accion","reval") >> | | | | bwchist <-as.data.frame(bwc[order(bwc$reval), ]) >> >> Best, >> Rasmus >> > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
Free forum by Nabble | Edit this page |