2017-04-27 131 views
-1

我試圖通過使用for循環來更新和繪製曲線中的多條曲線。不過,我無法找到一種方法來使用par函數在多個圖中執行此操作。我已經嘗試使用帶參數的par函數:new = TRUE。這與一個情節一起工作,並且該情節在一個圖中包含多條曲線。但是,當我試圖用多個重複圖做到這一點時,我就失敗了。 有什麼建議嗎?更新圖中的多條曲線R

這裏是我的代碼的示例: 與來自載體的多個參數林配件2 parametr威布爾曲線:boot.shape.vec和boot.scale.vec

boot.shape.vec <- c(0.7,0.6,0.8,0.5,0.65) 
boot.scale.vec <- c(5,4.5,6,5.3,4.9) 

#many curves 

for (ii in 1:length(boot.shape.vec[1:10])) 
{ 
    for (jj in 1:length(boot.scale.vec[1:10])) 
    { 
    par(new= TRUE, mfrow=c(1,4),mar=c(5.1,5.1,4.1,2.1)) # Make room for the hat. 

    curve(pweibull(x, scale=exp(boot.shape.vec[ii]), shape=1/boot.scale.vec[jj], lower.tail=FALSE), 
      from=0, to=40, col='grey70', lwd=0.5, ylab=expression(hat(S)(t)), xlab='t',bty='n',ylim=c(0,1)) 

    curve(pweibull(x, scale=exp(boot.shape.vec[ii]), shape=1/boot.scale.vec[jj], lower.tail=FALSE), 
      from=0, to=40, col='darkorange', lwd=0.5, ylab=expression(hat(S)(t)), xlab='t',bty='n',ylim=c(0,1)) 

    # h(t), the hazard function 
    curve((scale/intercept)*(x/boot.shape.vec[ii])^(boot.scale.vec[jj]-1), from=0,to=20,ylab=expression(hat(h)(t)),bty='n', col="darkgreen",xlab="t", lwd=0.5) 

    curve(dweibull(x, scale=exp(boot.shape.vec[ii]), shape=1/boot.scale.vec[jj]) 
      /pweibull(x, scale=exp(boot.shape.vec[ii]), shape=1/boot.scale.vec[jj], lower.tail=FALSE), 
      from=0, to=40, col='blue', lwd=0.5, ylab=expression(hat(H)(t)), xlab='t',bty='n') 

    } 
} 

enter image description here

我可以產生所需的輸出只有一個曲線:

for (ii in 1:length(boot.shape.vec)) 
{ 
    for (jj in 1:length(boot.scale.vec)) 
    { 
    par(new= TRUE, mfrow=c(1,1),mar=c(5.1,5.1,4.1,2.1)) # Make room for the hat. 

    curve(pweibull(x, scale=exp(boot.shape.vec[ii]), shape=1/boot.scale.vec[jj], lower.tail=FALSE), 
      from=0, to=100, col='grey70', lwd=1, ylab=expression(hat(S)(t)), xlab='t',bty='n',ylim=c(0,1)) 


    } 
} 

enter image description here

#################羅蘭茲提案更新

附上代碼和圖,我想所有的圖都包含多條曲線。現在只有最後一個包含所需的輸出。

boot.shape.vec <- xfit.boot$estim$shape 
boot.scale.vec <- xfit.boot$estim$scale 



par(new=TRUE,mfrow=c(1,4),mar=c(5.1,5.1,4.1,2.1)) # Make room for the hat. 

curve(pweibull(x, scale=exp(boot.shape.vec[1]), shape=1/boot.scale.vec[1], lower.tail=FALSE), 
     from=0, to=40,add= FALSE, col='grey70', lwd=0.5, ylab=expression(hat(S)(t)), xlab='t',bty='n',ylim=c(0,1)) 

curve(pweibull(x, scale=exp(boot.shape.vec[1]), shape=1/boot.scale.vec[1], lower.tail=FALSE), 
     from=0, to=40,add= FALSE, col='darkorange', lwd=0.5, ylab=expression(hat(S)(t)), xlab='t',bty='n',ylim=c(0,1)) 

