# import the data
nhanes<-read.csv('nhanes_l.csv')Homework 3
round()is a function that rounds numeric values. Examine the Usage and Arguments sections ofround()’s help page. In the console, useround()to roundpito 2 digits.At the top of a new script, such as
homework_3.r, include code for importing thenhanes_l.csvdata file. Add a comment. Run that code.TipSolutionYou 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 133Examine 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.TipSolution# 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 133We can use
binom.test()to obtain confidence intervals for percents.In the console, first
table()the asthma (asthma) variable. You should see thatHistory of asthmais 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 oft.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 fortable()above withinbinom.test(). By default,binom.test()will provide a confidence interval for the “first group.” As mentioned above, the “first group” forasthmaisHistory of asthma. So, yourbinom.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.TipSolution# 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.182488Write code for obtaining percents for
asthma. Add this code to your script, with a comment.TipSolution# calculate percents for asthma prop.table(table(nhanes$asthma)) #| #| History of asthma No #| 0.182488 0.817512The
sum()function returns the sum of a set of values. Use this function, along withlength()(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 themean()function and verify that the answers match.TipSolution# calculate the mean manually sum(nhanes$ridageyr)/length(nhanes$ridageyr) #| [1] 52.14436 # calculate the mean using mean() mean(nhanes$ridageyr) #| [1] 52.14436