Testing confidence intervals in R












0














I currently have constructed a 95% confidence interval and have then used replicate() to randomly generate 1000 confidence intervals. I want to measure how many of the intervals contain my mean. I know in theory it should be in 950 of them but how do I get a definite answer? The function I used and the mean are listed below.



z <- function(a,b,c){
error <- rnorm(a, b, c) * c / sqrt(a)
left <- b - error
right <- j + error
paste("[",round(left,2),";",round(right,2),"]")
}

set.seed(123)
replicate(1000, z(10,1,1))


Where do I go from here?










share|improve this question
























  • I can't make much sense of your z() function. Could you format it better/tidy it up?
    – AkselA
    Nov 23 '18 at 11:16










  • @AkselA is that better?
    – John Fitz
    Nov 23 '18 at 11:22










  • It still looks like non-sense :). Have you written functions in R before?
    – AkselA
    Nov 23 '18 at 11:28










  • No I'm new to R. It generates the 1000 confidence intervals but how do I then test if the mean is contained in each confidence interval?
    – John Fitz
    Nov 23 '18 at 11:31










  • Confidence interval of what? Estimating the mean of a normal distribution?
    – AkselA
    Nov 23 '18 at 11:45
















0














I currently have constructed a 95% confidence interval and have then used replicate() to randomly generate 1000 confidence intervals. I want to measure how many of the intervals contain my mean. I know in theory it should be in 950 of them but how do I get a definite answer? The function I used and the mean are listed below.



z <- function(a,b,c){
error <- rnorm(a, b, c) * c / sqrt(a)
left <- b - error
right <- j + error
paste("[",round(left,2),";",round(right,2),"]")
}

set.seed(123)
replicate(1000, z(10,1,1))


Where do I go from here?










share|improve this question
























  • I can't make much sense of your z() function. Could you format it better/tidy it up?
    – AkselA
    Nov 23 '18 at 11:16










  • @AkselA is that better?
    – John Fitz
    Nov 23 '18 at 11:22










  • It still looks like non-sense :). Have you written functions in R before?
    – AkselA
    Nov 23 '18 at 11:28










  • No I'm new to R. It generates the 1000 confidence intervals but how do I then test if the mean is contained in each confidence interval?
    – John Fitz
    Nov 23 '18 at 11:31










  • Confidence interval of what? Estimating the mean of a normal distribution?
    – AkselA
    Nov 23 '18 at 11:45














0












0








0







I currently have constructed a 95% confidence interval and have then used replicate() to randomly generate 1000 confidence intervals. I want to measure how many of the intervals contain my mean. I know in theory it should be in 950 of them but how do I get a definite answer? The function I used and the mean are listed below.



z <- function(a,b,c){
error <- rnorm(a, b, c) * c / sqrt(a)
left <- b - error
right <- j + error
paste("[",round(left,2),";",round(right,2),"]")
}

set.seed(123)
replicate(1000, z(10,1,1))


Where do I go from here?










share|improve this question















I currently have constructed a 95% confidence interval and have then used replicate() to randomly generate 1000 confidence intervals. I want to measure how many of the intervals contain my mean. I know in theory it should be in 950 of them but how do I get a definite answer? The function I used and the mean are listed below.



z <- function(a,b,c){
error <- rnorm(a, b, c) * c / sqrt(a)
left <- b - error
right <- j + error
paste("[",round(left,2),";",round(right,2),"]")
}

set.seed(123)
replicate(1000, z(10,1,1))


Where do I go from here?







r confidence-interval






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 11:57









Roland

99.1k6112183




99.1k6112183










asked Nov 23 '18 at 11:11









John FitzJohn Fitz

33




