2015-04-23 79 views
0

另一個變量的參數下繪製一個獨立變量我有一個函數中的R

predictshrine<-0*rain-399.8993+5*crops+50.4296*log(citysize)+ 
    4.5071*wonders*chief+.02301*children*deaths+1.806*children+ 
    .10799*deaths-2.0755*wonders-.0878*children^2+.001062*children^3- 
    .000004288*children^4-.009*deaths^2+.0000530238*deaths^3+ 
    7.974*sqrt(children)+.026937*wonders^2-.0001305*wonders^3 

我也有環的序列

children<-seq(0,100,length=500) 

而一個查閱

for(deaths in c(0,5,10,50,100,200)) 

我想要做的就是能夠預測死亡率等於某些數量時的預測值與兒童的比例,並將這些圖表全部顯示在o處NCE與票面(mfrow)函數

plot(predictshrine, children) 

我希望能夠做到這一點功能時死亡= 0,當死亡= 10,當死亡= 50等

這樣我可以有6個不同的圖形顯示

理想我可以做類似

plot(predictshrine, children, when deaths = 10 & 20 & 50) 
在迴歸的變化210

但這不會作爲代碼工作。

我想將我的4循環合併到等式中。

只是爲了說清楚,死亡和孩子兩個變量在我的多變量方程提前

- 最大

+0

但是所有過在迴歸變量?例如「奇蹟」和「莊稼」。請使用示例輸入數據製作[可重現示例](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example),以使其更容易幫助您。 – MrFlick

回答

1

感謝您可以使用mapply遍歷幾個參數。請記住,如果你想這樣做,你需要定義所有其他變量。此外,這不是最有效的內存方式,但它應該適用於較小尺寸的組合。

predictshrine<- function(rain,citysize,wonders,chief,children,deaths,crops) { 
    0*rain-399.8993+5*crops+50.4296*log(citysize)+ 
    4.5071*wonders*chief+.02301*children*deaths+1.806*children+ 
    .10799*deaths-2.0755*wonders-.0878*children^2+.001062*children^3- 
    .000004288*children^4-.009*deaths^2+.0000530238*deaths^3+ 
    7.974*sqrt(children)+.026937*wonders^2-.0001305*wonders^3 
} 

deathlist = c(0,5,10,50,100,200) 
#note that the children value is recycled 
res = mapply(predictshrine,children = 1:100,deaths = 
rep(deathlist,each = 100), 
rain = 0.1, wonders = 1, chief = 1, crops = 0.5,citysize = 1000) 

然後你可以繪製這六次。

#allow six plots on the panel if you want 
par(mfrow = c(3,2)) 
#loop through different plots 
for (i in 1:6) 
    plot(1:100,res[1:100 + (i-1)*100]) 

這是什麼樣子

enter image description here

+0

賓果寶貝。工作很好,謝謝。 –