Explore Statistics with R (EDX)
WEEK3-Using the z distribution in R视频笔记
一、 一些概念和用法
1. The Normal Distribution
Density, distribution function, quantile function and random generation for the normal distribution with mean equal to mean and standard deviation equal to sd.
dnorm(x, mean = 0, sd = 1, log = FALSE)
pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
rnorm(n, mean = 0, sd = 1)
2. ?dt() ; ?dpois() ; ?dbinom() ; ?dchisq()
The Student t Distribution;The Poisson Distribution;The Binomial Distribution;The (non-central) Chi-Squared Distribution
最后一个是卡方分布,英文长得很好吃的样子。。
二、画图画图
1. dnorm() #Density function
x<-seq(-4, 4, 0.01) #start at minus-four, continue to number four and the interval between the points is 0.01
plot(x, dnorm(x), type="l") #l = line
2. pnorm() #Distribution function, or Cumulative distribution function 所以其实是累积分布函数
plot(x, pnorm(x))
3. qnorm() #Quantile function
> qnorm(0.25) #the value at the 25th percentile
[1] -0.6744898
> qnorm(c(0.025, 0.975)) #confidence interval #置信区间
[1] -1.959964 1.959964
三、例子
1. z-tests
(A Z-test is any statistical test for which the distribution of the test statistic under the null hypothesis can be approximated by a normal distribution. ---from wiki)
bt <- seq(60, 120, 1)
plot(bt, dnorm(bt, 90, 10), type="l", xlim=c(60, 120), main="blood pressure")
#mean = 90, standard deviation = 10
2. one tailed test #单侧检验
> bt <- seq(60, 120, 1)
> plot(bt, dnorm(bt, 90, 10), type="l", xlim=c(60, 120), main="one tailed test")
> pnorm(72, 90, 10) # probability of randomly selecting a subject at 72 or lower
[1] 0.03593032
> abline(v=72) # Draw a line for 72
> cord.x <- c(60,seq(60,72,1),72)
> cord.y <- c(0,dnorm(seq(60, 72, 1), 90, 10),0)
> polygon(cord.x,cord.y,col='skyblue')
> text(70, 0.005, "blue area = p = 0.0359")
3. two-tailed test #双侧检验
> bt <- seq(60, 120, 1)
> plot(bt, dnorm(bt, 90, 10), type="l", xlim=c(60, 120), main="two-tailed test")
> pnorm(72, 90, 10)
[1] 0.03593032
> abline(v=72)
> cord.x <- c(60,seq(60,72,1),72)
> cord.y <- c(0,dnorm(seq(60, 72, 1), 90, 10),0)
> polygon(cord.x,cord.y,col='skyblue')
> cord.x1 <- c(108,seq(108,120,1),120)
> cord.y1 <- c(0,dnorm(seq(108, 120, 1), 90, 10),0)
> polygon(cord.x1,cord.y1,col='skyblue')
> text(65, 0.005, round(pnorm(72, 90, 10), 3))
> text(115, 0.005, round(pnorm(72, 90, 10), 3))
> text(75, 0.02, " p = 0.072 " )