什么是置信區(qū)間争舞?
我看了StatQuest 介紹置信區(qū)間的那一期視頻,大體理解了咽白,但是讓我用語言表述出來闺魏,還有點不知道如何表達。本來B站可以直接看StatQuest的視頻的,今天看到B站的up主發(fā)消息說StatQuest的原作者準備入駐B站了蜕径,所以他把原來獲得授權的那些視頻全都刪掉了两踏。所以要在B站看這些視頻還要等一陣子了。
具體概念先不介紹了兜喻,主要還是實際操作
今天的主要內(nèi)容來自 How to Calculate Confidence Interval in R : Statistics in R : Data Sharkie
計算置信區(qū)間用到的函數(shù)是CI()
函數(shù)梦染,來自R語言包Rmisc
R語言包Rmisc
第一次使用需要先安裝
install.packages("Rmisc")
計算某組數(shù)據(jù)均值95%的置信區(qū)間
x<-iris$Sepal.Length
library(Rmisc)
CI(x,ci=0.95)
返回的結果是
> CI(x,ci=0.95)
upper mean lower
5.976934 5.843333 5.709732
前面提到的參考鏈接里有一句話
Logically, as you increase the sample size, the closer is the value of a sample parameter to a population parameter, therefore the narrower the confidence interval gets.
樣本越大,樣本的均值越接近總體的均值朴皆,所以均值的置信區(qū)間就會越窄
正好昨天的推文是畫密度圖是給指定的區(qū)間填充顏色
下面使用ggplot2畫密度圖展示并且展示均值95%的置信區(qū)間
#install.packages("Rmisc")
library(Rmisc)
x<-iris$Sepal.Length
library(Rmisc)
x1<-CI(x,ci=0.95)
class(x1[1])
dat<-with(density(x),data.frame(x,y))
dat1<-dat[dat$x>x1[3]&dat$x<x1[1],]
library(ggplot2)
ggplot(iris,aes(Sepal.Length))+
geom_density(fill="grey")+
geom_vline(xintercept = x1[1],lty="dashed")+
geom_vline(xintercept = x1[3],lty="dashed")+
geom_area(data=dat1,aes(x=x,y=y),fill="red")+
geom_vline(xintercept = x1[2],lty="dashed")+
scale_y_continuous(expand = c(0,0),
limits = c(0,0.41))+
theme_minimal()
歡迎大家關注我的公眾號
小明的數(shù)據(jù)分析筆記本