Skip to content

Instantly share code, notes, and snippets.

@mick001
Last active August 28, 2015 19:09

Revisions

  1. mick001 revised this gist Aug 28, 2015. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions estimatDataParameters.R
    Original file line number Diff line number Diff line change
    @@ -54,15 +54,15 @@ 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
    #
    # > 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
  2. mick001 created this gist Aug 28, 2015.
    68 changes: 68 additions & 0 deletions estimatDataParameters.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    ###############################################################################
    # 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