User login

Navigation

Codebooks

Group Exercise: Confidence Interval

Tue, 10/15/2013 - 3:00pm — Professor Shortell

We can estimate the confidence interval for a mean using the t.test function. You need, of course, a numeric variable to calculate a mean. Identify the variable and a population value, mu, to test for (which should be derived from theory or make sense at face value). In the World95 data, we can use the percent population change variable to test for a population increase.

We can also test for a population proportion, using the prop.test function. In this example, we'll use the variable Q2 from the CBS2011 dataframe. (This variable asks respondents if they think the country is going in the right direction, 1, or the wrong direction, 2.)

Now we can test the proportion of respondents who said "right direction" (1). The syntax here is a bit complicated because we're nesting functions:

> prop.test(length(na.omit(Q2[Q2==1])),length(na.omit(Q2)))

We're using three different functions. The innermost function is na.omit, which tells R to drop cases coded as 'NA' -- that is, to treat them as missing. When we say na.omit(Q2), we are asking for a version of Q2 without the cases coded 'NA'. When we say na.omit(Q2[Q2==1]), we are asking for the cases for respondents who answered 1 to Q2 -- that is what [Q2==1] specifies. The length() function calculates the count of cases for the variable specified. The prop.test function evaluates the number of cases of respondents who answered '1' -- length(na.omit(Q2[Q2==1])) -- compared to the total number of cases for Q2 -- length(na.omit(Q2)).

By default, we test pi == 0.5, which is the proportion we would expect due to chance.

Group Exercise
Calculate a confidence interval for a mean or a proportion and interpret the results. Indicate in your comment the dataframe and variable name and the population parameter you tested. Remember to include everyone's name.