33












  • I can't make much sense of your z() function. Could you format it better/tidy it up?
    – AkselA
    Nov 23 '18 at 11:16










  • @AkselA is that better?
    – John Fitz
    Nov 23 '18 at 11:22










  • It still looks like non-sense :). Have you written functions in R before?
    – AkselA
    Nov 23 '18 at 11:28










  • No I'm new to R. It generates the 1000 confidence intervals but how do I then test if the mean is contained in each confidence interval?
    – John Fitz
    Nov 23 '18 at 11:31










  • Confidence interval of what? Estimating the mean of a normal distribution?
    – AkselA
    Nov 23 '18 at 11:45


















  • I can't make much sense of your z() function. Could you format it better/tidy it up?
    – AkselA
    Nov 23 '18 at 11:16










  • @AkselA is that better?
    – John Fitz
    Nov 23 '18 at 11:22










  • It still looks like non-sense :). Have you written functions in R before?
    – AkselA
    Nov 23 '18 at 11:28










  • No I'm new to R. It generates the 1000 confidence intervals but how do I then test if the mean is contained in each confidence interval?
    – John Fitz
    Nov 23 '18 at 11:31










  • Confidence interval of what? Estimating the mean of a normal distribution?
    – AkselA
    Nov 23 '18 at 11:45
















I can't make much sense of your z() function. Could you format it better/tidy it up?
– AkselA
Nov 23 '18 at 11:16




I can't make much sense of your z() function. Could you format it better/tidy it up?
– AkselA
Nov 23 '18 at 11:16












@AkselA is that better?
– John Fitz
Nov 23 '18 at 11:22




@AkselA is that better?
– John Fitz
Nov 23 '18 at 11:22












It still looks like non-sense :). Have you written functions in R before?
– AkselA
Nov 23 '18 at 11:28




It still looks like non-sense :). Have you written functions in R before?
– AkselA
Nov 23 '18 at 11:28












No I'm new to R. It generates the 1000 confidence intervals but how do I then test if the mean is contained in each confidence interval?
– John Fitz
Nov 23 '18 at 11:31




No I'm new to R. It generates the 1000 confidence intervals but how do I then test if the mean is contained in each confidence interval?
– John Fitz
Nov 23 '18 at 11:31












Confidence interval of what? Estimating the mean of a normal distribution?
– AkselA
Nov 23 '18 at 11:45




Confidence interval of what? Estimating the mean of a normal distribution?
– AkselA
Nov 23 '18 at 11:45












1 Answer
1






active

oldest

votes


















0














Maybe this is what you're trying to do?



This z() will return the confidence interval for the population mean of a normal distribution.



z <- function(N, mu, std, cl=95) {
alpha <- (1-cl/100)/2

# CI for population mean
sep <- std/sqrt(N)
z_s <- qnorm(1 - alpha)
pop_lower <- mu - z_s*sep
pop_upper <- mu + z_s*sep

c(lower=pop_lower, upper=pop_upper)
}


Meaning that if I produce a random variate mean(rnorm(20, 0, 1)), then we expect the value of that to lie within z(20, 0, 1, 95) with probability 0.95.



To test this we can do



# specify parameters
N <- 20
mu <- 0
std <- 1

# produce a good number (10,000) of population means
set.seed(1)
r <- replicate(1e4, mean(rnorm(N, mu, std)))

# calculate confidence interval
ci <- z(N, mu, std)

# find which are below, within and above the interval
rc <- cut(r, c(min(r), ci, max(r)), c("below", "within", "above"))

# create a proportion table
round(prop.table(table(rc))*100, 2)

