J'essaie d'obtenir les moyennes arithmétiques quotidiennes et géométriques quotidiennes pour chaque année, pour les données de stock APPL en utilisant R. Mon implémentation sur ce sera la fonction periodReturn dans le dernières lignes, mais cela ne semble pas fonctionner, et une erreur: '...' utilisé dans un contexte incorrect est donné.Arithmétique quotidienne et moyennes géométriques quotidiennes pour chaque année (AAPL)
Comment puis-je modifier mon code de sorte que je puisse obtenir la sortie désirée? De l'aide sera grandement appréciée.
# Get historical price data (daily)
getSymbols('AAPL', from = "2005-01-01")
AAPLdaily <- as.data.frame(AAPL)
head(AAPLdaily)
?to.period
# AAPLweekly <- to.weekly(to.weekly(AAPL, indexAt = 'endof'))
# AAPLweekly <- as.data.frame(AAPLweekly)
# Better to do it in one step like this:
AAPLweekly <- as.data.frame(to.weekly(AAPL, indexAt = 'endof'))
head(AAPLweekly)
AAPLmonthly <- as.data.frame(to.monthly(AAPL, indexAt = 'endof'))
head(AAPLmonthly)
AAPLyearly <- as.data.frame(to.yearly(AAPL, indexAt = 'endof'))
AAPLyearly
# Another way to do this
AAPLweekly1 <- as.data.frame(to.period(AAPL, period = 'weeks', indexAt = 'endof'))
head(AAPLweekly1)
AAPLmonthly1 <- as.data.frame(to.period(AAPL, period = 'months', indexAt = 'endof'))
head(AAPLmonthly1)
AAPLyearly1 <- as.data.frame(to.period(AAPL, period = 'years', indexAt = 'endof'))
head(AAPLyearly1)
########## Another possible method #########
# Change to data.frames
AAPL = as.data.frame(AAPL)
head(AAPL)
# Get Dates
dates <- as.Date(row.names(AAPL))
head(dates)
# Create a cloumn in APPL data frame with the dates
AAPL$dates <- as.Date(row.names(AAPL))
?aggregate
?substr
# Last Day of month
lastDayofMonth <- aggregate(AAPL["dates"], list(month = substr(AAPL$dates, 1, 7)), max)
head(lastDayofMonth)
AAPLmonth <- AAPL[dates %in% lastDayofMonth$dates, ]
head(AAPLmonth)
# Last day of year
lastDayofYear <- aggregate(AAPL["dates"], list(month = substr(AAPL$dates, 1, 4)), max)
head(lastDayofYear)
AAPLyear <- AAPL[dates %in% lastDayofYear$dates, ]
AAPLmonth
AAPLdaily <- as.data.frame(to.daily(AAPL, indexAt = 'endof'))
AAPLdaily
dailyReturn(AAPLdaily)
periodReturn(AAPL,
period='daily',
subset=NULL,
type='arithmetic',
leading=TRUE,
...
)
'' 'annualReturn (AAPLdaily)' '' est-ce ce dont vous avez besoin? –
@GeorgeSotiropoulos Oui, si la sortie pour chaque année est le rendement quotidien pour chaque année correspondante, calculé par la moyenne arithmétique. Cependant, cela ne donne pas la moyenne géométrique pour chaque année. – Stoner