Homework 3

  1. round() is a function that rounds numeric values. Examine the Usage and Arguments sections of round()’s help page. In the console, use round() to round pi to 2 digits.

  2. At the top of a new script, such as homework_3.r, include code for importing the nhanes_l.csv data file. Add a comment. Run that code.

    # import the data
    nhanes<-read.csv('nhanes_l.csv')
  3. You may have noticed that quantile() automatically includes labels above the quantiles. For example:

    quantile(nhanes$bpxosy1,c(0.25,0.5,0.75),na.rm=TRUE)
    #|  25% 50% 75% 
    #|  110 120 133

    Examine the Usage and Arguments sections of quantile()’s help page. Try to figure out how to obtain quartiles of systolic blood pressure (bpxosy1) without labels (25% 50% 75%) included in the output. Add this code to your script, with a comment.

    # obtain quantiles for systolic blood pressure, without labels
    quantile(nhanes$bpxosy1,c(0.25,0.5,0.75),na.rm=TRUE,names=FALSE)
    #|  [1] 110 120 133
  4. We can use binom.test() to obtain confidence intervals for percents.

    In the console, first table() the asthma (asthma) variable. You should see that History of asthma is listed first. This is because, by default, table() lists groups alphabetically.

    binom.test() will test the null hypothesis that the population percents are equal to 50%. However, as with our use of t.test() from Session 3, we are just going to use the function to get a confidence interval for the prevalence of asthma in the population (we will ignore the results from the test).

    To use binom.test(), place your code for table() above within binom.test(). By default, binom.test() will provide a confidence interval for the “first group.” As mentioned above, the “first group” for asthma is History of asthma. So, your binom.test() code is providing a confidence interval for the prevalence of asthma in the population.

    Add the binom.test() code to your script, with a comment.

    # obtain a confidence interval for the prevalence of asthma
    binom.test(table(nhanes$asthma))
    #|  
    #|   Exact binomial test
    #|  
    #|  data:  table(nhanes$asthma)
    #|  number of successes = 1486, number of trials = 8143, p-value < 2.2e-16
    #|  alternative hypothesis: true probability of success is not equal to 0.5
    #|  95 percent confidence interval:
    #|   0.1741522 0.1910517
    #|  sample estimates:
    #|  probability of success 
    #|                0.182488
  5. Write code for obtaining percents for asthma. Add this code to your script, with a comment.

    # calculate percents for asthma
    prop.table(table(nhanes$asthma))
    #|  
    #|  History of asthma                No 
    #|           0.182488          0.817512
  6. The sum() function returns the sum of a set of values. Use this function, along with length() (mentioned in Homework 2) to manually calculate the mean of the age variable (ridageyr): divide the sum by the length. Add this code to your script, with a comment. Also add code for the mean() function and verify that the answers match.

    # calculate the mean manually
    sum(nhanes$ridageyr)/length(nhanes$ridageyr)
    #|  [1] 52.14436
    
    # calculate the mean using mean()
    mean(nhanes$ridageyr)
    #|  [1] 52.14436