2010年10月20日 星期三

如何在 R 裏頭用各種顏色畫美美的圖~

會不會覺得在畫R的圖形時顏色都很單調呢...
不是黑色(default), 紅色(red), 綠色(green), 就是藍色(blue)...
現在給大家多一些選擇, 想不想畫出下面這種比較繽紛的圖形呢??


# R code:
barplot(VADeaths,beside=TRUE,col=c("lightblue","mistyrose","lightcyan","lavender","cornsilk"),
legend=rownames(VADeaths),ylim=c(0, 100))
title(main="Death Rates in Virginia",font.main=4)


眼尖的人可以看出來這些顏色的名稱是lightblue, mistyrose, lightcyan, lavender, cornsilk....
但是我哪知道這些名稱是對應到這些顏色啊....Orz...

現在救星來了, 請參考附加檔案的pdf檔案...
lightblue在第五頁的右下角數上來第二個, 編號400
mistyrose在第六頁編號479
...

所以現在是不是選擇性變多啦....
以後就可以畫美美的圖形了...
提供給大家參考...^^

2010年6月19日 星期六

Conditional logistic regression

雖然說WinBUGS中有三種寫法, 但是實際上測試結果只有第一種寫法最穩定..

參考資料 Volumn 2. Endo: conditional inference in case-control studies

直接用例子來說明(這裡的例子不是WinBUGS的, 而是R裡頭的School例子)

model
{
for (i in 1:N) {
y[i,1] <- 1 y[i,2] <- 0 }

for (i in 1:N) {

# model 1. 用一般的logistic regression去做, 缺點是只能處理1-1 matching
# 結果(update 10K, 取後面5K, 每隔10取一點)
# (b1,b2,b3)=(1.079 0.968 , -0.471 0.7271, -1.795 0.7117)

y[i,1] ~ dbin(p[i,1],1)
logit(p[i,1]) <- b1*(smoking[i,1]-smoking[i,2]) + b2*(rubber[i,1]-rubber[i,2]) + b3*(alcohol[i,1]-alcohol[i,2])

# model 2. 用multinomial distribution去做, 缺點是容易發散..
# y[i,1:2] ~ dmulti(p[i,1:2],1)
# for (j in 1:2) {
# p[i,j] <- e[i,j]/sum(e[i,]) # e[i,j] <- max(0.00001, min( exp(b1*smoking[i,j]+b2*rubber[i,j]+b3*alcohol[i,j]) , 0.99999)) # log(e[i,j]) <- b1*smoking[2*(i-1)+j]+b2*rubber[2*(i-1)+j]+b3*alcohol[2*(i-1)+j] # }


# model 3. 使用poisson regression去做, 但是我始終做不出來...(sigh)
# for (j in 1:2) {
# Y[i,j] ~ dpois(mu2[i,j])
# log(mu[i,j]) <- b0[i] + b1*smoking[2*(i-1)+j]+b2*rubber[2*(i-1)+j]+b3*alcohol[2*(i-1)+j] # mu[i,j] <- exp(b0[i] + b1*smoking[i,j]+b2*rubber[i,j]+b3*alcohol[i,j]) # mu2[i,j] <- max(mu[i,j], 0.0001) # }
# b0[i] ~ dnorm(0,1)

}

b1 ~ dnorm(0,1.0E-6)
b2 ~ dnorm(0,1.0E-6)
b3 ~ dnorm(0,1.0E-6)
}

list(b1=0,b2=0,b3=0)

list(N=26,
smoking=structure(.Data=c(1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,0,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,0,0),.Dim=c(26,2)),
alcohol=structure(.Data=c(0,1,0,1,1,1,0,0,0,1,0,1,0,1,1,1,1,1,0,1,1,0,0,0,1,1,0,0,0,1,0,1,1,1,0,1,0,1,0,1,1,1,1,0,0,0,1,0,0,1,0,0),.Dim=c(26,2)),
rubber=structure(.Data=c(0,0,0,0,0,0,1,0,1,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,1,1,0,1,1,1,0,1,0,0,0,0,0,1,1,0,0,0),.Dim=c(26,2)))


model
{
for (i in 1:N) {
y[i,1] <- 1
y[i,2] <- 0
}
for (i in 1:N) {
y[i,1] ~ dbin(p[i,1],1)
logit(p[i,1]) <- b1*(smoking[i,1]-smoking[i,2]) + b2*(rubber[i,1]-rubber[i,2]) + b3*(alcohol[i,1]-alcohol[i,2])
# Y[i,1:2] ~ dmulti(p[i,1:2],1)
# for (j in 1:2) {
# p[i,j] <- e[i,j]/sum(e[i,])
# e[i,j] <- max(0.00001, min( exp(b1*smoking[i,j]+b2*rubber[i,j]+b3*alcohol[i,j]) , 0.99999))
# log(e[i,j]) <- b1*smoking[2*(i-1)+j]+b2*rubber[2*(i-1)+j]+b3*alcohol[2*(i-1)+j]
# }
# for (j in 1:2) {
# Y[i,j] ~ dpois(mu2[i,j])
# log(mu[i,j]) <- b0[i] + b1*smoking[2*(i-1)+j]+b2*rubber[2*(i-1)+j]+b3*alcohol[2*(i-1)+j]
# mu[i,j] <- exp(b0[i] + b1*smoking[i,j]+b2*rubber[i,j]+b3*alcohol[i,j])
# mu2[i,j] <- max(mu[i,j], 0.0001)
# }
# b0[i] ~ dnorm(0,1)
}
b1 ~ dnorm(0,1.0E-6)
b2 ~ dnorm(0,1.0E-6)
b3 ~ dnorm(0,1.0E-6)
}
list(b1=0,b2=0,b3=0)
list(N=26,
smoking=structure(.Data=c(1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,0,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,0,0),.Dim=c(26,2)),
alcohol=structure(.Data=c(0,1,0,1,1,1,0,0,0,1,0,1,0,1,1,1,1,1,0,1,1,0,0,0,1,1,0,0,0,1,0,1,1,1,0,1,0,1,0,1,1,1,1,0,0,0,1,0,0,1,0,0),.Dim=c(26,2)),
rubber=structure(.Data=c(0,0,0,0,0,0,1,0,1,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,1,1,0,1,1,1,0,1,0,0,0,0,0,1,1,0,0,0),.Dim=c(26,2)))