[英]Vertical 95% Confidence Interval plot 2 groups comparison
所以我的最終目標是讓 plot 具有多個 95% 置信區間垂直繪制在 2 組中,如下例所示:
我找到了這個代碼: https://rpubs.com/nati147/703463
但是如何在 plot 中添加分組比較?
編寫一個 function 'CI_95' 以輸入樣本值向量,並輸出該樣本的 95% 置信區間。 您可以使用“margin_error_95”function。
CI_95 <- function(sample_vals, sig){
error <- margin_error_95(sample_vals, sig)
CI <- mean(sample_vals) + c(-1, 1)*error
}
編寫一個名為“margin_error_95”的 function,它以樣本值向量為輸入,並輸出 95% 置信區間的誤差范圍。
margin_error_95 <- function(sample_vals, sig){
n <- length(sample_vals)
mar_err <- 1.96*(sig/sqrt(n))
}
plot_CI_95 <- function(seed){
B <- 100
n <- 30
mu <- 5
sig <- 1.2
set.seed(seed)
# extract upper bound of CI's
x_1 <- replicate(B,
{samp <- rnorm(n, mean = mu, sd = sig )
max(CI_95(samp, sig))
}
)
#extract lower bound of CI's
set.seed(seed)
x_0 <- replicate(B,
{samp <- rnorm(n, mean = mu, sd = sig )
min(CI_95(samp, sig))
}
)
set.seed(seed)
means <- replicate(B, mean(rnorm(n, mean = mu, sd = sig)))
plot(means, 1:B, pch = 20,
xlim = c(mu - sig, mu + sig),
ylim = c(0,B+1),
xlab = "sample means",
ylab = "index of the CI",
main = paste(B, "Confidence intervals")
)
for (i in 1:B){
if(between(mu, x_0[i], x_1[i])){
segments(x_0[i], i, x_1[i], i, lwd = 2) #plot CI's that contain the mean in black
} else {
segments(x_0[i], i, x_1[i], i, col = "red", lwd = 2) #plot CI's that don't contain the mean in red
}
}
abline(v=mu, col = "blue") #plot a vertical line at the population mean
}
運行 plot:
plot_CI_95(1)
這是一個解決方案,盡管它可能需要根據您的喜好進行一些進一步的改進。 我保留了你的plot_CI_95
function 的一般結構,但在不同的組上添加了一個循環。 這意味着mu
和sig
變量現在必須有多個值,如果要顯示組間差異,則每個組一個值。 還有一些colors等圖形參數。 結果如下所示。
為了避免兩組的間隔重疊,有一些參數需要調整。 1) png
中的圖形height
(增加值使組之間的空間更大,2) offset
參數(最多可增加約0.3),或3) segments
函數中的lwd
(較小的值意味着更細的線條)。 使用png
或類似的 function 直接保存圖形將允許微調所需的外觀。
library(dplyr)
CI_95 <- function(sample_vals, sig){
error <- margin_error_95(sample_vals, sig)
CI <- mean(sample_vals) + c(-1, 1)*error
}
margin_error_95 <- function(sample_vals, sig){
n <- length(sample_vals)
mar_err <- 1.96*(sig/sqrt(n))
}
png("group_plot.png",height=7,width=3,units = 'in',res=1000)
plot_CI_95 <- function(seed){
B <- 100
n <- 30
# mean and std dev as a vector of values
# need to have same length
# assume one value per group
mu <- c(5,4.5) # group1, group2
sig <- c(1.2,1) # group1, group2
offset<- 0.25 # controls point and line offset from nominal value
# colors for 2 groups
colsuse <- c('steelblue','gold')
# loop over groups
# mu and sig are now indexed by this loop
for(j in 1:length(mu)){
# use seed+j to make different random sample for each group
# extract upper bound of CI's
set.seed(seed+j)
x_1 <- replicate(B,
{samp <- rnorm(n, mean = mu[j], sd = sig[j])
max(CI_95(samp, sig[j]))
}
)
#extract lower bound of CI's
set.seed(seed+j)
x_0 <- replicate(B,
{samp <- rnorm(n, mean = mu[j], sd = sig[j])
min(CI_95(samp, sig[j]))
}
)
set.seed(seed+j)
means <- replicate(B, mean(rnorm(n, mean = mu[j], sd = sig[j])))
# for first group, establish the plot
# for second group, add values to the plot
# if groups are very different this might need to be modified with the xlim
if(j == 1){
plot(means, (1:B)+offset*ifelse(j==1,1,-1), pch = 20,
xlim = c(mu[j] - sig[j], mu[j] + sig[j]),
ylim = c(0,B+1),
xlab = "sample means",
ylab = "index of the CI",
main = paste(B, "Confidence intervals"),
col=colsuse[j]
)
}else{
points(means, (1:B)+offset*ifelse(j==1,1,-1), pch = 20,
col=colsuse[j])
}
for (i in 1:B){
if(between(mu[j], x_0[i], x_1[i])){
segments(x_0[i], i+offset*ifelse(j==1,1,-1), x_1[i], i+offset*ifelse(j==1,1,-1), col=colsuse[j], lwd = 1) #plot CI's that contain the mean in black
} else {
segments(x_0[i], i+offset*ifelse(j==1,1,-1), x_1[i], i+offset*ifelse(j==1,1,-1), col = "red", lwd = 1) #plot CI's that don't contain the mean in red
}
}
abline(v=mu[j], col = colsuse[j]) #plot a vertical line at the population mean
}
par(xpd=F)
# legend for the groups
legend("topright",legend = c('Male','Female'),lty=1,col=colsuse,cex=0.5)
}
plot_CI_95(1)
dev.off()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.