2017-09-20 3 views
0

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, 
      ... 
      ) 
+1

'''yearlyReturn (AAPLdaily)'''이것이 필요한 것입니까? –

+0

@GeorgeSotiropoulos 예, 각 해의 출력이 산술 평균을 통해 계산 된 각 해당 연도의 일일 수익 인 경우 예. 그러나 이것은 각 해의 기하 평균을 의미하지는 않습니다. – Stoner

답변

1

은 당신이해야 할 연간, 월간, 주간 산술/기하학적 수익입니다 당신을 위해 무엇을 요구하는 것은 경우 :

기하학적 (로그) 반환
getSymbols('AAPL',from= '2010-01-01') 
    ROC(AAPL[endpoints(AAPL,on = 'years'),"AAPL.Adjusted"],type='discrete’) 

2012-12-31 0.32566879 
2013-12-31 0.08069493 
2014-12-31 0.40622488 
2015-12-31 -0.03013708 
2016-12-30 0.12480425 
2017-09-20 0.36428706 

가에 ROC 인수를 변경 '연속적'다른 기간

ROC(AAPL[endpoints(AAPL,on = 'years'),"AAPL.Adjusted"],type='continuous’) 

2012-12-31 0.28191708 
2013-12-31 0.07760429 
2014-12-31 0.34090873 
2015-12-31 -0.03060053 
2016-12-30 0.11760902 
2017-09-20 0.31063199 

months 또는 weeksendpoints 인수를 변경.

+0

quantmod를 사용하여도 위의 결과를 얻을 수 없습니다. 다음은 내가받은 출력입니다. > getSymbols ('AAPL', from = '2010-01-01') [1] "AAPL" ROC (AAPL, on = 'years'), "AAPL.Adjusted"], type = 'continuous') + – Stoner

+0

그리고 프로세스가 중지됩니다. – Stoner

관련 문제