R을 사용하여 APPL 주식 데이터에 대한 일별 산술 및 일일 기하 평균을 얻으려고합니다.이 구현에있는 periodReturn 함수는 마지막 몇 줄이지만 작동하지 않는 것 같고 잘못된 문맥에서 사용되는 '...'오류가 있습니다.일년 산술 및 일일 기하 평균 (AAPL)
코드를 수정하여 원하는 출력을 얻으려면 어떻게해야합니까? 어떤 도움을 깊이 감사 할 것입니다.
# 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,
...
)
'''yearlyReturn (AAPLdaily)'''이것이 필요한 것입니까? –
@GeorgeSotiropoulos 예, 각 해의 출력이 산술 평균을 통해 계산 된 각 해당 연도의 일일 수익 인 경우 예. 그러나 이것은 각 해의 기하 평균을 의미하지는 않습니다. – Stoner