2016-04-12 22 views
0

我已經嘗試了一遍又一遍來解決這個問題,但我無法理解它。我在R中估計了一個Beta-t-EGARCH模型和一個GARCH-t模型,現在我需要在同一個圖上繪製結果。最後的結果是可怕的,因爲變量在y軸上不共享相同的比例。我是R新手,所以請不要責怪我:)。 下面的代碼:在R的同一尺度上繪製幾個變量

library(quantmod) 
library(betategarch) 
library(fGarch) 
library(ggplot2) 

getSymbols("GOOG",src="yahoo") 
google_ret <- abs(periodReturn(GOOG, period="daily", subset=NULL, type="log"))-mean(abs(periodReturn(GOOG, period="daily", subset=NULL, type="log"))) 

googcomp <- tegarch(google_ret, asym=FALSE, skew=FALSE) 
goog1stdev <- fitted(googcomp) 


#now we try to fit a standard GARCH-t model 
googgarch <- garchFit(data=google_ret, cond.dist="sstd") 
googgarch2 <- garchFit(data=google_ret, cond.dist="sstd", include.mean = FALSE, include.delta = FALSE, include.skew = FALSE, include.shape = FALSE, leverage = FALSE, trace = TRUE) 
volatility <- volatility(googgarch2, type = "sigma") 


plot(google_ret) 
par(new=TRUE) 
plot(googgarch2, which=2) 
par(new=TRUE) 
plot(goog1stdev, col="red") 

最終的結果完全是一個曲線圖未按比例在y軸上,與具有上述較高的那些標繪更低的值的變量。非常感謝任何想幫助我的人!

+0

請添加代碼,加載包需要。 – eipi10

+0

我編輯過這條消息:) – james42

回答

1

建議的方法是繪製它們堆疊在彼此的頂部,不同的情節:

layout(matrix(1:3,3)) 
plot(google_ret) 
plot(googgarch2, which=2) 
plot(goog1stdev, col="red") 

enter image description here

可以擺脫空白與調用par("mar")調整邊距大小:

opar=par(mar=par("mar") -c(1,0,3,0)) # opar will then let your restore previous values 
..... plotting efforts 
par(opar) 

enter image description here

我不知道你的域名非常,但如果你使用CNA轉移的y座標然後這會產生一個有點清理版本的疊加圖:

png() 
plot(google_ret, ylim=c(0,1), ylab="ylab="Google Returns(black); GGarch x10 +0.5 (blue); STD + 0.3(red)") 
par(new=TRUE) 
plot([email protected] +.5, type="l", col="blue",axes=FALSE, ylab="", main="",ylim=c(0, 1)) ;abline(h=.5, col="blue") 
par(new=TRUE); 
plot(10*coredata(goog1stdev) + .3, col="red", type="l", axes=FALSE, main="",ylim=c(0,1), ylab=""); abline(h=.3, col="red") 
dev.off() 

enter image description here

+0

如果我無法將它們放在一個圖上,我會這樣做!感謝您的建議 – james42

+0

您會發現,沒有任何主要的繪圖範例對兩座標(少於三座標)繪圖方法感到滿意。 'plotrix'軟件包有兩種繪圖方法。 –