2017-09-20 17 views
0

Rを使用してAPPL株データの毎年の算術平均および日次幾何平均を求めようとしています。最後の数行は動作しないようですが、誤った文脈で使用される '...'というエラーが出ます。毎年の算術演算および毎日の幾何平均値(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またはweeksからendpoints引数を変更します。

+0

quantmodを使っても上記の結果は得られません。以下は、私が受け取った出力です: > getSymbols( 'AAPL'、from = '2010-01-01') [1] "AAPL" ROC(AAPL、on = 'years' )、 "AAPL.Adjusted"]、type = 'continuous') + – Stoner

+0

そしてプロセスは停止します。 – Stoner

関連する問題