# h(t), the hazard function 
curve((scale/intercept)*(x/boot.shape.vec[1])^(boot.scale.vec[1]-1), from=0,to=20,ylab=expression(hat(h)(t)),bty='n', 
     add= FALSE,col="darkgreen",xlab="t", lwd=0.5) 

curve(dweibull(x, scale=exp(boot.shape.vec[1]), shape=1/boot.scale.vec[1]) 
     /pweibull(x, scale=exp(boot.shape.vec[1]), shape=1/boot.scale.vec[1], lower.tail=FALSE), 
     from=0, to=40, add= FALSE,col='blue', lwd=0.5, ylab=expression(hat(H)(t)), xlab='t',bty='n') 



#many curves 

for (ii in 2:length(boot.shape.vec)) 
{ 
    for (jj in 2:length(boot.scale.vec)) 
    { 
    #par(new=TRUE,mfrow=c(1,4),mar=c(5.1,5.1,4.1,2.1)) # Make room for the hat. 

    curve(pweibull(x, scale=exp(boot.shape.vec[ii]), shape=1/boot.scale.vec[jj], lower.tail=FALSE), 
      from=0, to=40,add= TRUE, col='grey70', lwd=0.5, ylab=expression(hat(S)(t)), xlab='t',bty='n',ylim=c(0,1)) 

    curve(pweibull(x, scale=exp(boot.shape.vec[ii]), shape=1/boot.scale.vec[jj], lower.tail=FALSE), 
      from=0, to=40,add= TRUE, col='darkorange', lwd=0.5, ylab=expression(hat(S)(t)), xlab='t',bty='n',ylim=c(0,1)) 

    # h(t), the hazard function 
    curve((scale/intercept)*(x/boot.shape.vec[ii])^(boot.scale.vec[jj]-1), from=0,to=20,ylab=expression(hat(h)(t)),bty='n', 
      add= TRUE,col="darkgreen",xlab="t", lwd=0.5) 

    curve(dweibull(x, scale=exp(boot.shape.vec[ii]), shape=1/boot.scale.vec[jj]) 
      /pweibull(x, scale=exp(boot.shape.vec[ii]), shape=1/boot.scale.vec[jj], lower.tail=FALSE), 
      from=0, to=40, add= TRUE,col='blue', lwd=0.5, ylab=expression(hat(H)(t)), xlab='t',bty='n') 

    } 
} 

enter image description here

+1

您是否正在尋找的''中curve' add'參數? – Roland

+0

試圖用不同的參數繪製海誓山盟的曲線 – jonas

+0

是的,請查看'help(「curve」)'中的'add'參數。 – Roland

回答

1
boot.shape.vec <- c(0.7,0.6,0.8,0.5,0.65) 
boot.scale.vec <- c(5,4.5,6,5.3,4.9) 

par(mfrow=c(1,1),mar=c(5.1,5.1,4.1,2.1)) # Make room for the hat. 

curve(pweibull(x, scale=exp(boot.shape.vec[1]), shape=1/boot.scale.vec[1], lower.tail=FALSE), 
     from=0, to=100, col='grey70', lwd=1, ylab=expression(hat(S)(t)), xlab='t',bty='n',ylim=c(0,1)) 

for (ii in 2:length(boot.shape.vec)) 
{ 
    for (jj in 2:length(boot.scale.vec)) 
    { 
    curve(pweibull(x, scale=exp(boot.shape.vec[ii]), shape=1/boot.scale.vec[jj], lower.tail=FALSE), 
      from=0, to=100, col='grey70', lwd=1, ylab=expression(hat(S)(t)), xlab='t',bty='n',ylim=c(0,1), 
      add = TRUE) 
    } 
} 

resulting plot

+0

是的,這一個我設法執行,但使用par功能和多個情節是我的問題。 – jonas

+0

然後,我不明白你想達到什麼。如果你從我的代碼中刪除了'add = TRUE',你會得到多個圖。 – Roland

+0

讓我更新我的問題,使其更清晰... – jonas