# below within above
# 2.59 95.08 2.33





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445602%2ftesting-confidence-intervals-in-r%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Maybe this is what you're trying to do?



    This z() will return the confidence interval for the population mean of a normal distribution.



    z <- function(N, mu, std, cl=95) {
    alpha <- (1-cl/100)/2

    # CI for population mean
    sep <- std/sqrt(N)
    z_s <- qnorm(1 - alpha)
    pop_lower <- mu - z_s*sep
    pop_upper <- mu + z_s*sep

    c(lower=pop_lower, upper=pop_upper)
    }


    Meaning that if I produce a random variate mean(rnorm(20, 0, 1)), then we expect the value of that to lie within z(20, 0, 1, 95) with probability 0.95.



    To test this we can do



    # specify parameters
    N <- 20
    mu <- 0
    std <- 1

    # produce a good number (10,000) of population means
    set.seed(1)
    r <- replicate(1e4, mean(rnorm(N, mu, std)))

    # calculate confidence interval
    ci <- z(N, mu, std)

    # find which are below, within and above the interval
    rc <- cut(r, c(min(r), ci, max(r)), c("below", "within", "above"))

    # create a proportion table
    round(prop.table(table(rc))*100, 2)

    # below within above
    # 2.59 95.08 2.33





    share|improve this answer


























      0














      Maybe this is what you're trying to do?



      This z() will return the confidence interval for the population mean of a normal distribution.



      z <- function(N, mu, std, cl=95) {
      alpha <- (1-cl/100)/2

      # CI for population mean
      sep <- std/sqrt(N)
      z_s <- qnorm(1 - alpha)
      pop_lower <- mu - z_s*sep
      pop_upper <- mu + z_s*sep

      c(lower=pop_lower, upper=pop_upper)
      }


      Meaning that if I produce a random variate mean(rnorm(20, 0, 1)), then we expect the value of that to lie within z(20, 0, 1, 95) with probability 0.95.



      To test this we can do



      # specify parameters
      N <- 20
      mu <- 0
      std <- 1

      # produce a good number (10,000) of population means
      set.seed(1)
      r <- replicate(1e4, mean(rnorm(N, mu, std)))

      # calculate confidence interval
      ci <- z(N, mu, std)

      # find which are below, within and above the interval
      rc <- cut(r, c(min(r), ci, max(r)), c("below", "within", "above"))

      # create a proportion table
      round(prop.table(table(rc))*100, 2)

      # below within above
      # 2.59 95.08 2.33





      share|improve this answer
























        0












        0








        0






        Maybe this is what you're trying to do?



        This z() will return the confidence interval for the population mean of a normal distribution.



        z <- function(N, mu, std, cl=95) {
        alpha <- (1-cl/100)/2

        # CI for population mean
        sep <- std/sqrt(N)
        z_s <- qnorm(1 - alpha)
        pop_lower <- mu - z_s*sep
        pop_upper <- mu + z_s*sep

        c(lower=pop_lower, upper=pop_upper)
        }


        Meaning that if I produce a random variate mean(rnorm(20, 0, 1)), then we expect the value of that to lie within z(20, 0, 1, 95) with probability 0.95.



        To test this we can do



        # specify parameters
        N <- 20
        mu <- 0
        std <- 1

        # produce a good number (10,000) of population means
        set.seed(1)
        r <- replicate(1e4, mean(rnorm(N, mu, std)))

        # calculate confidence interval
        ci <- z(N, mu, std)

        # find which are below, within and above the interval
        rc <- cut(r, c(min(r), ci, max(r)), c("below", "within", "above"))

        # create a proportion table
        round(prop.table(table(rc))*100, 2)

        # below within above
        # 2.59 95.08 2.33





        share|improve this answer












        Maybe this is what you're trying to do?



        This z() will return the confidence interval for the population mean of a normal distribution.



        z <- function(N, mu, std, cl=95) {
        alpha <- (1-cl/100)/2

        # CI for population mean
        sep <- std/sqrt(N)
        z_s <- qnorm(1 - alpha)
        pop_lower <- mu - z_s*sep
        pop_upper <- mu + z_s*sep

        c(lower=pop_lower, upper=pop_upper)
        }


        Meaning that if I produce a random variate mean(rnorm(20, 0, 1)), then we expect the value of that to lie within z(20, 0, 1, 95) with probability 0.95.



        To test this we can do



        # specify parameters
        N <- 20
        mu <- 0
        std <- 1

        # produce a good number (10,000) of population means
        set.seed(1)
        r <- replicate(1e4, mean(rnorm(N, mu, std)))

        # calculate confidence interval
        ci <- z(N, mu, std)

        # find which are below, within and above the interval
        rc <- cut(r, c(min(r), ci, max(r)), c("below", "within", "above"))

        # create a proportion table
        round(prop.table(table(rc))*100, 2)

        # below within above
        # 2.59 95.08 2.33






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 13:07









        AkselAAkselA

        4,32621225




        4,32621225






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445602%2ftesting-confidence-intervals-in-r%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo