Last active
August 28, 2015 19:09
Estimating data parameters using R (confidence intervals). Full article at http://www.firsttimeprogrammer.blogspot.com/2015/07/estimating-data-parameters-using-r.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################### | |
# Confidence intervals for the population mean (t-student distribution) | |
# We assume that | |
# 1. Data is normally distributed | |
# 2. Samples are iid | |
# | |
# Note that population variance is unknown and therefore must be | |
# estimated. In this case Student distribution should be used. | |
# The number of degree of freedom is n-1 where n is the size of | |
# the sample. Note that as n -> Inf the Student distribution tends to | |
# the Normal distribution. | |
# Data | |
data_vector <- c(63, 75, 92, 53, 45, 92, 69, 54, 75, 87) | |
# Two sided confidence interval (CI) | |
# a <= mu <= b | |
two.sided.int <- function(data,alpha) | |
{ | |
x <- mean(data) | |
sd.mean <- sd(data) | |
degree.of.freedom <- length(data)-1 | |
t.value <- qt(1-alpha/2,degree.of.freedom) | |
CI <- (sd.mean/sqrt(length(data)))*t.value | |
confidenceInterval <- c(x-CI,x+CI) | |
print("Confidence interval for the population mean") | |
print(confidenceInterval) | |
} | |
# One sided CI | |
# upper: mu <= c | |
# lower: mu >= c | |
one.sided.int <- function(data,alpha,type) | |
{ | |
x <- mean(data) | |
sd.mean <- sd(data) | |
degree.of.freedom <- length(data)-1 | |
t.value <- qt(1-alpha,degree.of.freedom) | |
CI <- (sd.mean/sqrt(length(data)))*t.value | |
if(type == 'upper'){confidenceInt <- x+CI} | |
else if(type == "lower"){confidenceInt <- x-CI} | |
else{return(0)} | |
print(paste(type," bound for the population mean:")) | |
print(confidenceInt) | |
} | |
one.sided.int(data_vector,0.025,"upper") | |
one.sided.int(data_vector,0.025,"lower") | |
two.sided.int(data_vector,0.025) | |
############################################################################# | |
# Output | |
# | |
# > one.sided.int(data_vector,0.025,"upper") | |
# [1] "upper bound for the population mean:" | |
# [1] 82.48089 | |
# > one.sided.int(data_vector,0.025,"lower") | |
# [1] "lower bound for the population mean:" | |
# [1] 58.51911 | |
# > two.sided.int(data_vector,0.025) | |
# [1] "Confidence interval for the population mean" | |
# [1] 56.27958 84.72042 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment