/ / Wykreślanie przedziałów ufności z ramki danych - r, losowe, wykres

Wykreślanie przedziałów ufności z ramki danych - r, losowy, wykres

Napisałem trochę kodu R, który produkujegranice przedziałów ufności, a także informacje, jeśli każdy przedział ufności obejmuje prawdziwy parametr. Chciałbym to sobie wyobrazić, ale nie mam pojęcia jak.

confInt <- function(runs){
result<-NULL
vleft<-NULL
vright<-NULL

for (i in 1:runs) {
data<-rnorm(1000)
n<-length(data)
a<-mean(data)
s<-sd(data)
error <- qnorm(0.975)*s/sqrt(n)
left <- a-error
right <- a+error

result[i] = left<0 & 0<right
vleft[i] = left
vright[i] = right
}
data.frame(result,vleft,vright)
}


confInt(100)

pewność siebie

EDYCJA: Znalazłem sposób użycia ggplot2

confInt <- function(runs){
x<-1:runs
mu<-NULL
sigma<-NULL
result<-NULL
vleft<-NULL
vright<-NULL

for (i in 1:runs) {
data<-rnorm(1000)
n<-length(data)
a<-mean(data)
mu[i]<-a
s<-sd(data)
sigma[i]<-s
error <- qnorm(0.975)*s/sqrt(n)
left <- a-error
right <- a+error

result[i] = left<0 & 0<right
vleft[i] = left
vright[i] = right
}
data.frame(x,mu,sigma,result,vleft,vright)
}


df<-confInt(100)

require(ggplot2)

myplot<-ggplot(df, aes(x = x, y = mu)) +
geom_point(size = 2) +
geom_errorbar(aes(ymax = vleft, ymin = vright,colour=result*3))
myplot + theme_bw()
summary(df)

Odpowiedzi:

1 dla odpowiedzi № 1

Istnieje wiele sposobów podejścia do tego. Poniżej używam mapply aby podać punkt początkowy i końcowy każdego przedziału ufności segments.

ci <- confInt(100)

plot(y = c(0, 100), x = c(-.15,.15), type = "n", ylab = "",
xlab = "", yaxt = "n")

with(ci, mapply(segments, x0 = vleft, x1 = vright,
y0 = 1:100, y1 = 1:100, col = (!result) + 1))

abline(v = 0, col = "purple")

wprowadź opis obrazu tutaj