forked from ulrichw/Finance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCI.R
More file actions
45 lines (34 loc) · 1.47 KB
/
CI.R
File metadata and controls
45 lines (34 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#repository of 3 simple functions to compute confidence intervals
#for mean, standard deviation, and proportion
#simpler than using the stats package!
#for the mean: provide the sample x, and significance level alpha
#assumes we do not know the standard dev. of the population
#so we use the t-distribution rather than the z-distribution
CImean <- function(x,alpha=0.05) {
dof = length(x)-1 #degrees of freddom for t-distribution
critical_t = qt(1-alpha/2,dof)
Error = critical_t*sd(x)/sqrt(length(x))
Mean=mean(x)
cat("The confidence interval is:",Mean-Error,"<",Mean,"<",Mean+Error,"\n")
}
#for the standard deviation: provide the sample x, and significance level alpha
#we use the chi-square distribution
CIsd <- function(x,alpha=0.05) {
dof = length(x)-1
criticalL = qchisq(alpha/2,dof) #left-tail critical value for chi-square
criticalR = qchisq(1-alpha/2,dof) #right-tail critical value for chi-square
Sd=sd(x)
Var=var(x)
ErrorL = sqrt(dof*Var/criticalL)
ErrorR = sqrt(dof*Var/criticalR)
cat("The confidence interval is:",ErrorR,"<",Sd,"<",ErrorL)
}
#for a proportion: provide the sample proportion p
#and the size of your sample n
#assumes that the sample proportion follows a normal distribution
#but for the standard deviation uses a binomial one
CIprop <- function(p,n,alpha=0.05){
critical=qnorm(1-alpha/2)
Error=critical*sqrt(p*(1-p)/n)
cat("The confidence interval is:",p-Error,"<",p,"<",p+Error)
}